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