Oppaitime's version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

global.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. "use strict";
  2. //PHP ports
  3. function html_entity_decode(str) {
  4. var el = document.createElement("div");
  5. el.innerHTML = str;
  6. for (var i = 0, ret = ''; i < el.childNodes.length; i++) {
  7. ret += el.childNodes[i].nodeValue;
  8. }
  9. return ret;
  10. }
  11. function get_size(size) {
  12. var steps = 0;
  13. for (; size >= 1024; size /= 1024, steps++);
  14. var exts = ['B','KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB']
  15. return (size.toFixed(2) + (exts[steps]||''))
  16. }
  17. function ratio(a, b) {
  18. var rc = 'r50';
  19. for (var i of [[5, '20'],[2, '10'],[1, '09'],[0.9,'08'],[0.8,'07'],[0.7,'06'],
  20. [0.6,'05'],[0.5,'04'],[0.4,'03'],[0.3,'02'],[0.2,'01'],[0.1,'00']]) {
  21. if (a/b < i[0]) rc = 'r'+i[1];
  22. }
  23. if (b == 0) return a ? '<span class="r99">∞</span>' : '--';
  24. return '<span class="'+rc+'">'+((a/b)-0.005).toFixed(2)+'</span>';
  25. }
  26. function save_message(message, err = false) {
  27. var messageDiv = document.createElement("div");
  28. messageDiv.className = err ? "error_message" : "save_message";
  29. messageDiv.innerHTML = message;
  30. $("#content").raw().insertBefore(messageDiv,$("#content").raw().firstChild);
  31. }
  32. $.fn.extend({
  33. results: function () {
  34. return this.size();
  35. },
  36. gshow: function () {
  37. return this.remove_class('hidden');
  38. },
  39. ghide: function (force) {
  40. return this.add_class('hidden', force);
  41. },
  42. gtoggle: function (force) {
  43. if (this[0].className.split(' ').indexOf('hidden') == -1) {
  44. this.add_class('hidden', force);
  45. } else {
  46. this.remove_class('hidden');
  47. }
  48. return this;
  49. },
  50. listen: function (event, callback) {
  51. for (var i = 0, il = this.size(); i < il; i++) {
  52. var object = this[i];
  53. if (document.addEventListener) {
  54. object.addEventListener(event, callback, false);
  55. } else {
  56. object.attachEvent('on' + event, callback);
  57. }
  58. }
  59. return this;
  60. },
  61. add_class: function (class_name, force) {
  62. for (var i = 0, il = this.size(); i < il; i++) {
  63. var object = this[i];
  64. if (object.className === '') {
  65. object.className = class_name;
  66. } else if (force || object.className.split(' ').indexOf(class_name) == -1) {
  67. object.className = object.className + ' ' + class_name;
  68. }
  69. }
  70. return this;
  71. },
  72. remove_class: function (class_name) {
  73. for (var i = 0, il = this.size(); i < il; i++) {
  74. var object = this[i];
  75. var classes = object.className.split(' ');
  76. var result = classes.indexOf(class_name);
  77. if (result != -1) {
  78. classes.splice(result, 1);
  79. object.className = classes.join(' ');
  80. }
  81. }
  82. return this;
  83. },
  84. has_class: function(class_name) {
  85. for (var i = 0, il = this.size(); i < il; i++) {
  86. var object = this[i];
  87. var classes = object.className.split(' ');
  88. if (classes.indexOf(class_name) != -1) {
  89. return true;
  90. }
  91. }
  92. return false;
  93. },
  94. toggle_class: function(class_name) {
  95. for (var i = 0, il = this.size(); i < il; i++) {
  96. var object = this[i];
  97. var classes = object.className.split(' ');
  98. var result = classes.indexOf(class_name);
  99. if (result != -1) {
  100. classes.splice(result, 1);
  101. object.className = classes.join(' ');
  102. } else {
  103. if (object.className === '') {
  104. object.className = class_name;
  105. } else {
  106. object.className = object.className + ' ' + class_name;
  107. }
  108. }
  109. }
  110. return this;
  111. },
  112. disable : function () {
  113. $(this).prop('disabled', true);
  114. return this;
  115. },
  116. enable : function () {
  117. $(this).prop('disabled', false);
  118. return this;
  119. },
  120. raw: function (number) {
  121. if (typeof number == 'undefined') {
  122. number = 0;
  123. }
  124. return $(this).get(number);
  125. },
  126. nextElementSibling: function () {
  127. var here = this[0];
  128. if (here.nextElementSibling) {
  129. return $(here.nextElementSibling);
  130. }
  131. do {
  132. here = here.nextSibling;
  133. } while (here.nodeType != 1);
  134. return $(here);
  135. },
  136. previousElementSibling: function () {
  137. var here = this[0];
  138. if (here.previousElementSibling) {
  139. return $(here.previousElementSibling);
  140. }
  141. do {
  142. here = here.nextSibling;
  143. } while (here.nodeType != 1);
  144. return $(here);
  145. },
  146. updateTooltip: function(tooltip) {
  147. if ($.fn.tooltipster) {
  148. $(this).tooltipster('update', tooltip);
  149. } else {
  150. $(this).attr('title', tooltip);
  151. }
  152. return this;
  153. },
  154. // Disable unset form elements to allow search URLs cleanups
  155. disableUnset: function() {
  156. $('input, select', this).filter(function() {
  157. return $(this).val() === "";
  158. }).disable();
  159. return this;
  160. },
  161. // Prevent double submission of forms
  162. preventDoubleSubmission: function() {
  163. $(this).submit(function(e) {
  164. var $form = $(this);
  165. if ($form.data('submitted') === true) {
  166. e.preventDefault();
  167. } else {
  168. $form.data('submitted', true);
  169. }
  170. });
  171. return this;
  172. }
  173. });
  174. if ($('meta[name=authkey]').raw()) {
  175. var authkey = $('meta[name=authkey]').raw().content;
  176. var userid = parseInt($('meta[name=userid]').raw().content);
  177. }
  178. /**
  179. * Check or uncheck checkboxes in formElem
  180. * If masterElem is false, toggle each box, otherwise use masterElem's status on all boxes
  181. * If elemSelector is false, act on all checkboxes in formElem
  182. */
  183. function toggleChecks(formElem, masterElem, elemSelector) {
  184. elemSelector = elemSelector || 'input:checkbox';
  185. if (masterElem) {
  186. $('#' + formElem + ' ' + elemSelector).prop('checked', masterElem.checked);
  187. } else {
  188. $('#' + formElem + ' ' + elemSelector).each(function() {
  189. this.checked = !this.checked;
  190. })
  191. }
  192. }
  193. var lightbox = {
  194. init: function (image, size) {
  195. if ($('#lightbox').length == 0 || $('#curtain').length == 0) {
  196. var lightboxEl = document.createElement('div')
  197. lightboxEl.id = 'lightbox'
  198. lightboxEl.className = 'lightbox hidden'
  199. var curtainEl = document.createElement('div')
  200. curtainEl.id = 'curtain'
  201. curtainEl.className = 'curtain hidden'
  202. $('#wrapper')[0].appendChild(lightboxEl)
  203. $('#wrapper')[0].appendChild(curtainEl)
  204. }
  205. if (typeof(image) == 'string') {
  206. $('#lightbox').gshow().listen('click', lightbox.unbox).raw().innerHTML =
  207. '<p size="7" style="color: gray; font-size: 50px;">Loading...<p>';
  208. $('#curtain').gshow().listen('click', lightbox.unbox);
  209. var src = image;
  210. image = new Image();
  211. image.onload = function() {
  212. lightbox.box_async(image);
  213. }
  214. image.src = src;
  215. }
  216. if (image.naturalWidth === undefined) {
  217. var tmp = document.createElement('img');
  218. tmp.style.visibility = 'hidden';
  219. tmp.src = image.src;
  220. image.naturalWidth = tmp.width;
  221. }
  222. if (image.naturalWidth > size) {
  223. lightbox.box(image);
  224. }
  225. },
  226. box: function (image) {
  227. var hasA = false;
  228. if (image.parentNode != null && image.parentNode.tagName.toUpperCase() == 'A') {
  229. hasA = true;
  230. }
  231. if (!hasA) {
  232. $('#lightbox').gshow().listen('click', lightbox.unbox).raw().innerHTML = '<img src="' + image.src + '" alt="" />';
  233. $('#curtain').gshow().listen('click', lightbox.unbox);
  234. }
  235. },
  236. box_async: function (image) {
  237. var hasA = false;
  238. if (image.parentNode != null && image.parentNode.tagName.toUpperCase() == 'A') {
  239. hasA = true;
  240. }
  241. if (!hasA) {
  242. $('#lightbox').raw().innerHTML = '<img src="' + image.src + '" alt="" />';
  243. }
  244. },
  245. unbox: function (data) {
  246. $('#curtain').ghide();
  247. $('#lightbox').ghide().raw().innerHTML = '';
  248. }
  249. };
  250. // Horrible hack to let arrow keys work as forward/back in lightbox
  251. window.onkeydown = function(e) {
  252. e = e || window.event
  253. if (e.keyCode == 37 || e.keyCode == 39) {
  254. if ($('#lightbox').raw() && !$('#lightbox').raw().classList.contains('hidden')) {
  255. ($('[id!="lightbox"] > [src="'+$('#lightbox > img').raw().src+'"]').raw()[((e.keyCode==39)?'next':'previous')+'Sibling'].click()||function(){})()
  256. }
  257. }
  258. }
  259. function resize(id) {
  260. var textarea = document.getElementById(id);
  261. if (textarea.scrollHeight > textarea.clientHeight) {
  262. textarea.style.height = Math.min(1000, textarea.scrollHeight + textarea.style.fontSize) + 'px';
  263. }
  264. }
  265. //ZIP downloader stuff
  266. function add_selection() {
  267. var selected = $('#formats').raw().options[$('#formats').raw().selectedIndex];
  268. if (selected.disabled === false) {
  269. var listitem = document.createElement("li");
  270. listitem.id = 'list' + selected.value;
  271. listitem.innerHTML = ' <input type="hidden" name="list[]" value="' + selected.value + '" /> ' +
  272. ' <span style="float: left;">' + selected.innerHTML + '</span>' +
  273. ' <a href="#" onclick="remove_selection(\'' + selected.value + '\'); return false;" style="float: right;" class="brackets">X</a>' +
  274. ' <br style="clear: all;" />';
  275. $('#list').raw().appendChild(listitem);
  276. $('#opt' + selected.value).raw().disabled = true;
  277. }
  278. }
  279. function remove_selection(index) {
  280. $('#list' + index).remove();
  281. $('#opt' + index).raw().disabled = '';
  282. }
  283. function preload(image) {
  284. var img = document.createElement('img')
  285. img.style.display = 'none'
  286. img.src = image
  287. document.body.appendChild(img)
  288. document.body.removeChild(img)
  289. }
  290. function getCover(event) {
  291. var image = event.target.attributes['data-cover'].value
  292. $('#coverCont img').remove()
  293. var coverCont = ($('#coverCont').length==0)?document.body.appendChild(document.createElement('div')):$('#coverCont')[0]
  294. coverCont.id = 'coverCont'
  295. if ($('#coverCont img').length == 0) {
  296. coverCont.appendChild(document.createElement('img'))
  297. }
  298. $('#coverCont img')[0].src = image?image:'/static/common/noartwork/nocover.png'
  299. coverCont.className = (event.clientX > (window.innerWidth/2)) ? 'left' : 'right'
  300. coverCont.style.display = 'block'
  301. //Preload next image
  302. if ($('.torrent_table, .request_table').length > 0) {
  303. var as = $('[data-cover]')
  304. var a = event.target
  305. preload((as[as.toArray().indexOf(a)+1]||as[0]).attributes['data-cover'].value)
  306. preload((as[as.toArray().indexOf(a)-1]||as[0]).attributes['data-cover'].value)
  307. }
  308. }
  309. function ungetCover(event) {
  310. $('#coverCont img').remove()
  311. coverCont.style.display = 'none'
  312. }
  313. // Apparently firefox doesn't implement NodeList.forEach until FF50
  314. // Remove this shim after that's stable for a while
  315. if (typeof NodeList.prototype.forEach !== 'function') {
  316. NodeList.prototype.forEach = Array.prototype.forEach
  317. }
  318. $(function() {
  319. if ($('.request_table').length > 0) {
  320. var a = $('[data-cover]')[0]
  321. if (a) preload(a.attributes['data-cover'].value)
  322. }
  323. document.querySelectorAll('[data-toggle-target]').forEach(function(el) {
  324. el.addEventListener('click', function(event) {
  325. $(el.attributes['data-toggle-target'].value).gtoggle()
  326. if (el.attributes['data-toggle-replace']) {
  327. [el.innerHTML, el.attributes['data-toggle-replace'].value] = [el.attributes['data-toggle-replace'].value, el.innerHTML]
  328. }
  329. })
  330. })
  331. document.querySelectorAll('.lightbox-init').forEach(function(el) {
  332. el.addEventListener('click', function(event) {
  333. lightbox.init(el.attributes['lightbox-img']||el.src, el.attributes['lightbox-size']||el.width)
  334. })
  335. })
  336. })