]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/thickbox/thickbox.js
WordPress 3.9.2-scripts
[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('body').on('click', domChunk, 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                                 if ( e.which == 27 ){ // close
160                                         tb_remove();
161
162                                 } else if ( e.which == 190 ){ // display previous image
163                                         if(!(TB_NextHTML == "")){
164                                                 jQuery(document).unbind('thickbox');
165                                                 goNext();
166                                         }
167                                 } else if ( e.which == 188 ){ // display next image
168                                         if(!(TB_PrevHTML == "")){
169                                                 jQuery(document).unbind('thickbox');
170                                                 goPrev();
171                                         }
172                                 }
173                                 return false;
174                         });
175
176                         tb_position();
177                         jQuery("#TB_load").remove();
178                         jQuery("#TB_ImageOff").click(tb_remove);
179                         jQuery("#TB_window").css({'visibility':'visible'}); //for safari using css instead of show
180                         };
181
182                         imgPreloader.src = url;
183                 }else{//code to show html
184
185                         var queryString = url.replace(/^[^\?]+\??/,'');
186                         var params = tb_parseQuery( queryString );
187
188                         TB_WIDTH = (params['width']*1) + 30 || 630; //defaults to 630 if no paramaters were added to URL
189                         TB_HEIGHT = (params['height']*1) + 40 || 440; //defaults to 440 if no paramaters were added to URL
190                         ajaxContentW = TB_WIDTH - 30;
191                         ajaxContentH = TB_HEIGHT - 45;
192
193                         if(url.indexOf('TB_iframe') != -1){// either iframe or ajax window
194                                         urlNoQuery = url.split('TB_');
195                                         jQuery("#TB_iframeContent").remove();
196                                         if(params['modal'] != "true"){//iframe no modal
197                                                 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>");
198                                         }else{//iframe modal
199                                         jQuery("#TB_overlay").unbind();
200                                                 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>");
201                                         }
202                         }else{// not an iframe, ajax
203                                         if(jQuery("#TB_window").css("visibility") != "visible"){
204                                                 if(params['modal'] != "true"){//ajax no modal
205                                                 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>");
206                                                 }else{//ajax modal
207                                                 jQuery("#TB_overlay").unbind();
208                                                 jQuery("#TB_window").append("<div id='TB_ajaxContent' class='TB_modal' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
209                                                 }
210                                         }else{//this means the window is already up, we are just loading new content via ajax
211                                                 jQuery("#TB_ajaxContent")[0].style.width = ajaxContentW +"px";
212                                                 jQuery("#TB_ajaxContent")[0].style.height = ajaxContentH +"px";
213                                                 jQuery("#TB_ajaxContent")[0].scrollTop = 0;
214                                                 jQuery("#TB_ajaxWindowTitle").html(caption);
215                                         }
216                         }
217
218                         jQuery("#TB_closeWindowButton").click(tb_remove);
219
220                                 if(url.indexOf('TB_inline') != -1){
221                                         jQuery("#TB_ajaxContent").append(jQuery('#' + params['inlineId']).children());
222                                         jQuery("#TB_window").bind('tb_unload', function () {
223                                                 jQuery('#' + params['inlineId']).append( jQuery("#TB_ajaxContent").children() ); // move elements back when you're finished
224                                         });
225                                         tb_position();
226                                         jQuery("#TB_load").remove();
227                                         jQuery("#TB_window").css({'visibility':'visible'});
228                                 }else if(url.indexOf('TB_iframe') != -1){
229                                         tb_position();
230                                         jQuery("#TB_load").remove();
231                                         jQuery("#TB_window").css({'visibility':'visible'});
232                                 }else{
233                                         jQuery("#TB_ajaxContent").load(url += "&random=" + (new Date().getTime()),function(){//to do a post change this load method
234                                                 tb_position();
235                                                 jQuery("#TB_load").remove();
236                                                 tb_init("#TB_ajaxContent a.thickbox");
237                                                 jQuery("#TB_window").css({'visibility':'visible'});
238                                         });
239                                 }
240
241                 }
242
243                 if(!params['modal']){
244                         jQuery(document).bind('keydown.thickbox', function(e){
245                                 if ( e.which == 27 ){ // close
246                                         tb_remove();
247                                         return false;
248                                 }
249                         });
250                 }
251
252         } catch(e) {
253                 //nothing here
254         }
255 }
256
257 //helper functions below
258 function tb_showIframe(){
259         jQuery("#TB_load").remove();
260         jQuery("#TB_window").css({'visibility':'visible'});
261 }
262
263 function tb_remove() {
264         jQuery("#TB_imageOff").unbind("click");
265         jQuery("#TB_closeWindowButton").unbind("click");
266         jQuery("#TB_window").fadeOut("fast",function(){jQuery('#TB_window,#TB_overlay,#TB_HideSelect').trigger("tb_unload").unbind().remove();});
267         jQuery("#TB_load").remove();
268         if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
269                 jQuery("body","html").css({height: "auto", width: "auto"});
270                 jQuery("html").css("overflow","");
271         }
272         jQuery(document).unbind('.thickbox');
273         return false;
274 }
275
276 function tb_position() {
277 var isIE6 = typeof document.body.style.maxHeight === "undefined";
278 jQuery("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
279         if ( ! isIE6 ) { // take away IE6
280                 jQuery("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
281         }
282 }
283
284 function tb_parseQuery ( query ) {
285    var Params = {};
286    if ( ! query ) {return Params;}// return empty object
287    var Pairs = query.split(/[;&]/);
288    for ( var i = 0; i < Pairs.length; i++ ) {
289       var KeyVal = Pairs[i].split('=');
290       if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
291       var key = unescape( KeyVal[0] );
292       var val = unescape( KeyVal[1] );
293       val = val.replace(/\+/g, ' ');
294       Params[key] = val;
295    }
296    return Params;
297 }
298
299 function tb_getPageSize(){
300         var de = document.documentElement;
301         var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
302         var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
303         arrayPageSize = [w,h];
304         return arrayPageSize;
305 }
306
307 function tb_detectMacXFF() {
308   var userAgent = navigator.userAgent.toLowerCase();
309   if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
310     return true;
311   }
312 }