/var/www/static/js/jquery.ThickBox.js
JavaScript | 341 lines | 251 code | 48 blank | 42 comment | 75 complexity | b434a738c59764e3dc02aacf26813780 MD5 | raw file
Possible License(s): BSD-3-Clause
1/*
2 * Thickbox 2.1 - jQuery plugin for displaying content in a box above the page
3 *
4 * Copyright (c) 2006, 2007 Cody Lindley (http://www.codylindley.com)
5 *
6 * Licensed under the MIT License:
7 * http://www.opensource.org/licenses/mit-license.php
8 */
9
10// on page load call TB_init
11$(document).ready(TB_init);
12
13// add thickbox to href elements that have a class of .thickbox
14function TB_init(){
15 $("a.thickbox").click(function(event){
16 // stop default behaviour
17 event.preventDefault();
18 // remove click border
19 this.blur();
20
21 // get caption: either title or name attribute
22 var caption = this.title || this.name || "";
23
24 // get rel attribute for image groups
25 var group = this.rel || false;
26
27 // display the box for the elements href
28 TB_show(caption, this.href, group);
29 });
30}
31
32// called when the user clicks on a thickbox link
33function TB_show(caption, url, rel) {
34
35 // create iframe, overlay and box if non-existent
36 if ( !$("#TB_HideSelect").length ) {
37 $("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
38 $("#TB_overlay").click(TB_remove);
39 }
40 // TODO replace or check if event is already assigned
41 $(window).scroll(TB_position);
42
43 // TODO replace
44 TB_overlaySize();
45
46 // TODO create loader only once, hide and show on demand
47 $("body").append("<div id='TB_load'><img src='/images/loadingAnimation.gif' /></div>");
48 TB_load_position();
49
50 // check if a query string is involved
51 var baseURL = url.match(/(.+)?/)[1] || url;
52
53 // regex to check if a href refers to an image
54 var imageURL = /\.(jpe?g|png|gif|bmp)/gi;
55
56 // check for images
57 if ( baseURL.match(imageURL) ) {
58 var dummy = { caption: "", url: "", html: "" };
59
60 var prev = dummy,
61 next = dummy,
62 imageCount = "";
63
64 // if an image group is given
65 if ( rel ) {
66 function getInfo(image, id, label) {
67 return {
68 caption: image.title,
69 url: image.href,
70 html: "<span id='TB_" + id + "'> <a href='#'>" + label + "</a></span>"
71 }
72 }
73
74 // find the anchors that point to the group
75 var imageGroup = $("a[@rel="+rel+"]").get();
76 var foundSelf = false;
77
78 // loop through the anchors, looking for ourself, saving information about previous and next image
79 for (var i = 0; i < imageGroup.length; i++) {
80 var image = imageGroup[i];
81 var urlTypeTemp = image.href.match(imageURL);
82
83 // look for ourself
84 if ( image.href == url ) {
85 foundSelf = true;
86 imageCount = "Image " + (i + 1) + " of "+ (imageGroup.length);
87 } else {
88 // when we found ourself, the current is the next image
89 if ( foundSelf ) {
90 next = getInfo(image, "next", "Next >");
91 // stop searching
92 break;
93 } else {
94 // didn't find ourself yet, so this may be the one before ourself
95 prev = getInfo(image, "prev", "< Prev");
96 }
97 }
98 }
99 }
100
101 imgPreloader = new Image();
102 imgPreloader.onload = function() {
103 imgPreloader.onload = null;
104
105 // Resizing large images
106 var pagesize = TB_getPageSize();
107 var x = pagesize[0] - 150;
108 var y = pagesize[1] - 150;
109 var imageWidth = imgPreloader.width;
110 var imageHeight = imgPreloader.height;
111 if (imageWidth > x) {
112 imageHeight = imageHeight * (x / imageWidth);
113 imageWidth = x;
114 if (imageHeight > y) {
115 imageWidth = imageWidth * (y / imageHeight);
116 imageHeight = y;
117 }
118 } else if (imageHeight > y) {
119 imageWidth = imageWidth * (y / imageHeight);
120 imageHeight = y;
121 if (imageWidth > x) {
122 imageHeight = imageHeight * (x / imageWidth);
123 imageWidth = x;
124 }
125 }
126 // End Resizing
127
128 // TODO don't use globals
129 TB_WIDTH = imageWidth + 30;
130 TB_HEIGHT = imageHeight + 60;
131
132 // TODO empty window content instead
133 $("#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'>" + imageCount + prev.html + next.html + "</div></div><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton' title='Close'>close</a></div>");
134
135 $("#TB_closeWindowButton").click(TB_remove);
136
137 function buildClickHandler(image) {
138 return function() {
139 $("#TB_window").remove();
140 $("body").append("<div id='TB_window'></div>");
141 TB_show(image.caption, image.url, rel);
142 return false;
143 };
144 }
145 var goPrev = buildClickHandler(prev);
146 var goNext = buildClickHandler(next);
147 if ( prev.html ) {
148 $("#TB_prev").click(goPrev);
149 }
150
151 if ( next.html ) {
152 $("#TB_next").click(goNext);
153 }
154
155 // TODO use jQuery, maybe with event fix plugin, or just get the necessary parts of it
156 document.onkeydown = function(e) {
157 if (e == null) { // ie
158 keycode = event.keyCode;
159 } else { // mozilla
160 keycode = e.which;
161 }
162 switch(keycode) {
163 case 27:
164 TB_remove();
165 break;
166 case 190:
167 if( next.html ) {
168 document.onkeydown = null;
169 goNext();
170 }
171 break;
172 case 188:
173 if( prev.html ) {
174 document.onkeydown = null;
175 goPrev();
176 }
177 break;
178 }
179 }
180
181 // TODO don't remove loader etc., just hide and show later
182 TB_position();
183 $("#TB_load").remove();
184 $("#TB_ImageOff").click(TB_remove);
185
186 // for safari using css instead of show
187 // TODO is that necessary? can't test safari
188 $("#TB_window").css({display:"block"});
189 }
190 imgPreloader.src = url;
191
192 } else { //code to show html pages
193
194 var queryString = url.match(/\?(.+)/)[1];
195 var params = TB_parseQuery( queryString );
196
197 TB_WIDTH = (params['width']*1) + 30;
198 TB_HEIGHT = (params['height']*1) + 40;
199
200 var ajaxContentW = TB_WIDTH - 30,
201 ajaxContentH = TB_HEIGHT - 45;
202
203 if(url.indexOf('TB_iframe') != -1){
204 urlNoQuery = url.split('TB_');
205 $("#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></div></div><iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' onload='TB_showIframe()'> </iframe>");
206 } else {
207 $("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton'>close</a></div></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
208 }
209
210 $("#TB_closeWindowButton").click(TB_remove);
211
212 if(url.indexOf('TB_inline') != -1){
213 $("#TB_ajaxContent").html($('#' + params['inlineId']).html());
214 TB_position();
215 $("#TB_load").remove();
216 $("#TB_window").css({display:"block"});
217 }else if(url.indexOf('TB_iframe') != -1){
218 TB_position();
219 if(frames['TB_iframeContent'] == undefined){//be nice to safari
220 $("#TB_load").remove();
221 $("#TB_window").css({display:"block"});
222 $(document).keyup( function(e){ var key = e.keyCode; if(key == 27){TB_remove()} });
223 }
224 }else{
225 $("#TB_ajaxContent").load(url, function(){
226 TB_position();
227 $("#TB_load").remove();
228 $("#TB_window").css({display:"block"});
229 });
230 }
231
232 }
233
234 $(window).resize(TB_position);
235
236 document.onkeyup = function(e){
237 if (e == null) { // ie
238 keycode = event.keyCode;
239 } else { // mozilla
240 keycode = e.which;
241 }
242 if(keycode == 27){ // close
243 TB_remove();
244 }
245 }
246
247}
248
249//helper functions below
250
251function TB_showIframe(){
252 $("#TB_load").remove();
253 $("#TB_window").css({display:"block"});
254}
255
256function TB_remove() {
257 $("#TB_imageOff").unbind("click");
258 $("#TB_overlay").unbind("click");
259 $("#TB_closeWindowButton").unbind("click");
260 $("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').remove();});
261 $("#TB_load").remove();
262 return false;
263}
264
265function TB_position() {
266 var pagesize = TB_getPageSize();
267 var arrayPageScroll = TB_getPageScrollTop();
268 var style = {width: TB_WIDTH, left: (arrayPageScroll[0] + (pagesize[0] - TB_WIDTH)/2), top: (arrayPageScroll[1] + (pagesize[1]-TB_HEIGHT)/2)};
269 $("#TB_window").css(style);
270}
271
272function TB_overlaySize(){
273 if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {
274 yScroll = window.innerHeight + window.scrollMaxY;
275 xScroll = window.innerWidth + window.scrollMaxX;
276 var deff = document.documentElement;
277 var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
278 var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
279 xScroll -= (window.innerWidth - wff);
280 yScroll -= (window.innerHeight - hff);
281 } else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
282 yScroll = document.body.scrollHeight;
283 xScroll = document.body.scrollWidth;
284 } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
285 yScroll = document.body.offsetHeight;
286 xScroll = document.body.offsetWidth;
287 }
288 $("#TB_overlay").css({"height": yScroll, "width": xScroll});
289 $("#TB_HideSelect").css({"height": yScroll,"width": xScroll});
290}
291
292function TB_load_position() {
293 var pagesize = TB_getPageSize();
294 var arrayPageScroll = TB_getPageScrollTop();
295 $("#TB_load")
296 .css({left: (arrayPageScroll[0] + (pagesize[0] - 100)/2), top: (arrayPageScroll[1] + ((pagesize[1]-100)/2)) })
297 .css({display:"block"});
298}
299
300function TB_parseQuery ( query ) {
301 // return empty object
302 if( !query )
303 return {};
304 var params = {};
305
306 // parse query
307 var pairs = query.split(/[;&]/);
308 for ( var i = 0; i < pairs.length; i++ ) {
309 var pair = pairs[i].split('=');
310 if ( !pair || pair.length != 2 )
311 continue;
312 // unescape both key and value, replace "+" with spaces in value
313 params[unescape(pair[0])] = unescape(pair[1]).replace(/\+/g, ' ');
314 }
315 return params;
316}
317
318function TB_getPageScrollTop(){
319 var yScrolltop;
320 var xScrollleft;
321 if (self.pageYOffset || self.pageXOffset) {
322 yScrolltop = self.pageYOffset;
323 xScrollleft = self.pageXOffset;
324 } else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){ // Explorer 6 Strict
325 yScrolltop = document.documentElement.scrollTop;
326 xScrollleft = document.documentElement.scrollLeft;
327 } else if (document.body) {// all other Explorers
328 yScrolltop = document.body.scrollTop;
329 xScrollleft = document.body.scrollLeft;
330 }
331 arrayPageScroll = new Array(xScrollleft,yScrolltop)
332 return arrayPageScroll;
333}
334
335function TB_getPageSize(){
336 var de = document.documentElement;
337 var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
338 var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
339 arrayPageSize = new Array(w,h)
340 return arrayPageSize;
341}