foldbox 0.0.0-stage → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +134 -2
- package/dist/foldbox.min.css +4 -0
- package/dist/foldbox.min.js +5 -0
- package/package.json +34 -5
- package/src/foldbox.css +571 -0
- package/src/foldbox.js +1627 -0
package/src/foldbox.js
ADDED
|
@@ -0,0 +1,1627 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Foldbox 1.2.0 – lightbox and filterable grid for photos and videos.
|
|
3
|
+
* https://github.com/wpgenius/foldbox
|
|
4
|
+
* (c) WPGenius. Released under the MIT License.
|
|
5
|
+
*/
|
|
6
|
+
( function ( root, factory ) {
|
|
7
|
+
if ( typeof define === 'function' && define.amd ) {
|
|
8
|
+
define( [], factory );
|
|
9
|
+
} else if ( typeof module === 'object' && module.exports ) {
|
|
10
|
+
module.exports = factory();
|
|
11
|
+
} else {
|
|
12
|
+
root.Foldbox = factory();
|
|
13
|
+
}
|
|
14
|
+
} )( typeof self !== 'undefined' ? self : this, function () {
|
|
15
|
+
'use strict';
|
|
16
|
+
|
|
17
|
+
const VERSION = '1.2.0';
|
|
18
|
+
|
|
19
|
+
// Built-in English labels. Any label missing from Foldbox.defaults.labels or
|
|
20
|
+
// options.labels falls back to these.
|
|
21
|
+
const LABELS = {
|
|
22
|
+
dialog: 'Gallery',
|
|
23
|
+
close: 'Close',
|
|
24
|
+
prev: 'Previous',
|
|
25
|
+
next: 'Next',
|
|
26
|
+
zoomIn: 'Zoom in',
|
|
27
|
+
zoomOut: 'Zoom out',
|
|
28
|
+
fullscreen: 'Fullscreen',
|
|
29
|
+
exitFullscreen: 'Exit fullscreen',
|
|
30
|
+
play: 'Start slideshow',
|
|
31
|
+
pause: 'Pause slideshow',
|
|
32
|
+
download: 'Download',
|
|
33
|
+
playVideo: 'Play video',
|
|
34
|
+
counter: '%1 / %2',
|
|
35
|
+
error: 'This item could not be loaded.',
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const DEFAULTS = {
|
|
39
|
+
index: 0,
|
|
40
|
+
loop: true,
|
|
41
|
+
animation: 'slide', // 'slide' or 'fade'.
|
|
42
|
+
speed: 400,
|
|
43
|
+
origin: null, // Element the gallery opens from (zoom-in effect).
|
|
44
|
+
theme: 'dark', // 'dark', 'light' or 'auto' (follows the system setting).
|
|
45
|
+
className: '',
|
|
46
|
+
counter: true,
|
|
47
|
+
captions: true,
|
|
48
|
+
htmlCaptions: true, // false inserts captions as plain text. bind() defaults to false.
|
|
49
|
+
thumbnails: true,
|
|
50
|
+
hideThumbsInFullscreen: false,
|
|
51
|
+
zoom: true,
|
|
52
|
+
maxZoom: 3,
|
|
53
|
+
fullscreen: true,
|
|
54
|
+
slideshow: true,
|
|
55
|
+
slideshowAutostart: false,
|
|
56
|
+
slideshowInterval: 5000,
|
|
57
|
+
download: false,
|
|
58
|
+
keyboard: true,
|
|
59
|
+
swipe: true,
|
|
60
|
+
swipeToClose: true,
|
|
61
|
+
closeOnBackdrop: true,
|
|
62
|
+
youtubeNoCookie: true,
|
|
63
|
+
youtubeThumbnails: true, // Fetch the thumbnail from i.ytimg.com when no thumb/poster is given.
|
|
64
|
+
youtubeParams: { rel: 0 },
|
|
65
|
+
vimeoParams: {},
|
|
66
|
+
labels: Object.assign( {}, LABELS ),
|
|
67
|
+
onOpen: null,
|
|
68
|
+
onChange: null,
|
|
69
|
+
onClose: null,
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const ICONS = {
|
|
73
|
+
close: '<path d="M6 6l12 12M18 6L6 18"/>',
|
|
74
|
+
prev: '<path d="M15 5l-7 7 7 7"/>',
|
|
75
|
+
next: '<path d="M9 5l7 7-7 7"/>',
|
|
76
|
+
zoomIn: '<circle cx="11" cy="11" r="6"/><path d="M20 20l-4.5-4.5M11 8v6M8 11h6"/>',
|
|
77
|
+
zoomOut: '<circle cx="11" cy="11" r="6"/><path d="M20 20l-4.5-4.5M8 11h6"/>',
|
|
78
|
+
fullscreen: '<path d="M4 9V4h5M20 9V4h-5M4 15v5h5M20 15v5h-5"/>',
|
|
79
|
+
exitFullscreen: '<path d="M9 4v5H4M15 4v5h5M9 20v-5H4M15 20v-5h5"/>',
|
|
80
|
+
play: '<path d="M8 5l11 7-11 7z"/>',
|
|
81
|
+
pause: '<path d="M8 5v14M16 5v14"/>',
|
|
82
|
+
download: '<path d="M12 4v11M7 10l5 5 5-5M5 20h14"/>',
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const reducedMotion = function () {
|
|
86
|
+
return (
|
|
87
|
+
typeof window.matchMedia === 'function' &&
|
|
88
|
+
window.matchMedia( '(prefers-reduced-motion: reduce)' ).matches
|
|
89
|
+
);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
function el( tag, className, attrs ) {
|
|
93
|
+
const node = document.createElement( tag );
|
|
94
|
+
if ( className ) {
|
|
95
|
+
node.className = className;
|
|
96
|
+
}
|
|
97
|
+
if ( attrs ) {
|
|
98
|
+
Object.keys( attrs ).forEach( function ( key ) {
|
|
99
|
+
node.setAttribute( key, attrs[ key ] );
|
|
100
|
+
} );
|
|
101
|
+
}
|
|
102
|
+
return node;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function icon( name ) {
|
|
106
|
+
return (
|
|
107
|
+
'<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">' +
|
|
108
|
+
ICONS[ name ] +
|
|
109
|
+
'</svg>'
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function button( name, label ) {
|
|
114
|
+
const node = el( 'button', 'foldbox__btn foldbox__btn--' + name, {
|
|
115
|
+
type: 'button',
|
|
116
|
+
'aria-label': label,
|
|
117
|
+
title: label,
|
|
118
|
+
} );
|
|
119
|
+
node.innerHTML = icon( name );
|
|
120
|
+
return node;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function query( params ) {
|
|
124
|
+
return Object.keys( params || {} )
|
|
125
|
+
.map( function ( key ) {
|
|
126
|
+
return (
|
|
127
|
+
encodeURIComponent( key ) +
|
|
128
|
+
'=' +
|
|
129
|
+
encodeURIComponent( params[ key ] )
|
|
130
|
+
);
|
|
131
|
+
} )
|
|
132
|
+
.join( '&' );
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Return the URL if it is http(s) or blob (relative URLs included), else ''.
|
|
137
|
+
*
|
|
138
|
+
* @param {string} url URL to check.
|
|
139
|
+
* @return {string} The URL or ''.
|
|
140
|
+
*/
|
|
141
|
+
function safeUrl( url ) {
|
|
142
|
+
if ( ! url ) {
|
|
143
|
+
return '';
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
const protocol = new URL( url, document.baseURI ).protocol;
|
|
147
|
+
return protocol === 'http:' || protocol === 'https:' || protocol === 'blob:' ? url : '';
|
|
148
|
+
} catch ( e ) {
|
|
149
|
+
return '';
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/* ------------------------------------------------------------------ */
|
|
154
|
+
/* Items */
|
|
155
|
+
/* ------------------------------------------------------------------ */
|
|
156
|
+
|
|
157
|
+
const YOUTUBE =
|
|
158
|
+
/(?:youtube(?:-nocookie)?\.com\/(?:watch\?(?:.*&)?v=|embed\/|shorts\/|live\/|v\/)|youtu\.be\/)([A-Za-z0-9_-]{11})/;
|
|
159
|
+
const VIMEO = /vimeo\.com\/(?:.*\/)?(?:video\/)?(\d{5,})(?:\/([A-Za-z0-9]+))?/;
|
|
160
|
+
const VIDEO_FILE = /\.(mp4|webm|ogv|mov|m4v)(?:[?#]|$)/i;
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Seconds from a video link's time: '90', '90s', '1m30s' or '1h2m3s'.
|
|
164
|
+
*
|
|
165
|
+
* @param {string} value Time from the link.
|
|
166
|
+
* @return {number} Seconds, or 0 if not understood.
|
|
167
|
+
*/
|
|
168
|
+
function seconds( value ) {
|
|
169
|
+
const m = /^(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s?)?$/.exec( value );
|
|
170
|
+
return m ? ( Number( m[ 1 ] ) || 0 ) * 3600 + ( Number( m[ 2 ] ) || 0 ) * 60 + ( Number( m[ 3 ] ) || 0 ) : 0;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Fill in the type of an item from its fields or URL.
|
|
175
|
+
*
|
|
176
|
+
* @param {Object} raw Item as given by the caller.
|
|
177
|
+
* @param {Object} options Lightbox options.
|
|
178
|
+
* @return {Object} Normalised copy.
|
|
179
|
+
*/
|
|
180
|
+
function normalize( raw, options ) {
|
|
181
|
+
const item = Object.assign( {}, typeof raw === 'string' ? { src: raw } : raw );
|
|
182
|
+
if ( item.src ) {
|
|
183
|
+
item.src = String( item.src ); // Accept URL objects.
|
|
184
|
+
}
|
|
185
|
+
const src = item.src || '';
|
|
186
|
+
let m;
|
|
187
|
+
|
|
188
|
+
// Unknown types, and a youtube/vimeo type without a video ID, can't be
|
|
189
|
+
// shown as such; detect the type from the URL instead.
|
|
190
|
+
if (
|
|
191
|
+
( item.type && [ 'image', 'video', 'youtube', 'vimeo' ].indexOf( item.type ) === -1 ) ||
|
|
192
|
+
( item.type === 'youtube' && ! item.videoId && ! YOUTUBE.test( src ) ) ||
|
|
193
|
+
( item.type === 'vimeo' && ! item.videoId && ! VIMEO.test( src ) )
|
|
194
|
+
) {
|
|
195
|
+
delete item.type;
|
|
196
|
+
}
|
|
197
|
+
if ( ! item.type ) {
|
|
198
|
+
if ( item.sources && item.sources.length ) {
|
|
199
|
+
item.type = 'video';
|
|
200
|
+
} else if ( ( m = src.match( YOUTUBE ) ) ) {
|
|
201
|
+
item.type = 'youtube';
|
|
202
|
+
item.videoId = m[ 1 ];
|
|
203
|
+
} else if ( ( m = src.match( VIMEO ) ) ) {
|
|
204
|
+
item.type = 'vimeo';
|
|
205
|
+
item.videoId = m[ 1 ];
|
|
206
|
+
const hash = src.match( /[?&]h=([A-Za-z0-9]+)/ );
|
|
207
|
+
item.hash = m[ 2 ] || ( hash ? hash[ 1 ] : '' );
|
|
208
|
+
} else if ( VIDEO_FILE.test( src ) ) {
|
|
209
|
+
item.type = 'video';
|
|
210
|
+
} else {
|
|
211
|
+
item.type = 'image';
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if ( item.type === 'video' && ! ( item.sources && item.sources.length ) ) {
|
|
215
|
+
item.sources = [ { src } ];
|
|
216
|
+
}
|
|
217
|
+
if ( item.type === 'youtube' && ! item.videoId && ( m = src.match( YOUTUBE ) ) ) {
|
|
218
|
+
item.videoId = m[ 1 ];
|
|
219
|
+
}
|
|
220
|
+
if ( item.type === 'vimeo' && ! item.videoId && ( m = src.match( VIMEO ) ) ) {
|
|
221
|
+
item.videoId = m[ 1 ];
|
|
222
|
+
item.hash = m[ 2 ] || '';
|
|
223
|
+
}
|
|
224
|
+
// Start time from the link: ?t=90, &t=1m30s, ?start=90 or Vimeo's #t=90s.
|
|
225
|
+
if ( item.type === 'youtube' || item.type === 'vimeo' ) {
|
|
226
|
+
if ( ! item.start && ( m = src.match( /[?&#](?:t|start)=([\dhms]+)/ ) ) ) {
|
|
227
|
+
item.start = seconds( m[ 1 ] );
|
|
228
|
+
}
|
|
229
|
+
item.start = Math.max( 0, Math.floor( Number( item.start ) || 0 ) );
|
|
230
|
+
}
|
|
231
|
+
if ( ! item.thumb && item.poster ) {
|
|
232
|
+
item.thumb = item.poster;
|
|
233
|
+
}
|
|
234
|
+
if ( ! item.thumb && item.type === 'youtube' && item.videoId && options.youtubeThumbnails ) {
|
|
235
|
+
item.thumb = 'https://i.ytimg.com/vi/' + item.videoId + '/hqdefault.jpg';
|
|
236
|
+
}
|
|
237
|
+
if ( ! item.thumb && item.type === 'image' ) {
|
|
238
|
+
item.thumb = src;
|
|
239
|
+
}
|
|
240
|
+
return item;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/* ------------------------------------------------------------------ */
|
|
244
|
+
/* Lightbox */
|
|
245
|
+
/* ------------------------------------------------------------------ */
|
|
246
|
+
|
|
247
|
+
let openInstance = null;
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* A lightbox showing a list of items.
|
|
251
|
+
*
|
|
252
|
+
* @param {Array} items Items: { src, thumb, alt, caption, type, sources, poster, download }.
|
|
253
|
+
* @param {Object} options See DEFAULTS.
|
|
254
|
+
*/
|
|
255
|
+
function Lightbox( items, options ) {
|
|
256
|
+
this.options = Object.assign( {}, DEFAULTS, options || {} );
|
|
257
|
+
this.options.labels = Object.assign(
|
|
258
|
+
{},
|
|
259
|
+
LABELS,
|
|
260
|
+
DEFAULTS.labels,
|
|
261
|
+
( options && options.labels ) || {}
|
|
262
|
+
);
|
|
263
|
+
const self = this;
|
|
264
|
+
this.items = ( items || [] ).map( function ( item ) {
|
|
265
|
+
return normalize( item, self.options );
|
|
266
|
+
} );
|
|
267
|
+
this.index = 0;
|
|
268
|
+
this.slides = [];
|
|
269
|
+
this.zoomState = { scale: 1, x: 0, y: 0 };
|
|
270
|
+
this.pointers = new Map();
|
|
271
|
+
this.timer = null;
|
|
272
|
+
this.isOpen = false;
|
|
273
|
+
this.handlers = {};
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
Lightbox.prototype = {
|
|
277
|
+
/**
|
|
278
|
+
* Build the DOM and show the gallery.
|
|
279
|
+
*/
|
|
280
|
+
open() {
|
|
281
|
+
if ( ! this.items.length || this.isOpen ) {
|
|
282
|
+
return this;
|
|
283
|
+
}
|
|
284
|
+
if ( openInstance ) {
|
|
285
|
+
openInstance.close( true );
|
|
286
|
+
}
|
|
287
|
+
this.lastFocus = document.activeElement;
|
|
288
|
+
|
|
289
|
+
// Register as the open gallery only once it is built. If building
|
|
290
|
+
// throws, undo what was done so the next open() still works.
|
|
291
|
+
let locked = false;
|
|
292
|
+
try {
|
|
293
|
+
this.build();
|
|
294
|
+
this.lockScroll();
|
|
295
|
+
locked = true;
|
|
296
|
+
this.bind();
|
|
297
|
+
|
|
298
|
+
const start = Math.min(
|
|
299
|
+
Math.max( parseInt( this.options.index, 10 ) || 0, 0 ),
|
|
300
|
+
this.items.length - 1
|
|
301
|
+
);
|
|
302
|
+
this.goTo( start, true );
|
|
303
|
+
} catch ( error ) {
|
|
304
|
+
this.unbind();
|
|
305
|
+
if ( locked ) {
|
|
306
|
+
this.unlockScroll();
|
|
307
|
+
}
|
|
308
|
+
if ( this.root && this.root.parentNode ) {
|
|
309
|
+
this.root.parentNode.removeChild( this.root );
|
|
310
|
+
}
|
|
311
|
+
throw error;
|
|
312
|
+
}
|
|
313
|
+
openInstance = this;
|
|
314
|
+
this.isOpen = true;
|
|
315
|
+
|
|
316
|
+
const self = this;
|
|
317
|
+
window.requestAnimationFrame( function () {
|
|
318
|
+
self.root.classList.add( 'is-open' );
|
|
319
|
+
self.zoomFromOrigin();
|
|
320
|
+
} );
|
|
321
|
+
this.buttons.close.focus( { preventScroll: true } );
|
|
322
|
+
|
|
323
|
+
if ( this.options.slideshow && this.options.slideshowAutostart ) {
|
|
324
|
+
this.play();
|
|
325
|
+
}
|
|
326
|
+
this.emit( 'onOpen' );
|
|
327
|
+
return this;
|
|
328
|
+
},
|
|
329
|
+
|
|
330
|
+
build() {
|
|
331
|
+
const o = this.options;
|
|
332
|
+
const L = o.labels;
|
|
333
|
+
const root = el( 'div', 'foldbox foldbox--' + ( o.animation === 'fade' ? 'fade' : 'slide' ), {
|
|
334
|
+
role: 'dialog',
|
|
335
|
+
'aria-modal': 'true',
|
|
336
|
+
'aria-label': L.dialog,
|
|
337
|
+
tabindex: '-1',
|
|
338
|
+
} );
|
|
339
|
+
if ( o.theme === 'light' || o.theme === 'auto' ) {
|
|
340
|
+
root.classList.add( 'foldbox--' + o.theme );
|
|
341
|
+
}
|
|
342
|
+
if ( o.className ) {
|
|
343
|
+
root.className += ' ' + o.className;
|
|
344
|
+
}
|
|
345
|
+
if ( o.hideThumbsInFullscreen ) {
|
|
346
|
+
root.classList.add( 'foldbox--hide-thumbs-fs' );
|
|
347
|
+
}
|
|
348
|
+
if ( this.items.length < 2 ) {
|
|
349
|
+
root.classList.add( 'foldbox--single' );
|
|
350
|
+
}
|
|
351
|
+
root.style.setProperty( '--foldbox-speed', o.speed + 'ms' );
|
|
352
|
+
|
|
353
|
+
const backdrop = el( 'div', 'foldbox__backdrop' );
|
|
354
|
+
const toolbar = el( 'div', 'foldbox__toolbar' );
|
|
355
|
+
const counter = el( 'div', 'foldbox__counter', { 'aria-live': 'polite' } );
|
|
356
|
+
const actions = el( 'div', 'foldbox__actions' );
|
|
357
|
+
const progress = el( 'div', 'foldbox__progress' );
|
|
358
|
+
progress.appendChild( el( 'span' ) );
|
|
359
|
+
|
|
360
|
+
this.buttons = {};
|
|
361
|
+
if ( o.zoom ) {
|
|
362
|
+
this.buttons.zoom = actions.appendChild( button( 'zoomIn', L.zoomIn ) );
|
|
363
|
+
}
|
|
364
|
+
if ( o.slideshow && this.items.length > 1 ) {
|
|
365
|
+
this.buttons.play = actions.appendChild( button( 'play', L.play ) );
|
|
366
|
+
}
|
|
367
|
+
if ( o.fullscreen && ( document.fullscreenEnabled || document.webkitFullscreenEnabled ) ) {
|
|
368
|
+
this.buttons.fullscreen = actions.appendChild( button( 'fullscreen', L.fullscreen ) );
|
|
369
|
+
}
|
|
370
|
+
if ( o.download ) {
|
|
371
|
+
const link = el( 'a', 'foldbox__btn foldbox__btn--download', {
|
|
372
|
+
'aria-label': L.download,
|
|
373
|
+
title: L.download,
|
|
374
|
+
download: '',
|
|
375
|
+
} );
|
|
376
|
+
link.innerHTML = icon( 'download' );
|
|
377
|
+
this.buttons.download = actions.appendChild( link );
|
|
378
|
+
}
|
|
379
|
+
this.buttons.close = actions.appendChild( button( 'close', L.close ) );
|
|
380
|
+
|
|
381
|
+
if ( o.counter ) {
|
|
382
|
+
toolbar.appendChild( counter );
|
|
383
|
+
}
|
|
384
|
+
toolbar.appendChild( actions );
|
|
385
|
+
|
|
386
|
+
const stage = el( 'div', 'foldbox__stage' );
|
|
387
|
+
const track = el( 'div', 'foldbox__track' );
|
|
388
|
+
this.slides = this.items.map( function ( item, i ) {
|
|
389
|
+
const slide = el( 'div', 'foldbox__slide foldbox__slide--' + item.type, {
|
|
390
|
+
'data-index': String( i ),
|
|
391
|
+
'aria-hidden': 'true',
|
|
392
|
+
} );
|
|
393
|
+
track.appendChild( slide );
|
|
394
|
+
return slide;
|
|
395
|
+
} );
|
|
396
|
+
stage.appendChild( track );
|
|
397
|
+
|
|
398
|
+
const prev = button( 'prev', L.prev );
|
|
399
|
+
const next = button( 'next', L.next );
|
|
400
|
+
prev.classList.add( 'foldbox__nav', 'foldbox__nav--prev' );
|
|
401
|
+
next.classList.add( 'foldbox__nav', 'foldbox__nav--next' );
|
|
402
|
+
this.buttons.prev = prev;
|
|
403
|
+
this.buttons.next = next;
|
|
404
|
+
|
|
405
|
+
const footer = el( 'div', 'foldbox__footer' );
|
|
406
|
+
const caption = el( 'div', 'foldbox__caption' );
|
|
407
|
+
footer.appendChild( caption );
|
|
408
|
+
|
|
409
|
+
let thumbs = null;
|
|
410
|
+
if ( o.thumbnails && this.items.length > 1 ) {
|
|
411
|
+
thumbs = el( 'div', 'foldbox__thumbs' );
|
|
412
|
+
const total = String( this.items.length );
|
|
413
|
+
this.thumbButtons = this.items.map( function ( item, i ) {
|
|
414
|
+
const thumb = el( 'button', 'foldbox__thumb foldbox__thumb--' + item.type, {
|
|
415
|
+
type: 'button',
|
|
416
|
+
'aria-label': L.counter.replace( '%1', String( i + 1 ) ).replace( '%2', total ),
|
|
417
|
+
'data-index': String( i ),
|
|
418
|
+
} );
|
|
419
|
+
if ( item.thumb ) {
|
|
420
|
+
const img = el( 'img', '', { alt: '', loading: 'lazy', draggable: 'false' } );
|
|
421
|
+
img.src = item.thumb;
|
|
422
|
+
thumb.appendChild( img );
|
|
423
|
+
}
|
|
424
|
+
thumbs.appendChild( thumb );
|
|
425
|
+
return thumb;
|
|
426
|
+
} );
|
|
427
|
+
footer.appendChild( thumbs );
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
root.appendChild( backdrop );
|
|
431
|
+
root.appendChild( toolbar );
|
|
432
|
+
root.appendChild( progress );
|
|
433
|
+
root.appendChild( stage );
|
|
434
|
+
if ( this.items.length > 1 ) {
|
|
435
|
+
root.appendChild( prev );
|
|
436
|
+
root.appendChild( next );
|
|
437
|
+
}
|
|
438
|
+
root.appendChild( footer );
|
|
439
|
+
document.body.appendChild( root );
|
|
440
|
+
|
|
441
|
+
this.root = root;
|
|
442
|
+
this.stage = stage;
|
|
443
|
+
this.track = track;
|
|
444
|
+
this.counter = counter;
|
|
445
|
+
this.caption = caption;
|
|
446
|
+
this.footer = footer;
|
|
447
|
+
this.thumbs = thumbs;
|
|
448
|
+
this.progress = progress;
|
|
449
|
+
this.backdrop = backdrop;
|
|
450
|
+
},
|
|
451
|
+
|
|
452
|
+
bind() {
|
|
453
|
+
const self = this;
|
|
454
|
+
const on = function ( target, type, fn, opts ) {
|
|
455
|
+
target.addEventListener( type, fn, opts );
|
|
456
|
+
( self.handlers[ type ] = self.handlers[ type ] || [] ).push( [ target, fn, opts ] );
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
on( this.buttons.close, 'click', function () {
|
|
460
|
+
self.close();
|
|
461
|
+
} );
|
|
462
|
+
on( this.buttons.prev, 'click', function () {
|
|
463
|
+
self.prev();
|
|
464
|
+
} );
|
|
465
|
+
on( this.buttons.next, 'click', function () {
|
|
466
|
+
self.next();
|
|
467
|
+
} );
|
|
468
|
+
if ( this.buttons.zoom ) {
|
|
469
|
+
on( this.buttons.zoom, 'click', function () {
|
|
470
|
+
self.toggleZoom();
|
|
471
|
+
} );
|
|
472
|
+
}
|
|
473
|
+
if ( this.buttons.play ) {
|
|
474
|
+
on( this.buttons.play, 'click', function () {
|
|
475
|
+
if ( self.timer ) {
|
|
476
|
+
self.pause();
|
|
477
|
+
} else {
|
|
478
|
+
self.play();
|
|
479
|
+
}
|
|
480
|
+
} );
|
|
481
|
+
}
|
|
482
|
+
if ( this.buttons.fullscreen ) {
|
|
483
|
+
on( this.buttons.fullscreen, 'click', function () {
|
|
484
|
+
self.toggleFullscreen();
|
|
485
|
+
} );
|
|
486
|
+
on( document, 'fullscreenchange', function () {
|
|
487
|
+
self.onFullscreenChange();
|
|
488
|
+
} );
|
|
489
|
+
on( document, 'webkitfullscreenchange', function () {
|
|
490
|
+
self.onFullscreenChange();
|
|
491
|
+
} );
|
|
492
|
+
}
|
|
493
|
+
if ( this.thumbs ) {
|
|
494
|
+
on( this.thumbs, 'click', function ( event ) {
|
|
495
|
+
const thumb = event.target.closest( '.foldbox__thumb' );
|
|
496
|
+
if ( thumb ) {
|
|
497
|
+
self.goTo( parseInt( thumb.getAttribute( 'data-index' ), 10 ) );
|
|
498
|
+
}
|
|
499
|
+
} );
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
on( document, 'keydown', function ( event ) {
|
|
503
|
+
self.onKey( event );
|
|
504
|
+
} );
|
|
505
|
+
|
|
506
|
+
// Pointer: swipe, swipe to close, pan and pinch zoom.
|
|
507
|
+
on( this.stage, 'pointerdown', function ( event ) {
|
|
508
|
+
self.onPointerDown( event );
|
|
509
|
+
} );
|
|
510
|
+
on( window, 'pointermove', function ( event ) {
|
|
511
|
+
self.onPointerMove( event );
|
|
512
|
+
} );
|
|
513
|
+
on( window, 'pointerup', function ( event ) {
|
|
514
|
+
self.onPointerUp( event );
|
|
515
|
+
} );
|
|
516
|
+
on( window, 'pointercancel', function ( event ) {
|
|
517
|
+
self.onPointerUp( event );
|
|
518
|
+
} );
|
|
519
|
+
on( this.stage, 'dblclick', function ( event ) {
|
|
520
|
+
if ( self.options.zoom && event.target.classList.contains( 'foldbox__img' ) ) {
|
|
521
|
+
self.toggleZoom( event.clientX, event.clientY );
|
|
522
|
+
}
|
|
523
|
+
} );
|
|
524
|
+
on(
|
|
525
|
+
this.stage,
|
|
526
|
+
'wheel',
|
|
527
|
+
function ( event ) {
|
|
528
|
+
self.onWheel( event );
|
|
529
|
+
},
|
|
530
|
+
{ passive: false }
|
|
531
|
+
);
|
|
532
|
+
on( this.stage, 'click', function ( event ) {
|
|
533
|
+
if ( self.dragged ) {
|
|
534
|
+
self.dragged = false;
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
const play = event.target.closest( '.foldbox__play' );
|
|
538
|
+
if ( play ) {
|
|
539
|
+
self.playEmbed( parseInt( play.getAttribute( 'data-index' ), 10 ) );
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
if (
|
|
543
|
+
self.options.closeOnBackdrop &&
|
|
544
|
+
( event.target.classList.contains( 'foldbox__slide' ) ||
|
|
545
|
+
event.target === self.stage ||
|
|
546
|
+
event.target.classList.contains( 'foldbox__track' ) )
|
|
547
|
+
) {
|
|
548
|
+
self.close();
|
|
549
|
+
}
|
|
550
|
+
} );
|
|
551
|
+
// Keep the zoom level (mobile address bars fire resize while panning);
|
|
552
|
+
// only keep the image inside the new stage size.
|
|
553
|
+
on( window, 'resize', function () {
|
|
554
|
+
self.clampZoom();
|
|
555
|
+
self.applyZoom( false );
|
|
556
|
+
} );
|
|
557
|
+
},
|
|
558
|
+
|
|
559
|
+
unbind() {
|
|
560
|
+
const handlers = this.handlers;
|
|
561
|
+
Object.keys( handlers ).forEach( function ( type ) {
|
|
562
|
+
handlers[ type ].forEach( function ( entry ) {
|
|
563
|
+
entry[ 0 ].removeEventListener( type, entry[ 1 ], entry[ 2 ] );
|
|
564
|
+
} );
|
|
565
|
+
} );
|
|
566
|
+
this.handlers = {};
|
|
567
|
+
},
|
|
568
|
+
|
|
569
|
+
/* Navigation ---------------------------------------------------- */
|
|
570
|
+
|
|
571
|
+
goTo( index, instant ) {
|
|
572
|
+
const count = this.items.length;
|
|
573
|
+
if ( this.options.loop ) {
|
|
574
|
+
index = ( ( index % count ) + count ) % count;
|
|
575
|
+
} else {
|
|
576
|
+
index = Math.min( Math.max( index, 0 ), count - 1 );
|
|
577
|
+
}
|
|
578
|
+
const previous = this.index;
|
|
579
|
+
const changed = index !== previous || instant;
|
|
580
|
+
if ( ! changed ) {
|
|
581
|
+
return this;
|
|
582
|
+
}
|
|
583
|
+
// The slide being left turns inert (or loses its player), so focus
|
|
584
|
+
// inside it would drop to <body>; keep it in the dialog instead.
|
|
585
|
+
const hadFocus = index !== previous && this.slides[ previous ] && this.slides[ previous ].contains( document.activeElement );
|
|
586
|
+
if ( this.slides[ previous ] ) {
|
|
587
|
+
this.leaveSlide( previous );
|
|
588
|
+
}
|
|
589
|
+
this.index = index;
|
|
590
|
+
this.resetZoom();
|
|
591
|
+
|
|
592
|
+
const self = this;
|
|
593
|
+
this.slides.forEach( function ( slide, i ) {
|
|
594
|
+
const active = i === index;
|
|
595
|
+
slide.classList.toggle( 'is-active', active );
|
|
596
|
+
slide.setAttribute( 'aria-hidden', active ? 'false' : 'true' );
|
|
597
|
+
// Keep controls in off-screen slides out of the tab order.
|
|
598
|
+
slide.inert = ! active;
|
|
599
|
+
if ( self.isNear( i ) ) {
|
|
600
|
+
self.load( i );
|
|
601
|
+
}
|
|
602
|
+
} );
|
|
603
|
+
if ( hadFocus ) {
|
|
604
|
+
this.root.focus( { preventScroll: true } );
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
this.track.style.transition = instant ? 'none' : '';
|
|
608
|
+
if ( this.options.animation !== 'fade' ) {
|
|
609
|
+
this.track.style.transform = 'translate3d(' + -index * 100 + '%, 0, 0)';
|
|
610
|
+
}
|
|
611
|
+
if ( instant ) {
|
|
612
|
+
// Force the jump, then restore transitions.
|
|
613
|
+
void this.track.offsetWidth;
|
|
614
|
+
this.track.style.transition = '';
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
this.updateUi();
|
|
618
|
+
if ( this.timer ) {
|
|
619
|
+
this.restartProgress();
|
|
620
|
+
}
|
|
621
|
+
if ( ! instant ) {
|
|
622
|
+
this.emit( 'onChange' );
|
|
623
|
+
}
|
|
624
|
+
return this;
|
|
625
|
+
},
|
|
626
|
+
|
|
627
|
+
next() {
|
|
628
|
+
if ( ! this.options.loop && this.index >= this.items.length - 1 ) {
|
|
629
|
+
this.bounce( -1 );
|
|
630
|
+
return this;
|
|
631
|
+
}
|
|
632
|
+
return this.goTo( this.index + 1 );
|
|
633
|
+
},
|
|
634
|
+
|
|
635
|
+
prev() {
|
|
636
|
+
if ( ! this.options.loop && this.index <= 0 ) {
|
|
637
|
+
this.bounce( 1 );
|
|
638
|
+
return this;
|
|
639
|
+
}
|
|
640
|
+
return this.goTo( this.index - 1 );
|
|
641
|
+
},
|
|
642
|
+
|
|
643
|
+
bounce( direction ) {
|
|
644
|
+
if ( reducedMotion() || ! this.track.animate ) {
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
const base = this.options.animation === 'fade' ? 0 : -this.index * 100;
|
|
648
|
+
this.track.animate(
|
|
649
|
+
[
|
|
650
|
+
{ transform: 'translate3d(' + base + '%,0,0)' },
|
|
651
|
+
{ transform: 'translate3d(calc(' + base + '% + ' + direction * 40 + 'px),0,0)' },
|
|
652
|
+
{ transform: 'translate3d(' + base + '%,0,0)' },
|
|
653
|
+
],
|
|
654
|
+
{ duration: 300, easing: 'ease-out' }
|
|
655
|
+
);
|
|
656
|
+
},
|
|
657
|
+
|
|
658
|
+
isNear( i ) {
|
|
659
|
+
const count = this.items.length;
|
|
660
|
+
const d = Math.abs( i - this.index );
|
|
661
|
+
return d <= 1 || ( this.options.loop && count > 2 && d === count - 1 );
|
|
662
|
+
},
|
|
663
|
+
|
|
664
|
+
updateUi() {
|
|
665
|
+
const o = this.options;
|
|
666
|
+
const item = this.items[ this.index ];
|
|
667
|
+
const count = this.items.length;
|
|
668
|
+
|
|
669
|
+
this.counter.textContent = o.labels.counter
|
|
670
|
+
.replace( '%1', String( this.index + 1 ) )
|
|
671
|
+
.replace( '%2', String( count ) );
|
|
672
|
+
|
|
673
|
+
if ( o.captions && item.caption ) {
|
|
674
|
+
if ( o.htmlCaptions ) {
|
|
675
|
+
this.caption.innerHTML = item.caption;
|
|
676
|
+
} else {
|
|
677
|
+
this.caption.textContent = item.caption;
|
|
678
|
+
}
|
|
679
|
+
this.caption.hidden = false;
|
|
680
|
+
} else {
|
|
681
|
+
this.caption.innerHTML = '';
|
|
682
|
+
this.caption.hidden = true;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
if ( this.buttons.prev ) {
|
|
686
|
+
this.buttons.prev.disabled = ! o.loop && this.index === 0;
|
|
687
|
+
this.buttons.next.disabled = ! o.loop && this.index === count - 1;
|
|
688
|
+
}
|
|
689
|
+
// An image that failed to load has nothing to zoom, and its own URL is
|
|
690
|
+
// not worth downloading (an explicit `download` URL still is).
|
|
691
|
+
const failed = this.slides[ this.index ].classList.contains( 'has-error' );
|
|
692
|
+
if ( this.buttons.zoom ) {
|
|
693
|
+
this.buttons.zoom.hidden = item.type !== 'image' || failed;
|
|
694
|
+
}
|
|
695
|
+
if ( this.buttons.download ) {
|
|
696
|
+
const url = safeUrl( item.download === false ? '' : item.download || ( failed ? '' : item.type === 'image' ? item.src : item.type === 'video' ? item.sources[ 0 ].src : '' ) );
|
|
697
|
+
this.buttons.download.hidden = ! url;
|
|
698
|
+
if ( url ) {
|
|
699
|
+
this.buttons.download.href = url;
|
|
700
|
+
// Browsers ignore `download` for other origins and would navigate
|
|
701
|
+
// the page to the file; open those in a new tab instead.
|
|
702
|
+
if ( new URL( url, document.baseURI ).origin !== window.location.origin ) {
|
|
703
|
+
this.buttons.download.target = '_blank';
|
|
704
|
+
this.buttons.download.rel = 'noopener';
|
|
705
|
+
} else {
|
|
706
|
+
this.buttons.download.removeAttribute( 'target' );
|
|
707
|
+
this.buttons.download.removeAttribute( 'rel' );
|
|
708
|
+
}
|
|
709
|
+
} else {
|
|
710
|
+
this.buttons.download.removeAttribute( 'href' );
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
if ( this.thumbButtons ) {
|
|
714
|
+
const index = this.index;
|
|
715
|
+
this.thumbButtons.forEach( function ( thumb, i ) {
|
|
716
|
+
thumb.classList.toggle( 'is-active', i === index );
|
|
717
|
+
if ( i === index ) {
|
|
718
|
+
thumb.setAttribute( 'aria-current', 'true' );
|
|
719
|
+
} else {
|
|
720
|
+
thumb.removeAttribute( 'aria-current' );
|
|
721
|
+
}
|
|
722
|
+
} );
|
|
723
|
+
const active = this.thumbButtons[ index ];
|
|
724
|
+
const strip = this.thumbs;
|
|
725
|
+
const left = active.offsetLeft - ( strip.clientWidth - active.offsetWidth ) / 2;
|
|
726
|
+
if ( typeof strip.scrollTo === 'function' ) {
|
|
727
|
+
strip.scrollTo( { left, behavior: reducedMotion() ? 'auto' : 'smooth' } );
|
|
728
|
+
} else {
|
|
729
|
+
strip.scrollLeft = left;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
},
|
|
733
|
+
|
|
734
|
+
/* Slide content ------------------------------------------------- */
|
|
735
|
+
|
|
736
|
+
load( i ) {
|
|
737
|
+
const slide = this.slides[ i ];
|
|
738
|
+
if ( slide.getAttribute( 'data-loaded' ) ) {
|
|
739
|
+
return;
|
|
740
|
+
}
|
|
741
|
+
slide.setAttribute( 'data-loaded', '1' );
|
|
742
|
+
const item = this.items[ i ];
|
|
743
|
+
const L = this.options.labels;
|
|
744
|
+
|
|
745
|
+
if ( item.type === 'image' ) {
|
|
746
|
+
const wrap = el( 'div', 'foldbox__zoom' );
|
|
747
|
+
const img = el( 'img', 'foldbox__img', { alt: item.alt || '', draggable: 'false' } );
|
|
748
|
+
slide.classList.add( 'is-loading' );
|
|
749
|
+
img.addEventListener( 'load', function () {
|
|
750
|
+
slide.classList.remove( 'is-loading' );
|
|
751
|
+
} );
|
|
752
|
+
const self = this;
|
|
753
|
+
img.addEventListener( 'error', function () {
|
|
754
|
+
slide.classList.remove( 'is-loading' );
|
|
755
|
+
slide.classList.add( 'has-error' );
|
|
756
|
+
wrap.textContent = L.error;
|
|
757
|
+
if ( i === self.index && self.isOpen ) {
|
|
758
|
+
self.updateUi(); // Hide the zoom and download buttons.
|
|
759
|
+
}
|
|
760
|
+
} );
|
|
761
|
+
if ( item.srcset ) {
|
|
762
|
+
img.srcset = item.srcset;
|
|
763
|
+
img.sizes = '100vw';
|
|
764
|
+
}
|
|
765
|
+
img.src = item.src;
|
|
766
|
+
wrap.appendChild( img );
|
|
767
|
+
slide.appendChild( wrap );
|
|
768
|
+
return;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
if ( item.type === 'video' ) {
|
|
772
|
+
const video = el( 'video', 'foldbox__video', {
|
|
773
|
+
controls: '',
|
|
774
|
+
playsinline: '',
|
|
775
|
+
preload: 'metadata',
|
|
776
|
+
} );
|
|
777
|
+
if ( item.poster ) {
|
|
778
|
+
video.poster = item.poster;
|
|
779
|
+
}
|
|
780
|
+
item.sources.forEach( function ( source ) {
|
|
781
|
+
const node = el( 'source' );
|
|
782
|
+
node.src = source.src;
|
|
783
|
+
if ( source.type ) {
|
|
784
|
+
node.type = source.type;
|
|
785
|
+
}
|
|
786
|
+
video.appendChild( node );
|
|
787
|
+
} );
|
|
788
|
+
const self = this;
|
|
789
|
+
video.addEventListener( 'play', function () {
|
|
790
|
+
self.pause();
|
|
791
|
+
} );
|
|
792
|
+
slide.appendChild( video );
|
|
793
|
+
return;
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
// YouTube and Vimeo: a poster with a play button. The player is only
|
|
797
|
+
// loaded when the visitor presses play.
|
|
798
|
+
const poster = el( 'button', 'foldbox__play', {
|
|
799
|
+
type: 'button',
|
|
800
|
+
'aria-label': L.playVideo,
|
|
801
|
+
'data-index': String( i ),
|
|
802
|
+
} );
|
|
803
|
+
const image = item.poster || item.thumb;
|
|
804
|
+
if ( image ) {
|
|
805
|
+
const img = el( 'img', '', { alt: item.alt || '', draggable: 'false' } );
|
|
806
|
+
img.src = image;
|
|
807
|
+
poster.appendChild( img );
|
|
808
|
+
}
|
|
809
|
+
poster.appendChild( el( 'span', 'foldbox__play-icon' ) );
|
|
810
|
+
slide.appendChild( poster );
|
|
811
|
+
},
|
|
812
|
+
|
|
813
|
+
embedUrl( item ) {
|
|
814
|
+
const o = this.options;
|
|
815
|
+
if ( item.type === 'youtube' ) {
|
|
816
|
+
const host = o.youtubeNoCookie ? 'https://www.youtube-nocookie.com' : 'https://www.youtube.com';
|
|
817
|
+
const start = item.start ? { start: item.start } : {};
|
|
818
|
+
return host + '/embed/' + encodeURIComponent( item.videoId ) + '?' + query( Object.assign( { autoplay: 1, playsinline: 1 }, start, o.youtubeParams ) );
|
|
819
|
+
}
|
|
820
|
+
const params = Object.assign( { autoplay: 1 }, o.vimeoParams );
|
|
821
|
+
if ( item.hash ) {
|
|
822
|
+
params.h = item.hash;
|
|
823
|
+
}
|
|
824
|
+
return 'https://player.vimeo.com/video/' + encodeURIComponent( item.videoId ) + '?' + query( params ) + ( item.start ? '#t=' + item.start + 's' : '' );
|
|
825
|
+
},
|
|
826
|
+
|
|
827
|
+
playEmbed( i ) {
|
|
828
|
+
const item = this.items[ i ];
|
|
829
|
+
const slide = this.slides[ i ];
|
|
830
|
+
if ( ! item || ( item.type !== 'youtube' && item.type !== 'vimeo' ) ) {
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
if ( i !== this.index ) {
|
|
834
|
+
this.goTo( i );
|
|
835
|
+
}
|
|
836
|
+
this.pause();
|
|
837
|
+
const frame = el( 'iframe', 'foldbox__iframe', {
|
|
838
|
+
src: this.embedUrl( item ),
|
|
839
|
+
title: item.title || item.alt || this.options.labels.playVideo,
|
|
840
|
+
allow: 'autoplay; fullscreen; picture-in-picture; encrypted-media',
|
|
841
|
+
allowfullscreen: '',
|
|
842
|
+
// YouTube needs the referrer (Error 153 without it), even on sites
|
|
843
|
+
// that set Referrer-Policy: no-referrer. Sends the origin only.
|
|
844
|
+
referrerpolicy: 'strict-origin-when-cross-origin',
|
|
845
|
+
} );
|
|
846
|
+
const holder = el( 'div', 'foldbox__embed' );
|
|
847
|
+
holder.appendChild( frame );
|
|
848
|
+
// The play button is about to be removed; hand its focus to the player.
|
|
849
|
+
const hadFocus = slide.contains( document.activeElement );
|
|
850
|
+
slide.innerHTML = '';
|
|
851
|
+
slide.appendChild( holder );
|
|
852
|
+
if ( hadFocus ) {
|
|
853
|
+
frame.focus();
|
|
854
|
+
}
|
|
855
|
+
slide.setAttribute( 'data-playing', '1' );
|
|
856
|
+
},
|
|
857
|
+
|
|
858
|
+
leaveSlide( i ) {
|
|
859
|
+
const slide = this.slides[ i ];
|
|
860
|
+
const video = slide.querySelector( 'video' );
|
|
861
|
+
if ( video && ! video.paused ) {
|
|
862
|
+
video.pause();
|
|
863
|
+
}
|
|
864
|
+
if ( slide.getAttribute( 'data-playing' ) ) {
|
|
865
|
+
// Drop the player so it stops; show the poster again.
|
|
866
|
+
slide.innerHTML = '';
|
|
867
|
+
slide.removeAttribute( 'data-playing' );
|
|
868
|
+
slide.removeAttribute( 'data-loaded' );
|
|
869
|
+
this.load( i );
|
|
870
|
+
}
|
|
871
|
+
},
|
|
872
|
+
|
|
873
|
+
/* Opening effect ------------------------------------------------- */
|
|
874
|
+
|
|
875
|
+
zoomFromOrigin() {
|
|
876
|
+
const origin = this.options.origin;
|
|
877
|
+
const img = this.slides[ this.index ].querySelector( '.foldbox__img' );
|
|
878
|
+
if ( ! origin || ! img || reducedMotion() || ! img.animate ) {
|
|
879
|
+
return;
|
|
880
|
+
}
|
|
881
|
+
const self = this;
|
|
882
|
+
const run = function () {
|
|
883
|
+
const from = origin.getBoundingClientRect();
|
|
884
|
+
const to = img.getBoundingClientRect();
|
|
885
|
+
if ( ! from.width || ! to.width ) {
|
|
886
|
+
return;
|
|
887
|
+
}
|
|
888
|
+
const scale = Math.max( from.width / to.width, from.height / to.height );
|
|
889
|
+
const dx = from.left + from.width / 2 - ( to.left + to.width / 2 );
|
|
890
|
+
const dy = from.top + from.height / 2 - ( to.top + to.height / 2 );
|
|
891
|
+
img.animate(
|
|
892
|
+
[
|
|
893
|
+
{ transform: 'translate(' + dx + 'px,' + dy + 'px) scale(' + scale + ')', opacity: 0.6 },
|
|
894
|
+
{ transform: 'none', opacity: 1 },
|
|
895
|
+
],
|
|
896
|
+
{ duration: self.options.speed, easing: 'cubic-bezier(.2,.8,.2,1)' }
|
|
897
|
+
);
|
|
898
|
+
};
|
|
899
|
+
if ( img.complete && img.naturalWidth ) {
|
|
900
|
+
run();
|
|
901
|
+
} else {
|
|
902
|
+
img.addEventListener( 'load', run, { once: true } );
|
|
903
|
+
}
|
|
904
|
+
},
|
|
905
|
+
|
|
906
|
+
/* Zoom ---------------------------------------------------------- */
|
|
907
|
+
|
|
908
|
+
currentImage() {
|
|
909
|
+
const item = this.items[ this.index ];
|
|
910
|
+
return item && item.type === 'image' ? this.slides[ this.index ].querySelector( '.foldbox__img' ) : null;
|
|
911
|
+
},
|
|
912
|
+
|
|
913
|
+
applyZoom( animate ) {
|
|
914
|
+
const img = this.currentImage();
|
|
915
|
+
if ( ! img ) {
|
|
916
|
+
return;
|
|
917
|
+
}
|
|
918
|
+
const z = this.zoomState;
|
|
919
|
+
const wrap = img.parentNode;
|
|
920
|
+
wrap.style.transition = animate ? '' : 'none';
|
|
921
|
+
wrap.style.transform = z.scale === 1 ? '' : 'translate3d(' + z.x + 'px,' + z.y + 'px,0) scale(' + z.scale + ')';
|
|
922
|
+
this.root.classList.toggle( 'is-zoomed', z.scale > 1 );
|
|
923
|
+
if ( this.buttons.zoom ) {
|
|
924
|
+
const zoomed = z.scale > 1;
|
|
925
|
+
const label = zoomed ? this.options.labels.zoomOut : this.options.labels.zoomIn;
|
|
926
|
+
this.buttons.zoom.innerHTML = icon( zoomed ? 'zoomOut' : 'zoomIn' );
|
|
927
|
+
this.buttons.zoom.setAttribute( 'aria-label', label );
|
|
928
|
+
this.buttons.zoom.title = label;
|
|
929
|
+
}
|
|
930
|
+
},
|
|
931
|
+
|
|
932
|
+
clampZoom() {
|
|
933
|
+
const img = this.currentImage();
|
|
934
|
+
if ( ! img ) {
|
|
935
|
+
return;
|
|
936
|
+
}
|
|
937
|
+
const z = this.zoomState;
|
|
938
|
+
const maxX = Math.max( 0, ( img.offsetWidth * z.scale - this.stage.clientWidth ) / 2 );
|
|
939
|
+
const maxY = Math.max( 0, ( img.offsetHeight * z.scale - this.stage.clientHeight ) / 2 );
|
|
940
|
+
z.x = Math.min( maxX, Math.max( -maxX, z.x ) );
|
|
941
|
+
z.y = Math.min( maxY, Math.max( -maxY, z.y ) );
|
|
942
|
+
},
|
|
943
|
+
|
|
944
|
+
/**
|
|
945
|
+
* Zoom to a scale, keeping the point (clientX, clientY) under the pointer.
|
|
946
|
+
*
|
|
947
|
+
* @param {number} scale Target scale.
|
|
948
|
+
* @param {number} clientX Pointer x (optional, default centre).
|
|
949
|
+
* @param {number} clientY Pointer y.
|
|
950
|
+
* @param {boolean} animate Use a transition.
|
|
951
|
+
*/
|
|
952
|
+
zoomTo( scale, clientX, clientY, animate ) {
|
|
953
|
+
const img = this.currentImage();
|
|
954
|
+
if ( ! img ) {
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
const z = this.zoomState;
|
|
958
|
+
scale = Math.min( Math.max( scale, 1 ), this.options.maxZoom );
|
|
959
|
+
const rect = this.stage.getBoundingClientRect();
|
|
960
|
+
const cx = clientX === undefined ? rect.left + rect.width / 2 : clientX;
|
|
961
|
+
const cy = clientY === undefined ? rect.top + rect.height / 2 : clientY;
|
|
962
|
+
// Point relative to the image centre, in unscaled pixels.
|
|
963
|
+
const px = ( cx - ( rect.left + rect.width / 2 ) - z.x ) / z.scale;
|
|
964
|
+
const py = ( cy - ( rect.top + rect.height / 2 ) - z.y ) / z.scale;
|
|
965
|
+
z.x = cx - ( rect.left + rect.width / 2 ) - px * scale;
|
|
966
|
+
z.y = cy - ( rect.top + rect.height / 2 ) - py * scale;
|
|
967
|
+
z.scale = scale;
|
|
968
|
+
if ( scale === 1 ) {
|
|
969
|
+
z.x = 0;
|
|
970
|
+
z.y = 0;
|
|
971
|
+
}
|
|
972
|
+
this.clampZoom();
|
|
973
|
+
this.applyZoom( animate );
|
|
974
|
+
},
|
|
975
|
+
|
|
976
|
+
toggleZoom( clientX, clientY ) {
|
|
977
|
+
if ( this.zoomState.scale > 1 ) {
|
|
978
|
+
this.zoomTo( 1, undefined, undefined, true );
|
|
979
|
+
} else {
|
|
980
|
+
this.zoomTo( Math.min( 2, this.options.maxZoom ), clientX, clientY, true );
|
|
981
|
+
}
|
|
982
|
+
},
|
|
983
|
+
|
|
984
|
+
resetZoom() {
|
|
985
|
+
const had = this.zoomState.scale !== 1;
|
|
986
|
+
this.zoomState = { scale: 1, x: 0, y: 0 };
|
|
987
|
+
if ( this.root ) {
|
|
988
|
+
this.root.classList.remove( 'is-zoomed' );
|
|
989
|
+
this.slides.forEach( function ( slide ) {
|
|
990
|
+
const wrap = slide.querySelector( '.foldbox__zoom' );
|
|
991
|
+
if ( wrap ) {
|
|
992
|
+
wrap.style.transform = '';
|
|
993
|
+
}
|
|
994
|
+
} );
|
|
995
|
+
if ( had || this.buttons.zoom ) {
|
|
996
|
+
this.applyZoom( false );
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
},
|
|
1000
|
+
|
|
1001
|
+
onWheel( event ) {
|
|
1002
|
+
if ( ! this.options.zoom || ! this.currentImage() ) {
|
|
1003
|
+
return;
|
|
1004
|
+
}
|
|
1005
|
+
// Trackpad pinch arrives as ctrl + wheel; plain wheel zooms once zoomed in.
|
|
1006
|
+
if ( ! event.ctrlKey && this.zoomState.scale === 1 ) {
|
|
1007
|
+
return;
|
|
1008
|
+
}
|
|
1009
|
+
event.preventDefault();
|
|
1010
|
+
const factor = Math.exp( -event.deltaY * ( event.ctrlKey ? 0.01 : 0.002 ) );
|
|
1011
|
+
this.zoomTo( this.zoomState.scale * factor, event.clientX, event.clientY, false );
|
|
1012
|
+
},
|
|
1013
|
+
|
|
1014
|
+
/* Pointer gestures ---------------------------------------------- */
|
|
1015
|
+
|
|
1016
|
+
onPointerDown( event ) {
|
|
1017
|
+
if ( event.button !== undefined && event.button !== 0 ) {
|
|
1018
|
+
return;
|
|
1019
|
+
}
|
|
1020
|
+
// Leave native controls (video bar, iframes, buttons) alone.
|
|
1021
|
+
if ( event.target.closest( 'video, iframe, .foldbox__play, a, button' ) && this.zoomState.scale === 1 ) {
|
|
1022
|
+
if ( ! event.target.closest( '.foldbox__play' ) ) {
|
|
1023
|
+
return;
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
this.pointers.set( event.pointerId, { x: event.clientX, y: event.clientY } );
|
|
1027
|
+
if ( this.pointers.size === 1 ) {
|
|
1028
|
+
this.gesture = {
|
|
1029
|
+
startX: event.clientX,
|
|
1030
|
+
startY: event.clientY,
|
|
1031
|
+
zoomX: this.zoomState.x,
|
|
1032
|
+
zoomY: this.zoomState.y,
|
|
1033
|
+
time: Date.now(),
|
|
1034
|
+
axis: null,
|
|
1035
|
+
};
|
|
1036
|
+
this.dragged = false;
|
|
1037
|
+
} else if ( this.pointers.size === 2 && this.options.zoom && this.currentImage() ) {
|
|
1038
|
+
const pts = Array.from( this.pointers.values() );
|
|
1039
|
+
this.pinch = {
|
|
1040
|
+
distance: Math.hypot( pts[ 0 ].x - pts[ 1 ].x, pts[ 0 ].y - pts[ 1 ].y ),
|
|
1041
|
+
scale: this.zoomState.scale,
|
|
1042
|
+
};
|
|
1043
|
+
this.resetDrag();
|
|
1044
|
+
}
|
|
1045
|
+
},
|
|
1046
|
+
|
|
1047
|
+
onPointerMove( event ) {
|
|
1048
|
+
if ( ! this.pointers.has( event.pointerId ) ) {
|
|
1049
|
+
return;
|
|
1050
|
+
}
|
|
1051
|
+
this.pointers.set( event.pointerId, { x: event.clientX, y: event.clientY } );
|
|
1052
|
+
|
|
1053
|
+
if ( this.pinch && this.pointers.size === 2 ) {
|
|
1054
|
+
const pts = Array.from( this.pointers.values() );
|
|
1055
|
+
const distance = Math.hypot( pts[ 0 ].x - pts[ 1 ].x, pts[ 0 ].y - pts[ 1 ].y );
|
|
1056
|
+
this.zoomTo( ( this.pinch.scale * distance ) / this.pinch.distance, ( pts[ 0 ].x + pts[ 1 ].x ) / 2, ( pts[ 0 ].y + pts[ 1 ].y ) / 2, false );
|
|
1057
|
+
this.dragged = true;
|
|
1058
|
+
return;
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
const g = this.gesture;
|
|
1062
|
+
if ( ! g ) {
|
|
1063
|
+
return;
|
|
1064
|
+
}
|
|
1065
|
+
const dx = event.clientX - g.startX;
|
|
1066
|
+
const dy = event.clientY - g.startY;
|
|
1067
|
+
if ( Math.abs( dx ) > 5 || Math.abs( dy ) > 5 ) {
|
|
1068
|
+
this.dragged = true;
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
if ( this.zoomState.scale > 1 ) {
|
|
1072
|
+
this.zoomState.x = g.zoomX + dx;
|
|
1073
|
+
this.zoomState.y = g.zoomY + dy;
|
|
1074
|
+
this.clampZoom();
|
|
1075
|
+
this.applyZoom( false );
|
|
1076
|
+
return;
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
if ( ! g.axis && this.dragged ) {
|
|
1080
|
+
g.axis = Math.abs( dx ) >= Math.abs( dy ) ? 'x' : 'y';
|
|
1081
|
+
}
|
|
1082
|
+
if ( g.axis === 'x' && this.options.swipe && this.items.length > 1 ) {
|
|
1083
|
+
this.track.style.transition = 'none';
|
|
1084
|
+
const base = this.options.animation === 'fade' ? 0 : -this.index * 100;
|
|
1085
|
+
this.track.style.transform = 'translate3d(calc(' + base + '% + ' + dx + 'px),0,0)';
|
|
1086
|
+
} else if ( g.axis === 'y' && this.options.swipeToClose ) {
|
|
1087
|
+
this.stage.style.transition = 'none';
|
|
1088
|
+
this.stage.style.transform = 'translate3d(0,' + dy + 'px,0)';
|
|
1089
|
+
this.backdrop.style.opacity = String( Math.max( 0.2, 1 - Math.abs( dy ) / 400 ) );
|
|
1090
|
+
}
|
|
1091
|
+
},
|
|
1092
|
+
|
|
1093
|
+
onPointerUp( event ) {
|
|
1094
|
+
if ( ! this.pointers.has( event.pointerId ) ) {
|
|
1095
|
+
return;
|
|
1096
|
+
}
|
|
1097
|
+
this.pointers.delete( event.pointerId );
|
|
1098
|
+
if ( this.pinch ) {
|
|
1099
|
+
if ( this.pointers.size < 2 ) {
|
|
1100
|
+
this.pinch = null;
|
|
1101
|
+
this.gesture = null;
|
|
1102
|
+
// Let the finger still down keep panning the zoomed image.
|
|
1103
|
+
const rest = this.pointers.values().next().value;
|
|
1104
|
+
if ( rest && this.zoomState.scale > 1 ) {
|
|
1105
|
+
this.gesture = {
|
|
1106
|
+
startX: rest.x,
|
|
1107
|
+
startY: rest.y,
|
|
1108
|
+
zoomX: this.zoomState.x,
|
|
1109
|
+
zoomY: this.zoomState.y,
|
|
1110
|
+
time: Date.now(),
|
|
1111
|
+
axis: null,
|
|
1112
|
+
};
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
return;
|
|
1116
|
+
}
|
|
1117
|
+
const g = this.gesture;
|
|
1118
|
+
this.gesture = null;
|
|
1119
|
+
if ( ! g || this.zoomState.scale > 1 ) {
|
|
1120
|
+
return;
|
|
1121
|
+
}
|
|
1122
|
+
const dx = event.clientX - g.startX;
|
|
1123
|
+
const dy = event.clientY - g.startY;
|
|
1124
|
+
const fast = Date.now() - g.time < 250;
|
|
1125
|
+
|
|
1126
|
+
if ( g.axis === 'x' && this.options.swipe && this.items.length > 1 ) {
|
|
1127
|
+
this.track.style.transition = '';
|
|
1128
|
+
if ( dx < -60 || ( fast && dx < -20 ) ) {
|
|
1129
|
+
this.next();
|
|
1130
|
+
} else if ( dx > 60 || ( fast && dx > 20 ) ) {
|
|
1131
|
+
this.prev();
|
|
1132
|
+
}
|
|
1133
|
+
this.track.style.transform = this.options.animation === 'fade' ? '' : 'translate3d(' + -this.index * 100 + '%,0,0)';
|
|
1134
|
+
} else if ( g.axis === 'y' && this.options.swipeToClose ) {
|
|
1135
|
+
if ( Math.abs( dy ) > 120 || ( fast && Math.abs( dy ) > 50 ) ) {
|
|
1136
|
+
this.close();
|
|
1137
|
+
} else {
|
|
1138
|
+
this.resetDrag();
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
},
|
|
1142
|
+
|
|
1143
|
+
resetDrag() {
|
|
1144
|
+
this.stage.style.transition = '';
|
|
1145
|
+
this.stage.style.transform = '';
|
|
1146
|
+
this.backdrop.style.opacity = '';
|
|
1147
|
+
this.track.style.transition = '';
|
|
1148
|
+
if ( this.options.animation !== 'fade' ) {
|
|
1149
|
+
this.track.style.transform = 'translate3d(' + -this.index * 100 + '%,0,0)';
|
|
1150
|
+
}
|
|
1151
|
+
},
|
|
1152
|
+
|
|
1153
|
+
/* Keyboard ------------------------------------------------------ */
|
|
1154
|
+
|
|
1155
|
+
onKey( event ) {
|
|
1156
|
+
if ( ! this.isOpen ) {
|
|
1157
|
+
return;
|
|
1158
|
+
}
|
|
1159
|
+
// Escape and the focus trap always work; `keyboard` covers navigation.
|
|
1160
|
+
if ( event.key === 'Escape' ) {
|
|
1161
|
+
event.preventDefault();
|
|
1162
|
+
this.close();
|
|
1163
|
+
return;
|
|
1164
|
+
}
|
|
1165
|
+
if ( event.key === 'Tab' ) {
|
|
1166
|
+
this.trapFocus( event );
|
|
1167
|
+
return;
|
|
1168
|
+
}
|
|
1169
|
+
// Leave arrow, Home and End keys to a focused video (seeking), and
|
|
1170
|
+
// browser shortcuts such as Alt+Left (Back) to the browser.
|
|
1171
|
+
if ( ! this.options.keyboard || event.target.closest( 'video' ) || event.altKey || event.ctrlKey || event.metaKey ) {
|
|
1172
|
+
return;
|
|
1173
|
+
}
|
|
1174
|
+
// While zoomed, the arrow keys look around the image (as dragging does).
|
|
1175
|
+
const pan = { ArrowLeft: [ 1, 0 ], ArrowRight: [ -1, 0 ], ArrowUp: [ 0, 1 ], ArrowDown: [ 0, -1 ] }[ event.key ];
|
|
1176
|
+
if ( pan && this.zoomState.scale > 1 ) {
|
|
1177
|
+
event.preventDefault();
|
|
1178
|
+
this.zoomState.x += pan[ 0 ] * 80;
|
|
1179
|
+
this.zoomState.y += pan[ 1 ] * 80;
|
|
1180
|
+
this.clampZoom();
|
|
1181
|
+
this.applyZoom( true );
|
|
1182
|
+
return;
|
|
1183
|
+
}
|
|
1184
|
+
switch ( event.key ) {
|
|
1185
|
+
case 'ArrowLeft':
|
|
1186
|
+
event.preventDefault();
|
|
1187
|
+
this.prev();
|
|
1188
|
+
break;
|
|
1189
|
+
case 'ArrowRight':
|
|
1190
|
+
event.preventDefault();
|
|
1191
|
+
this.next();
|
|
1192
|
+
break;
|
|
1193
|
+
case 'Home':
|
|
1194
|
+
event.preventDefault();
|
|
1195
|
+
this.goTo( 0 );
|
|
1196
|
+
break;
|
|
1197
|
+
case 'End':
|
|
1198
|
+
event.preventDefault();
|
|
1199
|
+
this.goTo( this.items.length - 1 );
|
|
1200
|
+
break;
|
|
1201
|
+
}
|
|
1202
|
+
},
|
|
1203
|
+
|
|
1204
|
+
trapFocus( event ) {
|
|
1205
|
+
const focusable = Array.prototype.filter.call(
|
|
1206
|
+
this.root.querySelectorAll( 'button, a[href], video, iframe, [tabindex]:not([tabindex="-1"])' ),
|
|
1207
|
+
function ( node ) {
|
|
1208
|
+
return ! node.disabled && ! node.hidden && node.offsetParent !== null && ! node.closest( '[aria-hidden="true"]' );
|
|
1209
|
+
}
|
|
1210
|
+
);
|
|
1211
|
+
if ( ! focusable.length ) {
|
|
1212
|
+
event.preventDefault();
|
|
1213
|
+
return;
|
|
1214
|
+
}
|
|
1215
|
+
const first = focusable[ 0 ];
|
|
1216
|
+
const last = focusable[ focusable.length - 1 ];
|
|
1217
|
+
if ( event.shiftKey && ( document.activeElement === first || ! this.root.contains( document.activeElement ) ) ) {
|
|
1218
|
+
event.preventDefault();
|
|
1219
|
+
last.focus();
|
|
1220
|
+
} else if ( ! event.shiftKey && ( document.activeElement === last || ! this.root.contains( document.activeElement ) ) ) {
|
|
1221
|
+
event.preventDefault();
|
|
1222
|
+
first.focus();
|
|
1223
|
+
}
|
|
1224
|
+
},
|
|
1225
|
+
|
|
1226
|
+
/* Slideshow ----------------------------------------------------- */
|
|
1227
|
+
|
|
1228
|
+
play() {
|
|
1229
|
+
if ( this.timer || this.items.length < 2 ) {
|
|
1230
|
+
return this;
|
|
1231
|
+
}
|
|
1232
|
+
// Without loop the slideshow would stop at once on the last slide;
|
|
1233
|
+
// start again from the first instead.
|
|
1234
|
+
if ( ! this.options.loop && this.index >= this.items.length - 1 ) {
|
|
1235
|
+
this.goTo( 0 );
|
|
1236
|
+
}
|
|
1237
|
+
const self = this;
|
|
1238
|
+
this.root.classList.add( 'is-playing' );
|
|
1239
|
+
this.setPlayButton( true );
|
|
1240
|
+
this.timer = window.setInterval( function () {
|
|
1241
|
+
if ( ! self.options.loop && self.index >= self.items.length - 1 ) {
|
|
1242
|
+
self.pause();
|
|
1243
|
+
return;
|
|
1244
|
+
}
|
|
1245
|
+
self.goTo( self.index + 1 );
|
|
1246
|
+
}, this.options.slideshowInterval );
|
|
1247
|
+
this.restartProgress();
|
|
1248
|
+
return this;
|
|
1249
|
+
},
|
|
1250
|
+
|
|
1251
|
+
pause() {
|
|
1252
|
+
if ( ! this.timer ) {
|
|
1253
|
+
return this;
|
|
1254
|
+
}
|
|
1255
|
+
window.clearInterval( this.timer );
|
|
1256
|
+
this.timer = null;
|
|
1257
|
+
if ( this.root ) {
|
|
1258
|
+
this.root.classList.remove( 'is-playing' );
|
|
1259
|
+
this.progress.firstChild.style.transition = 'none';
|
|
1260
|
+
this.progress.firstChild.style.transform = 'scaleX(0)';
|
|
1261
|
+
}
|
|
1262
|
+
this.setPlayButton( false );
|
|
1263
|
+
return this;
|
|
1264
|
+
},
|
|
1265
|
+
|
|
1266
|
+
setPlayButton( playing ) {
|
|
1267
|
+
const btn = this.buttons && this.buttons.play;
|
|
1268
|
+
if ( ! btn ) {
|
|
1269
|
+
return;
|
|
1270
|
+
}
|
|
1271
|
+
const label = playing ? this.options.labels.pause : this.options.labels.play;
|
|
1272
|
+
btn.innerHTML = icon( playing ? 'pause' : 'play' );
|
|
1273
|
+
btn.setAttribute( 'aria-label', label );
|
|
1274
|
+
btn.title = label;
|
|
1275
|
+
btn.setAttribute( 'aria-pressed', playing ? 'true' : 'false' );
|
|
1276
|
+
},
|
|
1277
|
+
|
|
1278
|
+
restartProgress() {
|
|
1279
|
+
const bar = this.progress.firstChild;
|
|
1280
|
+
bar.style.transition = 'none';
|
|
1281
|
+
bar.style.transform = 'scaleX(0)';
|
|
1282
|
+
void bar.offsetWidth;
|
|
1283
|
+
bar.style.transition = 'transform ' + this.options.slideshowInterval + 'ms linear';
|
|
1284
|
+
bar.style.transform = 'scaleX(1)';
|
|
1285
|
+
},
|
|
1286
|
+
|
|
1287
|
+
/* Fullscreen ---------------------------------------------------- */
|
|
1288
|
+
|
|
1289
|
+
isFullscreen() {
|
|
1290
|
+
return ( document.fullscreenElement || document.webkitFullscreenElement ) === this.root;
|
|
1291
|
+
},
|
|
1292
|
+
|
|
1293
|
+
toggleFullscreen() {
|
|
1294
|
+
if ( this.isFullscreen() ) {
|
|
1295
|
+
( document.exitFullscreen || document.webkitExitFullscreen ).call( document );
|
|
1296
|
+
} else {
|
|
1297
|
+
const request = this.root.requestFullscreen || this.root.webkitRequestFullscreen;
|
|
1298
|
+
if ( request ) {
|
|
1299
|
+
const result = request.call( this.root );
|
|
1300
|
+
if ( result && result.catch ) {
|
|
1301
|
+
result.catch( function () {} );
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
},
|
|
1306
|
+
|
|
1307
|
+
onFullscreenChange() {
|
|
1308
|
+
const full = this.isFullscreen();
|
|
1309
|
+
this.root.classList.toggle( 'is-fullscreen', full );
|
|
1310
|
+
const btn = this.buttons.fullscreen;
|
|
1311
|
+
if ( btn ) {
|
|
1312
|
+
const label = full ? this.options.labels.exitFullscreen : this.options.labels.fullscreen;
|
|
1313
|
+
btn.innerHTML = icon( full ? 'exitFullscreen' : 'fullscreen' );
|
|
1314
|
+
btn.setAttribute( 'aria-label', label );
|
|
1315
|
+
btn.title = label;
|
|
1316
|
+
}
|
|
1317
|
+
this.resetZoom();
|
|
1318
|
+
},
|
|
1319
|
+
|
|
1320
|
+
/* Scroll lock --------------------------------------------------- */
|
|
1321
|
+
|
|
1322
|
+
lockScroll() {
|
|
1323
|
+
const html = document.documentElement;
|
|
1324
|
+
const gap = window.innerWidth - html.clientWidth;
|
|
1325
|
+
this.savedScroll = { overflow: html.style.overflow, paddingRight: html.style.paddingRight };
|
|
1326
|
+
html.style.overflow = 'hidden';
|
|
1327
|
+
if ( gap > 0 ) {
|
|
1328
|
+
html.style.paddingRight = gap + 'px';
|
|
1329
|
+
}
|
|
1330
|
+
},
|
|
1331
|
+
|
|
1332
|
+
unlockScroll() {
|
|
1333
|
+
const html = document.documentElement;
|
|
1334
|
+
if ( this.savedScroll ) {
|
|
1335
|
+
html.style.overflow = this.savedScroll.overflow;
|
|
1336
|
+
html.style.paddingRight = this.savedScroll.paddingRight;
|
|
1337
|
+
}
|
|
1338
|
+
},
|
|
1339
|
+
|
|
1340
|
+
/* Close --------------------------------------------------------- */
|
|
1341
|
+
|
|
1342
|
+
/**
|
|
1343
|
+
* Close the gallery.
|
|
1344
|
+
*
|
|
1345
|
+
* @param {boolean} immediate Skip the closing animation.
|
|
1346
|
+
*/
|
|
1347
|
+
close( immediate ) {
|
|
1348
|
+
if ( ! this.isOpen ) {
|
|
1349
|
+
return this;
|
|
1350
|
+
}
|
|
1351
|
+
this.isOpen = false;
|
|
1352
|
+
this.pause();
|
|
1353
|
+
if ( this.isFullscreen() ) {
|
|
1354
|
+
( document.exitFullscreen || document.webkitExitFullscreen ).call( document );
|
|
1355
|
+
}
|
|
1356
|
+
this.leaveSlide( this.index );
|
|
1357
|
+
this.unbind();
|
|
1358
|
+
this.unlockScroll();
|
|
1359
|
+
if ( openInstance === this ) {
|
|
1360
|
+
openInstance = null;
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
const root = this.root;
|
|
1364
|
+
const done = function () {
|
|
1365
|
+
if ( root.parentNode ) {
|
|
1366
|
+
root.parentNode.removeChild( root );
|
|
1367
|
+
}
|
|
1368
|
+
};
|
|
1369
|
+
root.classList.remove( 'is-open' );
|
|
1370
|
+
root.classList.add( 'is-closing' );
|
|
1371
|
+
if ( immediate || reducedMotion() ) {
|
|
1372
|
+
done();
|
|
1373
|
+
} else {
|
|
1374
|
+
window.setTimeout( done, this.options.speed );
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
if ( this.lastFocus && typeof this.lastFocus.focus === 'function' ) {
|
|
1378
|
+
this.lastFocus.focus( { preventScroll: true } );
|
|
1379
|
+
}
|
|
1380
|
+
this.emit( 'onClose' );
|
|
1381
|
+
return this;
|
|
1382
|
+
},
|
|
1383
|
+
|
|
1384
|
+
destroy() {
|
|
1385
|
+
return this.close( true );
|
|
1386
|
+
},
|
|
1387
|
+
|
|
1388
|
+
emit( name ) {
|
|
1389
|
+
const fn = this.options[ name ];
|
|
1390
|
+
if ( typeof fn === 'function' ) {
|
|
1391
|
+
fn.call( this, this.index, this.items[ this.index ], this );
|
|
1392
|
+
}
|
|
1393
|
+
},
|
|
1394
|
+
};
|
|
1395
|
+
|
|
1396
|
+
/* ------------------------------------------------------------------ */
|
|
1397
|
+
/* Filterable grid */
|
|
1398
|
+
/* ------------------------------------------------------------------ */
|
|
1399
|
+
|
|
1400
|
+
/**
|
|
1401
|
+
* Animate a grid between filters. Items that stay glide to their new
|
|
1402
|
+
* place, new ones scale in, removed ones scale out (FLIP technique).
|
|
1403
|
+
*
|
|
1404
|
+
* @param {Element} grid Grid container.
|
|
1405
|
+
* @param {Object} options { itemSelector, duration, easing }.
|
|
1406
|
+
*/
|
|
1407
|
+
function Grid( grid, options ) {
|
|
1408
|
+
this.grid = grid;
|
|
1409
|
+
this.options = Object.assign( { itemSelector: ':scope > *', duration: 400, easing: 'ease' }, options || {} );
|
|
1410
|
+
if ( window.getComputedStyle( grid ).position === 'static' ) {
|
|
1411
|
+
grid.style.position = 'relative';
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
Grid.prototype = {
|
|
1416
|
+
items() {
|
|
1417
|
+
return Array.prototype.filter.call( this.grid.querySelectorAll( this.options.itemSelector ), function ( item ) {
|
|
1418
|
+
return ! item.classList.contains( 'foldbox-ghost' );
|
|
1419
|
+
} );
|
|
1420
|
+
},
|
|
1421
|
+
|
|
1422
|
+
/**
|
|
1423
|
+
* Show only the items that match.
|
|
1424
|
+
*
|
|
1425
|
+
* @param {string|Function} filter '*' for all, a CSS selector, or a function( item ).
|
|
1426
|
+
* @param {boolean} instant Skip the animation.
|
|
1427
|
+
*/
|
|
1428
|
+
filter( filter, instant ) {
|
|
1429
|
+
const matches = function ( item ) {
|
|
1430
|
+
if ( typeof filter === 'function' ) {
|
|
1431
|
+
return !! filter( item );
|
|
1432
|
+
}
|
|
1433
|
+
return ! filter || filter === '*' || item.matches( filter );
|
|
1434
|
+
};
|
|
1435
|
+
const items = this.items();
|
|
1436
|
+
const o = this.options;
|
|
1437
|
+
const stopAnimations = function () {
|
|
1438
|
+
items.forEach( function ( item ) {
|
|
1439
|
+
if ( typeof item.getAnimations === 'function' ) {
|
|
1440
|
+
item.getAnimations().forEach( function ( animation ) {
|
|
1441
|
+
animation.cancel();
|
|
1442
|
+
} );
|
|
1443
|
+
}
|
|
1444
|
+
} );
|
|
1445
|
+
};
|
|
1446
|
+
|
|
1447
|
+
// Drop ghosts still fading out from an earlier filter, so a card
|
|
1448
|
+
// shown again isn't on screen twice.
|
|
1449
|
+
Array.prototype.forEach.call( this.grid.querySelectorAll( ':scope > .foldbox-ghost' ), function ( ghost ) {
|
|
1450
|
+
ghost.remove();
|
|
1451
|
+
} );
|
|
1452
|
+
|
|
1453
|
+
if ( instant || reducedMotion() || typeof this.grid.animate !== 'function' ) {
|
|
1454
|
+
stopAnimations();
|
|
1455
|
+
items.forEach( function ( item ) {
|
|
1456
|
+
item.hidden = ! matches( item );
|
|
1457
|
+
} );
|
|
1458
|
+
return this;
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
const grid = this.grid;
|
|
1462
|
+
const gridRect = grid.getBoundingClientRect();
|
|
1463
|
+
const before = new Map();
|
|
1464
|
+
items.forEach( function ( item ) {
|
|
1465
|
+
if ( ! item.hidden ) {
|
|
1466
|
+
before.set( item, item.getBoundingClientRect() );
|
|
1467
|
+
}
|
|
1468
|
+
} );
|
|
1469
|
+
// Positions above are where cards are on screen now. Stop animations
|
|
1470
|
+
// from an earlier filter so the new layout is measured without them.
|
|
1471
|
+
stopAnimations();
|
|
1472
|
+
|
|
1473
|
+
const ghosts = [];
|
|
1474
|
+
items.forEach( function ( item ) {
|
|
1475
|
+
if ( item.hidden || matches( item ) ) {
|
|
1476
|
+
return;
|
|
1477
|
+
}
|
|
1478
|
+
const rect = before.get( item );
|
|
1479
|
+
const ghost = item.cloneNode( true );
|
|
1480
|
+
ghost.setAttribute( 'aria-hidden', 'true' );
|
|
1481
|
+
ghost.inert = true;
|
|
1482
|
+
ghost.classList.add( 'foldbox-ghost' );
|
|
1483
|
+
ghost.style.position = 'absolute';
|
|
1484
|
+
ghost.style.margin = '0';
|
|
1485
|
+
ghost.style.pointerEvents = 'none';
|
|
1486
|
+
ghost.style.left = rect.left - gridRect.left - grid.clientLeft + 'px';
|
|
1487
|
+
ghost.style.top = rect.top - gridRect.top - grid.clientTop + 'px';
|
|
1488
|
+
ghost.style.width = rect.width + 'px';
|
|
1489
|
+
ghosts.push( ghost );
|
|
1490
|
+
} );
|
|
1491
|
+
|
|
1492
|
+
items.forEach( function ( item ) {
|
|
1493
|
+
item.hidden = ! matches( item );
|
|
1494
|
+
} );
|
|
1495
|
+
|
|
1496
|
+
// Read every new position before appending ghosts or starting any
|
|
1497
|
+
// animation: each animate() call invalidates style, so reading in
|
|
1498
|
+
// between would force a layout per card.
|
|
1499
|
+
const after = new Map();
|
|
1500
|
+
items.forEach( function ( item ) {
|
|
1501
|
+
if ( ! item.hidden ) {
|
|
1502
|
+
after.set( item, item.getBoundingClientRect() );
|
|
1503
|
+
}
|
|
1504
|
+
} );
|
|
1505
|
+
|
|
1506
|
+
ghosts.forEach( function ( ghost ) {
|
|
1507
|
+
grid.appendChild( ghost );
|
|
1508
|
+
ghost
|
|
1509
|
+
.animate(
|
|
1510
|
+
[
|
|
1511
|
+
{ opacity: 1, transform: 'scale(1)' },
|
|
1512
|
+
{ opacity: 0, transform: 'scale(0.6)' },
|
|
1513
|
+
],
|
|
1514
|
+
{ duration: o.duration, easing: 'ease-in' }
|
|
1515
|
+
)
|
|
1516
|
+
.finished.then( function () {
|
|
1517
|
+
ghost.remove();
|
|
1518
|
+
} );
|
|
1519
|
+
} );
|
|
1520
|
+
|
|
1521
|
+
items.forEach( function ( item ) {
|
|
1522
|
+
if ( item.hidden ) {
|
|
1523
|
+
return;
|
|
1524
|
+
}
|
|
1525
|
+
const old = before.get( item );
|
|
1526
|
+
if ( ! old ) {
|
|
1527
|
+
item.animate(
|
|
1528
|
+
[
|
|
1529
|
+
{ opacity: 0, transform: 'scale(0.6)' },
|
|
1530
|
+
{ opacity: 1, transform: 'scale(1)' },
|
|
1531
|
+
],
|
|
1532
|
+
{ duration: o.duration, easing: 'ease-out' }
|
|
1533
|
+
);
|
|
1534
|
+
return;
|
|
1535
|
+
}
|
|
1536
|
+
const now = after.get( item );
|
|
1537
|
+
const dx = old.left - now.left;
|
|
1538
|
+
const dy = old.top - now.top;
|
|
1539
|
+
if ( dx || dy ) {
|
|
1540
|
+
item.animate( [ { transform: 'translate(' + dx + 'px, ' + dy + 'px)' }, { transform: 'none' } ], {
|
|
1541
|
+
duration: o.duration,
|
|
1542
|
+
easing: o.easing,
|
|
1543
|
+
} );
|
|
1544
|
+
}
|
|
1545
|
+
} );
|
|
1546
|
+
return this;
|
|
1547
|
+
},
|
|
1548
|
+
};
|
|
1549
|
+
|
|
1550
|
+
/* ------------------------------------------------------------------ */
|
|
1551
|
+
/* Public API */
|
|
1552
|
+
/* ------------------------------------------------------------------ */
|
|
1553
|
+
|
|
1554
|
+
/**
|
|
1555
|
+
* Bind links inside a container: each <a href> opens the gallery of all of
|
|
1556
|
+
* them. Optional data attributes: data-thumb, data-caption, data-type,
|
|
1557
|
+
* data-poster, data-download.
|
|
1558
|
+
*
|
|
1559
|
+
* @param {Element|string} container Element or selector.
|
|
1560
|
+
* @param {Object} options Lightbox options plus { selector }.
|
|
1561
|
+
* @return {Function} Call to unbind.
|
|
1562
|
+
*/
|
|
1563
|
+
function bind( container, options ) {
|
|
1564
|
+
const rootEl = typeof container === 'string' ? document.querySelector( container ) : container;
|
|
1565
|
+
if ( ! rootEl ) {
|
|
1566
|
+
return function () {};
|
|
1567
|
+
}
|
|
1568
|
+
// Captions come from page markup here, so treat them as text unless asked.
|
|
1569
|
+
const opts = Object.assign( { selector: 'a[href]', htmlCaptions: false }, options || {} );
|
|
1570
|
+
const handler = function ( event ) {
|
|
1571
|
+
// Leave new-tab/window clicks (Ctrl, Cmd, Shift, middle button) to the browser.
|
|
1572
|
+
if ( event.defaultPrevented || event.button !== 0 || event.ctrlKey || event.metaKey || event.shiftKey || event.altKey ) {
|
|
1573
|
+
return;
|
|
1574
|
+
}
|
|
1575
|
+
const link = event.target.closest( opts.selector );
|
|
1576
|
+
if ( ! link || ! rootEl.contains( link ) ) {
|
|
1577
|
+
return;
|
|
1578
|
+
}
|
|
1579
|
+
event.preventDefault();
|
|
1580
|
+
const links = Array.prototype.slice.call( rootEl.querySelectorAll( opts.selector ) );
|
|
1581
|
+
const items = links.map( function ( a ) {
|
|
1582
|
+
const img = a.querySelector( 'img' );
|
|
1583
|
+
return {
|
|
1584
|
+
src: a.getAttribute( 'href' ),
|
|
1585
|
+
thumb: a.getAttribute( 'data-thumb' ) || ( img ? img.currentSrc || img.src : '' ),
|
|
1586
|
+
alt: img ? img.alt : '',
|
|
1587
|
+
caption: a.getAttribute( 'data-caption' ) || '',
|
|
1588
|
+
type: a.getAttribute( 'data-type' ) || undefined,
|
|
1589
|
+
poster: a.getAttribute( 'data-poster' ) || undefined,
|
|
1590
|
+
download: a.getAttribute( 'data-download' ) === 'false' ? false : a.hasAttribute( 'data-download' ) ? a.getAttribute( 'data-download' ) : undefined,
|
|
1591
|
+
};
|
|
1592
|
+
} );
|
|
1593
|
+
open(
|
|
1594
|
+
items,
|
|
1595
|
+
Object.assign( {}, opts, {
|
|
1596
|
+
index: links.indexOf( link ),
|
|
1597
|
+
origin: link.querySelector( 'img' ) || link,
|
|
1598
|
+
} )
|
|
1599
|
+
);
|
|
1600
|
+
};
|
|
1601
|
+
rootEl.addEventListener( 'click', handler );
|
|
1602
|
+
return function () {
|
|
1603
|
+
rootEl.removeEventListener( 'click', handler );
|
|
1604
|
+
};
|
|
1605
|
+
}
|
|
1606
|
+
|
|
1607
|
+
function open( items, options ) {
|
|
1608
|
+
return new Lightbox( items, options ).open();
|
|
1609
|
+
}
|
|
1610
|
+
|
|
1611
|
+
return {
|
|
1612
|
+
version: VERSION,
|
|
1613
|
+
defaults: DEFAULTS,
|
|
1614
|
+
open,
|
|
1615
|
+
bind,
|
|
1616
|
+
grid( element, options ) {
|
|
1617
|
+
return new Grid( element, options );
|
|
1618
|
+
},
|
|
1619
|
+
close() {
|
|
1620
|
+
if ( openInstance ) {
|
|
1621
|
+
openInstance.close();
|
|
1622
|
+
}
|
|
1623
|
+
},
|
|
1624
|
+
Lightbox,
|
|
1625
|
+
Grid,
|
|
1626
|
+
};
|
|
1627
|
+
} );
|