forked from CodeByZach/pace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpace.min.js
1 lines (1 loc) · 12.3 KB
/
pace.min.js
1
(function(){var e,t,n,r,s,i,o,u,a,l,c,f,p,h,g,d,m,y,v,w,b,k,S,q,L,x,P,T,R,j,E,O,M,A,N,_,F,C,U,W,X,D,H,I,z,G,B,J,K,Q=[].slice,V={}.hasOwnProperty,Y=function(e,t){function r(){this.constructor=e}for(var n in t)V.call(t,n)&&(e[n]=t[n]);return r.prototype=t.prototype,e.prototype=new r,e.__super__=t.prototype,e},Z=[].indexOf||function(e){for(var t=0,n=this.length;n>t;t++)if(t in this&&this[t]===e)return t;return-1};for(b={catchupTime:100,initialRate:.03,minTime:250,ghostTime:100,maxProgressPerFrame:20,easeFactor:1.25,startOnPageLoad:!0,restartOnPushState:!0,restartOnRequestAfter:500,target:"body",elements:{checkInterval:100,selectors:["body"]},eventLag:{minSamples:10,sampleCount:3,lagThreshold:3},ajax:{trackMethods:["GET"],trackWebSockets:!0,ignoreURLs:[]}},R=function(){var e;return null!=(e="undefined"!=typeof performance&&null!==performance&&"function"==typeof performance.now?performance.now():void 0)?e:+new Date},E=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.msRequestAnimationFrame,w=window.cancelAnimationFrame||window.mozCancelAnimationFrame,null==E&&(E=function(e){return setTimeout(e,50)},w=function(e){return clearTimeout(e)}),M=function(e){var t,n;return t=R(),(n=function(){var r;return r=R()-t,r>=33?(t=R(),e(r,function(){return E(n)})):setTimeout(n,33-r)})()},O=function(){var e,t,n;return n=arguments[0],t=arguments[1],e=3<=arguments.length?Q.call(arguments,2):[],"function"==typeof n[t]?n[t].apply(n,e):n[t]},k=function(){var e,t,n,r,s,i,o;for(t=arguments[0],r=2<=arguments.length?Q.call(arguments,1):[],i=0,o=r.length;o>i;i++)if(n=r[i])for(e in n)V.call(n,e)&&(s=n[e],null!=t[e]&&"object"==typeof t[e]&&null!=s&&"object"==typeof s?k(t[e],s):t[e]=s);return t},m=function(e){var t,n,r,s,i;for(n=t=0,s=0,i=e.length;i>s;s++)r=e[s],n+=Math.abs(r),t++;return n/t},q=function(e,t){var n,r,s;if(null==e&&(e="options"),null==t&&(t=!0),s=document.querySelector("[data-pace-"+e+"]")){if(n=s.getAttribute("data-pace-"+e),!t)return n;try{return JSON.parse(n)}catch(i){return r=i,"undefined"!=typeof console&&null!==console?console.error("Error parsing inline pace options",r):void 0}}},o=function(){function e(){}return e.prototype.on=function(e,t,n,r){var s;return null==r&&(r=!1),null==this.bindings&&(this.bindings={}),null==(s=this.bindings)[e]&&(s[e]=[]),this.bindings[e].push({handler:t,ctx:n,once:r})},e.prototype.once=function(e,t,n){return this.on(e,t,n,!0)},e.prototype.off=function(e,t){var n,r,s;if(null!=(null!=(r=this.bindings)?r[e]:void 0)){if(null==t)return delete this.bindings[e];for(n=0,s=[];n<this.bindings[e].length;)this.bindings[e][n].handler===t?s.push(this.bindings[e].splice(n,1)):s.push(n++);return s}},e.prototype.trigger=function(){var e,t,n,r,s,i,o,u,a;if(n=arguments[0],e=2<=arguments.length?Q.call(arguments,1):[],null!=(o=this.bindings)?o[n]:void 0){for(s=0,a=[];s<this.bindings[n].length;)u=this.bindings[n][s],r=u.handler,t=u.ctx,i=u.once,r.apply(null!=t?t:this,e),i?a.push(this.bindings[n].splice(s,1)):a.push(s++);return a}},e}(),l=window.Pace||{},window.Pace=l,k(l,o.prototype),j=l.options=k({},b,window.paceOptions,q()),B=["ajax","document","eventLag","elements"],H=0,z=B.length;z>H;H++)F=B[H],j[F]===!0&&(j[F]=b[F]);a=function(e){function t(){return J=t.__super__.constructor.apply(this,arguments)}return Y(t,e),t}(Error),t=function(){function e(){this.progress=0}return e.prototype.getElement=function(){var e;if(null==this.el){if(e=document.querySelector(j.target),!e)throw new a;this.el=document.createElement("div"),this.el.className="pace pace-active",document.body.className=document.body.className.replace(/pace-done/g,""),document.body.className+=" pace-running",this.el.innerHTML='<div class="pace-progress">\n <div class="pace-progress-inner"></div>\n</div>\n<div class="pace-activity"></div>',null!=e.firstChild?e.insertBefore(this.el,e.firstChild):e.appendChild(this.el)}return this.el},e.prototype.finish=function(){var e;return e=this.getElement(),e.className=e.className.replace("pace-active",""),e.className+=" pace-inactive",document.body.className=document.body.className.replace("pace-running",""),document.body.className+=" pace-done"},e.prototype.update=function(e){return this.progress=e,this.render()},e.prototype.destroy=function(){try{this.getElement().parentNode.removeChild(this.getElement())}catch(e){a=e}return this.el=void 0},e.prototype.render=function(){var e,t,n,r,s,i,o;if(null==document.querySelector(j.target))return!1;for(e=this.getElement(),r="translate3d("+this.progress+"%, 0, 0)",o=["webkitTransform","msTransform","transform"],s=0,i=o.length;i>s;s++)t=o[s],e.children[0].style[t]=r;return(!this.lastRenderedProgress||this.lastRenderedProgress|0!==this.progress|0)&&(e.children[0].setAttribute("data-progress-text",""+(0|this.progress)+"%"),this.progress>=100?n="99":(n=this.progress<10?"0":"",n+=0|this.progress),e.children[0].setAttribute("data-progress",""+n)),l.trigger("change",this.progress),this.lastRenderedProgress=this.progress},e.prototype.done=function(){return this.progress>=100},e}(),u=function(){function e(){this.bindings={}}return e.prototype.trigger=function(e,t){var n,r,s,i,o;if(null!=this.bindings[e]){for(i=this.bindings[e],o=[],r=0,s=i.length;s>r;r++)n=i[r],o.push(n.call(this,t));return o}},e.prototype.on=function(e,t){var n;return null==(n=this.bindings)[e]&&(n[e]=[]),this.bindings[e].push(t)},e}(),D=window.XMLHttpRequest,X=window.XDomainRequest,W=window.WebSocket,S=function(e,t){var n,r,s;s=[];for(r in t.prototype)try{null==e[r]&&"function"!=typeof t[r]?"function"==typeof Object.defineProperty?s.push(Object.defineProperty(e,r,{get:function(){return t.prototype[r]},configurable:!0,enumerable:!0})):s.push(e[r]=t.prototype[r]):s.push(void 0)}catch(i){n=i}return s},P=[],l.ignore=function(){var e,t,n;return t=arguments[0],e=2<=arguments.length?Q.call(arguments,1):[],P.unshift("ignore"),n=t.apply(null,e),P.shift(),n},l.track=function(){var e,t,n;return t=arguments[0],e=2<=arguments.length?Q.call(arguments,1):[],P.unshift("track"),n=t.apply(null,e),P.shift(),n},_=function(e){var t;if(null==e&&(e="GET"),"track"===P[0])return"force";if(!P.length&&j.ajax){if("socket"===e&&j.ajax.trackWebSockets)return!0;if(t=e.toUpperCase(),Z.call(j.ajax.trackMethods,t)>=0)return!0}return!1},c=function(e){function t(){var e,n=this;t.__super__.constructor.apply(this,arguments),e=function(e){var t;return t=e.open,e.open=function(r,s,i){return _(r)&&n.trigger("request",{type:r,url:s,request:e}),t.apply(e,arguments)}},window.XMLHttpRequest=function(t){var n;return n=new D(t),e(n),n};try{S(window.XMLHttpRequest,D)}catch(r){}if(null!=X){window.XDomainRequest=function(){var t;return t=new X,e(t),t};try{S(window.XDomainRequest,X)}catch(r){}}if(null!=W&&j.ajax.trackWebSockets){window.WebSocket=function(e,t){var r;return r=null!=t?new W(e,t):new W(e),_("socket")&&n.trigger("request",{type:"socket",url:e,protocols:t,request:r}),r};try{S(window.WebSocket,W)}catch(r){}}}return Y(t,e),t}(u),I=null,L=function(){return null==I&&(I=new c),I},N=function(e){var t,n,r,s;for(s=j.ajax.ignoreURLs,n=0,r=s.length;r>n;n++)if(t=s[n],"string"==typeof t){if(-1!==e.indexOf(t))return!0}else if(t.test(e))return!0;return!1},L().on("request",function(t){var n,r,s,i,o;return i=t.type,s=t.request,o=t.url,N(o)?void 0:l.running||j.restartOnRequestAfter===!1&&"force"!==_(i)?void 0:(r=arguments,n=j.restartOnRequestAfter||0,"boolean"==typeof n&&(n=0),setTimeout(function(){var t,n,o,u,a,c;if(t="socket"===i?s.readyState<2:0<(u=s.readyState)&&4>u){for(l.restart(),a=l.sources,c=[],n=0,o=a.length;o>n;n++){if(F=a[n],F instanceof e){F.watch.apply(F,r);break}c.push(void 0)}return c}},n))}),e=function(){function e(){var e=this;this.elements=[],L().on("request",function(){return e.watch.apply(e,arguments)})}return e.prototype.watch=function(e){var t,n,r,s;return r=e.type,t=e.request,s=e.url,N(s)?void 0:(n="socket"===r?new h(t):new g(t),this.elements.push(n))},e}(),g=function(){function e(e){var t,n,r,s,i,o,u=this;if(this.progress=0,null!=window.ProgressEvent)for(n=null,e.addEventListener("progress",function(e){return e.lengthComputable?u.progress=100*e.loaded/e.total:u.progress=u.progress+(100-u.progress)/2},!1),o=["load","abort","timeout","error"],r=0,s=o.length;s>r;r++)t=o[r],e.addEventListener(t,function(){return u.progress=100},!1);else i=e.onreadystatechange,e.onreadystatechange=function(){var t;return 0===(t=e.readyState)||4===t?u.progress=100:3===e.readyState&&(u.progress=50),"function"==typeof i?i.apply(null,arguments):void 0}}return e}(),h=function(){function e(e){var t,n,r,s,i=this;for(this.progress=0,s=["error","open"],n=0,r=s.length;r>n;n++)t=s[n],e.addEventListener(t,function(){return i.progress=100},!1)}return e}(),r=function(){function e(e){var t,n,r,i;for(null==e&&(e={}),this.elements=[],null==e.selectors&&(e.selectors=[]),i=e.selectors,n=0,r=i.length;r>n;n++)t=i[n],this.elements.push(new s(t))}return e}(),s=function(){function e(e){this.selector=e,this.progress=0,this.check()}return e.prototype.check=function(){var e=this;return document.querySelector(this.selector)?this.done():setTimeout(function(){return e.check()},j.elements.checkInterval)},e.prototype.done=function(){return this.progress=100},e}(),n=function(){function e(){var e,t,n=this;this.progress=null!=(t=this.states[document.readyState])?t:100,e=document.onreadystatechange,document.onreadystatechange=function(){return null!=n.states[document.readyState]&&(n.progress=n.states[document.readyState]),"function"==typeof e?e.apply(null,arguments):void 0}}return e.prototype.states={loading:0,interactive:50,complete:100},e}(),i=function(){function e(){var e,t,n,r,s,i=this;this.progress=0,e=0,s=[],r=0,n=R(),t=setInterval(function(){var o;return o=R()-n-50,n=R(),s.push(o),s.length>j.eventLag.sampleCount&&s.shift(),e=m(s),++r>=j.eventLag.minSamples&&e<j.eventLag.lagThreshold?(i.progress=100,clearInterval(t)):i.progress=100*(3/(e+3))},50)}return e}(),p=function(){function e(e){this.source=e,this.last=this.sinceLastUpdate=0,this.rate=j.initialRate,this.catchup=0,this.progress=this.lastProgress=0,null!=this.source&&(this.progress=O(this.source,"progress"))}return e.prototype.tick=function(e,t){var n;return null==t&&(t=O(this.source,"progress")),t>=100&&(this.done=!0),t===this.last?this.sinceLastUpdate+=e:(this.sinceLastUpdate&&(this.rate=(t-this.last)/this.sinceLastUpdate),this.catchup=(t-this.progress)/j.catchupTime,this.sinceLastUpdate=0,this.last=t),t>this.progress&&(this.progress+=this.catchup*e),n=1-Math.pow(this.progress/100,j.easeFactor),this.progress+=n*this.rate*e,this.progress=Math.min(this.lastProgress+j.maxProgressPerFrame,this.progress),this.progress=Math.max(0,this.progress),this.progress=Math.min(100,this.progress),this.lastProgress=this.progress,this.progress},e}(),C=null,A=null,y=null,U=null,d=null,v=null,l.running=!1,x=function(){return j.restartOnPushState?l.restart():void 0},null!=window.history.pushState&&(G=window.history.pushState,window.history.pushState=function(){return x(),G.apply(window.history,arguments)}),null!=window.history.replaceState&&(K=window.history.replaceState,window.history.replaceState=function(){return x(),K.apply(window.history,arguments)}),f={ajax:e,elements:r,document:n,eventLag:i},(T=function(){var e,n,r,s,i,o,u,a;for(l.sources=C=[],o=["ajax","elements","document","eventLag"],n=0,s=o.length;s>n;n++)e=o[n],j[e]!==!1&&C.push(new f[e](j[e]));for(a=null!=(u=j.extraSources)?u:[],r=0,i=a.length;i>r;r++)F=a[r],C.push(new F(j));return l.bar=y=new t,A=[],U=new p})(),l.stop=function(){return l.trigger("stop"),l.running=!1,y.destroy(),v=!0,null!=d&&("function"==typeof w&&w(d),d=null),T()},l.restart=function(){return l.trigger("restart"),l.stop(),l.start()},l.go=function(){var e;return l.running=!0,y.render(),e=R(),v=!1,d=M(function(t,n){var r,s,i,o,u,a,c,f,h,g,d,m,w,b,k,S;for(f=100-y.progress,s=d=0,i=!0,a=m=0,b=C.length;b>m;a=++m)for(F=C[a],g=null!=A[a]?A[a]:A[a]=[],u=null!=(S=F.elements)?S:[F],c=w=0,k=u.length;k>w;c=++w)o=u[c],h=null!=g[c]?g[c]:g[c]=new p(o),i&=h.done,h.done||(s++,d+=h.tick(t));return r=d/s,y.update(U.tick(t,r)),y.done()||i||v?(y.update(100),l.trigger("done"),setTimeout(function(){return y.finish(),l.running=!1,l.trigger("hide")},Math.max(j.ghostTime,Math.max(j.minTime-(R()-e),0)))):n()})},l.start=function(e){k(j,e),l.running=!0;try{y.render()}catch(t){a=t}return document.querySelector(".pace")?(l.trigger("start"),l.go()):setTimeout(l.start,50)},"function"==typeof define&&define.amd?define(["pace"],function(){return l}):"object"==typeof exports?module.exports=l:j.startOnPageLoad&&l.start()}).call(this);