Files

1 line
7.3 KiB
JavaScript
Raw Permalink Normal View History

(function(a,b){"function"==typeof define&&define.amd?define([],b):"object"==typeof module&&module.exports?module.exports=b():a.SimpleLightbox=b()})(this,function(){function a(a){for(let b=1;b<arguments.length;b++){const c=arguments[b];if(c)for(const b in c)c.hasOwnProperty(b)&&(a[b]=c[b])}return a}function b(a,b){a&&b&&(a.className+=" "+b)}function c(a,b){a&&b&&(a.className=a.className.replace(new RegExp("(\\s|^)"+b+"(\\s|$)")," ").trim())}function d(a){const b=document.createElement("div");return b.innerHTML=a.trim(),b.childNodes[0]}function f(a,b){return(a.matches||a.matchesSelector||a.msMatchesSelector).call(a,b)}function g(){return"innerHeight"in window?window.innerHeight:document.documentElement.offsetHeight}function h(){this.init.apply(this,arguments)}return h.defaults={elementClass:"",elementLoadingClass:"slbLoading",htmlClass:"slbActive",closeBtnClass:"",nextBtnClass:"",prevBtnClass:"",loadingTextClass:"",closeBtnCaption:"Close",nextBtnCaption:"Next",prevBtnCaption:"Previous",loadingCaption:"Loading...",bindToItems:!0,closeOnOverlayClick:!0,closeOnEscapeKey:!0,nextOnImageClick:!0,showCaptions:!0,captionAttribute:"title",urlAttribute:"href",startAt:0,loadingTimeout:100,appendTarget:"body",beforeSetContent:null,beforeClose:null,afterClose:null,beforeDestroy:null,afterDestroy:null,videoRegex:new RegExp(/youtube.com|youtu.be|vimeo.com/)},a(h.prototype,{init(b){b=this.options=a({},h.defaults,b);const c=this;let d;b.$items&&(d=b.$items.get()),b.elements&&(d=[].slice.call("string"==typeof b.elements?document.querySelectorAll(b.elements):b.elements)),this.eventRegistry={lightbox:[],thumbnails:[]},this.items=[],this.captions=[],d&&d.forEach(function(a,d){c.items.push(a.getAttribute(b.urlAttribute));var e=a.getAttribute(b.captionAttribute);e||(e=a.querySelector("img")?.getAttribute(b.captionAttribute),!e&&(e=a.parentElement?.querySelector("figcaption")?.textContent)),c.captions.push(e),b.bindToItems&&c.addEvent(a,"click",function(a){a.preventDefault(),c.showPosition(d)},"thumbnails")}),b.items&&(this.items=b.items),b.captions&&(this.captions=b.captions)},addEvent(a,b,c,d){return this.eventRegistry[d||"lightbox"].push({element:a,eventName:b,callback:c}),a.addEventListener(b,c),this},removeEvents(a){return this.eventRegistry[a].forEach(function(a){a.element.removeEventListener(a.eventName,a.callback)}),this.eventRegistry[a]=[],this},next(){return this.showPosition(this.currentPosition+1)},prev(){return this.showPosition(this.currentPosition-1)},normalizePosition(a){return a>=this.items.length?a=0:0>a&&(a=this.items.length-1),a},showPosition(a){const b=this.normalizePosition(a);return"undefined"!=typeof this.currentPosition&&(this.direction=b>this.currentPosition?"next":"prev"),this.currentPosition=b,this.setupLightboxHtml().prepareItem(this.currentPosition,this.setContent).show()},loading(a){const d=this,e=this.options;a?this.loadingTimeout=setTimeout(function(){b(d.$el,e.elementLoadingClass),d.$content.innerHTML="<p class=\"slbLoadingText "+e.loadingTextClass+"\">"+e.loadingCaption+"</p>",d.show()},e.loadingTimeout):(c(this.$el,e.elementLoadingClass),clearTimeout(this.loadingTimeout))},getVideoURL(a){const b=[{rx:/^(?:https?:)?\/\/(?:www\.)?vimeo\.com\/([^\?&"]+).*$/g,tmpl:"//_URL_".replace("_URL_","player.vimeo.com/video/$1")},{rx:/^.*(?:https?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=|embed\/|v\/|user\/.+\/)?([^\?&"]+).*$/g,tmpl:"//_URL_".replace("_URL_","www.youtube.com/embed/$1")},{rx:/^.*(?:https?:\/\/)?(?:www\.)?(?:youtube-nocookie\.com)\/(?:watch\?v=|embed\/|v\/|user\/.+\/)?([^\?&"]+).*$/g,tmpl:"//_URL_".replace("_URL_","www.youtube-nocookie.com/embed/$1")}];for(let c=0;c<b.length;c++)if(b[c].rx.test(a))return a.replace(b[c].rx,b[c].tmpl);return a},prepareItem(a,b){const c=this,e=this.items[a];if(this.loading(!0),this.options.videoRegex.test(e)){const a=this.getVideoURL(e);b.call(c,d("<div class=\"slbIframeCont\"><iframe class=\"slbIframe\" frameborder=\"0\" allowfullscreen src=\""+a+"\"></iframe></div>"))}else{const f=d("<div class=\"slbImageWrap\"><img class=\"slbImage\" src=\""+e+"\" /></div>");thi