source: trunk/spip/esqueleto-redcta/plugins/thickbox2/javascript/thickbox.js @ 71

Last change on this file since 71 was 71, checked in by guille, 16 years ago
File size: 14.9 KB
Line 
1/*
2 * Thickbox 2.1 - One Box To Rule Them All.
3 * By Cody Lindley (http://www.codylindley.com)
4 * Copyright (c) 2006 cody lindley
5 * Licensed under the MIT License:
6 *http://www.opensource.org/licenses/mit-license.php
7 * Thickbox is built on top of the very light weight jQuery library.
8 * Modified for SPIP <www.spip.net> by Fil <fil@rezo.net>:
9 * Modified by BoOz@rezo.net
10 * - added recognition of images based on a.type
11 * - added an image gallery
12 * - added keyboard navigation ('n'ext (->), 'p'revious (<-), 'q'uit (esc))
13 * - customize path to the css and wheel image
14 * - default size for thickbox links that have no width x height indicated ??
15 * - don't load css when not needed ??
16 * - TODO: don't load js when not needed!!
17 
18 */
19 
20 
21//on page load call TB_init
22//
23// init
24//
25var DELAI = 7000 ; //nombre de secondes entre deux images
26var imageArray = [];
27var FULL_S = false;
28var DIAPO = false;
29
30if(typeof TB_chemin_css == 'undefined') { TB_chemin_css = 'thickbox.css'; }
31if(typeof TB_chemin_animation == 'undefined') { TB_chemin_animation = 'circle_animation.gif'; }
32//add thickbox to href elements that have a class of .thickbox
33
34function TB_image() {
35        var t = this.title || this.name ;
36        TB_show(t,this.href,'image');
37        return false;
38}
39
40//add thickbox to href elements that have a class of .thickbox
41function TB_init(root) {
42        $("a.thickbox",root).each(
43                function(i) {
44                        this.onclick = TB_image;
45                        var t = this.alt || this.name || null;
46                        this.alt = t + " - view bigger version";
47                        if (
48                                (this.type && this.type.match(/^image[\/](jpeg|gif|png)$/i))
49                                || (this.href && this.href.match(/\.(jpeg|jpg|png|gif)$/i))
50                        ) {
51
52                                // we store image links in an array (for a gallery)
53                                imageArray.push ([
54                                        this.href,
55                                        this.title || this.name
56                                ]);
57
58                        }
59                }
60        );
61//console.log(imageArray);
62}
63
64function TB_show(caption, url) {//function called when the user clicks on a thickbox link
65        try {
66                if (document.getElementById("TB_HideSelect") == null) {
67                $("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay' title='Fermer'></div><div id='TB_window'></div>");
68                $("#TB_overlay").click(TB_remove);
69        }
70        $(".TB_hide").hide();
71
72        if(caption==null){caption=""};
73
74        if(!FULL_S){
75                $(window).scroll(TB_position);
76        }
77
78        TB_overlaySize();
79
80        $("body").append("<div id='TB_load'><img src='"+TB_chemin_animation+"' alt='loading' /></div>");
81        TB_load_position();
82
83        if(url.indexOf("?")!==-1){ //If there is a query string involved
84                var baseURL = url.substr(0, url.indexOf("?"));
85        }else{
86                var baseURL = url;
87        }
88        var urlString = /\.jpg|\.jpeg|\.png|\.gif|\.bmp/g;
89        var urlType = baseURL.toLowerCase().match(urlString);
90
91        if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){//code to show images
92
93        TB_PrevCaption = "";
94        TB_PrevURL = "";
95        TB_PrevHTML = "";
96        TB_NextCaption = "";
97        TB_NextURL = "";
98        TB_NextHTML = "";
99        TB_imageCount = "";
100        TB_Full_Size = "";
101        TB_FoundURL = false;
102       
103        if(imageArray.length > 0){
104                TB_TempArray = imageArray ;
105                for (TB_Counter = 0; ((TB_Counter < TB_TempArray.length) && (TB_NextHTML == "")); TB_Counter++) {
106                        var urlTypeTemp = TB_TempArray[TB_Counter][0].toLowerCase().match(urlString);
107                        if (!(TB_TempArray[TB_Counter][0] == url)) {
108                                if (TB_FoundURL) {
109                                        TB_NextCaption = TB_TempArray[TB_Counter][1];
110                                        TB_NextURL = TB_TempArray[TB_Counter][0];
111                                        TB_NextHTML = "<span id='TB_next'>&nbsp;&nbsp;<a href='#' title='Image suivante'><strong> &gt;</strong></a></span>";
112                                } else {
113                                        TB_PrevCaption = TB_TempArray[TB_Counter][1];
114                                        TB_PrevURL = TB_TempArray[TB_Counter][0];
115                                        TB_PrevHTML = "<span id='TB_prev'>&nbsp;&nbsp;<a href='#' title='Image precedente'><strong>&lt; </strong></a></span>";
116                                }
117                        } else {
118                                TB_FoundURL = true;
119                                TB_imageCount =(TB_Counter + 1) +" / "+ (TB_TempArray.length);
120                        }
121                }
122        }
123        if (!(TB_NextHTML == "")) { //preload de la prochaine image
124                imageSuivante = new Image();
125                imageSuivante.src = TB_NextURL ;
126        }
127
128        imgPreloader = new Image();
129        imgPreloader.onload = function(){
130        imgPreloader.onload = null;
131        //console.log("loaded" + url);
132        // Resizing large images - orginal by Christian Montoya edited by me.
133        TB_Big_Image = false ;
134        var pagesize = TB_getPageSize();
135        var x = pagesize[0] - 150;
136        var y = pagesize[1] - 150;
137        var imageWidth = imgPreloader.width;
138        var imageHeight = imgPreloader.height;
139        IMAGE_WIDTH = imageWidth ;
140                IMAGE_HEIGHT = imageHeight ;
141
142        if (imageWidth > x) {
143                TB_Big_Image = true ;
144                imageHeight = imageHeight * (x / imageWidth); 
145                imageWidth = x; 
146
147                if (imageHeight > y) { 
148                        TB_Big_Image = true ;
149                        imageWidth = imageWidth * (y / imageHeight); 
150                        imageHeight = y; 
151                }
152        } else if (imageHeight > y) {
153                TB_Big_Image = true ;
154                imageWidth = imageWidth * (y / imageHeight);
155                imageHeight = y;
156       
157                if (imageWidth > x) {
158                        TB_Big_Image = true ;
159                        imageHeight = imageHeight * (x / imageWidth); 
160                        imageWidth = x;
161                }
162        }
163        // End Resizing
164        if(!DIAPO){
165                TB_Diapo = "<span id='TB_Diapo'>&nbsp;&nbsp;<a href='#'><strong>[Diaporama]</strong></a></span>";
166        }else{
167                TB_Diapo = "<span id='TB_Diapo'>&nbsp;&nbsp;<a href='#' title='Stop'><strong>[Stop]</strong></a></span>";
168        }
169        if (TB_Big_Image)
170                TB_Full_Size = "<span id='TB_Full'>&nbsp;&nbsp;<a href='#'><strong>[Zoom]</strong></a></span>";
171                TB_WIDTH = imageWidth + 20;
172                TB_HEIGHT = imageHeight + 20;
173
174                $("#TB_window").append("<a href='#' id='TB_ImageOff'><img id='TB_Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' alt='"+caption+" - next picture'/></a>" + "<div id='TB_legend' style='background-color:#fff'><div id='TB_caption'>"+caption+"</div><div id='TB_secondLine'>" + TB_imageCount + TB_Full_Size + TB_PrevHTML + TB_NextHTML + TB_Diapo +"</div><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton'><img src='"+TB_chemin_close+"' alt='Fermer' /></a></div></div>"); 
175
176                $("#TB_closeWindowButton").click(TB_remove);
177                $("#TB_load").remove();
178                $("#TB_window").fadeIn("slow");
179                //setTimeout('$("#TB_legend").slideDown(800);',1600);
180
181        if (!(TB_NextHTML == "")) {
182                function goNext(){
183                        FULL_S = false ;
184                        $("#TB_window").remove();
185                        $("body").append("<div id='TB_window'></div>");
186                        TB_show(TB_NextCaption, TB_NextURL); 
187                        return false;
188                }
189                $("#TB_next").click(goNext);
190        }
191
192        if (!(TB_PrevHTML == "")) {
193                function goPrev(){
194                        FULL_S = false ;
195                        if($(document).unbind('click',goPrev)){$(document).unbind('click',goPrev)};
196                        $("#TB_window").remove();
197                        $("body").append("<div id='TB_window'></div>");
198                        TB_show(TB_PrevCaption, TB_PrevURL);
199                        return false;
200                }
201                $("#TB_prev").click(goPrev);
202        }
203
204        if (!(TB_Full_Size == "")) {
205                function fullSize(){
206                        var arrayPageScroll = TB_getPageScrollTop();
207                        var pagesize = TB_getPageSize();
208       
209                        if(!FULL_S){
210                                FULL_S = true ;
211                                TB_TOP = arrayPageScroll[1];
212
213                                if( (arrayPageScroll[0] + (pagesize[0] - IMAGE_WIDTH)/2) > 0 ){
214                                        TB_LEFT = arrayPageScroll[0] + (pagesize[0] - IMAGE_WIDTH)/2 ;
215                                }else{
216                                        TB_LEFT = 50 ;
217                                }
218
219                                $("#TB_window").animate({top:TB_TOP,left:TB_LEFT,width:(IMAGE_WIDTH+20),height:(IMAGE_HEIGHT+20)},1500);
220                                $("#TB_Image").animate({top:20,left:20,width:IMAGE_WIDTH,height:IMAGE_HEIGHT},1500, TB_recadre);
221                                }
222                        else{
223                                FULL_S = false ;
224                                $("#TB_window").animate({top: (arrayPageScroll[1] + (pagesize[1]-TB_HEIGHT)/2),left:(arrayPageScroll[0] + (pagesize[0] - TB_WIDTH)/2), width:TB_WIDTH,height:TB_HEIGHT},1500);
225                                $("#TB_Image").animate({top:20,left:20,width:(TB_WIDTH - 20),height:(TB_HEIGHT - 20)},1500,TB_recadre);
226                        }
227                        return false;
228                }
229                $("#TB_Full").click(fullSize);
230         }
231
232        if(!(TB_NextHTML == "")){
233                $("#TB_ImageOff").click(goNext);
234        }else{
235                $("#TB_ImageOff").click(TB_remove);
236        }
237
238        $("#TB_Diapo").click(diaporama);
239
240        document.onkeydown = function(e){
241                if (e == null) { // ie
242                        keycode = event.keyCode;
243                } else { // mozilla
244                        keycode = e.which;
245                }
246                if(keycode == 27 | keycode == 67 | keycode == 70){ // close
247                        TB_remove();
248                } else if(keycode == 190 | keycode == 39){ // display previous image <-
249                        if(!(TB_NextHTML == "")){
250                                document.onkeydown = "";
251                                goNext();
252                        }
253                } else if(keycode == 188| keycode == 37){ // display next image ->
254                        if(!(TB_PrevHTML == "")){
255                                document.onkeydown = "";
256                                goPrev();
257                        }
258                }
259        }
260
261        TB_position() ;
262         
263        $("#TB_load").remove();
264        $("#TB_window").css({display:"block"}); //for safari using css instead of show
265
266
267        //diapo
268        //console.log("deb " + DELAI);
269        if(DIAPO)
270                setTimeout('diapo();',DELAI);
271                $("#TB_ImageOff")[0].focus();
272        }
273
274        imgPreloader.src = url;
275        //console.log("hop" + url);
276
277        }else{//code to show html pages
278                //console.log(url);
279                var queryString = url.replace(/^[^\?]+\?+/,'');
280                //rhooooo, c'est pas du boulot ce thickbox
281                queryString = queryString.replace(/^[^\?]+\?+/,'');
282                //console.log(queryString);
283                var params = TB_parseQuery( queryString );
284                //console.log(params);
285
286                TB_WIDTH = (params['width']*1) + 30;
287                TB_HEIGHT = (params['height']*1) + 40;
288
289                ajaxContentW = TB_WIDTH - 30;
290                ajaxContentH = TB_HEIGHT - 45;
291
292                if(url.indexOf('TB_iframe') != -1){
293                        urlNoQuery = url.split('TB_');
294                        $("#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>");
295                }else{
296                        $("#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>");
297                }
298
299                $("#TB_closeWindowButton").click(TB_remove);
300
301                if(url.indexOf('TB_inline') != -1){
302                        $("#TB_ajaxContent").html($('#' + params['inlineId']).html());
303                        TB_position();
304                        $("#TB_load").remove();
305                        $("#TB_window").css({display:"block"}); 
306                }else if(url.indexOf('TB_iframe') != -1){
307                        TB_position();
308                        if(frames['TB_iframeContent'] == undefined){//be nice to safari
309                                $("#TB_load").remove();
310                                $("#TB_window").css({display:"block"});
311                                $(document).keyup( function(e){ var key = e.keyCode; if(key == 27){TB_remove()} });
312                        }
313                }else{
314                        $("#TB_ajaxContent").load(url, function(){
315                                TB_position();
316                                $("#TB_load").remove();
317                                $("#TB_window").css({display:"block"}); 
318                        });
319                }
320
321        }
322
323        $(window).resize(TB_position);
324
325        document.onkeyup = function(e){
326                if (e == null) { // ie
327                        keycode = event.keyCode;
328                } else { // mozilla
329                        keycode = e.which;
330                }
331
332                if(keycode == 27){ // close
333                        TB_remove();
334                }
335        }
336
337        } catch(e) {
338                alert( e );
339        }
340}
341
342//helper functions below
343function diaporama(){
344        //alert("diapo");
345        if(!DIAPO){
346                DIAPO = true ;
347                //console.log("deb");
348                diapo();
349                //$("TB_secondLine").html(TB_imageCount + TB_Full_Size + TB_PrevHTML + TB_NextHTML + "[Stop]");
350        } else {
351                DIAPO = false ;
352        }
353}
354
355function diapo(){
356        //console.log(DIAPO);
357        if(DIAPO){
358                if(TB_NextURL !=""){
359                        $("#TB_window").remove();
360                        $("body").append("<div id='TB_window'></div>");
361                        //console.log("hop");
362                        TB_show(TB_NextCaption, TB_NextURL); 
363                }else DIAPO = false ;
364        }
365        return false;
366}
367
368function TB_showIframe(){
369        $("#TB_load").remove();
370        $("#TB_window").css({display:"block"});
371}
372
373function TB_remove() {
374        DIAPO = false ;
375        FULL_S = false ;
376        $("#TB_imageOff").unbind('click');
377        $("#TB_overlay").unbind('click');
378        $("#TB_closeWindowButton").unbind('click');
379        $("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').remove();});
380        $("#TB_load").remove();
381        $(".TB_hide").show();
382       
383        return false;
384}
385
386function TB_position() {
387        var pagesize = TB_getPageSize();
388        var arrayPageScroll = TB_getPageScrollTop();
389        var legendHeight = $("#TB_legend").height() ;   
390
391        if(FULL_S && DIAPO){
392                FULL_S = false ;
393                $("#TB_window").animate({top: (arrayPageScroll[1] + (pagesize[1]-TB_HEIGHT)/2),left:(arrayPageScroll[0] + (pagesize[0] - TB_WIDTH)/2), width:TB_WIDTH,height:TB_HEIGHT},1500);
394                $("#TB_Image").animate({top:20,left:20,width:(TB_WIDTH - 20),height:(TB_HEIGHT - 20)},1500,TB_recadre);
395        }
396
397        if(!FULL_S || DIAPO){
398                $("#TB_window").css({width:TB_WIDTH+"px",left: (arrayPageScroll[0] + (pagesize[0] - TB_WIDTH)/2)+"px", top: (arrayPageScroll[1] + (pagesize[1]-TB_HEIGHT)/2 - legendHeight/2)+"px" });
399        }
400}
401
402function TB_overlaySize(){
403        if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {
404                yScroll = window.innerHeight + window.scrollMaxY;
405                xScroll = window.innerWidth + window.scrollMaxX;
406                var deff = document.documentElement;
407                var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
408                var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
409                xScroll -= (window.innerWidth - wff);
410                yScroll -= (window.innerHeight - hff);
411        } else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
412                yScroll = document.body.scrollHeight;
413                xScroll = document.body.scrollWidth;
414        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
415                yScroll = document.body.offsetHeight;
416                xScroll = document.body.offsetWidth;
417        }
418        $("#TB_overlay").css({"height":yScroll +"px", "width":xScroll +"px"});
419        $("#TB_HideSelect").css({"height":yScroll +"px","width":xScroll +"px"});
420}
421
422function TB_load_position() {
423        var pagesize = TB_getPageSize();
424        var arrayPageScroll = TB_getPageScrollTop();
425        $("#TB_load")
426        .css({left: (arrayPageScroll[0] + (pagesize[0] - 100)/2)+"px", top: (arrayPageScroll[1] + ((pagesize[1]-100)/2))+"px" })
427        .css({display:"block"});
428}
429
430function TB_parseQuery ( query ) {
431        var Params = new Object ();
432        if ( ! query ) return Params; // return empty object
433                var Pairs = query.split(/[;&]/);
434       
435        for ( var i = 0; i < Pairs.length; i++ ) {
436                var KeyVal = Pairs[i].split('=');
437
438                if ( ! KeyVal || KeyVal.length != 2 ) continue;
439                        var key = unescape( KeyVal[0] );
440                        var val = unescape( KeyVal[1] );
441                        val = val.replace(/\+/g, ' ');
442                        Params[key] = val;
443        }
444        return Params;
445}
446
447function TB_getPageScrollTop(){
448        var yScrolltop;
449        var xScrollleft;
450
451        if (self.pageYOffset || self.pageXOffset) {
452                yScrolltop = self.pageYOffset;
453                xScrollleft = self.pageXOffset;
454       
455        } else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){// Explorer 6 Strict
456                yScrolltop = document.documentElement.scrollTop;
457                xScrollleft = document.documentElement.scrollLeft;
458       
459        } else if (document.body) {// all other Explorers
460                yScrolltop = document.body.scrollTop;
461                xScrollleft = document.body.scrollLeft;
462        }
463        arrayPageScroll = new Array(xScrollleft,yScrolltop) 
464        return arrayPageScroll;
465}
466
467function TB_getPageSize(){
468        var de = document.documentElement;
469        var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
470        var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
471        arrayPageSize = new Array(w,h) 
472        return arrayPageSize;
473}
474
475function TB_recadre(){
476        TB_overlaySize();
477        TB_position();
478}
Note: See TracBrowser for help on using the repository browser.