-
Notifications
You must be signed in to change notification settings - Fork 14
/
jquery.gallerie.js
594 lines (465 loc) · 16.5 KB
/
jquery.gallerie.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
/*
Gallerie - A JQuery Gallery Plugin
Copyright (c) 2013 Patrick Brown
web: http://browniethoughts.com/
Released under MIT LICENSE
*/
;(function($) {
var _body = document.body || document.documentElement,
_style = _body.style,
_transition,
_transform;
// detect support for transition
if (_style.transition !== undefined) {
_transition = 'transition';
} else if (_style.WebkitTransition !== undefined) {
_transition = '-webkit-transition';
} else if (_style.MozTransition !== undefined) {
_transition = '-moz-transition';
} else if (_style.MsTransition !== undefined) {
_transition = '-ms-transition';
} else if (_style.OTransition !== undefined) {
_transition = '-o-transition';
}
// detect support for transform
if (_style.transform !== undefined) {
_transform = 'transform';
} else if (_style.WebkitTransform !== undefined) {
_transform = '-webkit-transform';
} else if (_style.MozTransform !== undefined) {
_transform = '-moz-transform';
} else if (_style.MsTransform !== undefined) {
_transform = '-ms-transform';
} else if (_style.OTransform !== undefined) {
_transform = '-o-transform';
}
$.fn.gallerie = function (method) {
function rebuildOverlay(target, imageLinks){
var $this = $(target),
options = $this.data('gallerie')['options'],
$overlay = $('<div class="gallerie-overlay"/>'),
$imageBox = $('<div class="gallerie-imagebox"/>'),
$image = $('<img class="gallerie-image"/>'),
$imageLoading = $('<div class="gallerie-loading"/>'),
$captionBox = $('<div class="gallerie-captionbox"><div class="gallerie-control gallerie-control-previous">«</div><div class="gallerie-text"><div class="gallerie-title"/><div class="gallerie-index"/></div><div class="gallerie-control gallerie-control-next">»</div></div>'),
$thumbList = $('<ul></ul>'),
$thumbBox = $('<div class="gallerie-thumbbox"/>'),
$thumbItem,
$imageLink;
$.each(imageLinks, function(index, imageLink) {
$imageLink = $(imageLink);
$thumbItem = $(imageLink).find('img');
// listbox thumbs
$thumbItem = $('<li></li>')
.append(
$('<a />').prop({
href: $imageLink.prop('href')
}).append(
$('<img/>').prop({
src: $thumbItem.prop('src'),
title: $thumbItem.prop('title')
})
)
);
$thumbList.append($thumbItem);
});
$overlay.append($imageBox.append($image).append($imageLoading))
.append($captionBox)
.append($thumbBox.append($thumbList));
$overlay.on('click.gallerie', function(event){
$this.gallerie('close');
});
$overlay.on('click.gallerie', '.gallerie-control-previous', function(event){
$this.gallerie('previous');
return false;
});
$overlay.on('click.gallerie', '.gallerie-control-next', function(event){
$this.gallerie('next');
return false;
});
var scrollHover = 0;
$thumbBox.mousemove(function(event){
var triggerWidth = options['thumbboxTriggerWidth'],
thumbboxWidth = $thumbList.outerWidth(),
windowWidth = $(window).width();
// adjust triggerWidth to pixels if it is a percentage
if (triggerWidth < 1)
triggerWidth = windowWidth * triggerWidth;
var oldHover = scrollHover;
if (event.pageX < triggerWidth) {
scrollHover = 0;
} else if (windowWidth - event.pageX < triggerWidth) {
scrollHover = 1;
} else {
scrollHover = -1;
}
if (oldHover != scrollHover) {
scrollStop($thumbList);
if (scrollHover < 0)
return;
var oldLeft=0,
newLeft,
travelAmount;
if (_transform !== undefined && _transition !== undefined) {
// get current transform
var matrix = $thumbList.css(_transform),
marray = matrixToArray(matrix);
if (marray.length > 4) {
oldLeft = parseInt(marray[4]);
}
} else {
oldLeft = parseInt($thumbList.css('left'),10);
}
newLeft = -(scrollHover * thumbboxWidth) + (windowWidth * scrollHover);
travelAmount = Math.abs(newLeft - oldLeft);
scrollAnimate($thumbList, newLeft, {
duration: travelAmount * 1/options['thumbboxSpeed'],
easing: 'linear'
});
}
}).mouseleave(function(event){
scrollStop($thumbList);
scrollHover = -1;
});
$overlay.find('.gallerie-thumbbox li').on('click.gallerie', function(event){
var imageLink = $(this).find('a:first')[0];
$this.gallerie('open', imageLink);
event.preventDefault();
event.stopPropagation();
}).hover(function(){
$(this).addClass('gallerie-thumbbox-hover');
}, function(){
$(this).removeClass('gallerie-thumbbox-hover');
});
// remove any old overlays
$this.find('.gallerie-overlay').remove();
$this.append($overlay.hide());
return $overlay;
}
function matrixToArray(matrix) {
var marray = matrix.substr(7, matrix.length - 8).split(',');
return marray;
}
function scrollStop($element) {
// transition support
if (_transition !== undefined) {
var css = {};
// transform support
if (_transform !== undefined) {
// get current transform
var matrix = $element.css(_transform),
marray = matrixToArray(matrix);
css[_transform] = 'translate('+marray[4]+'px)';
css[_transition] = _transform +' 0ms';
} else {
css['left'] = $element.css('left');
css[_transition] = 'left 0ms';
}
$element.css(css);
}else { // jquery animations
$element.clearQueue().stop();
}
}
function scrollAnimate($element, leftPos, options) {
// transition support
if (_transition !== undefined) {
if (options['easing'] == undefined) {
options['easing'] = 'ease';
}
var css = {};
// transform support
if (_transform !== undefined) {
css[_transform] = 'translate('+leftPos+'px)';
css[_transition] = _transform +' '+ options.duration + 'ms ' + options['easing'];
} else {
css['left'] = leftPos;
css[_transition] = 'left '+ options.duration + 'ms ' + options['easing'];
}
$element.css(css);
} else { // default to using jQuery's animate
$element.animate({
left: leftPos,
}, options);
}
}
function displayLoadedImage(targets) {
// unwrap if target is event
if (targets instanceof $.Event) {
targets = targets['data'];
}
var preloadImage = targets['preloadImage'],
$image = targets['$image'],
$imageBox = $image.closest('.gallerie-imagebox');
$imageLoading = targets['$imageLoading'],
maxWidth = $imageBox.width() - $image.outerWidth() + $image.width(),
maxHeight = $imageBox.height() - $image.outerHeight() + $image.height(),
height=0,
width=0;
if (preloadImage != $image.data('preloadImage'))
return;
// adjust width and height according to determined maxWidth
width = preloadImage.width > maxWidth ? maxWidth : preloadImage.width;
height = preloadImage.height * width / preloadImage.width;
// if height still too big, use maxHeight scale width & height
if (height > maxHeight) {
height = maxHeight;
width = preloadImage.width * height / preloadImage.height;
}
// load the target image
$image.prop({
src: preloadImage.src,
title: preloadImage.title
}).css({
width: width,
height: height
}).removeClass('loading');
$imageLoading.hide();
}
// public methods
var methods = {
init : function (options) {
var defaults = {
thumbboxTriggerWidth: 0.10,
thumbboxSpeed: 0.5,
imageEvent: 'click',
elem: 'a',
wrapAround: true
};
var options = $.extend(defaults, options);
return this.each(function(){
var $this = $(this),
plugindata = $this.data('gallerie');
// if plugin has not been initialized on element
if (!plugindata) {
$this.data('gallerie', {
options: options,
target: $this,
});
// load image data
$this.gallerie('load', options['elem']);
// set to the first image
$this.gallerie('setImage', 1);
}
});
},
setImage: function(imageLink){
return this.each(function(){
var $this = $(this),
options = $this.data('gallerie')['options'],
$overlay = $this.find('.gallerie-overlay'),
$thumbBox = $this.find('.gallerie-thumbbox'),
$captionBox = $this.find('.gallerie-captionbox'),
$thumbList = $thumbBox.find('ul:first'),
linkType = $.type(imageLink),
$imageLink,
$image,
preloadImage,
$imageLoading;
// if image is a number, we consider it the index of the target thumb
if (linkType == 'number') {
imageLink = $thumbList.find('li a')[imageLink-1];
} else if (linkType == 'string') {
imageLink = $('<a/>').prop('href', imageLink)[0];
}
// we assume it is a link otherwise
$imageLink = $(imageLink);
$image = $overlay.find('.gallerie-image');
$imageLoading = $overlay.find('.gallerie-loading');
$image.addClass('loading');
// construct new image element to work-around onLoad issues with various browsers
preloadImage = new Image();
var targetData = {
'preloadImage': preloadImage,
'$image': $image,
'$imageLoading': $imageLoading
};
// when image has loaded, call displayLoadedImage to update real image to preloadImage
$(preloadImage).on('load.gallerie', targetData, displayLoadedImage);
$image.data('preloadImage', preloadImage);
preloadImage.src = $imageLink.prop('href');
// give the image 250ms to load before showing imageLoading (lowers flicker chance of imageLoading)
setTimeout(function(){
// image still has not loaded, so we show imageLoading
if (!preloadImage.complete) {
$imageLoading.show();
// hide image loading if target already loaded while showing imageLoading
if (preloadImage.complete) {
displayLoadedImage(targetData);
}
}
}, 250);
// attempt to find link in thumbnails
var $thumbLink = $thumbBox.find(imageLink),
$targetThumb;
// could not find same link element, so we search by href
if ($thumbLink.length == 0) {
var imageLinkHref = $imageLink.prop('href');
$thumbBox.find('a').each(function(index, elem) {
if ($(elem).prop('href') == imageLinkHref) {
$thumbLink = $(elem);
return false;
}
});
}
$targetThumb = $thumbLink.closest('li');
// remove selected from old thumb
$thumbBox.find('.gallerie-thumbbox-selected').removeClass('gallerie-thumbbox-selected');
// add selected to new thumb
$targetThumb.addClass('gallerie-thumbbox-selected');
var $thumbs = $thumbBox.find('li'),
thumbPosition = $thumbs.index($targetThumb)+1,
thumbTotal = $thumbs.length;
// set caption
$captionBox.find(".gallerie-title").text($targetThumb.find('img').prop('title'));
$captionBox.find(".gallerie-index").text(thumbPosition + ' of ' + thumbTotal);
var thumbpos = $targetThumb.position().left + $targetThumb.outerWidth(true)/2,
winwidth = $(window).width();
if ($targetThumb.offset().left < 0 || thumbpos > winwidth) {
// calculate new left edge of thumblist
var newLeft = -(winwidth/2 - thumbpos);
// if edge is beyond normal scrolling bounds, bring it to within bounds
newLeft = Math.max(0, newLeft);
newLeft = Math.min($thumbList.outerWidth() - winwidth, newLeft);
// clear queue of effects (otherwise backlog happens when user advances quickly)
$thumbList.clearQueue();
// animate scroll to the position
scrollAnimate($thumbList, -newLeft, {'duration': 1000});
}
});
},
isOpen : function() {
var $this = $(this[0]),
$overlay = $this.find('.gallerie-overlay');
return $overlay.is(':visible');
},
open : function(imageLink){
return this.each(function(){
var $this = $(this),
options = $this.data('gallerie')['options'],
$overlay = $this.find('.gallerie-overlay'),
$imageBox = $this.find('.gallerie-imagebox'),
$captionBox = $this.find('.gallerie-captionbox'),
$thumbBox = $this.find('.gallerie-thumbbox');
if ($overlay.is(':hidden')) {
$(document).on('keyup.gallerie', function(e) {
if (e.keyCode == 13 || e.keyCode == 27) {
$this.gallerie('close');
} else if (e.keyCode == 37) {
$this.gallerie('previous');
} else if (e.keyCode == 39) {
$this.gallerie('next');
}
});
// remove scrollbar from window
$('body').css({ overflow: 'hidden' });
// resize imagebox to fill void not filled by captionBox and thumbBox
$imageBox.css({height: $overlay.height() - $captionBox.outerHeight() - $thumbBox.outerHeight() - parseInt($imageBox.css('margin-bottom'), 10) - parseInt($imageBox.css('margin-top'), 10)});
}
$overlay.fadeIn(500, function(){
if (imageLink) {
$this.gallerie('setImage', imageLink);
}
});
});
},
close : function(){
return this.each(function(){
$(this).find('.gallerie-overlay').hide();
$(document).off('keyup.gallerie');
// restore window scrollbar, etc
$("body").css({ overflow: 'inherit' });
});
},
next : function(){
return this.each(function(){
var $this = $(this),
options = $this.data('gallerie')['options'],
$thumbBox = $this.find('.gallerie-thumbbox');
var $selectedItem = $thumbBox.find('.gallerie-thumbbox-selected'),
$nextItem = $selectedItem.next();
if ($nextItem.length == 0) {
if (!options['wrapAround']) {
return;
}
$nextItem = $thumbBox.find('li:first');
}
$this.gallerie('setImage', $nextItem.find('a'));
});
},
previous : function(){
return this.each(function(){
var $this = $(this),
options = $this.data('gallerie')['options'],
$thumbBox = $this.find('.gallerie-thumbbox');
var $selectedItem = $thumbBox.find('.gallerie-thumbbox-selected'),
$prevItem = $selectedItem.prev();
if ($prevItem.length == 0) {
if (!options['wrapAround']) {
return;
}
$prevItem = $thumbBox.find('li:last');
}
$this.gallerie('setImage', $prevItem.find('a'));
});
},
load: function(elem){
return this.each(function(){
var $this = $(this),
options = $this.data('gallerie')['options'];
if (elem === undefined) {
elem = options['elem'];
}
rebuildOverlay(this, $this.find(elem).toArray());
$(document).on(options['imageEvent'] + '.gallerie', elem, function(e){
$this.gallerie('open', this);
e.stopPropagation();
e.preventDefault();
});
});
},
option: function(key, value) {
var newOptions = {};
if (value === undefined && $.type(key) != 'string') {
newOptions = key;
} else if (value === undefined) {
return $(this[0]).data('gallerie')['options'][key];
} else {
newOptions = {}
newOptions[key] = value;
}
return this.each(function(){
var $this = $(this),
data = $this.data('gallerie'),
options = data['options'];
$.each(newOptions, function(key, value){
if (key == 'elem' || key == 'imageEvent') {
// unbind old events
$(document).off(options['imageEvent'] + '.gallerie', options['elem']);
// update option
options[key] = value;
// rebind with new option
$(document).on(options['imageEvent'] + '.gallerie', options['elem'], function(e){
$this.gallerie('open', this);
e.stopPropagation();
e.preventDefault();
});
} else if (key in options) {
options[key] = value;
}
});
data['options'] = options;
$this.data('gallerie', data);
});
}
};
// handle accessing of public methods
// this is essentially bootstrapping this plugin
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.gallerie' );
}
}
})(jQuery);