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 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * Check or uncheck checkboxes in formElem
  3. * If masterElem is false, toggle each box, otherwise use masterElem's status on all boxes
  4. * If elemSelector is false, act on all checkboxes in formElem
  5. */
  6. function toggleChecks(formElem, masterElem, elemSelector) {
  7. elemSelector = elemSelector || 'input:checkbox';
  8. if (masterElem) {
  9. $('#' + formElem + ' ' + elemSelector).prop('checked', masterElem.checked);
  10. } else {
  11. $('#' + formElem + ' ' + elemSelector).each(function() {
  12. this.checked = !this.checked;
  13. })
  14. }
  15. }
  16. //Lightbox stuff
  17. /*
  18. * If loading from a thumbnail, the lightbox is shown first with a "loading" screen
  19. * while the full size image loads, then the HTML of the lightbox is replaced with the image.
  20. */
  21. var lightbox = {
  22. init: function (image, size) {
  23. if ($('#lightbox').length == 0 || $('#curtain').length == 0) {
  24. var lightboxEl = document.createElement('div')
  25. lightboxEl.id = 'lightbox'
  26. lightboxEl.className = 'lightbox hidden'
  27. var curtainEl = document.createElement('div')
  28. curtainEl.id = 'curtain'
  29. curtainEl.className = 'curtain hidden'
  30. $('#wrapper')[0].appendChild(lightboxEl)
  31. $('#wrapper')[0].appendChild(curtainEl)
  32. }
  33. if (typeof(image) == 'string') {
  34. $('#lightbox').gshow().listen('click', lightbox.unbox).raw().innerHTML =
  35. '<p size="7" style="color: gray; font-size: 50px;">Loading...<p>';
  36. $('#curtain').gshow().listen('click', lightbox.unbox);
  37. var src = image;
  38. image = new Image();
  39. image.onload = function() {
  40. lightbox.box_async(image);
  41. }
  42. image.src = src;
  43. }
  44. if (image.naturalWidth === undefined) {
  45. var tmp = document.createElement('img');
  46. tmp.style.visibility = 'hidden';
  47. tmp.src = image.src;
  48. image.naturalWidth = tmp.width;
  49. delete tmp;
  50. }
  51. if (image.naturalWidth > size) {
  52. lightbox.box(image);
  53. }
  54. },
  55. box: function (image) {
  56. var hasA = false;
  57. if (image.parentNode != null && image.parentNode.tagName.toUpperCase() == 'A') {
  58. hasA = true;
  59. }
  60. if (!hasA) {
  61. $('#lightbox').gshow().listen('click', lightbox.unbox).raw().innerHTML = '<img src="' + image.src + '" alt="" />';
  62. $('#curtain').gshow().listen('click', lightbox.unbox);
  63. }
  64. },
  65. box_async: function (image) {
  66. var hasA = false;
  67. if (image.parentNode != null && image.parentNode.tagName.toUpperCase() == 'A') {
  68. hasA = true;
  69. }
  70. if (!hasA) {
  71. $('#lightbox').raw().innerHTML = '<img src="' + image.src + '" alt="" />';
  72. }
  73. },
  74. unbox: function (data) {
  75. $('#curtain').ghide();
  76. $('#lightbox').ghide().raw().innerHTML = '';
  77. }
  78. };
  79. // Horrible hack to let arrow keys work as forward/back in lightbox
  80. window.onkeydown = function(e) {
  81. e = e || window.event
  82. if (e.keyCode == 37 || e.keyCode == 39) {
  83. if ($('#lightbox') && !$('#lightbox').raw().classList.contains('hidden')) {
  84. ($('[id!="lightbox"] > [src="'+$('#lightbox > img').raw().src+'"]').raw()[((e.keyCode==39)?'next':'previous')+'Sibling'].onclick||function(){})()
  85. }
  86. }
  87. }
  88. /* Still some issues
  89. function caps_check(e) {
  90. if (e === undefined) {
  91. e = window.event;
  92. }
  93. if (e.which === undefined) {
  94. e.which = e.keyCode;
  95. }
  96. if (e.which > 47 && e.which < 58) {
  97. return;
  98. }
  99. if ((e.which > 64 && e.which < 91 && !e.shiftKey) || (e.which > 96 && e.which < 123 && e.shiftKey)) {
  100. $('#capslock').gshow();
  101. }
  102. }
  103. */
  104. function hexify(str) {
  105. str = str.replace(/rgb\(|\)/g, "").split(",");
  106. str[0] = parseInt(str[0], 10).toString(16).toLowerCase();
  107. str[1] = parseInt(str[1], 10).toString(16).toLowerCase();
  108. str[2] = parseInt(str[2], 10).toString(16).toLowerCase();
  109. str[0] = (str[0].length == 1) ? '0' + str[0] : str[0];
  110. str[1] = (str[1].length == 1) ? '0' + str[1] : str[1];
  111. str[2] = (str[2].length == 1) ? '0' + str[2] : str[2];
  112. return (str.join(""));
  113. }
  114. function resize(id) {
  115. var textarea = document.getElementById(id);
  116. if (textarea.scrollHeight > textarea.clientHeight) {
  117. //textarea.style.overflowY = 'hidden';
  118. textarea.style.height = Math.min(1000, textarea.scrollHeight + textarea.style.fontSize) + 'px';
  119. }
  120. }
  121. //ZIP downloader stuff
  122. function add_selection() {
  123. var selected = $('#formats').raw().options[$('#formats').raw().selectedIndex];
  124. if (selected.disabled === false) {
  125. var listitem = document.createElement("li");
  126. listitem.id = 'list' + selected.value;
  127. listitem.innerHTML = ' <input type="hidden" name="list[]" value="' + selected.value + '" /> ' +
  128. ' <span style="float: left;">' + selected.innerHTML + '</span>' +
  129. ' <a href="#" onclick="remove_selection(\'' + selected.value + '\'); return false;" style="float: right;" class="brackets">X</a>' +
  130. ' <br style="clear: all;" />';
  131. $('#list').raw().appendChild(listitem);
  132. $('#opt' + selected.value).raw().disabled = true;
  133. }
  134. }
  135. function remove_selection(index) {
  136. $('#list' + index).remove();
  137. $('#opt' + index).raw().disabled = '';
  138. }
  139. // Thank you http://stackoverflow.com/questions/4578398/selecting-all-text-within-a-div-on-a-single-left-click-with-javascript
  140. function select_all(el) {
  141. if (typeof window.getSelection != "undefined" && typeof document.createRange != "undefined") {
  142. var range = document.createRange();
  143. range.selectNodeContents(el);
  144. var sel = window.getSelection();
  145. sel.removeAllRanges();
  146. sel.addRange(range);
  147. } else if (typeof document.selection != "undefined" && typeof document.body.createTextRange != "undefined") {
  148. var textRange = document.body.createTextRange();
  149. textRange.moveToElementText(el);
  150. textRange.select();
  151. }
  152. }
  153. function toggle_header_links(event) {
  154. event.stopPropagation()
  155. $('#userinfo_minor > li > ul').raw().style.display = ($('#userinfo_minor > li > ul').raw().style.display == 'block') ? 'none' : 'block'
  156. }
  157. function hide_header_links() {
  158. $('#userinfo_minor > li > ul').raw().style.display = 'none'
  159. }
  160. function preload(image) {
  161. var img = document.createElement('img')
  162. img.style.display = 'none'
  163. img.src = image
  164. document.body.appendChild(img)
  165. document.body.removeChild(img)
  166. }
  167. function getCover(event) {
  168. image = event.target.attributes.cover.value
  169. $('#coverCont img').remove()
  170. var coverCont = ($('#coverCont').length==0)?document.body.appendChild(document.createElement('div')):$('#coverCont')[0]
  171. coverCont.id = 'coverCont'
  172. if ($('#coverCont img').length == 0) {
  173. coverCont.appendChild(document.createElement('img'))
  174. }
  175. $('#coverCont img')[0].src = image?image:'/static/common/noartwork/comedy.png'
  176. coverCont.className = (event.clientX > (window.innerWidth/2)) ? 'left' : 'right'
  177. coverCont.style.display = 'block'
  178. //Preload next image
  179. if ($('.torrent_table, .request_table').length > 0) {
  180. var as = $('[cover]')
  181. var a = event.target
  182. preload((as[as.toArray().indexOf(a)+1]||as[0]).attributes.cover.value)
  183. preload((as[as.toArray().indexOf(a)-1]||as[0]).attributes.cover.value)
  184. }
  185. }
  186. function ungetCover(event) {
  187. $('#coverCont img').remove()
  188. coverCont.style.display = 'none'
  189. }
  190. $(function() {
  191. if ($('#header_links_menu').length > 0) {
  192. $('#header_links_menu')[0].addEventListener('click', toggle_header_links)
  193. $('body')[0].addEventListener('click', hide_header_links)
  194. }
  195. if ($('.request_table').length > 0) {
  196. var a = $('[cover]')[0]
  197. if (a) preload(a.attributes.cover.value)
  198. }
  199. document.querySelectorAll('[toggle-target]').forEach(function(el) {
  200. el.addEventListener('click', function(event) {
  201. $(el.attributes['toggle-target'].value).gtoggle()
  202. if (el.attributes['toggle-replace']) {
  203. [el.innerHTML, el.attributes['toggle-replace'].value] = [el.attributes['toggle-replace'].value, el.innerHTML]
  204. }
  205. })
  206. })
  207. })