forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgallery.js
510 lines (434 loc) · 18.9 KB
/
gallery.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
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
define([
'jquery',
'fotorama/fotorama',
'underscore',
'matchMedia',
'mage/template',
'text!mage/gallery/gallery.html',
'uiClass',
'mage/translate'
], function ($, fotorama, _, mediaCheck, template, galleryTpl, Class, $t) {
'use strict';
/**
* Retrieves index if the main item.
* @param {Array.<Object>} data - Set of gallery items.
*/
var getMainImageIndex = function (data) {
var mainIndex;
if (_.every(data, function (item) {
return _.isObject(item);
})
) {
mainIndex = _.findIndex(data, function (item) {
return item.isMain;
});
}
return mainIndex > 0 ? mainIndex : 0;
},
/**
* Helper for parse translate property
*
* @param {Element} el - el that to parse
* @returns {Array} - array of properties.
*/
getTranslate = function (el) {
var slideTransform = $(el).attr('style').split(';');
slideTransform = $.map(slideTransform, function (style) {
style = style.trim();
if (style.startsWith('transform: translate3d')) {
return style.match(/transform: translate3d\((.+)px,(.+)px,(.+)px\)/);
}
return false;
});
return slideTransform.filter(Boolean);
},
_toNumber = function (str) {
var type = typeof(str);
if (type === 'string') {
return parseInt(str);
} else {
return str;
}
};
return Class.extend({
defaults: {
settings: {},
config: {},
startConfig: {}
},
/**
* Checks if device has touch interface.
* @return {Boolean} The result of searching touch events on device.
*/
isTouchEnabled: (function () {
return 'ontouchstart' in document.documentElement;
})(),
/**
* Initializes gallery.
* @param {Object} config - Gallery configuration.
* @param {String} element - String selector of gallery DOM element.
*/
initialize: function (config, element) {
var self = this;
this._super();
_.bindAll(this,
'_focusSwitcher'
);
/*turn off arrows for touch devices*/
if (this.isTouchEnabled) {
config.options.arrows = false;
if (config.fullscreen) {
config.fullscreen.arrows = false;
}
}
config.options.width = _toNumber(config.options.width);
config.options.height = _toNumber(config.options.height);
config.options.thumbwidth = _toNumber(config.options.thumbwidth);
config.options.thumbheight = _toNumber(config.options.thumbheight);
config.options.swipe = true;
this.config = config;
this.settings = {
$element: $(element),
$pageWrapper: $('body>.page-wrapper'),
currentConfig: config,
defaultConfig: _.clone(config),
fullscreenConfig: _.clone(config.fullscreen),
breakpoints: config.breakpoints,
activeBreakpoint: {},
fotoramaApi: null,
isFullscreen: false,
api: null,
data: _.clone(config.data)
};
config.options.ratio = config.options.width / config.options.height;
config.options.height = null;
$.extend(true, this.startConfig, config);
this.initGallery();
this.initApi();
this.setupBreakpoints();
this.initFullscreenSettings();
this.settings.$element.on('mouseup', '.fotorama__stage__frame', function () {
if (
!$(this).parents('.fotorama__shadows--left, .fotorama__shadows--right').length &&
!$(this).hasClass('fotorama-video-container')
) {
self.openFullScreen();
}
});
if (this.isTouchEnabled) {
this.settings.$element.on('tap', '.fotorama__stage__frame', function () {
var translate = getTranslate($(this).parents('.fotorama__stage__shaft'));
if (translate[1] === '0' && !$(this).hasClass('fotorama-video-container')) {
self.openFullScreen();
self.settings.$pageWrapper.hide();
}
});
}
},
/**
* Open gallery fullscreen
*/
openFullScreen: function () {
this.settings.api.fotorama.requestFullScreen();
this.settings.$fullscreenIcon.css({
opacity: 1,
visibility: 'visible',
display: 'block'
});
},
/**
* Gallery fullscreen settings.
*/
initFullscreenSettings: function () {
var settings = this.settings,
self = this;
settings.$gallery = this.settings.$element.find('[data-gallery-role="gallery"]');
settings.$fullscreenIcon = this.settings.$element.find('[data-gallery-role="fotorama__fullscreen-icon"]');
settings.focusableStart = this.settings.$element.find('[data-gallery-role="fotorama__focusable-start"]');
settings.focusableEnd = this.settings.$element.find('[data-gallery-role="fotorama__focusable-end"]');
settings.closeIcon = this.settings.$element.find('[data-gallery-role="fotorama__fullscreen-icon"]');
settings.fullscreenConfig.swipe = true;
settings.$gallery.on('fotorama:fullscreenenter', function () {
settings.closeIcon.show();
settings.focusableStart.attr('tabindex', '0');
settings.focusableEnd.attr('tabindex', '0');
settings.focusableStart.bind('focusin', self._focusSwitcher);
settings.focusableEnd.bind('focusin', self._focusSwitcher);
settings.api.updateOptions(settings.defaultConfig.options, true);
settings.api.updateOptions(settings.fullscreenConfig, true);
if (!_.isEqual(settings.activeBreakpoint, {}) && settings.breakpoints) {
settings.api.updateOptions(settings.activeBreakpoint.options, true);
}
settings.isFullscreen = true;
});
settings.$gallery.on('fotorama:fullscreenexit', function () {
settings.closeIcon.hide();
settings.focusableStart.attr('tabindex', '-1');
settings.focusableEnd.attr('tabindex', '-1');
settings.api.updateOptions(settings.defaultConfig.options, true);
settings.focusableStart.unbind('focusin', this._focusSwitcher);
settings.focusableEnd.unbind('focusin', this._focusSwitcher);
settings.closeIcon.hide();
if (!_.isEqual(settings.activeBreakpoint, {}) && settings.breakpoints) {
settings.api.updateOptions(settings.activeBreakpoint.options, true);
}
settings.isFullscreen = false;
settings.$element.data('gallery').updateOptions({
swipe: true
});
});
},
/**
* Switcher focus.
*/
_focusSwitcher: function (e) {
var target = $(e.target),
settings = this.settings;
if (target.is(settings.focusableStart)) {
this._setFocus('start');
} else if (target.is(settings.focusableEnd)) {
this._setFocus('end');
}
},
/**
* Set focus to element.
* @param {String} position - can be "start" and "end"
* positions.
* If position is "end" - sets focus to first
* focusable element in modal window scope.
* If position is "start" - sets focus to last
* focusable element in modal window scope
*/
_setFocus: function (position) {
var settings = this.settings,
focusableElements,
infelicity;
if (position === 'end') {
settings.$gallery.find(settings.closeIcon).focus();
} else if (position === 'start') {
infelicity = 3; //Constant for find last focusable element
focusableElements = settings.$gallery.find(':focusable');
focusableElements.eq(focusableElements.length - infelicity).focus();
}
},
/**
* Initializes gallery with configuration options.
*/
initGallery: function () {
var breakpoints = {},
settings = this.settings,
config = this.config,
tpl = template(galleryTpl, {
next: $t('Next'),
previous: $t('Previous')
}),
mainImageIndex;
if (settings.breakpoints) {
_.each(_.values(settings.breakpoints), function (breakpoint) {
var conditions;
_.each(_.pairs(breakpoint.conditions), function (pair) {
conditions = conditions ? conditions + ' and (' + pair[0] + ': ' + pair[1] + ')' :
'(' + pair[0] + ': ' + pair[1] + ')';
});
breakpoints[conditions] = breakpoint.options;
});
settings.breakpoints = breakpoints;
}
_.extend(config, config.options);
config.options = undefined;
config.click = false;
config.breakpoints = null;
settings.currentConfig = config;
settings.$element.html(tpl);
settings.$element.removeClass('_block-content-loading');
settings.$elementF = $(settings.$element.children()[0]);
settings.$elementF.fotorama(config);
settings.fotoramaApi = settings.$elementF.data('fotorama');
$.extend(true, config, this.startConfig);
mainImageIndex = getMainImageIndex(config.data);
if (mainImageIndex) {
this.settings.fotoramaApi.show(mainImageIndex);
}
},
/**
* Creates breakpoints for gallery.
*/
setupBreakpoints: function () {
var pairs,
settings = this.settings,
config = this.config,
startConfig = this.startConfig,
triggeredBreakpoints = 0,
isTouchEnabled = this.isTouchEnabled;
if (_.isObject(settings.breakpoints)) {
pairs = _.pairs(settings.breakpoints);
_.each(pairs, function (pair) {
mediaCheck({
media: pair[0],
/**
* Is triggered when breakpoint enties.
*/
entry: function () {
$.extend(true, config, _.clone(startConfig));
settings.api.updateOptions(settings.defaultConfig.options, true);
if (settings.isFullscreen) {
settings.api.updateOptions(settings.fullscreenConfig, true);
}
if (isTouchEnabled) {
settings.breakpoints[pair[0]].options.arrows = false;
if (settings.breakpoints[pair[0]].options.fullscreen) {
settings.breakpoints[pair[0]].options.fullscreen.arrows = false;
}
}
settings.api.updateOptions(settings.breakpoints[pair[0]].options, true);
$.extend(true, config, settings.breakpoints[pair[0]]);
settings.activeBreakpoint = settings.breakpoints[pair[0]];
},
/**
* Is triggered when breakpoint exits.
*/
exit: function () {
$.extend(true, config, _.clone(startConfig));
settings.api.updateOptions(settings.defaultConfig.options, true);
if (settings.isFullscreen) {
settings.api.updateOptions(settings.fullscreenConfig, true);
}
settings.activeBreakpoint = {};
}
});
});
}
},
/**
* Creates gallery's API.
*/
initApi: function () {
var settings = this.settings,
config = this.config,
api = {
/**
* Contains fotorama's API methods.
*/
fotorama: settings.fotoramaApi,
/**
* Displays the last image on preview.
*/
last: function () {
settings.fotoramaApi.show('>>');
},
/**
* Displays the first image on preview.
*/
first: function () {
settings.fotoramaApi.show('<<');
},
/**
* Displays previous element on preview.
*/
prev: function () {
settings.fotoramaApi.show('<');
},
/**
* Displays next element on preview.
*/
next: function () {
settings.fotoramaApi.show('>');
},
/**
* Displays image with appropriate count number on preview.
* @param {Number} index - Number of image that should be displayed.
*/
seek: function (index) {
if (_.isNumber(index) && index !== 0) {
if (index > 0) {
index -= 1;
}
settings.fotoramaApi.show(index);
}
},
/**
* Updates gallery with new set of options.
* @param {Object} configuration - Standart gallery configuration object.
* @param {Boolean} isInternal - Is this function called via breakpoints.
*/
updateOptions: function (configuration, isInternal) {
var $selectable =
$('a[href], area[href], input, select, textarea, button, iframe, object, embed, *[tabindex], *[contenteditable]')
.not('[tabindex=-1], [disabled], :hidden'),
fotorama = settings.fotoramaApi,
$focus = $(':focus'),
index;
if (_.isObject(configuration)) {
//Saves index of focus
$selectable.each(function (number) {
if ($(this).is($focus)) {
index = number;
}
});
if (this.isTouchEnabled) {
configuration.arrows = false;
}
configuration.click = false;
configuration.breakpoints = null;
if (!isInternal) {
!_.isEqual(settings.activeBreakpoint, {} && settings.brekpoints) ?
$.extend(true, settings.activeBreakpoint.options, configuration) :
settings.isFullscreen ?
$.extend(true, settings.fullscreenConfig, configuration) :
$.extend(true, settings.defaultConfig.options, configuration);
}
$.extend(true, settings.currentConfig.options, configuration);
settings.fotoramaApi.setOptions(settings.currentConfig.options);
if (_.isNumber(index)) {
$selectable.eq(index).focus();
}
}
},
/**
* Updates gallery with specific set of items.
* @param {Array.<Object>} data - Set of gallery items to update.
*/
updateData: function (data) {
if (_.isArray(data)) {
settings.fotoramaApi.load(data);
$.extend(false, settings, {
data: data,
defaultConfig: data
});
$.extend(false, config, {
data: data
});
}
},
/**
* Returns current images list
*
* @returns {Array}
*/
returnCurrentImages: function () {
var images = [];
_.each(this.fotorama.data, function (item) {
images.push(_.omit(item, '$navThumbFrame', '$navDotFrame', '$stageFrame', 'labelledby'));
});
return images;
},
/**
* Updates gallery data partially by index
* @param {Number} index - Index of image in data array to be updated.
* @param {Object} item - Standart gallery image object.
*
*/
updateDataByIndex: function(index, item){
settings.fotoramaApi.spliceByIndex(index, item);
}
};
settings.$element.data('gallery', api);
settings.api = settings.$element.data('gallery');
settings.$element.trigger('gallery:loaded');
}
});
});