~elpatuleko71/jfleet/multi_image

« back to all changes in this revision

Viewing changes to static/lib/lightbox/js/jquery.lightbox.js

  • Committer: John Viloria
  • Date: 2014-08-05 04:05:00 UTC
  • Revision ID: elpatuleko71@gmail.com-20140805040500-4v5qgztwoxb8mve3
[INIT] add multi image module to jfleet module

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * jQuery Lightbox
 
3
 * @author Warren Krewenki
 
4
 *
 
5
 * This package is distributed under the BSD license.
 
6
 * For full license information, see LICENSE.TXT
 
7
 *
 
8
 * Based on Lightbox 2 by Lokesh Dhakar (http://www.huddletogether.com/projects/lightbox2/)
 
9
 *
 
10
 *
 
11
 **/
 
12
 
 
13
(function($) {
 
14
        $.fn.lightbox = function(options) {
 
15
                // build main options
 
16
                var opts = $.extend({}, $.fn.lightbox.defaults, options);
 
17
        
 
18
                $(window).resize(resizeOverlayToFitWindow);
 
19
        
 
20
                initialize();
 
21
                showLightbox(this);
 
22
                /*
 
23
                # Initialize the lightbox by creating our html and reading some image data
 
24
                # This method is called by the constructor after any click events trigger it
 
25
                # You will never call it by itself, to my knowledge.
 
26
                */
 
27
                function initialize() {
 
28
                        $('#overlay, #lightbox').remove();
 
29
                        opts.inprogress = false;
 
30
 
 
31
                        // if jsonData, build the imageArray from data provided in JSON format
 
32
                        if (opts.jsonData && opts.jsonData.length > 0) {
 
33
                                var parser = opts.jsonDataParser ? opts.jsonDataParser : $.fn.lightbox.parseJsonData;                
 
34
                                opts.imageArray = [];
 
35
                                opts.imageArray = parser(opts.jsonData);
 
36
                        }
 
37
    
 
38
                        var outerImage = '<div id="outerImageContainer"><div id="imageContainer" style="height: 520px; width: 720px;"><iframe id="lightboxIframe"></iframe><img id="lightboxImage" /><div id="hoverNav"><a href="javascript://" title="' + opts.strings.prevLinkTitle + '" id="prevLink"></a><a href="javascript://" id="nextLink" title="' + opts.strings.nextLinkTitle + '"></a></div><div id="loading"><a href="javascript://" id="loadingLink"><img src="'+opts.fileLoadingImage+'"></a></div></div></div>';
 
39
                        var imageData = '<div id="imageDataContainer" class="clearfix"><div id="imageData"><div id="imageDetails"><span id="caption"></span><span id="numberDisplay"></span></div><div id="bottomNav">';
 
40
 
 
41
//                      var outerImage = '<div id="outerImageContainer"><div id="imageContainer" style="height: 520px; width: 720px;"><iframe id="lightboxIframe"></iframe><img id="lightboxImage" syle="vertical-align: middle;" /><div id="hoverNav"><a href="javascript://" title="' + opts.strings.prevLinkTitle + '" id="prevLink"></a><a href="javascript://" id="nextLink" title="' + opts.strings.nextLinkTitle + '"></a></div><div id="loading"><a href="javascript://" id="loadingLink"><img src="'+opts.fileLoadingImage+'"></a></div></div></div>';
 
42
//                      var imageData = '<div id="imageDataContainer" class="clearfix"><div id="imageData"><div id="imageDetails"><span id="caption"></span><span id="numberDisplay"></span></div><div id="bottomNav">';
 
43
 
 
44
                        if (opts.displayHelp) {
 
45
                                imageData += '<span id="helpDisplay">' + opts.strings.help + '</span>';
 
46
                        }
 
47
 
 
48
                        imageData += '<a href="javascript://" id="bottomNavClose" title="' + opts.strings.closeTitle + '"><img src="'+opts.fileBottomNavCloseImage+'"></a></div></div></div>';
 
49
 
 
50
                        var string;
 
51
 
 
52
                        if (opts.navbarOnTop) {
 
53
                                string = '<div id="overlay"></div><div id="lightbox">' + imageData + outerImage + '</div>';
 
54
                                $("body").append(string);
 
55
                                $("#imageDataContainer").addClass('ontop');
 
56
                        } else {
 
57
                                string = '<div id="overlay"></div><div id="lightbox">' + outerImage + imageData + '</div>';
 
58
                                $("body").append(string);
 
59
                        }
 
60
 
 
61
                        //$("#overlay, #lightbox").click(function(){ end(); }).hide();
 
62
                        $("#loadingLink, #bottomNavClose").click(function(){ end(); return false;});
 
63
                        $('#outerImageContainer').width(opts.widthCurrent).height(opts.heightCurrent);
 
64
                        $('#imageDataContainer').width(opts.widthCurrent);
 
65
 
 
66
                        if (!opts.imageClickClose) {
 
67
                                $("#lightboxImage").click(function(){ return false; });
 
68
                                $("#hoverNav").click(function(){ return false; });
 
69
                        }
 
70
                        
 
71
                        return true;
 
72
                };
 
73
 
 
74
                /*
 
75
                # Get the document and window width/heigh
 
76
                #
 
77
                # Examples
 
78
                #
 
79
                #       getPageSize()
 
80
                #       # => [1024,768,1024,768]
 
81
                #
 
82
                # Returns a numerically indexed array of document width/height and window width/height
 
83
                */
 
84
                function getPageSize() {
 
85
                        var jqueryPageSize = new Array($(document).width(),$(document).height(), $(window).width(), $(window).height());
 
86
                        return jqueryPageSize;
 
87
                };
 
88
            
 
89
                function getPageScroll() {
 
90
                        var xScroll, yScroll;
 
91
 
 
92
                        if (self.pageYOffset) {
 
93
                                yScroll = self.pageYOffset;
 
94
                                xScroll = self.pageXOffset;
 
95
                        } else if (document.documentElement && document.documentElement.scrollTop){  // Explorer 6 Strict
 
96
                                yScroll = document.documentElement.scrollTop;
 
97
                                xScroll = document.documentElement.scrollLeft;
 
98
                        } else if (document.body) {// all other Explorers
 
99
                                yScroll = document.body.scrollTop;
 
100
                                xScroll = document.body.scrollLeft;
 
101
                        }
 
102
 
 
103
                        var arrayPageScroll = new Array(xScroll,yScroll);
 
104
                        return arrayPageScroll;
 
105
                };
 
106
 
 
107
                /*
 
108
                # Deploy the sexy overlay and display the lightbox
 
109
                #
 
110
                # imageObject - the jQuery object passed via the click event in the constructor
 
111
                #
 
112
                # Examples
 
113
                #
 
114
                #       showLightbox($('#CheesusCrust'))
 
115
                #
 
116
                # Returns a boolean true, because it's got nothing else to return. It should give visual feedback when run
 
117
                */
 
118
                function showLightbox(imageObject) {
 
119
                        /**
 
120
                        * select, embed and object tags render over the lightbox in some browsers
 
121
                        * Right now, the best way to fix it is to hide them, but that can trigger reloading of some flash content
 
122
                        * I don't have a better fix for this right now, but I want ot leave this comment here so you and I both 
 
123
                        * know that i'm aware of it, and I would love to fix it, if you have any suggestions.
 
124
                        **/
 
125
                        $("select, embed, object").hide();
 
126
 
 
127
                        // Resize and display the sexy, sexy overlay.
 
128
                        resizeOverlayToFitWindow();
 
129
                        $("#overlay").hide().css({ opacity : opts.overlayOpacity }).fadeIn();
 
130
                        imageNum = 0;
 
131
 
 
132
                        // if data is not provided by jsonData parameter
 
133
                        if (!opts.jsonData) {
 
134
                                opts.imageArray = [];
 
135
                                // if image is NOT part of a set..
 
136
                                if ((!imageObject.rel || (imageObject.rel == '')) && !opts.allSet) {
 
137
                                        // add single image to Lightbox.imageArray
 
138
                                        opts.imageArray.push(new Array(imageObject.href, opts.displayTitle ? imageObject.title : ''));
 
139
                                } else {
 
140
                                        // if image is part of a set..
 
141
                                        $("a").each(function() {
 
142
                                                if(this.href && (this.rel == imageObject.rel)) {
 
143
                                                        opts.imageArray.push(new Array(this.href, opts.displayTitle ? this.title : ''));
 
144
                                                }
 
145
                                        });
 
146
                                }
 
147
                        }
 
148
        
 
149
                        if (opts.imageArray.length > 1) {
 
150
                                for (i = 0; i < opts.imageArray.length; i++) {
 
151
                                        for (j = opts.imageArray.length - 1; j > i; j--) {
 
152
                                                if (opts.imageArray[i][0] == opts.imageArray[j][0]) {
 
153
                                                        opts.imageArray.splice(j, 1);
 
154
                                                }
 
155
                                        }
 
156
                                }
 
157
                                while (opts.imageArray[imageNum][0] != imageObject.href) { 
 
158
                                        imageNum++;
 
159
                                if (imageNum < opts.imageArray.length) {
 
160
                                                break;
 
161
                                        }
 
162
                                }
 
163
                        }
 
164
 
 
165
                        // calculate top and left offset for the lightbox
 
166
                        var arrayPageScroll = getPageScroll();
 
167
                        var lightboxTop = arrayPageScroll[1] + ($(window).height() / 10);
 
168
                        var lightboxLeft = arrayPageScroll[0];
 
169
                        $('#lightbox').css({top: lightboxTop+'px', left: lightboxLeft+'px'}).show();
 
170
 
 
171
                        if (!opts.slideNavBar) {
 
172
                                $('#imageData').hide();
 
173
                        }
 
174
 
 
175
                        changeImage(imageNum);
 
176
                };
 
177
            
 
178
                function changeImage(imageNum) {
 
179
                        if (opts.inprogress == false) {
 
180
                                opts.inprogress = true;
 
181
 
 
182
                                // update global var
 
183
                                opts.activeImage = imageNum;    
 
184
 
 
185
                                // hide elements during transition
 
186
                                $('#loading').show();
 
187
                                $('#lightboxImage, #hoverNav, #prevLink, #nextLink').hide();
 
188
 
 
189
                                // delay preloading image until navbar will slide up
 
190
                                if (opts.slideNavBar) { 
 
191
                                        $('#imageDataContainer').hide();
 
192
                                        $('#imageData').hide();
 
193
                                }
 
194
                                doChangeImage();
 
195
                        }
 
196
                };
 
197
 
 
198
                function doChangeImage() {
 
199
                        imgPreloader = new Image();
 
200
 
 
201
                        // once image is preloaded, resize image container
 
202
                        imgPreloader.onload = function() {
 
203
                                var newWidth = imgPreloader.width;
 
204
                                var newHeight = imgPreloader.height;
 
205
 
 
206
                                if (opts.scaleImages) {
 
207
                                        newWidth = parseInt(opts.xScale * newWidth);
 
208
                                        newHeight = parseInt(opts.yScale * newHeight);
 
209
                                }
 
210
 
 
211
                                if (opts.fitToScreen) {
 
212
                                        var arrayPageSize = getPageSize();
 
213
                                        var ratio;
 
214
                                        var initialPageWidth = arrayPageSize[2] - 2 * opts.borderSize;
 
215
                                        var initialPageHeight = arrayPageSize[3] - 200;
 
216
 
 
217
                                        var dI = initialPageWidth/initialPageHeight;
 
218
                                        var dP = imgPreloader.width/imgPreloader.height;
 
219
 
 
220
                                        if ((imgPreloader.height > initialPageHeight) || (imgPreloader.width > initialPageWidth)) {
 
221
                                                if (dI > dP) {
 
222
                                                        newWidth = parseInt((initialPageHeight/imgPreloader.height) * imgPreloader.width);
 
223
                                                        newHeight = initialPageHeight;
 
224
                                                } else {
 
225
                                                        newHeight = parseInt((initialPageWidth/imgPreloader.width) * imgPreloader.height);
 
226
                                                        newWidth = initialPageWidth;
 
227
                                                }
 
228
                                        }
 
229
                                }
 
230
                                if (newWidth > 700) {
 
231
                                        newWidth = 700
 
232
                                }
 
233
                                if (newHeight > 500) {
 
234
                                        newHeight = 500
 
235
                                }
 
236
                                var line_height = (newHeight / 100) * 7 + 24
 
237
                                $("#imageContainer").attr("style", "line-height:" + line_height)
 
238
                                $("#lightboxImage").attr("style", "line-height:" + line_height)
 
239
                                $('#lightboxImage').
 
240
                                        attr('src', opts.imageArray[opts.activeImage][0]).
 
241
                                        width(newWidth).
 
242
                                        height(newHeight);
 
243
 
 
244
                                        resizeImageContainer(newWidth, newHeight);
 
245
                                };
 
246
 
 
247
                                imgPreloader.src = opts.imageArray[opts.activeImage][0];
 
248
                        };
 
249
 
 
250
                        function end() {
 
251
                                $('#overlay, #lightbox, #outerImageContainer, #imageDataContainer').remove();
 
252
                        };
 
253
 
 
254
                        function preloadNeighborImages() {
 
255
                                if (opts.loopImages && opts.imageArray.length > 1) {
 
256
                                        preloadNextImage = new Image();
 
257
                                        preloadNextImage.src = opts.imageArray[(opts.activeImage == (opts.imageArray.length - 1)) ? 0 : opts.activeImage + 1][0];
 
258
 
 
259
                                        preloadPrevImage = new Image();
 
260
                                        preloadPrevImage.src = opts.imageArray[(opts.activeImage == 0) ? (opts.imageArray.length - 1) : opts.activeImage - 1][0];
 
261
                                } else {
 
262
                                        if ((opts.imageArray.length - 1) > opts.activeImage) {
 
263
                                                preloadNextImage = new Image();
 
264
                                                preloadNextImage.src = opts.imageArray[opts.activeImage + 1][0];
 
265
                                        }
 
266
                                        if (opts.activeImage > 0) {
 
267
                                                preloadPrevImage = new Image();
 
268
                                                preloadPrevImage.src = opts.imageArray[opts.activeImage - 1][0];
 
269
                                        }
 
270
                                }
 
271
                        };
 
272
 
 
273
                        function resizeImageContainer(imgWidth, imgHeight) {
 
274
                                // get current width and height
 
275
                                opts.widthCurrent = $("#outerImageContainer").outerWidth();
 
276
                                opts.heightCurrent = $("#outerImageContainer").outerHeight();
 
277
 
 
278
                                // get new width and height
 
279
                                var widthNew = 730;
 
280
                                var heightNew = 520;
 
281
 
 
282
                                // calculate size difference between new and old image, and resize if necessary
 
283
                                wDiff = opts.widthCurrent - widthNew;
 
284
                                hDiff = opts.heightCurrent - heightNew;
 
285
 
 
286
                                $('#imageDataContainer').animate({width: widthNew},opts.resizeSpeed,'linear');
 
287
                                $('#outerImageContainer').animate({width: widthNew},opts.resizeSpeed,'linear', function() {
 
288
                                        $('#outerImageContainer').animate({height: heightNew},opts.resizeSpeed,'linear', function() {
 
289
                                                showImage();
 
290
                                        });
 
291
                                });
 
292
                                
 
293
                                afterTimeout = function () {
 
294
                                $('#prevLink').height(500);
 
295
                                $('#nextLink').height(500);
 
296
                                };
 
297
 
 
298
                                // if new and old image are same size and no scaling transition is necessary,
 
299
                                // do a quick pause to prevent image flicker.
 
300
                                if((hDiff == 0) && (wDiff == 0)) {
 
301
                                        if (jQuery.browser.msie) { 
 
302
                                                setTimeout(afterTimeout, 250); 
 
303
                                        } else { 
 
304
                                                setTimeout(afterTimeout, 100);
 
305
                                        }
 
306
                                } else {
 
307
                                    // otherwise just trigger the height and width change
 
308
                                    afterTimeout();
 
309
                                }
 
310
 
 
311
                        };
 
312
 
 
313
                        function showImage() {
 
314
                                $('#loading').hide();
 
315
                                $('#lightboxImage').fadeIn("fast");
 
316
                                updateDetails();
 
317
                                preloadNeighborImages();
 
318
 
 
319
                                opts.inprogress = false;
 
320
                        };
 
321
 
 
322
                        function updateDetails() {
 
323
                                $('#numberDisplay').html('');
 
324
 
 
325
                                if (opts.imageArray[opts.activeImage][1]) {
 
326
                                        $('#caption').html(opts.imageArray[opts.activeImage][1]).show();
 
327
                                }
 
328
 
 
329
                                // if image is part of set display 'Image x of x'
 
330
                                if (opts.imageArray.length > 1) {
 
331
                                        var nav_html;
 
332
 
 
333
                                        nav_html = opts.strings.image + (opts.activeImage + 1) + opts.strings.of + opts.imageArray.length;
 
334
 
 
335
                                        if (opts.displayDownloadLink) {
 
336
                                                nav_html += "<a href='" + opts.imageArray[opts.activeImage][0] + "'>" + opts.strings.download + "</a>";
 
337
                                        }                               
 
338
 
 
339
                                        if (!opts.disableNavbarLinks) {
 
340
                                                // display previous / next text links
 
341
                                                if ((opts.activeImage) > 0 || opts.loopImages) {
 
342
                                                        nav_html = '<a title="' + opts.strings.prevLinkTitle + '" href="#" id="prevLinkText">' + opts.strings.prevLinkText + "</a>" + nav_html;
 
343
                                                }
 
344
 
 
345
                                                if (((opts.activeImage + 1) < opts.imageArray.length) || opts.loopImages) {
 
346
                                                        nav_html += '<a title="' + opts.strings.nextLinkTitle + '" href="#" id="nextLinkText">' + opts.strings.nextLinkText + "</a>";
 
347
                                                }
 
348
                                        }
 
349
 
 
350
                                        $('#numberDisplay').html(nav_html).show();
 
351
                                }
 
352
 
 
353
                                if (opts.slideNavBar) {
 
354
                                        $("#imageData").slideDown(opts.navBarSlideSpeed);
 
355
                                } else {
 
356
                                        $("#imageData").show();
 
357
                                }
 
358
 
 
359
                                resizeOverlayToFitWindow();
 
360
                                updateNav();
 
361
                        };
 
362
 
 
363
                        /*
 
364
                        # Resize the sexy overlay to fit the constraints of your current viewing environment
 
365
                        # 
 
366
                        # This should now happen whenever a window is resized, so you should always see a full overlay
 
367
                        */
 
368
                        function resizeOverlayToFitWindow(){
 
369
                                $('#overlay').css({width: $(document).width(), height: $(document).height()});
 
370
                                //  ^^^^^^^ <- sexy!
 
371
                        };
 
372
 
 
373
                        function updateNav() {
 
374
                                if (opts.imageArray.length > 1) {
 
375
                                        $('#hoverNav').show();
 
376
 
 
377
                                        // if loopImages is true, always show next and prev image buttons 
 
378
                                        if(opts.loopImages) {
 
379
                                                $('#prevLink,#prevLinkText').show().click(function() {
 
380
                                                        changeImage((opts.activeImage == 0) ? (opts.imageArray.length - 1) : opts.activeImage - 1); 
 
381
                                                        return false;
 
382
                                                });
 
383
 
 
384
                                                $('#nextLink,#nextLinkText').show().click(function() {
 
385
                                                        changeImage((opts.activeImage == (opts.imageArray.length - 1)) ? 0 : opts.activeImage + 1); 
 
386
                                                        return false;
 
387
                                                });
 
388
                                        } else {
 
389
                                                // if not first image in set, display prev image button
 
390
                                                if(opts.activeImage != 0) {
 
391
                                                        $('#prevLink,#prevLinkText').show().click(function() {
 
392
                                                                changeImage(opts.activeImage - 1); 
 
393
                                                                return false;
 
394
                                                        });
 
395
                                                }
 
396
 
 
397
                                                // if not last image in set, display next image button
 
398
                                                if(opts.activeImage != (opts.imageArray.length - 1)) {
 
399
                                                        $('#nextLink,#nextLinkText').show().click(function() {
 
400
                                                                changeImage(opts.activeImage +1); 
 
401
                                                                return false;
 
402
                                                        });
 
403
                                                }
 
404
                                        }
 
405
 
 
406
                                        enableKeyboardNav();
 
407
                                }
 
408
                        };
 
409
 
 
410
                        function keyboardAction(e) {
 
411
                                var o = e.data.opts;
 
412
                                var keycode = e.keyCode;
 
413
                                var escapeKey = 27;
 
414
 
 
415
                                var key = String.fromCharCode(keycode).toLowerCase();
 
416
 
 
417
                                // close lightbox
 
418
                                if ((key == 'x') || (key == 'o') || (key == 'c') || (keycode == escapeKey)) { 
 
419
                                        end();
 
420
 
 
421
                                        // display previous image       
 
422
                                } else if ((key == 'p') || (keycode == 37)) {  
 
423
                                        if(o.loopImages) {
 
424
                                                disableKeyboardNav();
 
425
                                                changeImage((o.activeImage == 0) ? (o.imageArray.length - 1) : o.activeImage - 1);
 
426
                                        } else if (o.activeImage != 0) {
 
427
                                                disableKeyboardNav();
 
428
                                                changeImage(o.activeImage - 1);
 
429
                                        }
 
430
 
 
431
                                        // display next image
 
432
                                } else if ((key == 'n') || (keycode == 39)) { 
 
433
                                        if (opts.loopImages) {
 
434
                                                disableKeyboardNav();
 
435
                                                changeImage((o.activeImage == (o.imageArray.length - 1)) ? 0 : o.activeImage + 1);
 
436
                                        } else if (o.activeImage != (o.imageArray.length - 1)) {
 
437
                                                disableKeyboardNav();
 
438
                                                changeImage(o.activeImage + 1);
 
439
                                        }
 
440
                                }
 
441
                        };
 
442
 
 
443
                        function enableKeyboardNav() {
 
444
                                $(document).bind('keydown', {opts: opts}, keyboardAction);
 
445
                        };
 
446
 
 
447
                        function disableKeyboardNav() {
 
448
                                $(document).unbind('keydown');
 
449
                        };          
 
450
                };
 
451
 
 
452
                $.fn.lightbox.parseJsonData = function(data) {
 
453
                        var imageArray = [];
 
454
 
 
455
                        $.each(data, function() {
 
456
                                imageArray.push(new Array(this.url, this.title));
 
457
                        });
 
458
 
 
459
                        return imageArray;
 
460
                };
 
461
                $.fn.lightbox.defaults = {
 
462
                    triggerEvent: "click",
 
463
                        allSet: false,
 
464
                        fileLoadingImage: '/multi_image/static/lib/lightbox/images/loading.gif',
 
465
                        fileBottomNavCloseImage: '/multi_image/static/lib/lightbox/images/closelabel.gif',
 
466
                        overlayOpacity: 0.6,
 
467
                        borderSize: 10,
 
468
                        imageArray: new Array,
 
469
                        activeImage: null,
 
470
                        inprogress: false,
 
471
                        resizeSpeed: 350,
 
472
                        widthCurrent: 250,
 
473
                        heightCurrent: 250,
 
474
                        scaleImages: false,
 
475
                        xScale: 1,
 
476
                        yScale: 1,
 
477
                        displayTitle: true,
 
478
                        navbarOnTop: true,
 
479
                        displayDownloadLink: false,
 
480
                        slideNavBar: false, 
 
481
                        navBarSlideSpeed: 350,
 
482
                        displayHelp: false,
 
483
                        strings: {
 
484
                                help: ' \u2190 / P - previous image\u00a0\u00a0\u00a0\u00a0\u2192 / N - next image\u00a0\u00a0\u00a0\u00a0ESC / X - close image gallery',
 
485
                                prevLinkTitle: 'previous image',
 
486
                                nextLinkTitle: 'next image',
 
487
                                prevLinkText:  '&laquo; Previous',
 
488
                                nextLinkText:  'Next &raquo;',
 
489
                                closeTitle: 'close image gallery',
 
490
                                image: 'Image ',
 
491
                                of: ' of ',
 
492
                                download: 'Download'
 
493
                        },
 
494
                        fitToScreen: false,             
 
495
                        disableNavbarLinks: false,
 
496
                        loopImages: false,
 
497
                        imageClickClose: true,
 
498
                        jsonData: null,
 
499
                        jsonDataParser: null
 
500
                };      
 
501
})(jQuery);
 
 
b'\\ No newline at end of file'