angelovcom.net

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

thickbox.js (13194B)


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