init
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
define(['jquery'], function ($) {
|
||||
'use strict';
|
||||
|
||||
var $modal;
|
||||
|
||||
function ensureModal() {
|
||||
if ($modal) {
|
||||
return $modal;
|
||||
}
|
||||
|
||||
$modal = $('<div/>', {'class': 'esun-review-lightbox', hidden: true})
|
||||
.append($('<button/>', {'class': 'esun-review-lightbox__close', type: 'button', text: '×'}))
|
||||
.append($('<button/>', {'class': 'esun-review-lightbox__prev', type: 'button', text: '‹'}))
|
||||
.append($('<img/>', {'class': 'esun-review-lightbox__image', alt: ''}))
|
||||
.append($('<video/>', {
|
||||
'class': 'esun-review-lightbox__video',
|
||||
controls: true,
|
||||
hidden: true,
|
||||
playsinline: true,
|
||||
preload: 'metadata'
|
||||
}))
|
||||
.append($('<button/>', {'class': 'esun-review-lightbox__next', type: 'button', text: '›'}))
|
||||
.appendTo(document.body);
|
||||
|
||||
$modal.on('click', '.esun-review-lightbox__close', close);
|
||||
$modal.on('click', '.esun-review-lightbox__prev', function () {
|
||||
show(($modal.data('index') || 0) - 1);
|
||||
});
|
||||
$modal.on('click', '.esun-review-lightbox__next', function () {
|
||||
show(($modal.data('index') || 0) + 1);
|
||||
});
|
||||
|
||||
return $modal;
|
||||
}
|
||||
|
||||
function stopVideo() {
|
||||
var video = $modal && $modal.find('.esun-review-lightbox__video').get(0);
|
||||
|
||||
if (!video) {
|
||||
return;
|
||||
}
|
||||
video.pause();
|
||||
video.removeAttribute('src');
|
||||
video.load();
|
||||
}
|
||||
|
||||
function show(index) {
|
||||
var media = $modal.data('media') || [],
|
||||
item,
|
||||
video;
|
||||
|
||||
if (!media.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
index = (index + media.length) % media.length;
|
||||
$modal.data('index', index);
|
||||
item = media[index];
|
||||
|
||||
if (item.type === 'video') {
|
||||
$modal.find('.esun-review-lightbox__image').attr('hidden', true).removeAttr('src');
|
||||
video = $modal.find('.esun-review-lightbox__video')
|
||||
.removeAttr('hidden')
|
||||
.attr('src', item.url)
|
||||
.get(0);
|
||||
video.load();
|
||||
video.play().catch(function () {});
|
||||
return;
|
||||
}
|
||||
|
||||
stopVideo();
|
||||
$modal.find('.esun-review-lightbox__video').attr('hidden', true);
|
||||
$modal.find('.esun-review-lightbox__image').removeAttr('hidden').attr('src', item.url);
|
||||
}
|
||||
|
||||
function close() {
|
||||
stopVideo();
|
||||
$modal.attr('hidden', true);
|
||||
}
|
||||
|
||||
function getMedia($button) {
|
||||
var media = $button.data('media'),
|
||||
images;
|
||||
|
||||
if (Array.isArray(media)) {
|
||||
return media;
|
||||
}
|
||||
images = $button.data('images') || [];
|
||||
return images.map(function (url) {
|
||||
return {type: 'image', url: url, thumb: url};
|
||||
});
|
||||
}
|
||||
|
||||
return function (config, element) {
|
||||
$(element).on('click', '.esun-review-images__thumb', function () {
|
||||
var $button = $(this),
|
||||
media = getMedia($button);
|
||||
|
||||
ensureModal();
|
||||
$modal.data('media', media).attr('hidden', false);
|
||||
show(parseInt($button.data('index'), 10) || 0);
|
||||
});
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user