-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslider.min.js
1 lines (1 loc) · 4.94 KB
/
slider.min.js
1
var Slider=function(){function a(f,g,h){return"undefined"==typeof f[g]?h:f[g]}function b(f,g){var h=this;if("undefined"==typeof f)throw new Error("Parameter 1 must be set");if("string"==typeof f){if(f=document.getElementById(f),"undefined"==typeof f)throw new Error("The container was not found");}else if(!(f instanceof Node))throw new Error("Parameter 1 must be either string or DOM-Node");if("undefined"==typeof g)g={};else if("object"!=typeof g)throw new Error("Parameter 2 must be object");this.container=f,this.slides=[];var j=null,k=-1,l={};this.prev=function(m){if("undefined"==typeof m&&(m=1),!(0>=m)){var n=k-m;0>n&&(n=this.slides.length-1),a(g.events,"prev",function(){})(),h.showSlide(n)}},this.next=function(m){if("undefined"==typeof m&&(m=1),!(0>=m)){var n=k+m;n>=this.slides.length&&(n=0),a(g.events,"next",function(){})(),h.showSlide(n)}},this.showSlide=function(m){if("undefined"==typeof m&&(amount=0),this.slides[m]){g.playing&&h.resetTimeout(),a(g.events,"change_slide",function(){})(m,this.slides[m]),k=m;for(var n=0;n<this.slides.length;n++)this.slides[n].classList.remove("active"),l.indicators_list&&l.indicators_list[n].classList.remove("active");this.slides[m].classList.add("active"),l.indicators_list&&l.indicators_list[m].classList.add("active"),l.slides_container&&(l.slides_container.style.left="-"+100*parseInt(m)+"%"),l.indicator_select&&(l.indicator_select.selectedIndex=m)}},this.pause=function(){j&&(clearTimeout(j),j=null),g.playing=!1,l.pause&&(l.pause.innerHTML=a(g,"play_icon",e.pause)),a(g.events,"pause",function(){})()},this.play=function(){h.resetTimeout(),g.playing=!0,l.pause&&(l.pause.innerHTML=a(g,"pause_icon",e.play)),a(g.events,"play",function(){})()},this.togglePlay=function(){j?h.pause():h.play(),a(g.events,"toggle_play",function(){})()},this.isPlaying=function(){return!!j},this.resetTimeout=function(){j&&clearTimeout(j),j=setTimeout(function(){a(g,"play_direction",b.FORWARDS)==b.BACKWARDS?h.prev():h.next()},1e3*c)},this.showUI=function(m){if(m instanceof Array){l.prev.classList.add("hidden"),l.next.classList.add("hidden"),l.pause.classList.add("hidden"),l.indicators.classList.add("hidden");for(var n=0;n<m.length;n++)switch(m[n]){case b.UI.PREV:{l.prev.classList.remove("hidden");break}case b.UI.NEXT:{l.next.classList.remove("hidden");break}case b.UI.PAUSE:{l.pause.classList.remove("hidden");break}case b.UI.INDICATORS:{l.indicators.classList.remove("hidden");break}}}else"number"==typeof m?h.showUI([m]):"boolean"==typeof m?m?h.showUI([b.UI.PREV,b.UI.NEXT,b.UI.PAUSE,b.UI.INDICATORS]):h.showUI([]):h.showUI(!0)},this.updateView=function(){f.innerHTML="";var m=document.createElement("div");m.className="slides";for(var n=0;n<h.slides.length;n++)m.appendChild(h.slides[n]);l.slides_container=m,f.appendChild(m);var o=document.createElement("div");o.className="slider_btn_nav slider_btn_prev slider_controls",o.innerHTML=a(g,"prev_icon",e.prev),o.addEventListener("click",function(){h.prev()}),l.prev=o;var p=document.createElement("div");p.className="slider_btn_nav slider_btn_next slider_controls",p.innerHTML=a(g,"next_icon",e.next),p.addEventListener("click",function(){h.next()}),l.next=p;var q=document.createElement("div");q.className="slider_btn_pause slider_controls",q.addEventListener("click",function(){h.togglePlay()}),l.pause=q;var r=document.createElement("div");r.className="slider_indicators slider_controls",l.indicators=r,l.indicators_list=[];var s=document.createElement("select");s.className="slider_indicator_select",s.onchange=function(){h.showSlide(this.value)};for(var t=a(g,"indicator_icon",""),n=0;n<h.slides.length;n++)(function(v){var w=document.createElement("div");w.className="slider_indicator",""==t?w.classList.add("slider_indicator_icon"):w.innerHTML=t,w.addEventListener("click",function(){h.showSlide(v)}),l.indicators_list.push(w),r.appendChild(w);var x=document.createElement("option");x.value=v,x.innerHTML=v+1,-1==k&&0==v?x.selected="true":k==v&&(x.selected="true"),s.appendChild(x)})(n);l.indicator_select=s,f.appendChild(o),f.appendChild(p),f.appendChild(q),f.appendChild(r),f.appendChild(s),-1==k?h.showSlide(a(g,"start_slide",0)):h.showSlide(k),g.playing=a(g,"playing",!0),g.playing?h.play():h.pause();for(var u=m.getElementsByTagName("img"),n=0;n<u.length;n++)u[n].getAttribute("data-src-async")&&(u[n].src=u[n].getAttribute("data-src-async"))},function(){for(var m=f.getElementsByClassName("slide"),n=0;n<m.length;n++)h.slides.push(m[n]);f.classList.add("slider"),"object"!=typeof g.events&&(g.events={}),h.updateView(),a(g,"show_ui",!0)||h.showUI(!1),a(g.events,"load",function(){})(l.slides_container)}(),d.push(this)}var c=5,d=[];const e={pause:"<span>▶</span>",play:"<span>⏸</span>",next:"<span>></span>",prev:"<span><</span>"};return b.setDelay=function(f){if("number"==typeof f){c=f;for(var g=0;g<d.length;g++)d[g].resetTimeout()}else console.warn("Parameter 1 must be a number")},b.getDelay=function(){return c},b.FORWARDS="play_direction_next",b.BACKWARDS="play_direction_prev",b.UI={PREV:0,NEXT:1,PAUSE:2,INDICATORS:3},b}();