forked from noelboss/featherlight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatherlight.gallery.js
More file actions
163 lines (144 loc) · 5.21 KB
/
featherlight.gallery.js
File metadata and controls
163 lines (144 loc) · 5.21 KB
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
/**
* Featherlight Gallery – an extension for the ultra slim jQuery lightbox
* Version 1.5.0 - http://noelboss.github.io/featherlight/
*
* Copyright 2016, Noël Raoul Bossart (http://www.noelboss.com)
* MIT Licensed.
**/
(function($) {
"use strict";
var warn = function(m) {
if(window.console && window.console.warn) {
window.console.warn('FeatherlightGallery: ' + m);
}
};
if('undefined' === typeof $) {
return warn('Too much lightness, Featherlight needs jQuery.');
} else if(!$.featherlight) {
return warn('Load the featherlight plugin before the gallery plugin');
}
var isTouchAware = ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch,
jQueryConstructor = $.event && $.event.special.swipeleft && $,
hammerConstructor = window.Hammer && function($el){
var mc = new window.Hammer.Manager($el[0]);
mc.add(new window.Hammer.Swipe());
return mc;
},
swipeAwareConstructor = isTouchAware && (jQueryConstructor || hammerConstructor);
if(isTouchAware && !swipeAwareConstructor) {
warn('No compatible swipe library detected; one must be included before featherlightGallery for swipe motions to navigate the galleries.');
}
var callbackChain = {
afterClose: function(_super, event) {
var self = this;
self.$instance.off('next.'+self.namespace+' previous.'+self.namespace);
if (self._swiper) {
self._swiper
.off('swipeleft', self._swipeleft) /* See http://stackoverflow.com/questions/17367198/hammer-js-cant-remove-event-listener */
.off('swiperight', self._swiperight);
self._swiper = null;
}
return _super(event);
},
beforeOpen: function(_super, event){
var self = this;
self.$instance.on('next.'+self.namespace+' previous.'+self.namespace, function(event){
var offset = event.type === 'next' ? +1 : -1;
self.navigateTo(self.currentNavigation() + offset);
});
if (swipeAwareConstructor) {
self._swiper = swipeAwareConstructor(self.$instance)
.on('swipeleft', self._swipeleft = function() { self.$instance.trigger('next'); })
.on('swiperight', self._swiperight = function() { self.$instance.trigger('previous'); });
} else {
self.$instance.find('.'+self.namespace+'-content')
.append(self.createNavigation('previous'))
.append(self.createNavigation('next'));
}
return _super(event);
},
beforeContent: function(_super, event) {
var index = this.currentNavigation();
var len = this.slides().length;
this.$instance
.toggleClass(this.namespace+'-first-slide', index === 0)
.toggleClass(this.namespace+'-last-slide', index === len - 1);
return _super(event);
},
onKeyUp: function(_super, event){
var dir = {
37: 'previous', /* Left arrow */
39: 'next' /* Rigth arrow */
}[event.keyCode];
if(dir) {
this.$instance.trigger(dir);
return false;
} else {
return _super(event);
}
}
};
function FeatherlightGallery($source, config) {
if(this instanceof FeatherlightGallery) { /* called with new */
$.featherlight.apply(this, arguments);
this.chainCallbacks(callbackChain);
} else {
var flg = new FeatherlightGallery($.extend({$source: $source, $currentTarget: $source.first()}, config));
flg.open();
return flg;
}
}
$.featherlight.extend(FeatherlightGallery, {
autoBind: '[data-featherlight-gallery]'
});
$.extend(FeatherlightGallery.prototype, {
/** Additional settings for Gallery **/
previousIcon: '◀', /* Code that is used as previous icon */
nextIcon: '▶', /* Code that is used as next icon */
galleryFadeIn: 100, /* fadeIn speed when image is loaded */
galleryFadeOut: 300, /* fadeOut speed before image is loaded */
slides: function() {
if (this.filter) {
return this.$source.find(this.filter);
}
return this.$source;
},
images: function() {
warn('images is deprecated, please use slides instead');
return this.slides();
},
currentNavigation: function() {
return this.slides().index(this.$currentTarget);
},
navigateTo: function(index) {
var self = this,
source = self.slides(),
len = source.length,
$inner = self.$instance.find('.' + self.namespace + '-inner');
index = ((index % len) + len) % len; /* pin index to [0, len[ */
self.$currentTarget = source.eq(index);
self.beforeContent();
return $.when(
self.getContent(),
$inner.fadeTo(self.galleryFadeOut,0.2)
).always(function($newContent) {
self.setContent($newContent);
self.afterContent();
$newContent.fadeTo(self.galleryFadeIn,1);
});
},
createNavigation: function(target) {
var self = this;
return $('<span title="'+target+'" class="'+this.namespace+'-'+target+'"><span>'+this[target+'Icon']+'</span></span>').click(function(){
$(this).trigger(target+'.'+self.namespace);
});
}
});
$.featherlightGallery = FeatherlightGallery;
/* extend jQuery with selector featherlight method $(elm).featherlight(config, elm); */
$.fn.featherlightGallery = function(config) {
return FeatherlightGallery.attach(this, config);
};
/* bind featherlight on ready if config autoBind is set */
$(document).ready(function(){ FeatherlightGallery._onReady(); });
}(jQuery));