This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
thumbs.js
259 lines (214 loc) · 6.79 KB
/
thumbs.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
// ==========================================================================
//
// Thumbs
// Displays thumbnails in a grid
//
// ==========================================================================
(function(document, $) {
"use strict";
var CLASS = "fancybox-thumbs",
CLASS_ACTIVE = CLASS + "-active",
CLASS_LOAD = CLASS + "-loading";
// Make sure there are default values
$.fancybox.defaults = $.extend(
true,
{
btnTpl: {
thumbs:
'<button data-fancybox-thumbs class="fancybox-button fancybox-button--thumbs" title="{{THUMBS}}">' +
'<svg viewBox="0 0 120 120">' +
'<path d="M30,30 h14 v14 h-14 Z M50,30 h14 v14 h-14 Z M70,30 h14 v14 h-14 Z M30,50 h14 v14 h-14 Z M50,50 h14 v14 h-14 Z M70,50 h14 v14 h-14 Z M30,70 h14 v14 h-14 Z M50,70 h14 v14 h-14 Z M70,70 h14 v14 h-14 Z" />' +
"</svg>" +
"</button>"
},
thumbs: {
autoStart: false, // Display thumbnails on opening
hideOnClose: true, // Hide thumbnail grid when closing animation starts
parentEl: ".fancybox-container", // Container is injected into this element
axis: "y" // Vertical (y) or horizontal (x) scrolling
}
},
$.fancybox.defaults
);
var FancyThumbs = function(instance) {
this.init(instance);
};
$.extend(FancyThumbs.prototype, {
$button: null,
$grid: null,
$list: null,
isVisible: false,
isActive: false,
init: function(instance) {
var self = this,
first,
second;
self.instance = instance;
instance.Thumbs = self;
self.opts = instance.group[instance.currIndex].opts.thumbs;
// Enable thumbs if at least two group items have thumbnails
first = instance.group[0];
first = first.opts.thumb || (first.opts.$thumb && first.opts.$thumb.length ? first.opts.$thumb.attr("src") : false);
if (instance.group.length > 1) {
second = instance.group[1];
second = second.opts.thumb || (second.opts.$thumb && second.opts.$thumb.length ? second.opts.$thumb.attr("src") : false);
}
self.$button = instance.$refs.toolbar.find("[data-fancybox-thumbs]");
if (self.opts && first && second && first && second) {
self.$button.show().on("click", function() {
self.toggle();
});
self.isActive = true;
} else {
self.$button.hide();
}
},
create: function() {
var self = this,
instance = self.instance,
parentEl = self.opts.parentEl,
list = [],
src;
if (!self.$grid) {
// Create main element
self.$grid = $('<div class="' + CLASS + " " + CLASS + "-" + self.opts.axis + '"></div>').appendTo(
instance.$refs.container
.find(parentEl)
.addBack()
.filter(parentEl)
);
// Add "click" event that performs gallery navigation
self.$grid.on("click", "li", function() {
instance.jumpTo($(this).attr("data-index"));
});
}
// Build the list
if (!self.$list) {
self.$list = $("<ul>").appendTo(self.$grid);
}
$.each(instance.group, function(i, item) {
src = item.opts.thumb || (item.opts.$thumb ? item.opts.$thumb.attr("src") : null);
if (!src && item.type === "image") {
src = item.src;
}
list.push(
'<li data-index="' +
i +
'" tabindex="0" class="' +
CLASS_LOAD +
'"' +
(src && src.length ? ' style="background-image:url(\'' + src + '\')" />' : "") +
"></li>"
);
});
self.$list[0].innerHTML = list.join("");
if (self.opts.axis === "x") {
// Set fixed width for list element to enable horizontal scrolling
self.$list.width(
parseInt(self.$grid.css("padding-right"), 10) +
instance.group.length *
self.$list
.children()
.eq(0)
.outerWidth(true)
);
}
},
focus: function(duration) {
var self = this,
$list = self.$list,
$grid = self.$grid,
thumb,
thumbPos;
if (!self.instance.current) {
return;
}
thumb = $list
.children()
.removeClass(CLASS_ACTIVE)
.filter('[data-index="' + self.instance.current.index + '"]')
.addClass(CLASS_ACTIVE);
thumbPos = thumb.position();
// Check if need to scroll to make current thumb visible
if (self.opts.axis === "y" && (thumbPos.top < 0 || thumbPos.top > $list.height() - thumb.outerHeight())) {
$list.stop().animate(
{
scrollTop: $list.scrollTop() + thumbPos.top
},
duration
);
} else if (
self.opts.axis === "x" &&
(thumbPos.left < $grid.scrollLeft() || thumbPos.left > $grid.scrollLeft() + ($grid.width() - thumb.outerWidth()))
) {
$list
.parent()
.stop()
.animate(
{
scrollLeft: thumbPos.left
},
duration
);
}
},
update: function() {
var that = this;
that.instance.$refs.container.toggleClass("fancybox-show-thumbs", this.isVisible);
if (that.isVisible) {
if (!that.$grid) {
that.create();
}
that.instance.trigger("onThumbsShow");
that.focus(0);
} else if (that.$grid) {
that.instance.trigger("onThumbsHide");
}
// Update content position
that.instance.update();
},
hide: function() {
this.isVisible = false;
this.update();
},
show: function() {
this.isVisible = true;
this.update();
},
toggle: function() {
this.isVisible = !this.isVisible;
this.update();
}
});
$(document).on({
"onInit.fb": function(e, instance) {
var Thumbs;
if (instance && !instance.Thumbs) {
Thumbs = new FancyThumbs(instance);
if (Thumbs.isActive && Thumbs.opts.autoStart === true) {
Thumbs.show();
}
}
},
"beforeShow.fb": function(e, instance, item, firstRun) {
var Thumbs = instance && instance.Thumbs;
if (Thumbs && Thumbs.isVisible) {
Thumbs.focus(firstRun ? 0 : 250);
}
},
"afterKeydown.fb": function(e, instance, current, keypress, keycode) {
var Thumbs = instance && instance.Thumbs;
// "G"
if (Thumbs && Thumbs.isActive && keycode === 71) {
keypress.preventDefault();
Thumbs.toggle();
}
},
"beforeClose.fb": function(e, instance) {
var Thumbs = instance && instance.Thumbs;
if (Thumbs && Thumbs.isVisible && Thumbs.opts.hideOnClose !== false) {
Thumbs.$grid.hide();
}
}
});
})(document, window.jQuery || jQuery);