/*!
* lightgallery | 2.4.0-beta.0 | December 12th 2021
* http://www.lightgalleryjs.com/
* Copyright (c) 2020 Sachin Neravath;
* @license GPLv3
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.lgVideo = factory());
}(this, (function () { 'use strict';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var videoSettings = {
autoplayFirstVideo: true,
youTubePlayerParams: false,
vimeoPlayerParams: false,
wistiaPlayerParams: false,
gotoNextSlideOnVideoEnd: true,
autoplayVideoOnSlide: false,
videojs: false,
videojsOptions: {},
};
/**
* List of lightGallery events
* All events should be documented here
* Below interfaces are used to build the website documentations
* */
var lGEvents = {
afterAppendSlide: 'lgAfterAppendSlide',
init: 'lgInit',
hasVideo: 'lgHasVideo',
containerResize: 'lgContainerResize',
updateSlides: 'lgUpdateSlides',
afterAppendSubHtml: 'lgAfterAppendSubHtml',
beforeOpen: 'lgBeforeOpen',
afterOpen: 'lgAfterOpen',
slideItemLoad: 'lgSlideItemLoad',
beforeSlide: 'lgBeforeSlide',
afterSlide: 'lgAfterSlide',
posterClick: 'lgPosterClick',
dragStart: 'lgDragStart',
dragMove: 'lgDragMove',
dragEnd: 'lgDragEnd',
beforeNextSlide: 'lgBeforeNextSlide',
beforePrevSlide: 'lgBeforePrevSlide',
beforeClose: 'lgBeforeClose',
afterClose: 'lgAfterClose',
rotateLeft: 'lgRotateLeft',
rotateRight: 'lgRotateRight',
flipHorizontal: 'lgFlipHorizontal',
flipVertical: 'lgFlipVertical',
autoplay: 'lgAutoplay',
autoplayStart: 'lgAutoplayStart',
autoplayStop: 'lgAutoplayStop',
};
var param = function (obj) {
return Object.keys(obj)
.map(function (k) {
return encodeURIComponent(k) + '=' + encodeURIComponent(obj[k]);
})
.join('&');
};
var getVimeoURLParams = function (defaultParams, videoInfo) {
if (!videoInfo || !videoInfo.vimeo)
return '';
var urlParams = videoInfo.vimeo[2] || '';
urlParams =
urlParams[0] == '?' ? '&' + urlParams.slice(1) : urlParams || '';
var defaultPlayerParams = defaultParams
? '&' + param(defaultParams)
: '';
// For vimeo last parms gets priority if duplicates found
var vimeoPlayerParams = "?autoplay=0&muted=1" + defaultPlayerParams + urlParams;
return vimeoPlayerParams;
};
/**
* Video module for lightGallery
* Supports HTML5, YouTube, Vimeo, wistia videos
*
*
* @ref Wistia
* https://wistia.com/support/integrations/wordpress(How to get url)
* https://wistia.com/support/developers/embed-options#using-embed-options
* https://wistia.com/support/developers/player-api
* https://wistia.com/support/developers/construct-an-embed-code
* http://jsfiddle.net/xvnm7xLm/
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video
* https://wistia.com/support/embed-and-share/sharing-videos
* https://private-sharing.wistia.com/medias/mwhrulrucj
*
* @ref Youtube
* https://developers.google.com/youtube/player_parameters#enablejsapi
* https://developers.google.com/youtube/iframe_api_reference
* https://developer.chrome.com/blog/autoplay/#iframe-delegation
*
* @ref Vimeo
* https://stackoverflow.com/questions/10488943/easy-way-to-get-vimeo-id-from-a-vimeo-url
* https://vimeo.zendesk.com/hc/en-us/articles/360000121668-Starting-playback-at-a-specific-timecode
* https://vimeo.zendesk.com/hc/en-us/articles/360001494447-Using-Player-Parameters
*/
var Video = /** @class */ (function () {
function Video(instance) {
// get lightGallery core plugin instance
this.core = instance;
this.settings = __assign(__assign({}, videoSettings), this.core.settings);
return this;
}
Video.prototype.init = function () {
var _this = this;
/**
* Event triggered when video url found without poster
* Append video HTML
* Play if autoplayFirstVideo is true
*/
this.core.LGel.on(lGEvents.hasVideo + ".video", this.onHasVideo.bind(this));
this.core.LGel.on(lGEvents.posterClick + ".video", function () {
var $el = _this.core.getSlideItem(_this.core.index);
_this.loadVideoOnPosterClick($el);
});
this.core.LGel.on(lGEvents.slideItemLoad + ".video", this.onSlideItemLoad.bind(this));
// @desc fired immediately before each slide transition.
this.core.LGel.on(lGEvents.beforeSlide + ".video", this.onBeforeSlide.bind(this));
// @desc fired immediately after each slide transition.
this.core.LGel.on(lGEvents.afterSlide + ".video", this.onAfterSlide.bind(this));
};
/**
* @desc Event triggered when a slide is completely loaded
*
* @param {Event} event - lightGalley custom event
*/
Video.prototype.onSlideItemLoad = function (event) {
var _this = this;
var _a = event.detail, isFirstSlide = _a.isFirstSlide, index = _a.index;
// Should check the active slide as well as user may have moved to different slide before the first slide is loaded
if (this.settings.autoplayFirstVideo &&
isFirstSlide &&
index === this.core.index) {
// Delay is just for the transition effect on video load
setTimeout(function () {
_this.loadAndPlayVideo(index);
}, 200);
}
// Should not call on first slide. should check only if the slide is active
if (!isFirstSlide &&
this.settings.autoplayVideoOnSlide &&
index === this.core.index) {
this.loadAndPlayVideo(index);
}
};
/**
* @desc Event triggered when video url or poster found
* Append video HTML is poster is not given
* Play if autoplayFirstVideo is true
*
* @param {Event} event - Javascript Event object.
*/
Video.prototype.onHasVideo = function (event) {
var _a = event.detail, index = _a.index, src = _a.src, html5Video = _a.html5Video, hasPoster = _a.hasPoster;
if (!hasPoster) {
// All functions are called separately if poster exist in loadVideoOnPosterClick function
this.appendVideos(this.core.getSlideItem(index), {
src: src,
addClass: 'lg-object',
index: index,
html5Video: html5Video,
});
// Automatically navigate to next slide once video reaches the end.
this.gotoNextSlideOnVideoEnd(src, index);
}
};
/**
* @desc fired immediately before each slide transition.
* Pause the previous video
* Hide the download button if the slide contains YouTube, Vimeo, or Wistia videos.
*
* @param {Event} event - Javascript Event object.
* @param {number} prevIndex - Previous index of the slide.
* @param {number} index - Current index of the slide
*/
Video.prototype.onBeforeSlide = function (event) {
if (this.core.lGalleryOn) {
var prevIndex = event.detail.prevIndex;
this.pauseVideo(prevIndex);
}
};
/**
* @desc fired immediately after each slide transition.
* Play video if autoplayVideoOnSlide option is enabled.
*
* @param {Event} event - Javascript Event object.
* @param {number} prevIndex - Previous index of the slide.
* @param {number} index - Current index of the slide
* @todo should check on onSlideLoad as well if video is not loaded on after slide
*/
Video.prototype.onAfterSlide = function (event) {
var _this = this;
var _a = event.detail, index = _a.index, prevIndex = _a.prevIndex;
// Do not call on first slide
var $slide = this.core.getSlideItem(index);
if (this.settings.autoplayVideoOnSlide && index !== prevIndex) {
if ($slide.hasClass('lg-complete')) {
setTimeout(function () {
_this.loadAndPlayVideo(index);
}, 100);
}
}
};
Video.prototype.loadAndPlayVideo = function (index) {
var $slide = this.core.getSlideItem(index);
var currentGalleryItem = this.core.galleryItems[index];
if (currentGalleryItem.poster) {
this.loadVideoOnPosterClick($slide, true);
}
else {
this.playVideo(index);
}
};
/**
* Play HTML5, Youtube, Vimeo or Wistia videos in a particular slide.
* @param {number} index - Index of the slide
*/
Video.prototype.playVideo = function (index) {
this.controlVideo(index, 'play');
};
/**
* Pause HTML5, Youtube, Vimeo or Wistia videos in a particular slide.
* @param {number} index - Index of the slide
*/
Video.prototype.pauseVideo = function (index) {
this.controlVideo(index, 'pause');
};
Video.prototype.getVideoHtml = function (src, addClass, index, html5Video) {
var video = '';
var videoInfo = this.core.galleryItems[index]
.__slideVideoInfo || {};
var currentGalleryItem = this.core.galleryItems[index];
var videoTitle = currentGalleryItem.title || currentGalleryItem.alt;
videoTitle = videoTitle ? 'title="' + videoTitle + '"' : '';
var commonIframeProps = "allowtransparency=\"true\"\n frameborder=\"0\"\n scrolling=\"no\"\n allowfullscreen\n mozallowfullscreen\n webkitallowfullscreen\n oallowfullscreen\n msallowfullscreen";
if (videoInfo.youtube) {
var videoId = 'lg-youtube' + index;
var slideUrlParams = videoInfo.youtube[2]
? videoInfo.youtube[2] + '&'
: '';
// For youtube first parms gets priority if duplicates found
var youTubePlayerParams = "?" + slideUrlParams + "wmode=opaque&autoplay=0&mute=1&enablejsapi=1";
var playerParams = youTubePlayerParams +
(this.settings.youTubePlayerParams
? '&' + param(this.settings.youTubePlayerParams)
: '');
video = "";
}
else if (videoInfo.vimeo) {
var videoId = 'lg-vimeo' + index;
var playerParams = getVimeoURLParams(this.settings.vimeoPlayerParams, videoInfo);
video = "";
}
else if (videoInfo.wistia) {
var wistiaId = 'lg-wistia' + index;
var playerParams = param(this.settings.wistiaPlayerParams);
playerParams = playerParams ? '?' + playerParams : '';
video = "";
}
else if (videoInfo.html5) {
var html5VideoMarkup = '';
for (var i = 0; i < html5Video.source.length; i++) {
html5VideoMarkup += "";
}
if (html5Video.tracks) {
var _loop_1 = function (i) {
var trackAttributes = '';
var track = html5Video.tracks[i];
Object.keys(track || {}).forEach(function (key) {
trackAttributes += key + "=\"" + track[key] + "\" ";
});
html5VideoMarkup += "