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