Browse Source

Merge some javascript

- Removed unused JS functions
- Inlined functions not often used
- Minimized some overly verbose functions
spaghetti 8 years ago
parent
commit
f92dfea941

+ 1
- 1
classes/view.class.php View File

@@ -30,7 +30,7 @@ class View {
30 30
       require(SERVER_ROOT.'/design/publicheader.php');
31 31
     } else {
32 32
       // HTTP/2 Server Push headers for cloudflare
33
-      $Scripts = array_merge(['jquery', 'script_start', 'ajax.class', 'global', 'jquery.autocomplete', 'autocomplete', 'tooltipster', 'tooltipster_settings'], explode(',', $JSIncludes));
33
+      $Scripts = array_merge(['jquery', 'global', 'ajax.class', 'jquery.autocomplete', 'autocomplete', 'tooltipster', 'tooltipster_settings'], explode(',', $JSIncludes));
34 34
       $Styles = array_merge(['tooltipster'], explode(',', $CSSIncludes));
35 35
       foreach ($Scripts as $Script) {
36 36
         if (trim($Script) == '') { continue; }

+ 4
- 5
design/privateheader.php View File

@@ -95,11 +95,10 @@ foreach ($ExtraCSS as $CSS) {
95 95
 <?
96 96
 }
97 97
 
98
-$Scripts = array_merge(array('jquery', 'script_start', 'ajax.class', 'global', 'jquery.autocomplete', 'autocomplete', 'tooltipster', 'tooltipster_settings'), explode(',', $JSIncludes));
98
+$Scripts = array_merge(array('jquery', 'global', 'ajax.class', 'jquery.autocomplete', 'autocomplete', 'tooltipster', 'tooltipster_settings'), explode(',', $JSIncludes));
99 99
 foreach ($Scripts as $Script) {
100
-  if (trim($Script) == '') {
101
-    continue;
102
-  }
100
+  if (trim($Script) == '') { continue; }
101
+  $Async = (in_array($Script, ['jquery','global','ajax.class'])) ? '' : 'async';
103 102
   if (($ScriptStats = G::$Cache->get_value("script_stats_$Script")) === false || $ScriptStats['mtime'] != filemtime(SERVER_ROOT.STATIC_SERVER."functions/$Script.js")) {
104 103
     $ScriptStats['mtime'] = filemtime(SERVER_ROOT.STATIC_SERVER."functions/$Script.js");
105 104
     $ScriptStats['hash'] = base64_encode(hash_file(INTEGRITY_ALGO, SERVER_ROOT.STATIC_SERVER."functions/$Script.js", true));
@@ -107,7 +106,7 @@ foreach ($Scripts as $Script) {
107 106
     G::$Cache->cache_value("script_stats_$Script", $ScriptStats);
108 107
   }
109 108
 ?>
110
-  <script src="<?=STATIC_SERVER."functions/$Script.js?v=$ScriptStats[mtime]"?>" type="text/javascript" integrity="<?="$ScriptStats[algo]-$ScriptStats[hash]"?>"></script>
109
+  <script src="<?=STATIC_SERVER."functions/$Script.js?v=$ScriptStats[mtime]"?>" type="text/javascript" integrity="<?="$ScriptStats[algo]-$ScriptStats[hash]"?>" <?=$Async?>></script>
111 110
 <?
112 111
 }
113 112
 

+ 1
- 1
design/publicheader.php View File

@@ -11,7 +11,7 @@ define('FOOTER_FILE',SERVER_ROOT.'/design/publicfooter.php');
11 11
   <link rel="shortcut icon" href="favicon.ico?v=<?=md5_file('favicon.ico');?>" />
12 12
   <link href="<?=STATIC_SERVER ?>styles/public/style.css?v=<?=filemtime(SERVER_ROOT.'/static/styles/public/style.css')?>" rel="stylesheet" type="text/css" />
13 13
 <?
14
-  $Scripts = ['jquery', 'script_start', 'ajax.class', 'cookie.class', 'storage.class', 'global', 'public'];
14
+  $Scripts = ['jquery', 'global', 'ajax.class', 'cookie.class', 'storage.class', 'public'];
15 15
   foreach($Scripts as $Script) {
16 16
     if (($ScriptStats = G::$Cache->get_value("script_stats_$Script")) === false || $ScriptStats['mtime'] != filemtime(SERVER_ROOT.STATIC_SERVER."functions/$Script.js")) {
17 17
       $ScriptStats['mtime'] = filemtime(SERVER_ROOT.STATIC_SERVER."functions/$Script.js");

+ 4
- 22
static/functions/ajax.class.js View File

@@ -21,27 +21,9 @@
21 21
 
22 22
 */
23 23
 "use strict";
24
-var json = {
25
-  encode: function (object) {
26
-    try {
27
-      return JSON.stringify(object);
28
-    } catch (err) {
29
-      return '';
30
-    }
31
-  },
32
-  decode: function (string) {
33
-    if (window.JSON && JSON.parse) {
34
-      return JSON.parse(string);
35
-    } else {
36
-      return eval("(" + string + ")");
37
-      //return (new Function("return " + data))();
38
-    }
39
-  }
40
-};
41
-
42 24
 var ajax = {
43 25
   get: function (url, callback) {
44
-    var req = (typeof(window.ActiveXObject) === 'undefined') ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
26
+    var req = new XMLHttpRequest();
45 27
     if (callback !== undefined) {
46 28
       req.onreadystatechange = function () {
47 29
         if (req.readyState !== 4 || req.status !== 200) {
@@ -54,7 +36,7 @@ var ajax = {
54 36
     req.send(null);
55 37
   },
56 38
   post: function (url, data, callback) {
57
-    var req = isset(window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
39
+    var req = new XMLHttpRequest();
58 40
     var params = ajax.serialize(data);
59 41
     if (callback !== undefined) {
60 42
       req.onreadystatechange = function () {
@@ -71,7 +53,7 @@ var ajax = {
71 53
   serialize: function (data) {
72 54
     var query = '',
73 55
       elements;
74
-    if (is_array(data)) {
56
+    if (Array.isArray(data)) {
75 57
       for (var key in data) {
76 58
         query += key + '=' + encodeURIComponent(data[key]) + '&';
77 59
       }
@@ -79,7 +61,7 @@ var ajax = {
79 61
       elements = document.getElementById(data).elements;
80 62
       for (var i = 0, il = elements.length; i < il; i++) {
81 63
         var element = elements[i];
82
-        if (!isset(element) || element.disabled || element.name === '') {
64
+        if (!element || element.disabled || element.name === '') {
83 65
           continue;
84 66
         }
85 67
         switch (element.type) {

+ 4
- 1
static/functions/autocomplete.js View File

@@ -4,7 +4,10 @@ var SELECTOR = '[data-gazelle-autocomplete="true"]';
4 4
 $(document).ready(initAutocomplete)
5 5
 
6 6
 function initAutocomplete() {
7
-  var url = new URL();
7
+  var url = {
8
+    path: window.location.pathname.split('/').reverse()[0].split(".")[0],
9
+    query: window.location.search.slice(1).split('&').reduce((a,b)=>Object.assign(a,{[b.split('=')[0]]:b.split('=')[1]}),{})
10
+  }
8 11
 
9 12
   $('#artistsearch' + SELECTOR).autocomplete({
10 13
     deferRequestBy: 300,

+ 7
- 7
static/functions/comments.js View File

@@ -1,6 +1,6 @@
1 1
 var username;
2 2
 var postid;
3
-var url = new URL();
3
+var url = {path: window.location.pathname.split('/').reverse()[0].split('.')[0]};
4 4
 
5 5
 function QuoteJump(event, post) {
6 6
   var button = event.button;
@@ -42,12 +42,8 @@ function QuoteJump(event, post) {
42 42
   }
43 43
 }
44 44
 
45
-function Quote(post, user) {
46
-  Quote(post, user, false)
47
-}
48
-
49 45
 var original_post;
50
-function Quote(post, user, link) {
46
+function Quote(post, user, link = false) {
51 47
   username = user;
52 48
   postid = post;
53 49
 
@@ -65,7 +61,11 @@ function Quote(post, user, link) {
65 61
         }
66 62
       }).done(function(response) {
67 63
         $("#content" + postid).html(response['response']['body']);
68
-        select_all($("#content" + postid).get(0));
64
+        var range = document.createRange();
65
+        range.selectNodeContents($("#content" + postid).get(0));
66
+        var sel = window.getSelection();
67
+        sel.removeAllRanges();
68
+        sel.addRange(range);
69 69
       });
70 70
     } else {
71 71
       document.getSelection().removeAllRanges();

+ 10
- 44
static/functions/form_validate.js View File

@@ -1,46 +1,12 @@
1 1
 $(document).ready(function() {
2
-  var url = new URL();
3
-  var query = url.query;
4
-  switch (url.path) {
5
-    case "forums":
6
-      if (query['action'] == "new") {
7
-        $("#newthreadform").validate();
8
-      }
9
-      break;
10
-    case "reports":
11
-      if (query['action'] == "report") {
12
-        $("#report_form").validate();
13
-      }
14
-      break;
15
-    case "inbox":
16
-      if (query['action'] == "viewconv" || query['action'] == "compose") {
17
-        $("#messageform").validate();
18
-      }
19
-      break;
20
-    case "user":
21
-      if (query['action'] == "notify") {
22
-        $("#filter_form").validate();
23
-      }
24
-      break;
25
-    case "requests":
26
-      if (query['action'] == "new") {
27
-        $("#request_form").preventDoubleSubmission();
28
-      }
29
-      break;
30
-    case "sitehistory":
31
-      if (query['action'] == "edit") {
32
-        $("#event_form").validate();
33
-      }
34
-      break;
35
-    case "tools":
36
-      if (query['action'] == "calendar") {
37
-        $("#event_form").validate();
38
-      }
39
-      if (query['action'] == "mass_pm") {
40
-        $("#messageform").validate();
41
-      }
42
-      break;
43
-    default:
44
-      break;
45
-  }
2
+  var path = window.location.pathname.split('/').reverse()[0].split(".")[0];
3
+  var action = (window.location.search.match(/action=([^&]+q)/)||[,''])[1];
4
+  if (path == "forums" && action == "new") $("#newthreadform").validate();
5
+  if (path == "reports" && action == "report") $("#report_form").validate();
6
+  if (path == "inbox" && (action == "viewconv" || action == "compose")) $("#messageform").validate();
7
+  if (path == "user" && action == "notify") $("#filter_form").validate();
8
+  if (path == "requests" && action == "new") $("#request_form").preventDoubleSubmission();
9
+  if (path == "sitehistory" && action == "edit") $("#event_form").validate();
10
+  if (path == "tools" && action == "calendar") $("#event_form").validate();
11
+  if (path == "tools" && action == "mass_pm") $("#messageform").validate();
46 12
 });

+ 188
- 54
static/functions/global.js View File

@@ -1,3 +1,189 @@
1
+"use strict";
2
+
3
+//PHP ports
4
+function html_entity_decode(str) {
5
+  var el = document.createElement("div");
6
+  el.innerHTML = str;
7
+  for (var i = 0, ret = ''; i < el.childNodes.length; i++) {
8
+    ret += el.childNodes[i].nodeValue;
9
+  }
10
+  return ret;
11
+}
12
+
13
+function get_size(size) {
14
+  var steps = 0;
15
+  for (; size >= 1024; size /= 1024, steps++);
16
+  var exts = ['B','KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB']
17
+  return (size.toFixed(2) + (exts[steps]||''))
18
+}
19
+
20
+function ratio(a, b) {
21
+  var rc = 'r50';
22
+  for (var i of [[5,  '20'],[2,  '10'],[1,  '09'],[0.9,'08'],[0.8,'07'],[0.7,'06'],
23
+                 [0.6,'05'],[0.5,'04'],[0.4,'03'],[0.3,'02'],[0.2,'01'],[0.1,'00']]) {
24
+    if (a/b < i[0]) rc = 'r'+i[1];
25
+  }
26
+  if (b == 0) return a ? '<span class="r99">∞</span>' : '--';
27
+  return '<span class="'+rc+'">'+((a/b)-0.005).toFixed(2)+'</span>';
28
+}
29
+
30
+function save_message(message, err = false) {
31
+  var messageDiv = document.createElement("div");
32
+  messageDiv.className = err ? "error_message" : "save_message";
33
+  messageDiv.innerHTML = message;
34
+  $("#content").raw().insertBefore(messageDiv,$("#content").raw().firstChild);
35
+}
36
+
37
+$.fn.extend({
38
+  results: function () {
39
+    return this.size();
40
+  },
41
+  gshow: function () {
42
+    return this.remove_class('hidden');
43
+  },
44
+  ghide: function (force) {
45
+    return this.add_class('hidden', force);
46
+  },
47
+  gtoggle: function (force) {
48
+    if (this[0].className.split(' ').indexOf('hidden') == -1) {
49
+      this.add_class('hidden', force);
50
+    } else {
51
+      this.remove_class('hidden');
52
+    }
53
+    return this;
54
+  },
55
+  listen: function (event, callback) {
56
+    for (var i = 0, il = this.size(); i < il; i++) {
57
+      var object = this[i];
58
+      if (document.addEventListener) {
59
+        object.addEventListener(event, callback, false);
60
+      } else {
61
+        object.attachEvent('on' + event, callback);
62
+      }
63
+    }
64
+    return this;
65
+  },
66
+  add_class: function (class_name, force) {
67
+    for (var i = 0, il = this.size(); i < il; i++) {
68
+      var object = this[i];
69
+      if (object.className === '') {
70
+        object.className = class_name;
71
+      } else if (force || object.className.split(' ').indexOf(class_name) == -1) {
72
+        object.className = object.className + ' ' + class_name;
73
+      }
74
+    }
75
+    return this;
76
+  },
77
+  remove_class: function (class_name) {
78
+    for (var i = 0, il = this.size(); i < il; i++) {
79
+      var object = this[i];
80
+      var classes = object.className.split(' ');
81
+      var result = classes.indexOf(class_name);
82
+      if (result != -1) {
83
+        classes.splice(result, 1);
84
+        object.className = classes.join(' ');
85
+      }
86
+    }
87
+    return this;
88
+  },
89
+  has_class: function(class_name) {
90
+    for (var i = 0, il = this.size(); i < il; i++) {
91
+      var object = this[i];
92
+      var classes = object.className.split(' ');
93
+      if (classes.indexOf(class_name) != -1) {
94
+        return true;
95
+      }
96
+    }
97
+    return false;
98
+  },
99
+  toggle_class: function(class_name) {
100
+    for (var i = 0, il = this.size(); i < il; i++) {
101
+      var object = this[i];
102
+      var classes = object.className.split(' ');
103
+      var result = classes.indexOf(class_name);
104
+      if (result != -1) {
105
+        classes.splice(result, 1);
106
+        object.className = classes.join(' ');
107
+      } else {
108
+        if (object.className === '') {
109
+          object.className = class_name;
110
+        } else {
111
+          object.className = object.className + ' ' + class_name;
112
+        }
113
+      }
114
+    }
115
+    return this;
116
+  },
117
+  disable : function () {
118
+    $(this).prop('disabled', true);
119
+    return this;
120
+  },
121
+  enable : function () {
122
+    $(this).prop('disabled', false);
123
+    return this;
124
+  },
125
+  raw: function (number) {
126
+    if (typeof number == 'undefined') {
127
+      number = 0;
128
+    }
129
+    return $(this).get(number);
130
+  },
131
+  nextElementSibling: function () {
132
+    var here = this[0];
133
+    if (here.nextElementSibling) {
134
+      return $(here.nextElementSibling);
135
+    }
136
+    do {
137
+      here = here.nextSibling;
138
+    } while (here.nodeType != 1);
139
+    return $(here);
140
+  },
141
+  previousElementSibling: function () {
142
+    var here = this[0];
143
+    if (here.previousElementSibling) {
144
+      return $(here.previousElementSibling);
145
+    }
146
+    do {
147
+      here = here.nextSibling;
148
+    } while (here.nodeType != 1);
149
+    return $(here);
150
+  },
151
+  updateTooltip: function(tooltip) {
152
+    if ($.fn.tooltipster) {
153
+      $(this).tooltipster('update', tooltip);
154
+    } else {
155
+      $(this).attr('title', tooltip);
156
+    }
157
+    return this;
158
+  },
159
+
160
+  // Disable unset form elements to allow search URLs cleanups
161
+  disableUnset: function() {
162
+    $('input, select', this).filter(function() {
163
+      return $(this).val() === "";
164
+    }).disable();
165
+    return this;
166
+  },
167
+
168
+  // Prevent double submission of forms
169
+  preventDoubleSubmission: function() {
170
+    $(this).submit(function(e) {
171
+      var $form = $(this);
172
+      if ($form.data('submitted') === true) {
173
+        e.preventDefault();
174
+      } else {
175
+        $form.data('submitted', true);
176
+      }
177
+    });
178
+    return this;
179
+  }
180
+});
181
+
182
+if ($('meta[name=authkey]').raw()) {
183
+  var authkey = $('meta[name=authkey]').raw().content;
184
+  var userid = parseInt($('meta[name=userid]').raw().content);
185
+}
186
+
1 187
 /**
2 188
  * Check or uncheck checkboxes in formElem
3 189
  * If masterElem is false, toggle each box, otherwise use masterElem's status on all boxes
@@ -14,13 +200,6 @@ function toggleChecks(formElem, masterElem, elemSelector) {
14 200
   }
15 201
 }
16 202
 
17
-//Lightbox stuff
18
-
19
-/*
20
- * If loading from a thumbnail, the lightbox is shown first with a "loading" screen
21
- * while the full size image loads, then the HTML of the lightbox is replaced with the image.
22
- */
23
-
24 203
 var lightbox = {
25 204
   init: function (image, size) {
26 205
     if ($('#lightbox').length == 0 || $('#curtain').length == 0) {
@@ -49,7 +228,6 @@ var lightbox = {
49 228
       tmp.style.visibility = 'hidden';
50 229
       tmp.src = image.src;
51 230
       image.naturalWidth = tmp.width;
52
-      delete tmp;
53 231
     }
54 232
     if (image.naturalWidth > size) {
55 233
       lightbox.box(image);
@@ -90,38 +268,9 @@ window.onkeydown = function(e) {
90 268
   }
91 269
 }
92 270
 
93
-/* Still some issues
94
-function caps_check(e) {
95
-  if (e === undefined) {
96
-    e = window.event;
97
-  }
98
-  if (e.which === undefined) {
99
-    e.which = e.keyCode;
100
-  }
101
-  if (e.which > 47 && e.which < 58) {
102
-    return;
103
-  }
104
-  if ((e.which > 64 && e.which < 91 && !e.shiftKey) || (e.which > 96 && e.which < 123 && e.shiftKey)) {
105
-    $('#capslock').gshow();
106
-  }
107
-}
108
-*/
109
-
110
-function hexify(str) {
111
-  str = str.replace(/rgb\(|\)/g, "").split(",");
112
-  str[0] = parseInt(str[0], 10).toString(16).toLowerCase();
113
-  str[1] = parseInt(str[1], 10).toString(16).toLowerCase();
114
-  str[2] = parseInt(str[2], 10).toString(16).toLowerCase();
115
-  str[0] = (str[0].length == 1) ? '0' + str[0] : str[0];
116
-  str[1] = (str[1].length == 1) ? '0' + str[1] : str[1];
117
-  str[2] = (str[2].length == 1) ? '0' + str[2] : str[2];
118
-  return (str.join(""));
119
-}
120
-
121 271
 function resize(id) {
122 272
   var textarea = document.getElementById(id);
123 273
   if (textarea.scrollHeight > textarea.clientHeight) {
124
-    //textarea.style.overflowY = 'hidden';
125 274
     textarea.style.height = Math.min(1000, textarea.scrollHeight + textarea.style.fontSize) + 'px';
126 275
   }
127 276
 }
@@ -146,21 +295,6 @@ function remove_selection(index) {
146 295
   $('#opt' + index).raw().disabled = '';
147 296
 }
148 297
 
149
-// Thank you http://stackoverflow.com/questions/4578398/selecting-all-text-within-a-div-on-a-single-left-click-with-javascript
150
-function select_all(el) {
151
-  if (typeof window.getSelection != "undefined" && typeof document.createRange != "undefined") {
152
-    var range = document.createRange();
153
-    range.selectNodeContents(el);
154
-    var sel = window.getSelection();
155
-    sel.removeAllRanges();
156
-    sel.addRange(range);
157
-  } else if (typeof document.selection != "undefined" && typeof document.body.createTextRange != "undefined") {
158
-    var textRange = document.body.createTextRange();
159
-    textRange.moveToElementText(el);
160
-    textRange.select();
161
-  }
162
-}
163
-
164 298
 function preload(image) {
165 299
   var img = document.createElement('img')
166 300
   img.style.display = 'none'
@@ -170,7 +304,7 @@ function preload(image) {
170 304
 }
171 305
 
172 306
 function getCover(event) {
173
-  image = event.target.attributes['data-cover'].value
307
+  var image = event.target.attributes['data-cover'].value
174 308
   $('#coverCont img').remove()
175 309
   var coverCont = ($('#coverCont').length==0)?document.body.appendChild(document.createElement('div')):$('#coverCont')[0]
176 310
   coverCont.id = 'coverCont'
@@ -194,7 +328,7 @@ function ungetCover(event) {
194 328
 }
195 329
 
196 330
 // Apparently firefox doesn't implement NodeList.forEach until FF50
197
-// Remove this shim awter that's stable for a while
331
+// Remove this shim after that's stable for a while
198 332
 if (typeof NodeList.prototype.forEach !== 'function') {
199 333
   NodeList.prototype.forEach = Array.prototype.forEach
200 334
 }

+ 1
- 1
static/functions/requests.js View File

@@ -31,7 +31,7 @@ function Vote(amount, requestid) {
31 31
 
32 32
   ajax.get('requests.php?action=takevote&id=' + requestid + '&auth=' + authkey + '&amount=' + amount, function (response) {
33 33
       if (response == 'bankrupt') {
34
-        error_message("You do not have sufficient upload credit to add " + get_size(amount) + " to this request");
34
+        save_message("You do not have sufficient upload credit to add " + get_size(amount) + " to this request", true);
35 35
         return;
36 36
       } else if (response == 'dupesuccess') {
37 37
         //No increment

+ 0
- 368
static/functions/script_start.js View File

@@ -1,368 +0,0 @@
1
-"use strict";
2
-
3
-/* Prototypes */
4
-if (!String.prototype.trim) {
5
-  String.prototype.trim = function () {
6
-    return this.replace(/^\s+|\s+$/g,'');
7
-  };
8
-}
9
-
10
-var listener = {
11
-  set: function (el,type,callback) {
12
-    if (document.addEventListener) {
13
-      el.addEventListener(type, callback, false);
14
-    } else {
15
-      // IE hack courtesy of http://blog.stchur.com/2006/10/12/fixing-ies-attachevent-failures
16
-      var f = function() {
17
-        callback.call(el);
18
-      };
19
-      el.attachEvent('on' + type, f);
20
-    }
21
-  }
22
-};
23
-
24
-/* Site wide functions */
25
-
26
-// http://www.thefutureoftheweb.com/blog/adddomloadevent
27
-// retrieved 2010-08-12
28
-var addDOMLoadEvent = (
29
-  function() {
30
-    var e = [], t, s, n, i, o, d = document, w = window, r = 'readyState', c = 'onreadystatechange',
31
-      x = function() {
32
-          n = 1;
33
-          clearInterval(t);
34
-          while (i = e.shift()) {
35
-            i();
36
-          }
37
-          if (s) {
38
-            s[c] = ''
39
-          }
40
-        };
41
-    return function(f) {
42
-        if (n) {
43
-          return f();
44
-        }
45
-        if (!e[0]) {
46
-          d.addEventListener && d.addEventListener("DOMContentLoaded", x, false);
47
-          /*@cc_on@*//*@if(@_win32)d.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");s=d.getElementById("__ie_onload");s[c]=function(){s[r]=="complete"&&x()};/*@end@*/
48
-          if (/WebKit/i.test(navigator.userAgent))
49
-            t = setInterval(function() {
50
-                /loaded|complete/.test(d[r]) && x()
51
-                }, 10);
52
-            o = w.onload;
53
-            w.onload = function() {
54
-                x();
55
-                o && o()
56
-                }
57
-        }
58
-        e.push(f)
59
-        }
60
-  }
61
-)();
62
-
63
-//PHP ports
64
-function isset(variable) {
65
-  return (typeof(variable) === 'undefined') ? false : true;
66
-}
67
-
68
-function is_array(input) {
69
-  return typeof(input) === 'object' && input instanceof Array;
70
-}
71
-
72
-function function_exists(function_name) {
73
-  return (typeof this.window[function_name] === 'function');
74
-}
75
-
76
-function html_entity_decode(str) {
77
-  var el = document.createElement("div");
78
-  el.innerHTML = str;
79
-  for (var i = 0, ret = ''; i < el.childNodes.length; i++) {
80
-    ret += el.childNodes[i].nodeValue;
81
-  }
82
-  return ret;
83
-}
84
-
85
-function get_size(size) {
86
-  var steps = 0
87
-  while (size >= 1024) {
88
-    steps++
89
-    size = size / 1024
90
-  }
91
-  var exts = ['B','KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB']
92
-  return (size.toFixed(2) + (exts[steps]||''))
93
-}
94
-
95
-function get_ratio_color(ratio) {
96
-  if (ratio < 0.1) { return 'r00'; }
97
-  if (ratio < 0.2) { return 'r01'; }
98
-  if (ratio < 0.3) { return 'r02'; }
99
-  if (ratio < 0.4) { return 'r03'; }
100
-  if (ratio < 0.5) { return 'r04'; }
101
-  if (ratio < 0.6) { return 'r05'; }
102
-  if (ratio < 0.7) { return 'r06'; }
103
-  if (ratio < 0.8) { return 'r07'; }
104
-  if (ratio < 0.9) { return 'r08'; }
105
-  if (ratio < 1) { return 'r09'; }
106
-  if (ratio < 2) { return 'r10'; }
107
-  if (ratio < 5) { return 'r20'; }
108
-  return 'r50';
109
-}
110
-
111
-function ratio(dividend, divisor, color) {
112
-  if (!color) {
113
-    color = true;
114
-  }
115
-  if (divisor == 0 && dividend == 0) {
116
-    return '--';
117
-  } else if (divisor == 0) {
118
-    return '<span class="r99">∞</span>';
119
-  } else if (dividend == 0 && divisor > 0) {
120
-    return '<span class="r00">-∞</span>';
121
-  }
122
-  var rat = ((dividend / divisor) - 0.005).toFixed(2); //Subtract .005 to floor to 2 decimals
123
-  if (color) {
124
-    var col = get_ratio_color(rat);
125
-    if (col) {
126
-      rat = '<span class="' + col + '">' + rat + '</span>';
127
-    }
128
-  }
129
-  return rat;
130
-}
131
-
132
-
133
-function save_message(message) {
134
-  var messageDiv = document.createElement("div");
135
-  messageDiv.className = "save_message";
136
-  messageDiv.innerHTML = message;
137
-  $("#content").raw().insertBefore(messageDiv,$("#content").raw().firstChild);
138
-}
139
-
140
-function error_message(message) {
141
-  var messageDiv = document.createElement("div");
142
-  messageDiv.className = "error_message";
143
-  messageDiv.innerHTML = message;
144
-  $("#content").raw().insertBefore(messageDiv,$("#content").raw().firstChild);
145
-}
146
-
147
-//returns key if true, and false if false. better than the PHP funciton
148
-function in_array(needle, haystack, strict) {
149
-  if (strict === undefined) {
150
-    strict = false;
151
-  }
152
-  for (var key in haystack) {
153
-    if ((haystack[key] == needle && strict === false) || haystack[key] === needle) {
154
-      return true;
155
-    }
156
-  }
157
-  return false;
158
-}
159
-
160
-function array_search(needle, haystack, strict) {
161
-  if (strict === undefined) {
162
-    strict = false;
163
-  }
164
-  for (var key in haystack) {
165
-    if ((strict === false && haystack[key] == needle) || haystack[key] === needle) {
166
-      return key;
167
-    }
168
-  }
169
-  return false;
170
-}
171
-
172
-var util = function (selector, context) {
173
-  return new util.fn.init(selector, context);
174
-}
175
-
176
-function URL() {
177
-  var path = window.location.pathname.split('/');
178
-  var path = path[path.length - 1].split(".")[0];
179
-  var splitted = window.location.search.substr(1).split("&");
180
-  var query = {};
181
-  var length = 0;
182
-  for (var i = 0; i < splitted.length; i++) {
183
-    var q = splitted[i].split("=");
184
-    if (q != "") {
185
-      query[q[0]] = q[1];
186
-      length++;
187
-    }
188
-  };
189
-  query['length'] = length;
190
-  var response = new Array();
191
-  response['path'] = path;
192
-  response['query'] = query;
193
-  return response;
194
-}
195
-
196
-function isNumberKey(e) {
197
-  var charCode = (e.which) ? e.which : e.keyCode
198
-  if (charCode == 46) {
199
-    return true;
200
-  }
201
-  if (charCode > 31 && (charCode < 48 || charCode > 57)) {
202
-    return false;
203
-  }
204
-  return true;
205
-}
206
-
207
-function sleep(milliseconds) {
208
-  var start = new Date().getTime();
209
-  for (var i = 0; i < 1e7; i++) {
210
-    if ((new Date().getTime() - start) > milliseconds){
211
-      break;
212
-    }
213
-  }
214
-}
215
-
216
-$.fn.extend({
217
-  results: function () {
218
-    return this.size();
219
-  },
220
-  gshow: function () {
221
-    return this.remove_class('hidden');
222
-  },
223
-  ghide: function (force) {
224
-    return this.add_class('hidden', force);
225
-  },
226
-  gtoggle: function (force) {
227
-    //Should we interate and invert all entries, or just go by the first?
228
-    if (!in_array('hidden', this[0].className.split(' '))) {
229
-      this.add_class('hidden', force);
230
-    } else {
231
-      this.remove_class('hidden');
232
-    }
233
-    return this;
234
-  },
235
-  listen: function (event, callback) {
236
-    for (var i = 0, il = this.size(); i < il; i++) {
237
-      var object = this[i];
238
-      if (document.addEventListener) {
239
-        object.addEventListener(event, callback, false);
240
-      } else {
241
-        object.attachEvent('on' + event, callback);
242
-      }
243
-    }
244
-    return this;
245
-  },
246
-  add_class: function (class_name, force) {
247
-    for (var i = 0, il = this.size(); i < il; i++) {
248
-      var object = this[i];
249
-      if (object.className === '') {
250
-        object.className = class_name;
251
-      } else if (force || !in_array(class_name, object.className.split(' '))) {
252
-        object.className = object.className + ' ' + class_name;
253
-      }
254
-    }
255
-    return this;
256
-  },
257
-  remove_class: function (class_name) {
258
-    for (var i = 0, il = this.size(); i < il; i++) {
259
-      var object = this[i];
260
-      var classes = object.className.split(' ');
261
-      var result = array_search(class_name, classes);
262
-      if (result !== false) {
263
-        classes.splice(result, 1);
264
-        object.className = classes.join(' ');
265
-      }
266
-    }
267
-    return this;
268
-  },
269
-  has_class: function(class_name) {
270
-    for (var i = 0, il = this.size(); i < il; i++) {
271
-      var object = this[i];
272
-      var classes = object.className.split(' ');
273
-      if (array_search(class_name, classes)) {
274
-        return true;
275
-      }
276
-    }
277
-    return false;
278
-  },
279
-  toggle_class: function(class_name) {
280
-    for (var i = 0, il = this.size(); i < il; i++) {
281
-      var object = this[i];
282
-      var classes = object.className.split(' ');
283
-      var result = array_search(class_name, classes);
284
-      if (result !== false) {
285
-        classes.splice(result, 1);
286
-        object.className = classes.join(' ');
287
-      } else {
288
-        if (object.className === '') {
289
-          object.className = class_name;
290
-        } else {
291
-          object.className = object.className + ' ' + class_name;
292
-        }
293
-      }
294
-    }
295
-    return this;
296
-  },
297
-  disable : function () {
298
-    $(this).prop('disabled', true);
299
-    return this;
300
-  },
301
-  enable : function () {
302
-    $(this).prop('disabled', false);
303
-    return this;
304
-  },
305
-  raw: function (number) {
306
-    if (typeof number == 'undefined') {
307
-      number = 0;
308
-    }
309
-    return $(this).get(number);
310
-  },
311
-  nextElementSibling: function () {
312
-    var here = this[0];
313
-    if (here.nextElementSibling) {
314
-      return $(here.nextElementSibling);
315
-    }
316
-    do {
317
-      here = here.nextSibling;
318
-    } while (here.nodeType != 1);
319
-    return $(here);
320
-  },
321
-  previousElementSibling: function () {
322
-    var here = this[0];
323
-    if (here.previousElementSibling) {
324
-      return $(here.previousElementSibling);
325
-    }
326
-    do {
327
-      here = here.nextSibling;
328
-    } while (here.nodeType != 1);
329
-    return $(here);
330
-  },
331
-  updateTooltip: function(tooltip) {
332
-    if ($.fn.tooltipster) {
333
-      $(this).tooltipster('update', tooltip);
334
-    } else {
335
-      $(this).attr('title', tooltip);
336
-    }
337
-    return this;
338
-  },
339
-
340
-  // Disable unset form elements to allow search URLs cleanups
341
-  disableUnset: function() {
342
-    $('input, select', this).filter(function() {
343
-      return $(this).val() === "";
344
-    }).disable();
345
-    return this;
346
-  },
347
-
348
-  // Prevent double submission of forms
349
-  preventDoubleSubmission: function() {
350
-    $(this).submit(function(e) {
351
-      var $form = $(this);
352
-      if ($form.data('submitted') === true) {
353
-        // Previously submitted - don't submit again
354
-        e.preventDefault();
355
-      } else {
356
-        // Mark it so that the next submit can be ignored
357
-        $form.data('submitted', true);
358
-      }
359
-    });
360
-    // Keep chainability
361
-    return this;
362
-  }
363
-});
364
-
365
-if ($('meta[name=authkey]').raw()) {
366
-  var authkey = $('meta[name=authkey]').raw().content;
367
-  var userid = parseInt($('meta[name=userid]').raw().content);
368
-}

+ 2
- 2
static/functions/torrent.js View File

@@ -130,7 +130,7 @@ function ArtistManagerSubmit() {
130 130
   $('#artists_selection').raw().value = Selection.join(',');
131 131
   if ((($('#artists_importance').raw().value != 1 && $('#artists_importance').raw().value != 4 && $('#artists_importance').raw().value != 6) || $('#manager_action').raw().value == 'delete') && MainSelectionCount == MainArtistCount) {
132 132
     if (!$('.error_message').raw()) {
133
-      error_message('All groups need to have at least one main artist, composer, or DJ.');
133
+      save_message('All groups need to have at least one main artist, composer, or DJ.', true);
134 134
     }
135 135
     $('.error_message').raw().scrollIntoView();
136 136
     return;
@@ -166,7 +166,7 @@ function Vote(amount, requestid) {
166 166
 
167 167
   ajax.get('requests.php?action=takevote&id=' + requestid + '&auth=' + authkey + '&amount=' + amount, function (response) {
168 168
       if (response == 'bankrupt') {
169
-        error_message("You do not have sufficient upload credit to add " + get_size(amount) + " to this request");
169
+        save_message("You do not have sufficient upload credit to add " + get_size(amount) + " to this request", true);
170 170
         return;
171 171
       } else if (response == 'dupesuccess') {
172 172
         //No increment

+ 2
- 2
static/functions/user.js View File

@@ -127,7 +127,7 @@ function ParanoiaResetOn() {
127 127
   $('input[name=p_collages_l]').raw().checked = false;
128 128
 }
129 129
 
130
-addDOMLoadEvent(AlterParanoia);
130
+document.addEventListener("DOMContentLoaded", AlterParanoia);
131 131
 
132 132
 function ToggleWarningAdjust(selector) {
133 133
   if (selector.options[selector.selectedIndex].value == '---') {
@@ -139,7 +139,7 @@ function ToggleWarningAdjust(selector) {
139 139
   }
140 140
 }
141 141
 
142
-addDOMLoadEvent(ToggleIdenticons);
142
+document.addEventListener("DOMContentLoaded", ToggleIdenticons);
143 143
 function ToggleIdenticons() {
144 144
   var disableAvatars = $('#disableavatars');
145 145
   if (disableAvatars.size()) {

+ 5
- 1
static/functions/user_notifications.js View File

@@ -14,7 +14,11 @@ $(document).ready(function() {
14 14
 });
15 15
 
16 16
 function getSkippedPage() {
17
-  var skip, url = new URL();
17
+  var skip
18
+  var url = {
19
+    path: window.location.pathname.split('/').reverse()[0].split('.')[0],
20
+    query: window.location.search.slice(1).split('&').reduce((a,b)=>Object.assign(a,{[b.split('=')[0]]:b.split('=')[1]}),{})
21
+  };
18 22
   switch(url.path) {
19 23
     case "inbox":
20 24
       if (url.query.length == 0 || (url.query.length == 1 && url.query.hasOwnProperty('sort'))) {

Loading…
Cancel
Save