thickbox.js 11 KB

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