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

2 lines (1 loc) • 3.61 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).lgHash=t()}(this,function(){"use strict";const e="lgAfterSlide",t="lgAfterClose",s={hash:!0,hashDriver:"auto",galleryId:"1",customSlideName:!1};var i=Object.defineProperty,r=Object.getOwnPropertySymbols,n=Object.prototype.hasOwnProperty,l=Object.prototype.propertyIsEnumerable,a=(e,t,s)=>t in e?i(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s,o=(e,t)=>{for(var s in t||(t={}))n.call(t,s)&&a(e,s,t[s]);if(r)for(var s of r(t))l.call(t,s)&&a(e,s,t[s]);return e};return class{constructor(e,t){return this.core=e,this.$LG=t,this.settings=o(o({},s),this.core.settings),this}init(){this.settings.hash&&(this.driver=function(e,t="auto"){const s=e.navigation,i=!!s&&"function"==typeof s.navigate&&"function"==typeof s.addEventListener&&"function"==typeof s.removeEventListener;return"history"!==t&&i?function(e,t){return{kind:"navigation",getHash:()=>{var s;const i=null==(s=t.currentEntry)?void 0:s.url;if("string"==typeof i){const e=i.indexOf("#");return-1===e?"":i.slice(e)}return e.location.hash||""},replaceHash:s=>function(e){var t,s,i,r;const n=e;null==(s=null==(t=null==n?void 0:n.committed)?void 0:t.catch)||s.call(t,()=>{}),null==(r=null==(i=null==n?void 0:n.finished)?void 0:i.catch)||r.call(i,()=>{})}(t.navigate(e.location.pathname+e.location.search+s,{history:"replace",state:{lgHash:s}})),clearHash:()=>e.history.replaceState(null,"",e.location.pathname+e.location.search),subscribe:e=>(t.addEventListener("currententrychange",e),()=>t.removeEventListener("currententrychange",e))}}(e,s):function(e){return{kind:"history",getHash:()=>e.location.hash||"",replaceHash:t=>e.history.replaceState(null,"",e.location.pathname+e.location.search+t),clearHash:()=>e.history.replaceState(null,"",e.location.pathname+e.location.search),subscribe:t=>(e.addEventListener("hashchange",t),()=>e.removeEventListener("hashchange",t))}}(e)}(window,this.settings.hashDriver),this.oldHash=this.driver.getHash(),setTimeout(()=>{this.buildFromHash()},100),this.core.LGel.on(`${e}.hash`,this.onAfterSlide.bind(this)),this.core.LGel.on(`${t}.hash`,this.onCloseAfter.bind(this)),this.unsubscribeDriver=this.driver.subscribe(this.onHashchange.bind(this)))}onAfterSlide(e){let t=this.core.galleryItems[e.detail.index].slideName;t=this.settings.customSlideName&&t||e.detail.index,this.driver.replaceHash("#lg="+this.settings.galleryId+"&slide="+t)}getIndexFromUrl(e=this.driver.getHash()){const t=e.split("&slide=")[1];let s=0;if(this.settings.customSlideName)for(let i=0;i<this.core.galleryItems.length;i++){if(this.core.galleryItems[i].slideName===t){s=i;break}}else s=parseInt(t,10);return isNaN(s)?0:s}buildFromHash(){const e=this.driver.getHash();if(e.indexOf("lg="+this.settings.galleryId)>0){this.$LG(document.body).addClass("lg-from-hash");const t=this.getIndexFromUrl(e);return this.core.openGallery(t),!0}}onCloseAfter(){this.oldHash&&this.oldHash.indexOf("lg="+this.settings.galleryId)<0?this.driver.replaceHash(this.oldHash):this.driver.clearHash()}onHashchange(){if(!this.core.lgOpened)return;const e=this.driver.getHash(),t=this.getIndexFromUrl(e);e.indexOf("lg="+this.settings.galleryId)>-1?this.core.slide(t,!1,!1):this.core.lGalleryOn&&this.core.closeGallery()}closeGallery(){this.settings.hash&&this.$LG(document.body).removeClass("lg-from-hash")}destroy(){var e;this.core.LGel.off(".lg.hash"),this.core.LGel.off(".hash"),null==(e=this.unsubscribeDriver)||e.call(this),this.unsubscribeDriver=void 0}}});