1 line
7.4 KiB
JavaScript
1 line
7.4 KiB
JavaScript
|
|
(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(var b,c=1;c<arguments.length;c++)if(b=arguments[c],b)for(var d in b)b.hasOwnProperty(d)&&(a[d]=b[d]);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){var 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:function(b){b=this.options=a({},h.defaults,b);var c,d=this;b.$items&&(c=b.$items.get()),b.elements&&(c=[].slice.call("string"==typeof b.elements?document.querySelectorAll(b.elements):b.elements)),this.eventRegistry={lightbox:[],thumbnails:[]},this.items=[],this.captions=[],c&&c.forEach(function(a,c){d.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)),d.captions.push(e),b.bindToItems&&d.addEvent(a,"click",function(a){a.preventDefault(),d.showPosition(c)},"thumbnails")}),b.items&&(this.items=b.items),b.captions&&(this.captions=b.captions)},addEvent:function(a,b,c,d){return this.eventRegistry[d||"lightbox"].push({element:a,eventName:b,callback:c}),a.addEventListener(b,c),this},removeEvents:function(a){return this.eventRegistry[a].forEach(function(a){a.element.removeEventListener(a.eventName,a.callback)}),this.eventRegistry[a]=[],this},next:function(){return this.showPosition(this.currentPosition+1)},prev:function(){return this.showPosition(this.currentPosition-1)},normalizePosition:function(a){return a>=this.items.length?a=0:0>a&&(a=this.items.length-1),a},showPosition:function(a){var 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:function(a){var 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:function(a){for(var b="//_URL_",c=[{rx:/^(?:https?:)?\/\/(?:www\.)?vimeo\.com\/([^\?&"]+).*$/g,tmpl:b.replace("_URL_","player.vimeo.com/video/$1")},{rx:/^.*(?:https?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=|embed\/|v\/|user\/.+\/)?([^\?&"]+).*$/g,tmpl:b.replace("_URL_","www.youtube.com/embed/$1")},{rx:/^.*(?:https?:\/\/)?(?:www\.)?(?:youtube-nocookie\.com)\/(?:watch\?v=|embed\/|v\/|user\/.+\/)?([^\?&"]+).*$/g,tmpl:b.replace("_URL_","www.youtube-nocookie.com/embed/$1")}],d=0;d<c.length;d++)if(c[d].rx.test(a))return a.replace(c[d].rx,c[d].tmpl);return a},prepareItem:function(a,b){var c=this,e=this.items[a];if(this.loading(!0),this.options.videoRegex.test(e)){var f=this.getVideoURL(e);b.call(c,d("<div class=\"slbIframeCont\"><iframe class=\"slbIframe\" frameborder=\"0\" allowfullscreen src=\""+f+"\"></iframe></div>"))}else{var g=d("<div class=\"slbImageWrap\"><img
|