glightbox
Version:
Pure Javascript lightbox
1,488 lines (1,475 loc) • 110 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.GLightbox = factory());
}(this, (function () { 'use strict';
function _classCallCheck(a, n) {
if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
}
function _defineProperties(e, r) {
for (var t = 0; t < r.length; t++) {
var o = r[t];
o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
}
}
function _createClass(e, r, t) {
return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", {
writable: !1
}), e;
}
function _toPrimitive(t, r) {
if ("object" != typeof t || !t) return t;
var e = t[Symbol.toPrimitive];
if (void 0 !== e) {
var i = e.call(t, r || "default");
if ("object" != typeof i) return i;
throw new TypeError("@@toPrimitive must return a primitive value.");
}
return ("string" === r ? String : Number)(t);
}
function _toPropertyKey(t) {
var i = _toPrimitive(t, "string");
return "symbol" == typeof i ? i : i + "";
}
function _typeof(o) {
"@babel/helpers - typeof";
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
return typeof o;
} : function (o) {
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
}, _typeof(o);
}
var uid = Date.now();
function extend() {
var extended = {};
var deep = true;
var i = 0;
var length = arguments.length;
if (Object.prototype.toString.call(arguments[0]) === '[object Boolean]') {
deep = arguments[0];
i++;
}
var merge = function merge(obj) {
for (var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
if (deep && Object.prototype.toString.call(obj[prop]) === '[object Object]') {
extended[prop] = extend(true, extended[prop], obj[prop]);
} else {
extended[prop] = obj[prop];
}
}
}
};
for (; i < length; i++) {
var obj = arguments[i];
merge(obj);
}
return extended;
}
function each(collection, callback) {
if (isNode(collection) || collection === window || collection === document) {
collection = [collection];
}
if (!isArrayLike(collection) && !isObject(collection)) {
collection = [collection];
}
if (size(collection) == 0) {
return;
}
if (isArrayLike(collection) && !isObject(collection)) {
var l = collection.length,
i = 0;
for (; i < l; i++) {
if (callback.call(collection[i], collection[i], i, collection) === false) {
break;
}
}
} else if (isObject(collection)) {
for (var key in collection) {
if (has(collection, key)) {
if (callback.call(collection[key], collection[key], key, collection) === false) {
break;
}
}
}
}
}
function getNodeEvents(node) {
var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
var fn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var cache = node[uid] = node[uid] || [];
var data = {
all: cache,
evt: null,
found: null
};
if (name && fn && size(cache) > 0) {
each(cache, function (cl, i) {
if (cl.eventName == name && cl.fn.toString() == fn.toString()) {
data.found = true;
data.evt = i;
return false;
}
});
}
return data;
}
function addEvent(eventName) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
onElement = _ref.onElement,
withCallback = _ref.withCallback,
_ref$avoidDuplicate = _ref.avoidDuplicate,
avoidDuplicate = _ref$avoidDuplicate === void 0 ? true : _ref$avoidDuplicate,
_ref$once = _ref.once,
once = _ref$once === void 0 ? false : _ref$once,
_ref$useCapture = _ref.useCapture,
useCapture = _ref$useCapture === void 0 ? false : _ref$useCapture;
var thisArg = arguments.length > 2 ? arguments[2] : undefined;
var element = onElement || [];
if (isString(element)) {
element = document.querySelectorAll(element);
}
function handler(event) {
if (isFunction(withCallback)) {
withCallback.call(thisArg, event, this);
}
if (once) {
handler.destroy();
}
}
handler.destroy = function () {
each(element, function (el) {
var events = getNodeEvents(el, eventName, handler);
if (events.found) {
events.all.splice(events.evt, 1);
}
if (el.removeEventListener) {
el.removeEventListener(eventName, handler, useCapture);
}
});
};
each(element, function (el) {
var events = getNodeEvents(el, eventName, handler);
if (el.addEventListener && avoidDuplicate && !events.found || !avoidDuplicate) {
el.addEventListener(eventName, handler, useCapture);
events.all.push({
eventName: eventName,
fn: handler
});
}
});
return handler;
}
function addClass(node, name) {
each(name.split(' '), function (cl) {
return node.classList.add(cl);
});
}
function removeClass(node, name) {
each(name.split(' '), function (cl) {
return node.classList.remove(cl);
});
}
function hasClass(node, name) {
return node.classList.contains(name);
}
function closest(elem, selector) {
while (elem !== document.body) {
elem = elem.parentElement;
if (!elem) {
return false;
}
var matches = typeof elem.matches == 'function' ? elem.matches(selector) : elem.msMatchesSelector(selector);
if (matches) {
return elem;
}
}
}
function animateElement(element) {
var animation = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var callback = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
if (!element || animation === '') {
return false;
}
if (animation === 'none') {
if (isFunction(callback)) {
callback();
}
return false;
}
var animationEnd = whichAnimationEvent();
var animationNames = animation.split(' ');
each(animationNames, function (name) {
addClass(element, 'g' + name);
});
addEvent(animationEnd, {
onElement: element,
avoidDuplicate: false,
once: true,
withCallback: function withCallback(event, target) {
each(animationNames, function (name) {
removeClass(target, 'g' + name);
});
if (isFunction(callback)) {
callback();
}
}
});
}
function cssTransform(node) {
var translate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
if (translate === '') {
node.style.webkitTransform = '';
node.style.MozTransform = '';
node.style.msTransform = '';
node.style.OTransform = '';
node.style.transform = '';
return false;
}
node.style.webkitTransform = translate;
node.style.MozTransform = translate;
node.style.msTransform = translate;
node.style.OTransform = translate;
node.style.transform = translate;
}
function show(element) {
element.style.display = 'block';
}
function hide(element) {
element.style.display = 'none';
}
function createHTML(htmlStr) {
var frag = document.createDocumentFragment(),
temp = document.createElement('div');
temp.innerHTML = htmlStr;
while (temp.firstChild) {
frag.appendChild(temp.firstChild);
}
return frag;
}
function windowSize() {
return {
width: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
height: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
};
}
function whichAnimationEvent() {
var t,
el = document.createElement('fakeelement');
var animations = {
animation: 'animationend',
OAnimation: 'oAnimationEnd',
MozAnimation: 'animationend',
WebkitAnimation: 'webkitAnimationEnd'
};
for (t in animations) {
if (el.style[t] !== undefined) {
return animations[t];
}
}
}
function whichTransitionEvent() {
var t,
el = document.createElement('fakeelement');
var transitions = {
transition: 'transitionend',
OTransition: 'oTransitionEnd',
MozTransition: 'transitionend',
WebkitTransition: 'webkitTransitionEnd'
};
for (t in transitions) {
if (el.style[t] !== undefined) {
return transitions[t];
}
}
}
function createIframe(config) {
var url = config.url,
allow = config.allow,
callback = config.callback,
appendTo = config.appendTo;
var iframe = document.createElement('iframe');
iframe.className = 'vimeo-video gvideo';
iframe.src = url;
iframe.style.width = '100%';
iframe.style.height = '100%';
if (allow) {
iframe.setAttribute('allow', allow);
}
iframe.onload = function () {
iframe.onload = null;
addClass(iframe, 'node-ready');
if (isFunction(callback)) {
callback();
}
};
if (appendTo) {
appendTo.appendChild(iframe);
}
return iframe;
}
function waitUntil(check, onComplete, delay, timeout) {
if (check()) {
onComplete();
return;
}
if (!delay) {
delay = 100;
}
var timeoutPointer;
var intervalPointer = setInterval(function () {
if (!check()) {
return;
}
clearInterval(intervalPointer);
if (timeoutPointer) {
clearTimeout(timeoutPointer);
}
onComplete();
}, delay);
if (timeout) {
timeoutPointer = setTimeout(function () {
clearInterval(intervalPointer);
}, timeout);
}
}
function injectAssets(url, waitFor, callback) {
if (isNil(url)) {
console.error('Inject assets error');
return;
}
if (isFunction(waitFor)) {
callback = waitFor;
waitFor = false;
}
if (isString(waitFor) && waitFor in window) {
if (isFunction(callback)) {
callback();
}
return;
}
var found;
if (url.indexOf('.css') !== -1) {
found = document.querySelectorAll('link[href="' + url + '"]');
if (found && found.length > 0) {
if (isFunction(callback)) {
callback();
}
return;
}
var head = document.getElementsByTagName('head')[0];
var headStyles = head.querySelectorAll('link[rel="stylesheet"]');
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = url;
link.media = 'all';
if (headStyles) {
head.insertBefore(link, headStyles[0]);
} else {
head.appendChild(link);
}
if (isFunction(callback)) {
callback();
}
return;
}
found = document.querySelectorAll('script[src="' + url + '"]');
if (found && found.length > 0) {
if (isFunction(callback)) {
if (isString(waitFor)) {
waitUntil(function () {
return typeof window[waitFor] !== 'undefined';
}, function () {
callback();
});
return false;
}
callback();
}
return;
}
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.onload = function () {
if (isFunction(callback)) {
if (isString(waitFor)) {
waitUntil(function () {
return typeof window[waitFor] !== 'undefined';
}, function () {
callback();
});
return false;
}
callback();
}
};
document.body.appendChild(script);
}
function isMobile() {
return 'navigator' in window && window.navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(Android)|(PlayBook)|(BB10)|(BlackBerry)|(Opera Mini)|(IEMobile)|(webOS)|(MeeGo)/i);
}
function isTouch() {
return isMobile() !== null || document.createTouch !== undefined || 'ontouchstart' in window || 'onmsgesturechange' in window || navigator.msMaxTouchPoints;
}
function isFunction(f) {
return typeof f === 'function';
}
function isString(s) {
return typeof s === 'string';
}
function isNode(el) {
return !!(el && el.nodeType && el.nodeType == 1);
}
function isArray(ar) {
return Array.isArray(ar);
}
function isArrayLike(ar) {
return ar && ar.length && isFinite(ar.length);
}
function isObject(o) {
var type = _typeof(o);
return type === 'object' && o != null && !isFunction(o) && !isArray(o);
}
function isNil(o) {
return o == null;
}
function has(obj, key) {
return obj !== null && hasOwnProperty.call(obj, key);
}
function size(o) {
if (isObject(o)) {
if (o.keys) {
return o.keys().length;
}
var l = 0;
for (var k in o) {
if (has(o, k)) {
l++;
}
}
return l;
} else {
return o.length;
}
}
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function getNextFocusElement() {
var current = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : -1;
var btns = document.querySelectorAll('.gbtn[data-taborder]:not(.disabled)');
if (!btns.length) {
return false;
}
if (btns.length == 1) {
return btns[0];
}
if (typeof current == 'string') {
current = parseInt(current);
}
var orders = [];
each(btns, function (btn) {
orders.push(btn.getAttribute('data-taborder'));
});
var highestOrder = Math.max.apply(Math, orders.map(function (order) {
return parseInt(order);
}));
var newIndex = current < 0 ? 1 : current + 1;
if (newIndex > highestOrder) {
newIndex = '1';
}
var nextOrders = orders.filter(function (el) {
return el >= parseInt(newIndex);
});
var nextFocus = nextOrders.sort()[0];
return document.querySelector(".gbtn[data-taborder=\"".concat(nextFocus, "\"]"));
}
function keyboardNavigation(instance) {
if (instance.events.hasOwnProperty('keyboard')) {
return false;
}
instance.events['keyboard'] = addEvent('keydown', {
onElement: window,
withCallback: function withCallback(event, target) {
event = event || window.event;
var key = event.keyCode;
if (key == 9) {
var focusedButton = document.querySelector('.gbtn.focused');
if (!focusedButton) {
var activeElement = document.activeElement && document.activeElement.nodeName ? document.activeElement.nodeName.toLocaleLowerCase() : false;
if (activeElement == 'input' || activeElement == 'textarea' || activeElement == 'button') {
return;
}
}
event.preventDefault();
var btns = document.querySelectorAll('.gbtn[data-taborder]');
if (!btns || btns.length <= 0) {
return;
}
if (!focusedButton) {
var first = getNextFocusElement();
if (first) {
first.focus();
addClass(first, 'focused');
}
return;
}
var currentFocusOrder = focusedButton.getAttribute('data-taborder');
var nextFocus = getNextFocusElement(currentFocusOrder);
removeClass(focusedButton, 'focused');
if (nextFocus) {
nextFocus.focus();
addClass(nextFocus, 'focused');
}
}
if (key == 39) {
instance.nextSlide();
}
if (key == 37) {
instance.prevSlide();
}
if (key == 27) {
instance.close();
}
}
});
}
var ZoomImages = function () {
function ZoomImages(el, slide) {
var _this = this;
var onclose = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
_classCallCheck(this, ZoomImages);
this.img = el;
this.slide = slide;
this.onclose = onclose;
if (this.img.setZoomEvents) {
return false;
}
this.active = false;
this.zoomedIn = false;
this.dragging = false;
this.currentX = null;
this.currentY = null;
this.initialX = null;
this.initialY = null;
this.xOffset = 0;
this.yOffset = 0;
this.img.addEventListener('mousedown', function (e) {
return _this.dragStart(e);
}, false);
this.img.addEventListener('mouseup', function (e) {
return _this.dragEnd(e);
}, false);
this.img.addEventListener('mousemove', function (e) {
return _this.drag(e);
}, false);
this.img.addEventListener('click', function (e) {
if (_this.slide.classList.contains('dragging-nav')) {
_this.zoomOut();
return false;
}
if (!_this.zoomedIn) {
return _this.zoomIn();
}
if (_this.zoomedIn && !_this.dragging) {
_this.zoomOut();
}
}, false);
this.img.setZoomEvents = true;
}
return _createClass(ZoomImages, [{
key: "zoomIn",
value: function zoomIn() {
var winWidth = this.widowWidth();
if (this.zoomedIn || winWidth <= 768) {
return;
}
var img = this.img;
img.setAttribute('data-style', img.getAttribute('style'));
img.style.maxWidth = img.naturalWidth + 'px';
img.style.maxHeight = img.naturalHeight + 'px';
if (img.naturalWidth > winWidth) {
var centerX = winWidth / 2 - img.naturalWidth / 2;
this.setTranslate(this.img.parentNode, centerX, 0);
}
this.slide.classList.add('zoomed');
this.zoomedIn = true;
}
}, {
key: "zoomOut",
value: function zoomOut() {
this.img.parentNode.setAttribute('style', '');
this.img.setAttribute('style', this.img.getAttribute('data-style'));
this.slide.classList.remove('zoomed');
this.zoomedIn = false;
this.currentX = null;
this.currentY = null;
this.initialX = null;
this.initialY = null;
this.xOffset = 0;
this.yOffset = 0;
if (this.onclose && typeof this.onclose == 'function') {
this.onclose();
}
}
}, {
key: "dragStart",
value: function dragStart(e) {
e.preventDefault();
if (!this.zoomedIn) {
this.active = false;
return;
}
if (e.type === 'touchstart') {
this.initialX = e.touches[0].clientX - this.xOffset;
this.initialY = e.touches[0].clientY - this.yOffset;
} else {
this.initialX = e.clientX - this.xOffset;
this.initialY = e.clientY - this.yOffset;
}
if (e.target === this.img) {
this.active = true;
this.img.classList.add('dragging');
}
}
}, {
key: "dragEnd",
value: function dragEnd(e) {
var _this2 = this;
e.preventDefault();
this.initialX = this.currentX;
this.initialY = this.currentY;
this.active = false;
setTimeout(function () {
_this2.dragging = false;
_this2.img.isDragging = false;
_this2.img.classList.remove('dragging');
}, 100);
}
}, {
key: "drag",
value: function drag(e) {
if (this.active) {
e.preventDefault();
if (e.type === 'touchmove') {
this.currentX = e.touches[0].clientX - this.initialX;
this.currentY = e.touches[0].clientY - this.initialY;
} else {
this.currentX = e.clientX - this.initialX;
this.currentY = e.clientY - this.initialY;
}
this.xOffset = this.currentX;
this.yOffset = this.currentY;
this.img.isDragging = true;
this.dragging = true;
this.setTranslate(this.img, this.currentX, this.currentY);
}
}
}, {
key: "onMove",
value: function onMove(e) {
if (!this.zoomedIn) {
return;
}
var xOffset = e.clientX - this.img.naturalWidth / 2;
var yOffset = e.clientY - this.img.naturalHeight / 2;
this.setTranslate(this.img, xOffset, yOffset);
}
}, {
key: "setTranslate",
value: function setTranslate(node, xPos, yPos) {
node.style.transform = 'translate3d(' + xPos + 'px, ' + yPos + 'px, 0)';
}
}, {
key: "widowWidth",
value: function widowWidth() {
return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
}
}]);
}();
var DragSlides = function () {
function DragSlides() {
var _this = this;
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, DragSlides);
var dragEl = config.dragEl,
_config$toleranceX = config.toleranceX,
toleranceX = _config$toleranceX === void 0 ? 40 : _config$toleranceX,
_config$toleranceY = config.toleranceY,
toleranceY = _config$toleranceY === void 0 ? 65 : _config$toleranceY,
_config$slide = config.slide,
slide = _config$slide === void 0 ? null : _config$slide,
_config$instance = config.instance,
instance = _config$instance === void 0 ? null : _config$instance;
this.el = dragEl;
this.active = false;
this.dragging = false;
this.currentX = null;
this.currentY = null;
this.initialX = null;
this.initialY = null;
this.xOffset = 0;
this.yOffset = 0;
this.direction = null;
this.lastDirection = null;
this.toleranceX = toleranceX;
this.toleranceY = toleranceY;
this.toleranceReached = false;
this.dragContainer = this.el;
this.slide = slide;
this.instance = instance;
this.el.addEventListener('mousedown', function (e) {
return _this.dragStart(e);
}, false);
this.el.addEventListener('mouseup', function (e) {
return _this.dragEnd(e);
}, false);
this.el.addEventListener('mousemove', function (e) {
return _this.drag(e);
}, false);
}
return _createClass(DragSlides, [{
key: "dragStart",
value: function dragStart(e) {
if (this.slide.classList.contains('zoomed')) {
this.active = false;
return;
}
if (e.type === 'touchstart') {
this.initialX = e.touches[0].clientX - this.xOffset;
this.initialY = e.touches[0].clientY - this.yOffset;
} else {
this.initialX = e.clientX - this.xOffset;
this.initialY = e.clientY - this.yOffset;
}
var clicked = e.target.nodeName.toLowerCase();
var exludeClicks = ['input', 'select', 'textarea', 'button', 'a'];
if (e.target.classList.contains('nodrag') || closest(e.target, '.nodrag') || exludeClicks.indexOf(clicked) !== -1) {
this.active = false;
return;
}
e.preventDefault();
if (e.target === this.el || clicked !== 'img' && closest(e.target, '.gslide-inline')) {
this.active = true;
this.el.classList.add('dragging');
this.dragContainer = closest(e.target, '.ginner-container');
}
}
}, {
key: "dragEnd",
value: function dragEnd(e) {
var _this2 = this;
e && e.preventDefault();
this.initialX = 0;
this.initialY = 0;
this.currentX = null;
this.currentY = null;
this.initialX = null;
this.initialY = null;
this.xOffset = 0;
this.yOffset = 0;
this.active = false;
if (this.doSlideChange) {
this.instance.preventOutsideClick = true;
this.doSlideChange == 'right' && this.instance.prevSlide();
this.doSlideChange == 'left' && this.instance.nextSlide();
}
if (this.doSlideClose) {
this.instance.close();
}
if (!this.toleranceReached) {
this.setTranslate(this.dragContainer, 0, 0, true);
}
setTimeout(function () {
_this2.instance.preventOutsideClick = false;
_this2.toleranceReached = false;
_this2.lastDirection = null;
_this2.dragging = false;
_this2.el.isDragging = false;
_this2.el.classList.remove('dragging');
_this2.slide.classList.remove('dragging-nav');
_this2.dragContainer.style.transform = '';
_this2.dragContainer.style.transition = '';
}, 100);
}
}, {
key: "drag",
value: function drag(e) {
if (this.active) {
e.preventDefault();
this.slide.classList.add('dragging-nav');
if (e.type === 'touchmove') {
this.currentX = e.touches[0].clientX - this.initialX;
this.currentY = e.touches[0].clientY - this.initialY;
} else {
this.currentX = e.clientX - this.initialX;
this.currentY = e.clientY - this.initialY;
}
this.xOffset = this.currentX;
this.yOffset = this.currentY;
this.el.isDragging = true;
this.dragging = true;
this.doSlideChange = false;
this.doSlideClose = false;
var currentXInt = Math.abs(this.currentX);
var currentYInt = Math.abs(this.currentY);
if (currentXInt > 0 && currentXInt >= Math.abs(this.currentY) && (!this.lastDirection || this.lastDirection == 'x')) {
this.yOffset = 0;
this.lastDirection = 'x';
this.setTranslate(this.dragContainer, this.currentX, 0);
var doChange = this.shouldChange();
if (!this.instance.settings.dragAutoSnap && doChange) {
this.doSlideChange = doChange;
}
if (this.instance.settings.dragAutoSnap && doChange) {
this.instance.preventOutsideClick = true;
this.toleranceReached = true;
this.active = false;
this.instance.preventOutsideClick = true;
this.dragEnd(null);
doChange == 'right' && this.instance.prevSlide();
doChange == 'left' && this.instance.nextSlide();
return;
}
}
if (this.toleranceY > 0 && currentYInt > 0 && currentYInt >= currentXInt && (!this.lastDirection || this.lastDirection == 'y')) {
this.xOffset = 0;
this.lastDirection = 'y';
this.setTranslate(this.dragContainer, 0, this.currentY);
var doClose = this.shouldClose();
if (!this.instance.settings.dragAutoSnap && doClose) {
this.doSlideClose = true;
}
if (this.instance.settings.dragAutoSnap && doClose) {
this.instance.close();
}
return;
}
}
}
}, {
key: "shouldChange",
value: function shouldChange() {
var doChange = false;
var currentXInt = Math.abs(this.currentX);
if (currentXInt >= this.toleranceX) {
var dragDir = this.currentX > 0 ? 'right' : 'left';
if (dragDir == 'left' && this.slide !== this.slide.parentNode.lastChild || dragDir == 'right' && this.slide !== this.slide.parentNode.firstChild) {
doChange = dragDir;
}
}
return doChange;
}
}, {
key: "shouldClose",
value: function shouldClose() {
var doClose = false;
var currentYInt = Math.abs(this.currentY);
if (currentYInt >= this.toleranceY) {
doClose = true;
}
return doClose;
}
}, {
key: "setTranslate",
value: function setTranslate(node, xPos, yPos) {
var animated = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
if (animated) {
node.style.transition = 'all .2s ease';
} else {
node.style.transition = '';
}
node.style.transform = "translate3d(".concat(xPos, "px, ").concat(yPos, "px, 0)");
}
}]);
}();
function slideImage(slide, data, index, callback) {
var slideMedia = slide.querySelector('.gslide-media');
var img = new Image();
var titleID = 'gSlideTitle_' + index;
var textID = 'gSlideDesc_' + index;
img.addEventListener('load', function () {
if (isFunction(callback)) {
callback();
}
}, false);
img.src = data.href;
if (data.sizes != '' && data.srcset != '') {
img.sizes = data.sizes;
img.srcset = data.srcset;
}
img.alt = '';
if (!isNil(data.alt) && data.alt !== '') {
img.alt = data.alt;
}
if (data.title !== '') {
img.setAttribute('aria-labelledby', titleID);
}
if (data.description !== '') {
img.setAttribute('aria-describedby', textID);
}
if (data.hasOwnProperty('_hasCustomWidth') && data._hasCustomWidth) {
img.style.width = data.width;
}
if (data.hasOwnProperty('_hasCustomHeight') && data._hasCustomHeight) {
img.style.height = data.height;
}
slideMedia.insertBefore(img, slideMedia.firstChild);
return;
}
function slideVideo(slide, data, index, callback) {
var _this = this;
var slideContainer = slide.querySelector('.ginner-container');
var videoID = 'gvideo' + index;
var slideMedia = slide.querySelector('.gslide-media');
var videoPlayers = this.getAllPlayers();
addClass(slideContainer, 'gvideo-container');
slideMedia.insertBefore(createHTML('<div class="gvideo-wrapper"></div>'), slideMedia.firstChild);
var videoWrapper = slide.querySelector('.gvideo-wrapper');
injectAssets(this.settings.plyr.css, 'Plyr');
var url = data.href;
var provider = data === null || data === void 0 ? void 0 : data.videoProvider;
var customPlaceholder = false;
slideMedia.style.maxWidth = data.width;
injectAssets(this.settings.plyr.js, 'Plyr', function () {
if (!provider && url.match(/vimeo\.com\/([0-9]*)/)) {
provider = 'vimeo';
}
if (!provider && (url.match(/(youtube\.com|youtube-nocookie\.com)\/watch\?v=([a-zA-Z0-9\-_]+)/) || url.match(/youtu\.be\/([a-zA-Z0-9\-_]+)/) || url.match(/(youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9\-_]+)/) || url.match(/(youtube\.com|youtube-nocookie\.com)\/shorts\/([a-zA-Z0-9\-_]+)/))) {
provider = 'youtube';
}
if (provider === 'local' || !provider) {
provider = 'local';
var html = '<video id="' + videoID + '" ';
html += "style=\"background:#000; max-width: ".concat(data.width, ";\" ");
html += 'preload="metadata" ';
html += 'x-webkit-airplay="allow" ';
html += 'playsinline ';
html += 'controls ';
html += 'class="gvideo-local">';
html += "<source src=\"".concat(url, "\">");
html += '</video>';
customPlaceholder = createHTML(html);
}
var placeholder = customPlaceholder ? customPlaceholder : createHTML("<div id=\"".concat(videoID, "\" data-plyr-provider=\"").concat(provider, "\" data-plyr-embed-id=\"").concat(url, "\"></div>"));
addClass(videoWrapper, "".concat(provider, "-video gvideo"));
videoWrapper.appendChild(placeholder);
videoWrapper.setAttribute('data-id', videoID);
videoWrapper.setAttribute('data-index', index);
var playerConfig = has(_this.settings.plyr, 'config') ? _this.settings.plyr.config : {};
var player = new Plyr('#' + videoID, playerConfig);
player.on('ready', function (event) {
videoPlayers[videoID] = event.detail.plyr;
if (isFunction(callback)) {
callback();
}
});
waitUntil(function () {
return slide.querySelector('iframe') && slide.querySelector('iframe').dataset.ready == 'true';
}, function () {
_this.resize(slide);
});
player.on('enterfullscreen', handleMediaFullScreen);
player.on('exitfullscreen', handleMediaFullScreen);
});
}
function handleMediaFullScreen(event) {
var media = closest(event.target, '.gslide-media');
if (event.type === 'enterfullscreen') {
addClass(media, 'fullscreen');
}
if (event.type === 'exitfullscreen') {
removeClass(media, 'fullscreen');
}
}
function slideInline(slide, data, index, callback) {
var _this = this;
var slideMedia = slide.querySelector('.gslide-media');
var hash = has(data, 'href') && data.href ? data.href.split('#').pop().trim() : false;
var content = has(data, 'content') && data.content ? data.content : false;
var innerContent;
if (content) {
if (isString(content)) {
innerContent = createHTML("<div class=\"ginlined-content\">".concat(content, "</div>"));
}
if (isNode(content)) {
if (content.style.display == 'none') {
content.style.display = 'block';
}
var container = document.createElement('div');
container.className = 'ginlined-content';
container.appendChild(content);
innerContent = container;
}
}
if (hash) {
var div = document.getElementById(hash);
if (!div) {
return false;
}
var cloned = div.cloneNode(true);
cloned.style.height = data.height;
cloned.style.maxWidth = data.width;
addClass(cloned, 'ginlined-content');
innerContent = cloned;
}
if (!innerContent) {
console.error('Unable to append inline slide content', data);
return false;
}
slideMedia.style.height = data.height;
slideMedia.style.width = data.width;
slideMedia.appendChild(innerContent);
this.events['inlineclose' + hash] = addEvent('click', {
onElement: slideMedia.querySelectorAll('.gtrigger-close'),
withCallback: function withCallback(e) {
e.preventDefault();
_this.close();
}
});
if (isFunction(callback)) {
callback();
}
return;
}
function slideIframe(slide, data, index, callback) {
var slideMedia = slide.querySelector('.gslide-media');
var iframe = createIframe({
url: data.href,
callback: callback
});
slideMedia.parentNode.style.maxWidth = data.width;
slideMedia.parentNode.style.height = data.height;
slideMedia.appendChild(iframe);
return;
}
var SlideConfigParser = function () {
function SlideConfigParser() {
var slideParamas = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, SlideConfigParser);
this.defaults = {
href: '',
sizes: '',
srcset: '',
title: '',
type: '',
videoProvider: '',
description: '',
alt: '',
descPosition: 'bottom',
effect: '',
width: '',
height: '',
content: false,
zoomable: true,
draggable: true
};
if (isObject(slideParamas)) {
this.defaults = extend(this.defaults, slideParamas);
}
}
return _createClass(SlideConfigParser, [{
key: "sourceType",
value: function sourceType(url) {
var origin = url;
url = url.toLowerCase();
if (url.match(/\.(jpeg|jpg|jpe|gif|png|apn|webp|avif|svg)/) !== null) {
return 'image';
}
if (url.match(/(youtube\.com|youtube-nocookie\.com)\/watch\?v=([a-zA-Z0-9\-_]+)/) || url.match(/youtu\.be\/([a-zA-Z0-9\-_]+)/) || url.match(/(youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9\-_]+)/) || url.match(/(youtube\.com|youtube-nocookie\.com)\/shorts\/([a-zA-Z0-9\-_]+)/)) {
return 'video';
}
if (url.match(/vimeo\.com\/([0-9]*)/)) {
return 'video';
}
if (url.match(/\.(mp4|ogg|webm|mov)/) !== null) {
return 'video';
}
if (url.match(/\.(mp3|wav|wma|aac|ogg)/) !== null) {
return 'audio';
}
if (url.indexOf('#') > -1) {
var hash = origin.split('#').pop();
if (hash.trim() !== '') {
return 'inline';
}
}
if (url.indexOf('goajax=true') > -1) {
return 'ajax';
}
return 'external';
}
}, {
key: "parseConfig",
value: function parseConfig(element, settings) {
var _this = this;
var data = extend({
descPosition: settings.descPosition
}, this.defaults);
if (isObject(element) && !isNode(element)) {
if (!has(element, 'type')) {
if (has(element, 'content') && element.content) {
element.type = 'inline';
} else if (has(element, 'href')) {
element.type = this.sourceType(element.href);
}
}
var objectData = extend(data, element);
this.setSize(objectData, settings);
return objectData;
}
var url = '';
var config = element.getAttribute('data-glightbox');
var nodeType = element.nodeName.toLowerCase();
if (nodeType === 'a') {
url = element.href;
}
if (nodeType === 'img') {
url = element.src;
data.alt = element.alt;
}
data.href = url;
each(data, function (val, key) {
if (has(settings, key) && key !== 'width') {
data[key] = settings[key];
}
var nodeData = element.dataset[key];
if (!isNil(nodeData)) {
data[key] = _this.sanitizeValue(nodeData);
}
});
if (data.content) {
data.type = 'inline';
}
if (!data.type && url) {
data.type = this.sourceType(url);
}
if (!isNil(config)) {
var cleanKeys = [];
each(data, function (v, k) {
cleanKeys.push(';\\s?' + k);
});
cleanKeys = cleanKeys.join('\\s?:|');
if (config.trim() !== '') {
each(data, function (val, key) {
var str = config;
var match = 's?' + key + 's?:s?(.*?)(' + cleanKeys + 's?:|$)';
var regex = new RegExp(match);
var matches = str.match(regex);
if (matches && matches.length && matches[1]) {
var value = matches[1].trim().replace(/;\s*$/, '');
data[key] = _this.sanitizeValue(value);
}
});
}
} else {
if (!data.title && nodeType == 'a') {
var title = element.title;
if (!isNil(title) && title !== '') {
data.title = title;
}
}
if (!data.title && nodeType == 'img') {
var alt = element.alt;
if (!isNil(alt) && alt !== '') {
data.title = alt;
}
}
}
if (data.description && data.description.substring(0, 1) === '.') {
var description;
try {
description = document.querySelector(data.description).innerHTML;
} catch (error) {
if (!(error instanceof DOMException)) {
throw error;
}
}
if (description) {
data.description = description;
}
}
if (!data.description) {
var nodeDesc = element.querySelector('.glightbox-desc');
if (nodeDesc) {
data.description = nodeDesc.innerHTML;
}
}
this.setSize(data, settings, element);
this.slideConfig = data;
return data;
}
}, {
key: "setSize",
value: function setSize(data, settings) {
var element = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var defaultWith = data.type == 'video' ? this.checkSize(settings.videosWidth) : this.checkSize(settings.width);
var defaultHeight = this.checkSize(settings.height);
data.width = has(data, 'width') && data.width !== '' ? this.checkSize(data.width) : defaultWith;
data.height = has(data, 'height') && data.height !== '' ? this.checkSize(data.height) : defaultHeight;
if (element && data.type == 'image') {
data._hasCustomWidth = element.dataset.width ? true : false;
data._hasCustomHeight = element.dataset.height ? true : false;
}
return data;
}
}, {
key: "checkSize",
value: function checkSize(size) {
return isNumber(size) ? "".concat(size, "px") : size;
}
}, {
key: "sanitizeValue",
value: function sanitizeValue(val) {
if (val !== 'true' && val !== 'false') {
return val;
}
return val === 'true';
}
}]);
}();
var Slide = function () {
function Slide(el, instance, index) {
_classCallCheck(this, Slide);
this.element = el;
this.instance = instance;
this.index = index;
}
return _createClass(Slide, [{
key: "setContent",
value: function setContent() {
var _this = this;
var slide = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
if (hasClass(slide, 'loaded')) {
return false;
}
var settings = this.instance.settings;
var slideConfig = this.slideConfig;
var isMobileDevice = isMobile();
if (isFunction(settings.beforeSlideLoad)) {
settings.beforeSlideLoad({
index: this.index,
slide: slide,
player: false
});
}
var type = slideConfig.type;
var position = slideConfig.descPosition;
var slideMedia = slide.querySelector('.gslide-media');
var slideTitle = slide.querySelector('.gslide-title');
var slideText = slide.querySelector('.gslide-desc');
var slideDesc = slide.querySelector('.gdesc-inner');
var finalCallback = callback;
var titleID = 'gSlideTitle_' + this.index;
var textID = 'gSlideDesc_' + this.index;
if (isFunction(settings.afterSlideLoad)) {
finalCallback = function finalCallback() {
if (isFunction(callback)) {
callback();
}
settings.afterSlideLoad({
index: _this.index,
slide: slide,
player: _this.instance.getSlidePlayerInstance(_this.index)
});
};
}
if (slideConfig.title == '' && slideConfig.description == '') {
if (slideDesc) {
slideDesc.parentNode.parentNode.removeChild(slideDesc.parentNode);
}
} else {
if (slideTitle && slideConfig.title !== '') {
slideTitle.id = titleID;
slideTitle.innerHTML = slideConfig.title;
} else {
slideTitle.parentNode.removeChild(slideTitle);
}
if (slideText && slideConfig.description !== '') {
slideText.id = textID;
if (isMobileDevice && settings.moreLength > 0) {
slideConfig.smallDescription = this.slideShortDesc(slideConfig.description, settings.moreLength, settings.moreText);
slideText.innerHTML = slideConfig.smallDescription;
this.descriptionEvents(slideText, slideConfig);
} else {
slideText.innerHTML = slideConfig.description;
}
} else {
slideText.parentNode.removeChild(slideText);
}
addClass(slideMedia.parentNode, "desc-".concat(position));
addClass(slideDesc.parentNode, "description-".concat(position));
}
addClass(slideMedia, "gslide-".concat(type));
addClass(slide, 'loaded');
if (type === 'video') {
slideVideo.apply(this.instance, [slide, slideConfig, this.index, finalCallback]);
return;
}
if (type === 'external') {
slideIframe.apply(this, [slide, slideConfig, this.index, finalCallback]);
return;
}
if (type === 'inline') {
slideInline.apply(this.instance, [slide, slideConfig, this.index, finalCallback]);
if (slideConfig.draggable) {
new DragSlides({
dragEl: slide.querySelector('.gslide-inline'),
toleranceX: settings.dragToleranceX,
toleranceY: settings.dragToleranceY,
slide: slide,
instance: this.instance
});
}
return;
}
if (type === 'image') {
slideImage(slide, slideConfig, this.index, function () {
var img = slide.querySelector('img');
if (slideConfig.draggable) {
new DragSlides({
dragEl: img,
toleranceX: settings.dragToleranceX,
toleranceY: settings.dragToleranceY,
slide: slide,
instance: _this.instance
});
}
if (slideConfig.zoomable && img.naturalWidth > img.offsetWidth) {
addClass(img, 'zoomable');
new ZoomImages(img, slide, function () {
_this.instance.resize();
});
}
if (isFunction(finalCallback)) {
finalCallback();
}
});
return;
}
if (isFunction(finalCallback)) {
finalCallback();
}
}
}, {
key: "slideShortDesc",
value: function slideShortDesc(string) {
var n = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 50;
var wordBoundary = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var div = document.createElement('div');
div.innerHTML = string;
var cleanedString = div.innerText;
var useWordBoundary = wordBoundary;
string = cleanedString.trim();
if (string.length <= n) {
return string;
}
var subString = string.substr(0, n - 1);
if (!useWordBoundary) {
return subString;
}
div = null;
return subString + '... <a href="#" class="desc-more">' + wordBoundary + '</a>';
}
}, {
key: "descriptionEvents",
value: function descriptionEvents(desc, data) {
var _this2 = this;
var moreLink = desc.querySelector('.desc-more');
if (!moreLink) {
return false;
}
addEvent('click', {
onElement: moreLink,
withCallback: function withCallback(event, target) {
event.preventDefault();
var body = document.body;
var desc = closest(target, '.gslide-desc');
if (!desc) {
return false;
}
desc.innerHTML = data.description;
addClass(body, 'gdesc-open');
var shortEvent = addEvent('click', {
onElement: [body, closest(desc, '.gslide-description')],
withCallback: function withCallback(event, target) {
if (event.target.nodeName.toLowerCase() !== 'a') {
removeClass(body, 'gdesc-open');
addClass(body, 'gdesc-closed');
desc.innerHTML = data.smallDescription;
_this2.descriptionEvents(desc, data);
setTimeout(function () {
removeClass(body, 'gdesc-closed');
}, 400);
shortEvent.destroy();
}
}
});
}
});
}
}, {
key: "create",
value: function create() {
return createHTML(this.instance.settings.slideHTML);
}
}, {
key: "getConfig",
value: function getConfig() {
if (!isNode(this.element) && !this.element.hasOwnProperty('draggable')) {
this.element.draggable = this.instance.settings.draggable;
}
var parser = new SlideConfigParser(this.instance.settings.slideExtraAttributes);
this.slideConfig = parser.parseConfig(this.element, this.instance.settings);
return this.slideConfig;
}
}]);
}();
function getLen(v) {
return Math.sqrt(v.x * v.x + v.y * v.y);
}
function dot(v1, v2) {
return v1.x * v2.x + v1.y * v2.y;
}
function getAngle(v1, v2) {
var mr = getLen(v1) * getLen(v2);
if (mr === 0) {
return 0;
}
var r = dot(v1, v2) / mr;
if (r > 1) {
r = 1;
}
return Math.acos(r);
}
function cross(v1, v2) {
return v1.x * v2.y - v2.x * v1.y;
}
function getRotateAngle(v1, v2) {
var angle = getAngle(v1, v2);
if (cross(v1, v2) > 0) {
angle *= -1;
}
return angle * 180 / Math.PI;
}
var EventsHandlerAdmin = function () {
function EventsHandlerAdmin(el) {
_classCallCheck(this, EventsHandlerAdmin);
this.handlers = [];
this.el = el;
}
return _createClass(EventsHandlerAdmin, [{
key: "add",
value: function add(handler) {
this.handlers.push(handler);
}
}, {
key: "del",
value: function del(handler) {
if (!handler) {
this.handlers = [];
}
for (var i = this.handle