-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjquery.exposure.js
1941 lines (1747 loc) · 61.8 KB
/
jquery.exposure.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Exposure (http://http://exposure.blogocracy.org/)
* Copyright 2011, Kristoffer Jelbring
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
;(function($) {
/**
* @name Exposure
* @author Kristoffer Jelbring ([email protected])
* @version 1.0
*
* @type jQuery
* @cat plugins/Media
*
* @desc Turn a simple HTML list into a rich and smart photo viewer that handles very large amounts of photos.
*
* @example $('#images').exposure({options});
*
* @options
* target: (selector string) Where to insert the image being displayed. Defaults to '#exposure'. If no target is found, one will be created.
* showThumbs: (boolean) Display thumbnails or not. Defaults to true.
* showControls: (boolean) Display paging controls or not. Defaults to true, but will be set to false if missing controlsTarget or if carouselControl is set to true.
* imageControls: (boolean) Switch paging controls to use images instead of pages. Defaults to false.
* controls: (object) Display only certain paging controls. All controls default to true. Usage example: controls : { prevNext : true, pageNumbers : true, firstLast : false }
* carouselControls: (boolean) Enable carousel type controls instead of the classic paging type controls. Defaults to false, but will be set to false if showThumbs is also set to false.
* enableSlideshow: (boolean) Enable slideshow. Defaults to true.
* slideshowControlsTarget: (selector string) Where to insert the slideshow controls. Defaults to null.
* autostartSlideshow: (boolean) Automatically start the slideshow when the gallery is loaded. Defaults to false.
* slideshowDelay: (number) Delay for each slide in the slideshow (in milliseconds). Defauts to 3000.
* onSlideshowPlayed: (function) Callback funcation that is called when the slideshow is played.
* onSlideshowPaused: (function) Callback funcation that is called when the slideshow is paused.
* showCaptions: (boolean) Display captions or not. Captions are added by setting a title attribute on the items in the list.
* showExtraData: (boolean) Display extra image data or not. This data is added by inserting inner HTML to the items in the list.
* dataTarget: (selector string) Where to insert captions and extra image data. Defaults to null, in which case the data container will appended to the main Exposure target.
* controlsTarget: (selector string) Where to insert the paging controls. Defaults to null.
* onThumb: (function) Callback function that is called when a thumbnail is displayed.
* onImage: (function) Callback function that is called when an image is displayed. Defaults to removing the previous image.
* onImageHoverOver (function) Callback function that is called when the mouse enters the displayed image.
* onImageHoverOut (function) Callback function that is called when the mouse leaves the displayed image.
* onCarousel: (function) Callback function that is called right before the image carousel is updated.
* onNext: (function) Callback function that is called when nextImage is called.
* onPrev: (function) Callback function that is called when prevImage is called.
* onPageChanged: (function) Callback function that is called when goToPage is called. Is not called when carouselControls is set. Defaults to showing all thumbnails on the current page.
* onPagingLink: (function) Callback function that is called when a new paging link has been added. Defaults to returning the link.
* separatePageBrowsing: (boolean) Enable separate page browsing (change page without changing the image being viewed). Defaults to false.
* loop: (boolean) Start over when last image is reached.
* onEndOfLoop: (function) Callback function that is called when the last image is reached and loop option is set to false.
* viewFirstImage: (boolean) Enable automatic showing of the first image in the gallery when the gallery is loaded. Defaults to true.
* pageSize: (number) Maximum number of images (thumbnails) per page. Defaults to 5.
* visiblePages: (number) Maxium number of pages visible in paging.
* preloadBuffer: (number) Maximum number of images to keep in load queue at any given time. Defaults to 3.
* keyboardNavigation: (boolean) Enable keyboard navigation. Defaults to true.
* clickingNavigation: (boolean) Enable browsing by clicking the image being shown. Defaults to true.
* fixedContainerSize: (boolean) Enable a fixed size target element (set the size using CSS) instead of one that adapts to the size of the current image. Defaults to false.
* maxWidth: (number) Maximum image width in the gallery (larger images will be downscaled). Defaults to null.
* maxHeight: (number) Maximum image height in the gallery (larger images will be downscaled). Defaults to null.
* stretchToMaxSize: (boolean) Stretch all images to maxWidth and maxHeight. Defaults to false.
* fullScreen: (boolean) Stretch all images to be viewn in full screen. Defaults to false.
* onEnterFullScreen: (function) Callback function that is called when entering full screen mode. Defaults to showing background mask.
* onExitFullScreen: (function) Callback function that is called when exiting full screen mode. Defaults to hiding target and background mask.
* showThumbToolTip: (boolean) Display captions as thumbnail tooltips or not. Defaults to true.
* onEmpty: (function) Called when the gallery is empty. Defaults to removing controls and targets and to hiding the list element that the plugin is called on.
* onInit: (function) Called when the gallery has been initialized.
* allowDuplicates: (boolean) Allow the same image to be added more than once. Defaults to true.
* jsonSource: (JSON data string/URL to JSON data/JSON object) Load additional images from an external source using JSON. Defaults to null.
*/
// Static Exposure instance.
$.exposure = {
v : '1.0.1',
// Predefined selectors.
defaultTargetId : 'exposure',
wrapperClass : 'exposureWrapper',
targetClass : 'exposureTarget',
currentImageClass : 'exposureCurrentImage',
lastImageClass : 'exposureLastImage',
captionClass : 'caption',
imageDataClass : 'extra',
dataContainerClass : 'exposureData',
controlsClass : 'exposureControls',
slideshowControlsClass : 'exposureSlideshowControls',
firstPageClass : 'exposureFirstPage',
prevPageClass : 'exposurePrevPage',
nextPageClass : 'exposureNextPage',
lastPageClass : 'exposureLastPage',
pagingClass : 'exposurePaging',
playSlideshowClass : 'exposurePlaySlideshow',
pauseSlideshowClass : 'exposurePauseSlideshow',
maskClass : 'exposureMask',
thumbsClass : 'exposureThumbs',
imageClass : 'exposureImage',
imageHoverClass : 'exposureHover',
selectedImageClass : 'selected',
activeThumbClass : 'active',
currentThumbClass : 'current',
firstThumbClass : 'first',
lastThumbClass : 'last',
loadedClass : 'loaded',
activeLinkClass : 'active',
disabledLinkClass : 'disabled',
/**
* Check if a variable is defined.
*
* @param v Variable to check.
*/
isDefined : function(v) {
return typeof v !== 'undefined';
},
/**
* Check if a variable is an object.
*
* @param v Variable to check.
*/
isObject : function(v) {
return typeof v === 'object';
},
/**
* Check if a string starts with another string.
*
* @param s1 String to check.
* @param s2 String to look for.
*/
startsWith : function(s1, s2) {
if (s1 && s2) {
return s1.match("^"+s2) === s2;
}
return false;
},
/**
* Calculate the differance in outerwidth and width of an element.
*
* @param el The element to check.
* @returns Width differance.
*/
widthDiff : function(el) {
return el ? el.outerWidth(true)-el.width() : 0;
},
/**
* Calculate the differance in outerHeight and height of an element.
*
* @param el The element to check.
* @returns Height differance.
*/
heightDiff : function(el) {
return el ? el.outerHeight(true)-el.height() : 0;
},
/**
* Create a link and connect it to an onclick callback function.
*
* @param text Link text.
* @param onClick Callback function to call when link is clicked.
* @param linkClass Class attribute to decorate the link with.
*/
createLink : function(text, onClick, linkClass) {
var a = $('<a href="javascript:void(0);"></a>').text(text).click(onClick);
if (linkClass) {
a.addClass(linkClass);
}
return a;
},
// Default texts. Use the localization files to override these.
texts : {
first : 'First',
previous : 'Prev',
next : 'Next',
last : 'Last',
play : 'Play slideshow',
pause : 'Pause slideshow'
}
};
// Default values. Override these using the options argument when invoking Exposure.
var defaults = {
target : '#' + $.exposure.defaultTargetId,
showThumbs : true,
showControls : true,
imageControls : false,
controls : {
prevNext : true,
firstLast : true,
pageNumbers : true
},
carouselControls: false,
enableSlideshow : true,
slideshowControlsTarget : null,
autostartSlideshow : false,
slideshowDelay : 3000,
onSlideshowPlayed : function() {},
onSlideshowPaused : function() {},
showCaptions : true,
showExtraData : true,
dataTarget : null,
controlsTarget : null,
onThumb : function(thumb) {},
onImage : function(image, imageData, thumb) {
image.siblings('.' + $.exposure.lastImageClass).remove();
},
onImageHoverOver : function() {},
onImageHoverOut : function() {},
onCarousel : function(firstImage, lastImage) {},
onNext : function() {},
onPrev : function() {},
onPageChanged : function() {},
onPagingLink : function(link) {
return link;
},
separatePageBrowsing : false,
loop : true,
onEndOfLoop : function() {},
pageSize : 5,
viewFirstImage : true,
visiblePages : 5,
preloadBuffer : 3,
keyboardNavigation : true,
clickingNavigation : true,
fixedContainerSize : false,
maxWidth : null,
maxHeight : null,
stretchToMaxSize : false,
fullScreen : false,
onEnterFullScreen : function(mask) {
mask.show();
},
onExitFullScreen : function(target, mask) {
target.hide();
mask.hide();
},
showThumbToolTip : true,
onEmpty : function(gallery) {
gallery.hide();
gallery.targetElement.remove();
if (gallery.showControls) {
gallery.controlsElement.remove();
}
if (gallery.slideshowControlsTarget) {
gallery.slideshowControlsElement.remove();
}
},
onInit : function() {},
allowDuplicates : true,
jsonSource : null
};
/**
* Value object representing an image in the viewer.
*
* @param src Source to the full size image.
* @param thumb Source to thumbnail version of the image.
* @param caption Image caption.
* @param data Extra image data.
*/
var Image = function(src, thumb, caption, data) {
this.src = src;
this.thumb = thumb;
this.caption = caption;
this.data = data;
this.loaded = false;
};
// Primary Exposure init function. To be called on thumbnail container.
$.fn.exposure = function(options) {
// Shortcuts.
var gallery = this;
var ex = $.exposure;
var txt = ex.texts;
$.extend(this, {
/**
* All the images in the viewer. Holds an array of Image objects that are filled up when the plugin is loaded.
*/
images : [],
/**
* All the image sources that's been previously added to the viewer.
*/
sources : {},
/**
* Create a new Image object and add it to images array.
*
* @param src Source to the full size image.
* @param thumb Source to thumbnail version of the image.
* @param caption Image caption.
* @param data Extra image data.
* @returns Index of the new image.
*/
newImage : function(src, thumb, caption, data) {
var alreadyAdded = ex.isDefined(gallery.sources[src]);
if (alreadyAdded && !gallery.allowDuplicates) {
return -1;
}
var image = new Image(src, thumb, caption, data);
var imageIndex = gallery.images.push(image) - 1;
if (!alreadyAdded) {
gallery.sources[src] = imageIndex;
}
return imageIndex;
},
/**
* Initialization flag.
*/
initialized : false,
/**
* Index of the image currently being viewed.
*/
current : -1,
/**
* Deselect the image currently being viewed.
*/
deselectCurrentImage : function() {
gallery.current = -1;
gallery.find('li.' + ex.activeThumbClass).removeClass(ex.activeThumbClass);
},
/**
* The load queue, holds an array of indices of images to load.
*/
loadQueue : [],
/**
* Add an image to the load queue.
*
* @param index Index of image to add.
*/
addToLoadQueue : function(index) {
if (!gallery.loaded(index) && !gallery.queued(index)) {
gallery.loadQueue.push(index);
}
},
/**
* Check if a specific image exists in the load queue.
*
* @param index Index of image to check.
*/
queued : function(index) {
return $.inArray(index, gallery.loadQueue) > -1;
},
/**
* Check if a specific image has been loaded.
*
* @param index Index of image to check.
*/
loaded : function(index) {
var image = gallery.getImage(index);
if (image !== null) {
return image.loaded;
}
return false;
},
/**
* Find the next, not already loaded image, in the load queue. This function is recursive and will continue until
* an image is found, or until the queue is empty.
*/
nextInLoadQueue : function() {
var i;
if (gallery.loadQueue.length > 0) {
var next = gallery.loadQueue.shift();
if (gallery.loaded(next)) {
// Image already loaded, remove from load queue.
i = $.inArray(next, this.loadQueue);
gallery.loadQueue.splice(i, 1);
// Find next in queue.
return gallery.nextInLoadQueue();
}
return next;
}
return null;
},
/**
* Preload the next image in the load queue.
*/
preloadNextInQueue : function() {
if (gallery.loadQueue.length > 0) {
var nextIndex = gallery.nextInLoadQueue();
if (nextIndex !== null) {
gallery.loadImage(nextIndex, gallery.preloadNextInQueue);
}
}
},
/**
* Load a specific image.
*
* @param index Index of image to load.
* @param onload Image onload callback function.
*/
loadImage : function(index, onload) {
var image = gallery.getImage(index);
var img = $('<img />').addClass(ex.imageClass);
var i;
if (image !== null) {
image.loaded = true;
if (gallery.queued(index)) {
// Since image already has been loaded, remove it from the load queue.
i = $.inArray(index, gallery.loadQueue);
gallery.loadQueue.splice(i, 1);
}
if (typeof onload === 'function') {
img.load(onload);
}
img.attr('src', image.src);
}
return img;
},
/**
* Calculate the page number of a specific image.
*
* @param index Index of image to get page number for.
*/
pageNumberForImage : function(index) {
return Math.ceil((index + 1) / gallery.pageSize);
},
/**
* Calculate the total number of pages using the set page size.
*/
numberOfPages : function() {
// Calculate the page number for the last image.
return this.pageNumberForImage(gallery.images.length-1);
},
/**
* Check if the the page currently being viewed is the first page.
*/
atFirstPage : function() {
return gallery.currentPage === 1;
},
/**
* Check if the the page currently being viewed is the last page.
*/
atLastPage : function() {
return gallery.currentPage === gallery.numberOfPages();
},
/**
* Check if a specific page number is a valid page number.
*
* @param page Page number to check.
*/
validPage : function(page) {
return page > 0 && page <= gallery.numberOfPages();
},
/**
* Create paging links.
*/
createPaging : function() {
var i;
if (gallery.showControls && gallery.controls.pageNumbers) {
// Create paging links.
var stop = gallery.imageControls ? gallery.numberOfImages() : gallery.numberOfPages();
gallery.controlsElement.find('.' + ex.pagingClass).each(function() {
for (i = 1; i <= stop; i++) {
$(this).append(gallery.newPagingLink(i));
}
});
}
},
/**
* Update paging links.
*/
updatePaging : function(newActivePage) {
if (gallery.showControls && gallery.controls.pageNumbers) {
var current = gallery.imageControls ? gallery.current+1 : gallery.currentPage;
var paging = gallery.controlsElement.find('.' + ex.pagingClass);
paging.find(' span.' + ex.activeLinkClass).each(function() {
$(this).replaceWith(gallery.newPagingLink(current));
});
paging.find('a[rel="' + newActivePage + '"]').each(function() {
$(this).replaceWith($('<span>' + newActivePage + '</span>').addClass(ex.activeLinkClass));
});
var pageCount = gallery.imageControls ? gallery.numberOfImages() : gallery.numberOfPages();
if (gallery.visiblePages > 0 && pageCount > gallery.visiblePages) {
var firstVisiblePage = newActivePage;
var lastVisiblePage = gallery.visiblePages;
var flooredVisiblePages = Math.floor(gallery.visiblePages/2);
if (newActivePage <= flooredVisiblePages) {
firstVisiblePage = 1;
} else if (newActivePage > (pageCount - flooredVisiblePages)) {
lastVisiblePage = pageCount;
firstVisiblePage = lastVisiblePage - gallery.visiblePages + 1;
} else {
firstVisiblePage -= flooredVisiblePages;
lastVisiblePage = firstVisiblePage + gallery.visiblePages - 1;
}
paging.each(function() {
$(this).children().each(function(i) {
var currentPage = i+1;
if (currentPage >= firstVisiblePage && currentPage <= lastVisiblePage) {
$(this).show();
} else {
$(this).hide();
}
});
});
}
}
},
/**
* Create a new paging link for a specific page.
*
* @param page Index of the image/number of the page (depending on the imageControls setting) to create the link for.
*/
newPagingLink : function(index) {
var onclick = function() {
// View the image/page defined in the rel attribute of the link.
var rel = Number($(this).attr('rel'));
if (gallery.imageControls) {
gallery.viewImage(rel-1);
} else {
gallery.goToPage(rel);
}
};
return ex.createLink(index, onclick).attr('rel', index);
},
/**
* Get the index of the next image.
*/
getNextImage : function() {
if (gallery.current === gallery.images.length-1) {
// Is at last image, return first image.
if (gallery.loop) {
return 0;
} else {
// Loop ended callback.
gallery.onEndOfLoop();
}
} else {
// Return next image.
return gallery.current+1;
}
return null;
},
/**
* Get the index of the previous image.
*/
getPrevImage : function() {
if (gallery.current === 0) {
// Is at first image, return last image.
if (gallery.loop) {
return gallery.images.length-1;
}
} else {
// Return previous image.
return gallery.current-1;
}
return null;
},
/**
* Number of the page currently being viewed.
*/
currentPage : 1,
/**
* View a specific page.
*
* @param page Number of the page to view.
* @param imageToView Index of the image to view (defaults to viewing first image on page if this parameter isn't set).
*/
goToPage : function(page, imageToView) {
if (this.validPage(page)) {
// Hide all thumbnail containers.
gallery.find('li').removeClass(ex.currentThumbClass).hide();
gallery.loadPage(page, imageToView);
if (!gallery.imageControls) {
gallery.updatePaging(page);
}
gallery.currentPage = page;
var disabled = ex.disabledLinkClass;
if (gallery.showControls) {
if (gallery.atFirstPage()) {
// Disable first page button.
if (gallery.controls.firstLast) {
gallery.find('.' + ex.firstPageClass).addClass(disabled);
}
// Hide previous page button.
if (!gallery.loop && gallery.controls.prevNext) {
gallery.find('.' + ex.prevPageClass).hide();
}
} else {
// Enable first page button.
if (gallery.controls.firstLast) {
gallery.find('.' + ex.firstPageClass).removeClass(disabled);
}
// Show previous page button.
if (!gallery.loop && gallery.controls.prevNext) {
gallery.find('.' + ex.prevPageClass).show();
}
}
if (gallery.atLastPage()) {
// Disable last page button.
if (gallery.controls.firstLast) {
gallery.find('.' + ex.lastPageClass).addClass(disabled);
}
// Hide next page button.
if (!gallery.loop && gallery.controls.prevNext) {
gallery.find('.' + ex.nextPageClass).hide();
}
} else {
// Enable last page button.
if (gallery.controls.firstLast) {
gallery.find('.' + ex.lastPageClass).removeClass(disabled);
}
// Show next page button.
if (!gallery.loop && gallery.controls.prevNext) {
gallery.find('.' + ex.nextPageClass).show();
}
}
}
// Page changed callback.
if (!gallery.carouselControls) {
// Reset height of thumbs on current page.
gallery.find('li.' + ex.currentThumbClass).show().each(function(i) {
var imageHeight = $(this).find('img').height();
if (imageHeight > 0) {
$(this).height(imageHeight);
}
});
gallery.onPageChanged(gallery);
}
}
},
/**
* Load a specific page.
*
* @param page Number of the page to load.
* @param imageToView Index of the image to view (defaults to viewing first image on page if this parameter isn't set).
*/
loadPage : function(page, imageToView) {
if (gallery.validPage(page)) {
// Calculate first and last images on this page.
var last = page * gallery.pageSize;
var first = last - gallery.pageSize;
if (last > gallery.images.length) {
last = gallery.images.length;
}
gallery.pageTransition = true;
gallery.viewThumbs(first, last-1);
if (!gallery.separatePageBrowsing) {
if (imageToView) {
// Moving backwards, set the last image on the page as active.
gallery.viewImage(imageToView);
} else {
if (page > 1 || ((page === 1 && gallery.viewFirstImage) || gallery.initialized)) {
// Set the first image on this page as active.
gallery.viewImage(first);
}
}
}
gallery.pageTransition = false;
}
},
/**
* Views thumbnails for a specific set of images (and creates them if needed).
*
* @param first Index of the first image to view.
* @param last Index of the last image to view.
*/
viewThumbs : function(first, last) {
var i;
if (gallery.showThumbs) {
// Go through images in set.
for (i = first; i <= last; i++) {
gallery.viewThumb(i, i === first, i === last, true);
}
if (!gallery.carouselControls && gallery.currentPage < gallery.numberOfPages()) {
// Preload next page of thumbnails.
var firstNext = last+1;
var lastNext = last+gallery.pageSize;
if (lastNext >= gallery.images.length) {
lastNext = gallery.images.length-1;
}
for (i = firstNext; i <= lastNext; i++) {
var container = gallery.viewThumb(i, i === firstNext, i === lastNext, false);
if (container && container.length) {
container.hide();
}
}
}
}
},
/**
* View thumbnail for a specific image (and create it if needed).
*
* @param index Index of the image to view.
* @param first If the image is the first on the page.
* @param last If the image is the last on the page.
* @param current If the image is a part of the current page.
*/
viewThumb : function(index, first, last, currentPage) {
// Make sure image index is in scope.
if (index < 0) {
index = gallery.images.length + index;
} else if (index >= gallery.images.length) {
index = index - gallery.images.length;
}
var image = gallery.images[index];
// Find thumbnail container.
var container = gallery.getThumb(index).parent();
if (!container.length) {
// Create a thumbnail if one doesn't already exist.
container = gallery.createThumbForImage(image, index);
// Add page number as rel attribute.
container.attr('rel', gallery.pageNumberForImage(index));
}
if (container.length) {
// Append in the end of the container in order to save the ordering of the images.
container.parent().append(container);
if (first) {
// Decorate thumbnail container for first image on page.
container.addClass(ex.firstThumbClass);
} else {
container.removeClass(ex.firstThumbClass);
}
if (last) {
// Decorate thumbnail container for last image on page.
container.addClass(ex.lastThumbClass);
} else {
container.removeClass(ex.lastThumbClass);
}
if (currentPage) {
if (gallery.carouselControls) {
container.show();
} else {
container.addClass(ex.currentThumbClass);
}
}
}
return container;
},
/**
* Get the thumbnail img element for a specific image.
*
* @param index Index of image to find thumbnail for.
*/
getThumb : function(index) {
return gallery.find('img[rel="'+index+'"]');
},
/**
* Create a thumbnail for a specific image.
*
* @param image Image object for the image.
* @param image Index of the image.
*/
createThumbForImage : function(image, index) {
if (gallery.showThumbs) {
var thumb = gallery.getThumb(index);
if (thumb === null || !thumb.length) {
// Create thumbnail container.
var container = $('<li></li>');
gallery.append(container);
// Create thumbnail img element.
thumb = $('<img />');
if (image.thumb) {
thumb.attr('src', image.thumb);
} else {
// Create a thumbnail from the original image.
thumb.attr('src', image.src);
// Downscale the new thumbnail.
var imageWidth = Math.ceil(thumb.width() / thumb.height() * container.height());
var imageHeight = Math.ceil(thumb.height() / thumb.width() * container.width());
if (imageWidth < imageHeight) {
thumb.css({height: 'auto', maxWidth: container.width()});
} else {
thumb.css({width: 'auto', maxHeight: container.height()});
}
}
container.append(thumb.css('display', 'block'));
// Add image index and caption as attributes.
thumb.attr('rel', index);
if (image.caption && gallery.showThumbToolTip) {
thumb.attr('title', image.caption);
}
// Save extra image data in thumbnail data.
thumb.data('data', image.data);
thumb.click(function() {
// When a thumbnail is clicked, view full version of that image.
gallery.viewImage(Number($(this).attr('rel')));
});
thumb.load(function() {
// Set the height of the thumbnail container to the height of the thumbnail.
var imageHeight = $(this).height();
if (imageHeight > 0) {
$(this).parent().height(imageHeight);
}
});
gallery.onThumb(thumb);
return container;
}
}
return null;
},
/**
* View the first page.
*/
firstPage : function() {
if (!gallery.atFirstPage()) {
gallery.goToPage(1);
}
},
/**
* View the last page.
*/
lastPage : function() {
if (!gallery.atLastPage()) {
gallery.goToPage(gallery.numberOfPages());
}
},
/**
* View the previous page.
*/
prevPage : function() {
if (!gallery.atFirstPage()) {
// Go to previous page.
gallery.goToPage(gallery.currentPage-1);
} else if (gallery.loop) {
// At first page, go to last page.
gallery.goToPage(gallery.numberOfPages());
}
},
/**
* View the next page.
*/
nextPage : function() {
if (!gallery.atLastPage()) {
// Go to next page.
gallery.goToPage(gallery.currentPage+1);
} else if (gallery.loop) {
// At last page, go back to first page.
gallery.goToPage(1);
}
},
/**
* Check if an image is the first image on its page.
*
* @param index Index of image to check. Will default to image currently being viewed if not set.
*/
firstImageOnPage : function(index) {
if (!index) {
index = gallery.current;
}
return gallery.pageSize === 1 || (index % gallery.pageSize === 0);
},
/**
* Check if the an image is the last image on its page.
*
* @param index Index of image to check. Will default to image currently being viewed if not set.
*/
lastImageOnPage : function(index) {
if (!index) {
index = gallery.current;
}
var imageCount = gallery.images.length;
if (gallery.pageSize === 1 || imageCount === 1) {
return true;
}
if (index > 0) {
var currentPageSize = gallery.pageSize;
var currentPage = gallery.pageNumberForImage(index);
if (currentPage === gallery.numberOfPages()) {
// Calculate the size of the last page as it may differ from the set page size.
var newPageSize = imageCount % gallery.pageSize;
if (newPageSize > 0) {
currentPageSize = newPageSize;
}
}
var imageIndex = index;
if (currentPage > 1) {
imageIndex -= (currentPage-1) * gallery.pageSize;
}
// Check if the current image is the last image of the current page.
return (imageIndex+1) % currentPageSize === 0;
}
return false;
},
/**
* Get the number of the current page.
*/
currentPageNumber : function() {
return gallery.currentPage;
},
/**
* Get the number of images.
*/
numberOfImages : function() {
return gallery.images.length;
},
/**
* Check if the image currently being viewed is the first image.
*/
atFirstImage : function() {
return gallery.current === 0;
},
/**
* Check if the image currently being viewed is the last image.
*/
atLastImage : function() {
return gallery.current === gallery.numberOfImages()-1;
},
/**
* Get a spefic image object from the images array.
*
* @param index Index of image to get.
*/
getImage : function(index) {
if (index !== null && index > -1 && index < gallery.images.length) {
return gallery.images[index];
}
return null;