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.

344 lines
13 KiB

1 year ago
  1. /*
  2. * Thickbox 3.1 - One Box To Rule Them All.
  3. * By Cody Lindley (http://www.codylindley.com)
  4. * Copyright (c) 2007 cody lindley
  5. * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
  6. */
  7. if ( typeof tb_pathToImage != 'string' ) {
  8. var tb_pathToImage = thickboxL10n.loadingAnimation;
  9. }
  10. /*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/
  11. //on page load call tb_init
  12. jQuery(document).ready(function(){
  13. tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
  14. imgLoader = new Image();// preload image
  15. imgLoader.src = tb_pathToImage;
  16. });
  17. /*
  18. * Add thickbox to href & area elements that have a class of .thickbox.
  19. * Remove the loading indicator when content in an iframe has loaded.
  20. */
  21. function tb_init(domChunk){
  22. jQuery( 'body' )
  23. .on( 'click', domChunk, tb_click )
  24. .on( 'thickbox:iframe:loaded', function() {
  25. jQuery( '#TB_window' ).removeClass( 'thickbox-loading' );
  26. });
  27. }
  28. function tb_click(){
  29. var t = this.title || this.name || null;
  30. var a = this.href || this.alt;
  31. var g = this.rel || false;
  32. tb_show(t,a,g);
  33. this.blur();
  34. return false;
  35. }
  36. function tb_show(caption, url, imageGroup) {//function called when the user clicks on a thickbox link
  37. var $closeBtn;
  38. try {
  39. if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
  40. jQuery("body","html").css({height: "100%", width: "100%"});
  41. jQuery("html").css("overflow","hidden");
  42. if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
  43. jQuery("body").append("<iframe id='TB_HideSelect'>"+thickboxL10n.noiframes+"</iframe><div id='TB_overlay'></div><div id='TB_window' class='thickbox-loading'></div>");
  44. jQuery("#TB_overlay").on( 'click', tb_remove );
  45. }
  46. }else{//all others
  47. if(document.getElementById("TB_overlay") === null){
  48. jQuery("body").append("<div id='TB_overlay'></div><div id='TB_window' class='thickbox-loading'></div>");
  49. jQuery("#TB_overlay").on( 'click', tb_remove );
  50. jQuery( 'body' ).addClass( 'modal-open' );
  51. }
  52. }
  53. if(tb_detectMacXFF()){
  54. jQuery("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
  55. }else{
  56. jQuery("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
  57. }
  58. if(caption===null){caption="";}
  59. jQuery("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' width='208' /></div>");//add loader to the page
  60. jQuery('#TB_load').show();//show loader
  61. var baseURL;
  62. if(url.indexOf("?")!==-1){ //ff there is a query string involved
  63. baseURL = url.substr(0, url.indexOf("?"));
  64. }else{
  65. baseURL = url;
  66. }
  67. var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$|\.webp$/;
  68. var urlType = baseURL.toLowerCase().match(urlString);
  69. if(urlType == '.jpg' ||
  70. urlType == '.jpeg' ||
  71. urlType == '.png' ||
  72. urlType == '.gif' ||
  73. urlType == '.bmp' ||
  74. urlType == '.webp'
  75. ){//code to show images
  76. TB_PrevCaption = "";
  77. TB_PrevURL = "";
  78. TB_PrevHTML = "";
  79. TB_NextCaption = "";
  80. TB_NextURL = "";
  81. TB_NextHTML = "";
  82. TB_imageCount = "";
  83. TB_FoundURL = false;
  84. if(imageGroup){
  85. TB_TempArray = jQuery("a[rel="+imageGroup+"]").get();
  86. for (TB_Counter = 0; ((TB_Counter < TB_TempArray.length) && (TB_NextHTML === "")); TB_Counter++) {
  87. var urlTypeTemp = TB_TempArray[TB_Counter].href.toLowerCase().match(urlString);
  88. if (!(TB_TempArray[TB_Counter].href == url)) {
  89. if (TB_FoundURL) {
  90. TB_NextCaption = TB_TempArray[TB_Counter].title;
  91. TB_NextURL = TB_TempArray[TB_Counter].href;
  92. TB_NextHTML = "<span id='TB_next'>&nbsp;&nbsp;<a href='#'>"+thickboxL10n.next+"</a></span>";
  93. } else {
  94. TB_PrevCaption = TB_TempArray[TB_Counter].title;
  95. TB_PrevURL = TB_TempArray[TB_Counter].href;
  96. TB_PrevHTML = "<span id='TB_prev'>&nbsp;&nbsp;<a href='#'>"+thickboxL10n.prev+"</a></span>";
  97. }
  98. } else {
  99. TB_FoundURL = true;
  100. TB_imageCount = thickboxL10n.image + ' ' + (TB_Counter + 1) + ' ' + thickboxL10n.of + ' ' + (TB_TempArray.length);
  101. }
  102. }
  103. }
  104. imgPreloader = new Image();
  105. imgPreloader.onload = function(){
  106. imgPreloader.onload = null;
  107. // Resizing large images - original by Christian Montoya edited by me.
  108. var pagesize = tb_getPageSize();
  109. var x = pagesize[0] - 150;
  110. var y = pagesize[1] - 150;
  111. var imageWidth = imgPreloader.width;
  112. var imageHeight = imgPreloader.height;
  113. if (imageWidth > x) {
  114. imageHeight = imageHeight * (x / imageWidth);
  115. imageWidth = x;
  116. if (imageHeight > y) {
  117. imageWidth = imageWidth * (y / imageHeight);
  118. imageHeight = y;
  119. }
  120. } else if (imageHeight > y) {
  121. imageWidth = imageWidth * (y / imageHeight);
  122. imageHeight = y;
  123. if (imageWidth > x) {
  124. imageHeight = imageHeight * (x / imageWidth);
  125. imageWidth = x;
  126. }
  127. }
  128. // End Resizing
  129. TB_WIDTH = imageWidth + 30;
  130. TB_HEIGHT = imageHeight + 60;
  131. jQuery("#TB_window").append("<a href='' id='TB_ImageOff'><span class='screen-reader-text'>"+thickboxL10n.close+"</span><img id='TB_Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' alt='"+caption+"'/></a>" + "<div id='TB_caption'>"+caption+"<div id='TB_secondLine'>" + TB_imageCount + TB_PrevHTML + TB_NextHTML + "</div></div><div id='TB_closeWindow'><button type='button' id='TB_closeWindowButton'><span class='screen-reader-text'>"+thickboxL10n.close+"</span><span class='tb-close-icon'></span></button></div>");
  132. jQuery("#TB_closeWindowButton").on( 'click', tb_remove );
  133. if (!(TB_PrevHTML === "")) {
  134. function goPrev(){
  135. if(jQuery(document).off("click",goPrev)){jQuery(document).off("click",goPrev);}
  136. jQuery("#TB_window").remove();
  137. jQuery("body").append("<div id='TB_window'></div>");
  138. tb_show(TB_PrevCaption, TB_PrevURL, imageGroup);
  139. return false;
  140. }
  141. jQuery("#TB_prev").on( 'click', goPrev );
  142. }
  143. if (!(TB_NextHTML === "")) {
  144. function goNext(){
  145. jQuery("#TB_window").remove();
  146. jQuery("body").append("<div id='TB_window'></div>");
  147. tb_show(TB_NextCaption, TB_NextURL, imageGroup);
  148. return false;
  149. }
  150. jQuery("#TB_next").on( 'click', goNext );
  151. }
  152. jQuery(document).on('keydown.thickbox', function(e){
  153. if ( e.which == 27 ){ // close
  154. tb_remove();
  155. } else if ( e.which == 190 ){ // display previous image
  156. if(!(TB_NextHTML == "")){
  157. jQuery(document).off('thickbox');
  158. goNext();
  159. }
  160. } else if ( e.which == 188 ){ // display next image
  161. if(!(TB_PrevHTML == "")){
  162. jQuery(document).off('thickbox');
  163. goPrev();
  164. }
  165. }
  166. return false;
  167. });
  168. tb_position();
  169. jQuery("#TB_load").remove();
  170. jQuery("#TB_ImageOff").on( 'click', tb_remove );
  171. jQuery("#TB_window").css({'visibility':'visible'}); //for safari using css instead of show
  172. };
  173. imgPreloader.src = url;
  174. }else{//code to show html
  175. var queryString = url.replace(/^[^\?]+\??/,'');
  176. var params = tb_parseQuery( queryString );
  177. TB_WIDTH = (params['width']*1) + 30 || 630; //defaults to 630 if no parameters were added to URL
  178. TB_HEIGHT = (params['height']*1) + 40 || 440; //defaults to 440 if no parameters were added to URL
  179. ajaxContentW = TB_WIDTH - 30;
  180. ajaxContentH = TB_HEIGHT - 45;
  181. if(url.indexOf('TB_iframe') != -1){// either iframe or ajax window
  182. urlNoQuery = url.split('TB_');
  183. jQuery("#TB_iframeContent").remove();
  184. if(params['modal'] != "true"){//iframe no modal
  185. jQuery("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><button type='button' id='TB_closeWindowButton'><span class='screen-reader-text'>"+thickboxL10n.close+"</span><span class='tb-close-icon'></span></button></div></div><iframe frameborder='0' hspace='0' allowtransparency='true' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' >"+thickboxL10n.noiframes+"</iframe>");
  186. }else{//iframe modal
  187. jQuery("#TB_overlay").off();
  188. jQuery("#TB_window").append("<iframe frameborder='0' hspace='0' allowtransparency='true' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;'>"+thickboxL10n.noiframes+"</iframe>");
  189. }
  190. }else{// not an iframe, ajax
  191. if(jQuery("#TB_window").css("visibility") != "visible"){
  192. if(params['modal'] != "true"){//ajax no modal
  193. jQuery("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><button type='button' id='TB_closeWindowButton'><span class='screen-reader-text'>"+thickboxL10n.close+"</span><span class='tb-close-icon'></span></button></div></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
  194. }else{//ajax modal
  195. jQuery("#TB_overlay").off();
  196. jQuery("#TB_window").append("<div id='TB_ajaxContent' class='TB_modal' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
  197. }
  198. }else{//this means the window is already up, we are just loading new content via ajax
  199. jQuery("#TB_ajaxContent")[0].style.width = ajaxContentW +"px";
  200. jQuery("#TB_ajaxContent")[0].style.height = ajaxContentH +"px";
  201. jQuery("#TB_ajaxContent")[0].scrollTop = 0;
  202. jQuery("#TB_ajaxWindowTitle").html(caption);
  203. }
  204. }
  205. jQuery("#TB_closeWindowButton").on( 'click', tb_remove );
  206. if(url.indexOf('TB_inline') != -1){
  207. jQuery("#TB_ajaxContent").append(jQuery('#' + params['inlineId']).children());
  208. jQuery("#TB_window").on('tb_unload', function () {
  209. jQuery('#' + params['inlineId']).append( jQuery("#TB_ajaxContent").children() ); // move elements back when you're finished
  210. });
  211. tb_position();
  212. jQuery("#TB_load").remove();
  213. jQuery("#TB_window").css({'visibility':'visible'});
  214. }else if(url.indexOf('TB_iframe') != -1){
  215. tb_position();
  216. jQuery("#TB_load").remove();
  217. jQuery("#TB_window").css({'visibility':'visible'});
  218. }else{
  219. var load_url = url;
  220. load_url += -1 === url.indexOf('?') ? '?' : '&';
  221. jQuery("#TB_ajaxContent").load(load_url += "random=" + (new Date().getTime()),function(){//to do a post change this load method
  222. tb_position();
  223. jQuery("#TB_load").remove();
  224. tb_init("#TB_ajaxContent a.thickbox");
  225. jQuery("#TB_window").css({'visibility':'visible'});
  226. });
  227. }
  228. }
  229. if(!params['modal']){
  230. jQuery(document).on('keydown.thickbox', function(e){
  231. if ( e.which == 27 ){ // close
  232. tb_remove();
  233. return false;
  234. }
  235. });
  236. }
  237. $closeBtn = jQuery( '#TB_closeWindowButton' );
  238. /*
  239. * If the native Close button icon is visible, move focus on the button
  240. * (e.g. in the Network Admin Themes screen).
  241. * In other admin screens is hidden and replaced by a different icon.
  242. */
  243. if ( $closeBtn.find( '.tb-close-icon' ).is( ':visible' ) ) {
  244. $closeBtn.trigger( 'focus' );
  245. }
  246. } catch(e) {
  247. //nothing here
  248. }
  249. }
  250. //helper functions below
  251. function tb_showIframe(){
  252. jQuery("#TB_load").remove();
  253. jQuery("#TB_window").css({'visibility':'visible'}).trigger( 'thickbox:iframe:loaded' );
  254. }
  255. function tb_remove() {
  256. jQuery("#TB_imageOff").off("click");
  257. jQuery("#TB_closeWindowButton").off("click");
  258. jQuery( '#TB_window' ).fadeOut( 'fast', function() {
  259. jQuery( '#TB_window, #TB_overlay, #TB_HideSelect' ).trigger( 'tb_unload' ).off().remove();
  260. jQuery( 'body' ).trigger( 'thickbox:removed' );
  261. });
  262. jQuery( 'body' ).removeClass( 'modal-open' );
  263. jQuery("#TB_load").remove();
  264. if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
  265. jQuery("body","html").css({height: "auto", width: "auto"});
  266. jQuery("html").css("overflow","");
  267. }
  268. jQuery(document).off('.thickbox');
  269. return false;
  270. }
  271. function tb_position() {
  272. var isIE6 = typeof document.body.style.maxHeight === "undefined";
  273. jQuery("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
  274. if ( ! isIE6 ) { // take away IE6
  275. jQuery("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
  276. }
  277. }
  278. function tb_parseQuery ( query ) {
  279. var Params = {};
  280. if ( ! query ) {return Params;}// return empty object
  281. var Pairs = query.split(/[;&]/);
  282. for ( var i = 0; i < Pairs.length; i++ ) {
  283. var KeyVal = Pairs[i].split('=');
  284. if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
  285. var key = unescape( KeyVal[0] );
  286. var val = unescape( KeyVal[1] );
  287. val = val.replace(/\+/g, ' ');
  288. Params[key] = val;
  289. }
  290. return Params;
  291. }
  292. function tb_getPageSize(){
  293. var de = document.documentElement;
  294. var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  295. var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
  296. arrayPageSize = [w,h];
  297. return arrayPageSize;
  298. }
  299. function tb_detectMacXFF() {
  300. var userAgent = navigator.userAgent.toLowerCase();
  301. if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
  302. return true;
  303. }
  304. }