PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/js/thickbox/thickbox.js

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