web/wp-includes/js/thickbox/thickbox.js
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
     4  * Copyright (c) 2007 cody lindley
     4  * Copyright (c) 2007 cody lindley
     5  * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
     5  * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
     6 */
     6 */
     7 
     7 
     8 if ( typeof tb_pathToImage != 'string' ) {
     8 if ( typeof tb_pathToImage != 'string' ) {
     9 	var tb_pathToImage = "../wp-includes/js/thickbox/loadingAnimation.gif";
     9 	var tb_pathToImage = thickboxL10n.loadingAnimation;
    10 }
    10 }
    11 if ( typeof tb_closeImage != 'string' ) {
    11 if ( typeof tb_closeImage != 'string' ) {
    12 	var tb_closeImage = "../wp-includes/js/thickbox/tb-close.png";
    12 	var tb_closeImage = thickboxL10n.closeImage;
    13 }
    13 }
    14 
    14 
    15 /*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/
    15 /*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/
    16 
    16 
    17 //on page load call tb_init
    17 //on page load call tb_init
    21 	imgLoader.src = tb_pathToImage;
    21 	imgLoader.src = tb_pathToImage;
    22 });
    22 });
    23 
    23 
    24 //add thickbox to href & area elements that have a class of .thickbox
    24 //add thickbox to href & area elements that have a class of .thickbox
    25 function tb_init(domChunk){
    25 function tb_init(domChunk){
    26 	jQuery(domChunk).click(function(){
    26 	jQuery(domChunk).live('click', tb_click);
       
    27 }
       
    28 
       
    29 function tb_click(){
    27 	var t = this.title || this.name || null;
    30 	var t = this.title || this.name || null;
    28 	var a = this.href || this.alt;
    31 	var a = this.href || this.alt;
    29 	var g = this.rel || false;
    32 	var g = this.rel || false;
    30 	tb_show(t,a,g);
    33 	tb_show(t,a,g);
    31 	this.blur();
    34 	this.blur();
    32 	return false;
    35 	return false;
    33 	});
       
    34 }
    36 }
    35 
    37 
    36 function tb_show(caption, url, imageGroup) {//function called when the user clicks on a thickbox link
    38 function tb_show(caption, url, imageGroup) {//function called when the user clicks on a thickbox link
    37 
    39 
    38 	try {
    40 	try {
    39 		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
    41 		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
    40 			jQuery("body","html").css({height: "100%", width: "100%"});
    42 			jQuery("body","html").css({height: "100%", width: "100%"});
    41 			jQuery("html").css("overflow","hidden");
    43 			jQuery("html").css("overflow","hidden");
    42 			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
    44 			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
    43 				jQuery("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
    45 				jQuery("body").append("<iframe id='TB_HideSelect'>"+thickboxL10n.noiframes+"</iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
    44 				jQuery("#TB_overlay").click(tb_remove);
    46 				jQuery("#TB_overlay").click(tb_remove);
    45 			}
    47 			}
    46 		}else{//all others
    48 		}else{//all others
    47 			if(document.getElementById("TB_overlay") === null){
    49 			if(document.getElementById("TB_overlay") === null){
    48 				jQuery("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
    50 				jQuery("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
   154 				}
   156 				}
   155 				jQuery("#TB_next").click(goNext);
   157 				jQuery("#TB_next").click(goNext);
   156 
   158 
   157 			}
   159 			}
   158 
   160 
   159 			document.onkeydown = function(e){
   161 			jQuery(document).bind('keydown.thickbox', function(e){
   160 				if (e == null) { // ie
   162 				e.stopImmediatePropagation();
   161 					keycode = event.keyCode;
   163 
   162 				} else { // mozilla
   164 				if ( e.which == 27 ){ // close
   163 					keycode = e.which;
   165 					if ( ! jQuery(document).triggerHandler( 'wp_CloseOnEscape', [{ event: e, what: 'thickbox', cb: tb_remove }] ) )
   164 				}
   166 						tb_remove();
   165 				if(keycode == 27){ // close
   167 
   166 					tb_remove();
   168 				} else if ( e.which == 190 ){ // display previous image
   167 				} else if(keycode == 190){ // display previous image
       
   168 					if(!(TB_NextHTML == "")){
   169 					if(!(TB_NextHTML == "")){
   169 						document.onkeydown = "";
   170 						jQuery(document).unbind('thickbox');
   170 						goNext();
   171 						goNext();
   171 					}
   172 					}
   172 				} else if(keycode == 188){ // display next image
   173 				} else if ( e.which == 188 ){ // display next image
   173 					if(!(TB_PrevHTML == "")){
   174 					if(!(TB_PrevHTML == "")){
   174 						document.onkeydown = "";
   175 						jQuery(document).unbind('thickbox');
   175 						goPrev();
   176 						goPrev();
   176 					}
   177 					}
   177 				}
   178 				}
   178 			};
   179 				return false;
       
   180 			});
   179 
   181 
   180 			tb_position();
   182 			tb_position();
   181 			jQuery("#TB_load").remove();
   183 			jQuery("#TB_load").remove();
   182 			jQuery("#TB_ImageOff").click(tb_remove);
   184 			jQuery("#TB_ImageOff").click(tb_remove);
   183 			jQuery("#TB_window").css({display:"block"}); //for safari using css instead of show
   185 			jQuery("#TB_window").css({'visibility':'visible'}); //for safari using css instead of show
   184 			};
   186 			};
   185 
   187 
   186 			imgPreloader.src = url;
   188 			imgPreloader.src = url;
   187 		}else{//code to show html
   189 		}else{//code to show html
   188 
   190 
   196 
   198 
   197 			if(url.indexOf('TB_iframe') != -1){// either iframe or ajax window
   199 			if(url.indexOf('TB_iframe') != -1){// either iframe or ajax window
   198 					urlNoQuery = url.split('TB_');
   200 					urlNoQuery = url.split('TB_');
   199 					jQuery("#TB_iframeContent").remove();
   201 					jQuery("#TB_iframeContent").remove();
   200 					if(params['modal'] != "true"){//iframe no modal
   202 					if(params['modal'] != "true"){//iframe no modal
   201 						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;' > </iframe>");
   203 						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>");
   202 					}else{//iframe modal
   204 					}else{//iframe modal
   203 					jQuery("#TB_overlay").unbind();
   205 					jQuery("#TB_overlay").unbind();
   204 						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;'> </iframe>");
   206 						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>");
   205 					}
   207 					}
   206 			}else{// not an iframe, ajax
   208 			}else{// not an iframe, ajax
   207 					if(jQuery("#TB_window").css("display") != "block"){
   209 					if(jQuery("#TB_window").css("visibility") != "visible"){
   208 						if(params['modal'] != "true"){//ajax no modal
   210 						if(params['modal'] != "true"){//ajax no modal
   209 						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>");
   211 						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>");
   210 						}else{//ajax modal
   212 						}else{//ajax modal
   211 						jQuery("#TB_overlay").unbind();
   213 						jQuery("#TB_overlay").unbind();
   212 						jQuery("#TB_window").append("<div id='TB_ajaxContent' class='TB_modal' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
   214 						jQuery("#TB_window").append("<div id='TB_ajaxContent' class='TB_modal' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
   221 
   223 
   222 			jQuery("#TB_closeWindowButton").click(tb_remove);
   224 			jQuery("#TB_closeWindowButton").click(tb_remove);
   223 
   225 
   224 				if(url.indexOf('TB_inline') != -1){
   226 				if(url.indexOf('TB_inline') != -1){
   225 					jQuery("#TB_ajaxContent").append(jQuery('#' + params['inlineId']).children());
   227 					jQuery("#TB_ajaxContent").append(jQuery('#' + params['inlineId']).children());
   226 					jQuery("#TB_window").unload(function () {
   228 					jQuery("#TB_window").bind('tb_unload', function () {
   227 						jQuery('#' + params['inlineId']).append( jQuery("#TB_ajaxContent").children() ); // move elements back when you're finished
   229 						jQuery('#' + params['inlineId']).append( jQuery("#TB_ajaxContent").children() ); // move elements back when you're finished
   228 					});
   230 					});
   229 					tb_position();
   231 					tb_position();
   230 					jQuery("#TB_load").remove();
   232 					jQuery("#TB_load").remove();
   231 					jQuery("#TB_window").css({display:"block"});
   233 					jQuery("#TB_window").css({'visibility':'visible'});
   232 				}else if(url.indexOf('TB_iframe') != -1){
   234 				}else if(url.indexOf('TB_iframe') != -1){
   233 					tb_position();
   235 					tb_position();
   234 					if($.browser.safari){//safari needs help because it will not fire iframe onload
   236 					if(jQuery.browser.safari){//safari needs help because it will not fire iframe onload
   235 						jQuery("#TB_load").remove();
   237 						jQuery("#TB_load").remove();
   236 						jQuery("#TB_window").css({display:"block"});
   238 						jQuery("#TB_window").css({'visibility':'visible'});
   237 					}
   239 					}
   238 				}else{
   240 				}else{
   239 					jQuery("#TB_ajaxContent").load(url += "&random=" + (new Date().getTime()),function(){//to do a post change this load method
   241 					jQuery("#TB_ajaxContent").load(url += "&random=" + (new Date().getTime()),function(){//to do a post change this load method
   240 						tb_position();
   242 						tb_position();
   241 						jQuery("#TB_load").remove();
   243 						jQuery("#TB_load").remove();
   242 						tb_init("#TB_ajaxContent a.thickbox");
   244 						tb_init("#TB_ajaxContent a.thickbox");
   243 						jQuery("#TB_window").css({display:"block"});
   245 						jQuery("#TB_window").css({'visibility':'visible'});
   244 					});
   246 					});
   245 				}
   247 				}
   246 
   248 
   247 		}
   249 		}
   248 
   250 
   249 		if(!params['modal']){
   251 		if(!params['modal']){
   250 			document.onkeyup = function(e){
   252 			jQuery(document).bind('keyup.thickbox', function(e){
   251 				if (e == null) { // ie
   253 
   252 					keycode = event.keyCode;
   254 				if ( e.which == 27 ){ // close
   253 				} else { // mozilla
   255 					e.stopImmediatePropagation();
   254 					keycode = e.which;
   256 					if ( ! jQuery(document).triggerHandler( 'wp_CloseOnEscape', [{ event: e, what: 'thickbox', cb: tb_remove }] ) )
   255 				}
   257 						tb_remove();
   256 				if(keycode == 27){ // close
   258 
   257 					tb_remove();
   259 					return false;
   258 				}
   260 				}
   259 			};
   261 			});
   260 		}
   262 		}
   261 
   263 
   262 	} catch(e) {
   264 	} catch(e) {
   263 		//nothing here
   265 		//nothing here
   264 	}
   266 	}
   265 }
   267 }
   266 
   268 
   267 //helper functions below
   269 //helper functions below
   268 function tb_showIframe(){
   270 function tb_showIframe(){
   269 	jQuery("#TB_load").remove();
   271 	jQuery("#TB_load").remove();
   270 	jQuery("#TB_window").css({display:"block"});
   272 	jQuery("#TB_window").css({'visibility':'visible'});
   271 }
   273 }
   272 
   274 
   273 function tb_remove() {
   275 function tb_remove() {
   274  	jQuery("#TB_imageOff").unbind("click");
   276  	jQuery("#TB_imageOff").unbind("click");
   275 	jQuery("#TB_closeWindowButton").unbind("click");
   277 	jQuery("#TB_closeWindowButton").unbind("click");
   276 	jQuery("#TB_window").fadeOut("fast",function(){jQuery('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
   278 	jQuery("#TB_window").fadeOut("fast",function(){jQuery('#TB_window,#TB_overlay,#TB_HideSelect').trigger("tb_unload").unbind().remove();});
   277 	jQuery("#TB_load").remove();
   279 	jQuery("#TB_load").remove();
   278 	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
   280 	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
   279 		jQuery("body","html").css({height: "auto", width: "auto"});
   281 		jQuery("body","html").css({height: "auto", width: "auto"});
   280 		jQuery("html").css("overflow","");
   282 		jQuery("html").css("overflow","");
   281 	}
   283 	}
   282 	document.onkeydown = "";
   284 	jQuery(document).unbind('.thickbox');
   283 	document.onkeyup = "";
       
   284 	return false;
   285 	return false;
   285 }
   286 }
   286 
   287 
   287 function tb_position() {
   288 function tb_position() {
   288 var isIE6 = typeof document.body.style.maxHeight === "undefined";
   289 var isIE6 = typeof document.body.style.maxHeight === "undefined";