thickbox.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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$/;
  68. var urlType = baseURL.toLowerCase().match(urlString);
  69. if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){//code to show images
  70. TB_PrevCaption = "";
  71. TB_PrevURL = "";
  72. TB_PrevHTML = "";
  73. TB_NextCaption = "";
  74. TB_NextURL = "";
  75. TB_NextHTML = "";
  76. TB_imageCount = "";
  77. TB_FoundURL = false;
  78. if(imageGroup){
  79. TB_TempArray = jQuery("a[rel="+imageGroup+"]").get();
  80. for (TB_Counter = 0; ((TB_Counter < TB_TempArray.length) && (TB_NextHTML === "")); TB_Counter++) {
  81. var urlTypeTemp = TB_TempArray[TB_Counter].href.toLowerCase().match(urlString);
  82. if (!(TB_TempArray[TB_Counter].href == url)) {
  83. if (TB_FoundURL) {
  84. TB_NextCaption = TB_TempArray[TB_Counter].title;
  85. TB_NextURL = TB_TempArray[TB_Counter].href;
  86. TB_NextHTML = "<span id='TB_next'>&nbsp;&nbsp;<a href='#'>"+thickboxL10n.next+"</a></span>";
  87. } else {
  88. TB_PrevCaption = TB_TempArray[TB_Counter].title;
  89. TB_PrevURL = TB_TempArray[TB_Counter].href;
  90. TB_PrevHTML = "<span id='TB_prev'>&nbsp;&nbsp;<a href='#'>"+thickboxL10n.prev+"</a></span>";
  91. }
  92. } else {
  93. TB_FoundURL = true;
  94. TB_imageCount = thickboxL10n.image + ' ' + (TB_Counter + 1) + ' ' + thickboxL10n.of + ' ' + (TB_TempArray.length);
  95. }
  96. }
  97. }
  98. imgPreloader = new Image();
  99. imgPreloader.onload = function(){
  100. imgPreloader.onload = null;
  101. // Resizing large images - original by Christian Montoya edited by me.
  102. var pagesize = tb_getPageSize();
  103. var x = pagesize[0] - 150;
  104. var y = pagesize[1] - 150;
  105. var imageWidth = imgPreloader.width;
  106. var imageHeight = imgPreloader.height;
  107. if (imageWidth > x) {
  108. imageHeight = imageHeight * (x / imageWidth);
  109. imageWidth = x;
  110. if (imageHeight > y) {
  111. imageWidth = imageWidth * (y / imageHeight);
  112. imageHeight = y;
  113. }
  114. } else if (imageHeight > y) {
  115. imageWidth = imageWidth * (y / imageHeight);
  116. imageHeight = y;
  117. if (imageWidth > x) {
  118. imageHeight = imageHeight * (x / imageWidth);
  119. imageWidth = x;
  120. }
  121. }
  122. // End Resizing
  123. TB_WIDTH = imageWidth + 30;
  124. TB_HEIGHT = imageHeight + 60;
  125. 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>");
  126. jQuery("#TB_closeWindowButton").on( 'click', tb_remove );
  127. if (!(TB_PrevHTML === "")) {
  128. function goPrev(){
  129. if(jQuery(document).off("click",goPrev)){jQuery(document).off("click",goPrev);}
  130. jQuery("#TB_window").remove();
  131. jQuery("body").append("<div id='TB_window'></div>");
  132. tb_show(TB_PrevCaption, TB_PrevURL, imageGroup);
  133. return false;
  134. }
  135. jQuery("#TB_prev").on( 'click', goPrev );
  136. }
  137. if (!(TB_NextHTML === "")) {
  138. function goNext(){
  139. jQuery("#TB_window").remove();
  140. jQuery("body").append("<div id='TB_window'></div>");
  141. tb_show(TB_NextCaption, TB_NextURL, imageGroup);
  142. return false;
  143. }
  144. jQuery("#TB_next").on( 'click', goNext );
  145. }
  146. jQuery(document).on('keydown.thickbox', function(e){
  147. if ( e.which == 27 ){ // close
  148. tb_remove();
  149. } else if ( e.which == 190 ){ // display previous image
  150. if(!(TB_NextHTML == "")){
  151. jQuery(document).off('thickbox');
  152. goNext();
  153. }
  154. } else if ( e.which == 188 ){ // display next image
  155. if(!(TB_PrevHTML == "")){
  156. jQuery(document).off('thickbox');
  157. goPrev();
  158. }
  159. }
  160. return false;
  161. });
  162. tb_position();
  163. jQuery("#TB_load").remove();
  164. jQuery("#TB_ImageOff").on( 'click', tb_remove );
  165. jQuery("#TB_window").css({'visibility':'visible'}); //for safari using css instead of show
  166. };
  167. imgPreloader.src = url;
  168. }else{//code to show html
  169. var queryString = url.replace(/^[^\?]+\??/,'');
  170. var params = tb_parseQuery( queryString );
  171. TB_WIDTH = (params['width']*1) + 30 || 630; //defaults to 630 if no parameters were added to URL
  172. TB_HEIGHT = (params['height']*1) + 40 || 440; //defaults to 440 if no parameters were added to URL
  173. ajaxContentW = TB_WIDTH - 30;
  174. ajaxContentH = TB_HEIGHT - 45;
  175. if(url.indexOf('TB_iframe') != -1){// either iframe or ajax window
  176. urlNoQuery = url.split('TB_');
  177. jQuery("#TB_iframeContent").remove();
  178. if(params['modal'] != "true"){//iframe no modal
  179. 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>");
  180. }else{//iframe modal
  181. jQuery("#TB_overlay").off();
  182. 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>");
  183. }
  184. }else{// not an iframe, ajax
  185. if(jQuery("#TB_window").css("visibility") != "visible"){
  186. if(params['modal'] != "true"){//ajax no modal
  187. 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>");
  188. }else{//ajax modal
  189. jQuery("#TB_overlay").off();
  190. jQuery("#TB_window").append("<div id='TB_ajaxContent' class='TB_modal' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
  191. }
  192. }else{//this means the window is already up, we are just loading new content via ajax
  193. jQuery("#TB_ajaxContent")[0].style.width = ajaxContentW +"px";
  194. jQuery("#TB_ajaxContent")[0].style.height = ajaxContentH +"px";
  195. jQuery("#TB_ajaxContent")[0].scrollTop = 0;
  196. jQuery("#TB_ajaxWindowTitle").html(caption);
  197. }
  198. }
  199. jQuery("#TB_closeWindowButton").on( 'click', tb_remove );
  200. if(url.indexOf('TB_inline') != -1){
  201. jQuery("#TB_ajaxContent").append(jQuery('#' + params['inlineId']).children());
  202. jQuery("#TB_window").on('tb_unload', function () {
  203. jQuery('#' + params['inlineId']).append( jQuery("#TB_ajaxContent").children() ); // move elements back when you're finished
  204. });
  205. tb_position();
  206. jQuery("#TB_load").remove();
  207. jQuery("#TB_window").css({'visibility':'visible'});
  208. }else if(url.indexOf('TB_iframe') != -1){
  209. tb_position();
  210. jQuery("#TB_load").remove();
  211. jQuery("#TB_window").css({'visibility':'visible'});
  212. }else{
  213. var load_url = url;
  214. load_url += -1 === url.indexOf('?') ? '?' : '&';
  215. jQuery("#TB_ajaxContent").load(load_url += "random=" + (new Date().getTime()),function(){//to do a post change this load method
  216. tb_position();
  217. jQuery("#TB_load").remove();
  218. tb_init("#TB_ajaxContent a.thickbox");
  219. jQuery("#TB_window").css({'visibility':'visible'});
  220. });
  221. }
  222. }
  223. if(!params['modal']){
  224. jQuery(document).on('keydown.thickbox', function(e){
  225. if ( e.which == 27 ){ // close
  226. tb_remove();
  227. return false;
  228. }
  229. });
  230. }
  231. $closeBtn = jQuery( '#TB_closeWindowButton' );
  232. /*
  233. * If the native Close button icon is visible, move focus on the button
  234. * (e.g. in the Network Admin Themes screen).
  235. * In other admin screens is hidden and replaced by a different icon.
  236. */
  237. if ( $closeBtn.find( '.tb-close-icon' ).is( ':visible' ) ) {
  238. $closeBtn.trigger( 'focus' );
  239. }
  240. } catch(e) {
  241. //nothing here
  242. }
  243. }
  244. //helper functions below
  245. function tb_showIframe(){
  246. jQuery("#TB_load").remove();
  247. jQuery("#TB_window").css({'visibility':'visible'}).trigger( 'thickbox:iframe:loaded' );
  248. }
  249. function tb_remove() {
  250. jQuery("#TB_imageOff").off("click");
  251. jQuery("#TB_closeWindowButton").off("click");
  252. jQuery( '#TB_window' ).fadeOut( 'fast', function() {
  253. jQuery( '#TB_window, #TB_overlay, #TB_HideSelect' ).trigger( 'tb_unload' ).off().remove();
  254. jQuery( 'body' ).trigger( 'thickbox:removed' );
  255. });
  256. jQuery( 'body' ).removeClass( 'modal-open' );
  257. jQuery("#TB_load").remove();
  258. if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
  259. jQuery("body","html").css({height: "auto", width: "auto"});
  260. jQuery("html").css("overflow","");
  261. }
  262. jQuery(document).off('.thickbox');
  263. return false;
  264. }
  265. function tb_position() {
  266. var isIE6 = typeof document.body.style.maxHeight === "undefined";
  267. jQuery("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
  268. if ( ! isIE6 ) { // take away IE6
  269. jQuery("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
  270. }
  271. }
  272. function tb_parseQuery ( query ) {
  273. var Params = {};
  274. if ( ! query ) {return Params;}// return empty object
  275. var Pairs = query.split(/[;&]/);
  276. for ( var i = 0; i < Pairs.length; i++ ) {
  277. var KeyVal = Pairs[i].split('=');
  278. if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
  279. var key = unescape( KeyVal[0] );
  280. var val = unescape( KeyVal[1] );
  281. val = val.replace(/\+/g, ' ');
  282. Params[key] = val;
  283. }
  284. return Params;
  285. }
  286. function tb_getPageSize(){
  287. var de = document.documentElement;
  288. var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  289. var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
  290. arrayPageSize = [w,h];
  291. return arrayPageSize;
  292. }
  293. function tb_detectMacXFF() {
  294. var userAgent = navigator.userAgent.toLowerCase();
  295. if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
  296. return true;
  297. }
  298. }