UNPKG

lightgallery

Version:

lightGallery is a feature-rich, modular JavaScript gallery plugin for building beautiful image and video galleries for the web and the mobile

9 lines (7 loc) 7.2 kB
/** * lightgallery | 2.1.7 | July 1st 2021 * http://www.lightgalleryjs.com/ * Copyright (c) 2020 Sachin Neravath; * @license GPLv3 */ !function(e,o){"object"==typeof exports&&"undefined"!=typeof module?module.exports=o():"function"==typeof define&&define.amd?define(o):(e="undefined"!=typeof globalThis?globalThis:e||self).lgVideo=o()}(this,function(){"use strict";var o=function(){return(o=Object.assign||function(e){for(var o,i=1,t=arguments.length;i<t;i++)for(var s in o=arguments[i])Object.prototype.hasOwnProperty.call(o,s)&&(e[s]=o[s]);return e}).apply(this,arguments)},i={autoplayFirstVideo:!0,youTubePlayerParams:!1,vimeoPlayerParams:!1,wistiaPlayerParams:!1,gotoNextSlideOnVideoEnd:!0,autoplayVideoOnSlide:!1,videojs:!1,videojsOptions:{}},e="lgHasVideo",t="lgBeforeSlide",s="lgAfterSlide",l="lgPosterClick";function g(o){return Object.keys(o).map(function(e){return encodeURIComponent(e)+"="+encodeURIComponent(o[e])}).join("&")}function n(e){return this.core=e,this.settings=o(o({},i),this.core.settings),this}return n.prototype.init=function(){var o=this;this.core.LGel.on(e+".video",this.onHasVideo.bind(this)),this.core.settings.enableSwipe||this.core.settings.enableDrag?this.core.LGel.on(l+".video",function(){var e=o.core.getSlideItem(o.core.index);o.loadVideoOnPosterClick(e)}):this.core.outer.find(".lg-item").first().on("click.lg",function(){var e=o.core.getSlideItem(o.core.index);o.loadVideoOnPosterClick(e)}),this.core.LGel.on(t+".video",this.onBeforeSlide.bind(this)),this.core.LGel.on(s+".video",this.onAfterSlide.bind(this))},n.prototype.onHasVideo=function(e){var o=e.detail,i=o.index,t=o.src,s=o.html5Video,e=o.hasPoster,o=o.isFirstSlide;e||(this.appendVideos(this.core.getSlideItem(i),{src:t,addClass:"lg-object",index:i,html5Video:s}),this.gotoNextSlideOnVideoEnd(t,i)),this.settings.autoplayFirstVideo&&o&&(e?(e=this.core.getSlideItem(i),this.loadVideoOnPosterClick(e)):this.playVideo(i))},n.prototype.onBeforeSlide=function(e){var o=e.detail,e=o.prevIndex,o=o.index;this.pauseVideo(e);o=this.core.galleryItems[o].__slideVideoInfo||{};(o.youtube||o.vimeo||o.wistia)&&this.core.outer.addClass("lg-hide-download")},n.prototype.onAfterSlide=function(e){var o=this,i=e.detail.index;this.settings.autoplayVideoOnSlide&&this.core.lGalleryOn&&setTimeout(function(){var e=o.core.getSlideItem(i);e.hasClass("lg-video-loaded")?o.playVideo(i):o.loadVideoOnPosterClick(e)},100)},n.prototype.playVideo=function(e){this.controlVideo(e,"play")},n.prototype.pauseVideo=function(e){this.controlVideo(e,"pause")},n.prototype.getVideoHtml=function(e,o,i,t){var s="",l=this.core.galleryItems[i].__slideVideoInfo||{},n=this.core.galleryItems[i],r=(r=n.title||n.alt)?'title="'+r+'"':"",n='allowtransparency="true" \n frameborder="0" \n scrolling="no" \n allowfullscreen \n mozallowfullscreen \n webkitallowfullscreen \n oallowfullscreen \n msallowfullscreen';if(l.youtube)var d="lg-youtube"+i,a="?wmode=opaque&autoplay=0&enablejsapi=1"+(this.settings.youTubePlayerParams?"&"+g(this.settings.youTubePlayerParams):""),s='<iframe allow="autoplay" id='+d+' class="lg-video-object lg-youtube '+o+'" '+r+' src="//www.youtube.com/embed/'+(l.youtube[1]+a)+'" '+n+"></iframe>";else if(l.vimeo){d="lg-vimeo"+i,a=g(this.settings.vimeoPlayerParams);s='<iframe allow="autoplay" id='+d+' class="lg-video-object lg-vimeo '+o+'" '+r+' src="//player.vimeo.com/video/'+(l.vimeo[1]+a)+'" '+n+"></iframe>"}else if(l.wistia){i="lg-wistia"+i,a=g(this.settings.wistiaPlayerParams);s='<iframe allow="autoplay" id="'+i+'" src="//fast.wistia.net/embed/iframe/'+(l.wistia[4]+a)+'" '+r+' class="wistia_embed lg-video-object lg-wistia '+o+'" name="wistia_embed" '+n+"></iframe>"}else if(l.html5){for(var c="",u=0;u<t.source.length;u++)c+='<source src="'+t.source[u].src+'" type="'+t.source[u].type+'">';var f="",h=t.attributes||{};Object.keys(h||{}).forEach(function(e){f+=e+'="'+h[e]+'" '}),s='<video class="lg-video-object lg-html5 '+(this.settings.videojs?"video-js":"")+'" '+f+">\n "+c+"\n Your browser does not support HTML5 video.\n </video>"}return s},n.prototype.appendVideos=function(e,o){var i,t=this.getVideoHtml(o.src,o.addClass,o.index,o.html5Video);e.find(".lg-video-cont").append(t);var s=e.find(".lg-video-object").first();if(this.settings.videojs&&null!==(i=this.core.galleryItems[o.index].__slideVideoInfo)&&void 0!==i&&i.html5)try{return videojs(s.get(),this.settings.videojsOptions)}catch(e){console.error("lightGallery:- Make sure you have included videojs")}},n.prototype.gotoNextSlideOnVideoEnd=function(e,o){var i=this,t=this.core.getSlideItem(o).find(".lg-video-object").first(),o=this.core.galleryItems[o].__slideVideoInfo||{};if(this.settings.gotoNextSlideOnVideoEnd)if(o.html5)t.on("ended",function(){i.core.goToNextSlide()});else if(o.vimeo)try{new Vimeo.Player(t.get()).on("ended",function(){i.core.goToNextSlide()})}catch(e){console.error("lightGallery:- Make sure you have included //github.com/vimeo/player.js")}else if(o.wistia)try{window._wq=window._wq||[],window._wq.push({id:t.attr("id"),onReady:function(e){e.bind("end",function(){i.core.goToNextSlide()})}})}catch(e){console.error("lightGallery:- Make sure you have included //fast.wistia.com/assets/external/E-v1.js")}},n.prototype.controlVideo=function(e,o){var i=this.core.getSlideItem(e).find(".lg-video-object").first(),e=this.core.galleryItems[e].__slideVideoInfo||{};if(i.get())if(e.youtube)try{i.get().contentWindow.postMessage('{"event":"command","func":"'+o+'Video","args":""}',"*")}catch(e){console.error("lightGallery:- "+e)}else if(e.vimeo)try{new Vimeo.Player(i.get())[o]()}catch(e){console.error("lightGallery:- Make sure you have included //github.com/vimeo/player.js")}else if(e.html5)if(this.settings.videojs)try{videojs(i.get())[o]()}catch(e){console.error("lightGallery:- Make sure you have included videojs")}else i.get()[o]();else if(e.wistia)try{window._wq=window._wq||[],window._wq.push({id:i.attr("id"),onReady:function(e){e[o]()}})}catch(e){console.error("lightGallery:- Make sure you have included //fast.wistia.com/assets/external/E-v1.js")}},n.prototype.loadVideoOnPosterClick=function(e){var o,i,t,s,l=this;e.hasClass("lg-video-loaded")||(e.hasClass("lg-has-video")?this.playVideo(this.core.index):(e.addClass("lg-has-video"),o=void 0,s=this.core.galleryItems[this.core.index].src,(i=this.core.galleryItems[this.core.index].video)&&(o="string"==typeof i?JSON.parse(i):i),t=this.appendVideos(e,{src:s,addClass:"",index:this.core.index,html5Video:o}),this.gotoNextSlideOnVideoEnd(s,this.core.index),s=e.find(".lg-object").first().get(),e.find(".lg-video-cont").first().append(s),e.addClass("lg-video-loading"),t&&t.ready(function(){t.on("loadedmetadata",function(){l.onVideoLoadAfterPosterClick(e,l.core.index)})}),e.find(".lg-video-object").first().on("load.lg error.lg loadeddata.lg",function(){setTimeout(function(){l.onVideoLoadAfterPosterClick(e,l.core.index)},50)})))},n.prototype.onVideoLoadAfterPosterClick=function(e,o){e.addClass("lg-video-loaded"),this.playVideo(o)},n.prototype.destroy=function(){this.core.LGel.off(".lg.video"),this.core.LGel.off(".video")},n});