riot-starter/app/vendor/riot-route/route.min.js

1 line
3.8 KiB
JavaScript
Raw Normal View History

var route=function(){"use strict";var e=function(u){u=u||{};var o={},l=Array.prototype.slice;Object.defineProperties(u,{on:{value:function(e,t){if(typeof t=="function"){(o[e]=o[e]||[]).push(t)}return u},enumerable:false,writable:false,configurable:false},off:{value:function(e,t){if(e=="*"&&!t){o={}}else{if(t){var n=o[e];for(var r=0,i;i=n&&n[r];++r){if(i==t){n.splice(r--,1)}}}else{delete o[e]}}return u},enumerable:false,writable:false,configurable:false},one:{value:function(e,t){function n(){u.off(e,n);t.apply(u,arguments)}return u.on(e,n)},enumerable:false,writable:false,configurable:false},trigger:{value:function(e){var t=arguments;var n=arguments.length-1,r=new Array(n),i,a,f;for(f=0;f<n;f++){r[f]=t[f+1]}i=l.call(o[e]||[],0);for(f=0;a=i[f];++f){a.apply(u,r)}if(o["*"]&&e!="*"){u.trigger.apply(u,["*",e].concat(r))}return u},enumerable:false,writable:false,configurable:false}});return u};var r=/^.+?\/\/+[^/]+/,t="EventListener",n="remove"+t,i="add"+t,a="hasAttribute",f="popstate",u="hashchange",o="trigger",l=3,s=typeof window!="undefined"&&window,c=typeof document!="undefined"&&document,h=s&&history,p=s&&(h.location||s.location),d=P.prototype,v=c&&c.ontouchstart?"touchstart":"click",m=e();var b=false,g=false,y,w,$,_,A=[],S=0;function x(e){return e.split(/[/?#]/)}function K(e,t){var n=t.replace(/\?/g,"\\?").replace(/\*/g,"([^/?#]+?)").replace(/\.\./,".*");var r=new RegExp("^"+n+"$");var i=e.match(r);if(i){return i.slice(1)}}function N(e,t){var n;return function(){clearTimeout(n);n=setTimeout(e,t)}}function O(e){y=N(q,1);s[i](f,y);s[i](u,y);c[i](v,B);if(e){q(true)}}function P(){this.$=[];e(this);m.on("stop",this.s.bind(this));m.on("emit",this.e.bind(this))}function T(e){return e.replace(/^\/|\/$/,"")}function E(e){return typeof e=="string"}function j(e){return(e||p.href).replace(r,"")}function k(e){var t=L._.base;return t[0]==="#"?(e||p.href||"").split(t)[1]||"":(p?j(e):e||"").replace(t,"")}function q(t){var e=S===0;if(l<=S){return}S++;A.push(function(){var e=k();if(t||e!==w){m[o]("emit",e);w=e}});if(e){var n;while(n=A.shift()){n()}S=0}}function B(e){if(e.which!==1||e.metaKey||e.ctrlKey||e.shiftKey||e.defaultPrevented){return}var t=e.target;while(t&&t.nodeName!=="A"){t=t.parentNode}if(!t||t.nodeName!=="A"||t[a]("download")||!t[a]("href")||t.target&&t.target!=="_self"||t.href.indexOf(p.href.match(r)[0])===-1){return}var n=L._.base;if(t.href!==p.href&&(t.href.split("#")[0]===p.href.split("#")[0]||n[0]!=="#"&&j(t.href).indexOf(n)!==0||n[0]==="#"&&t.href.split(n)[0]!==p.href.split(n)[0]||!D(k(t.href),t.title||c.title))){return}e.preventDefault()}function D(e,t,n){if(!h){return m[o]("emit",k(e))}e=L._.base+T(e);t=t||c.title;n?h.replaceState(null,t,e):h.pushState(null,t,e);c.title=t;g=false;q();return g}d.m=function(e,t,n){if(E(e)&&(!t||E(t))){D(e,t,n||false)}else if(t){this.r(e,t)}else{this.r("@",e)}};d.s=function(){this.off("*");this.$=[]};d.e=function(n){this.$.concat("@").some(function(e){var t=(e==="@"?$:_)(T(n),T(e));if(typeof t!="undefined"){this[o].apply(null,[e].concat(t));return g=true}},this)};d.r=function(e,t){if(e!=="@"){e="/"+T(e);this.$.push(e)}this.on(e,t)};var F=new P;var L=F.m.bind(F);L._={base:null,getPathFromBase:k};L.create=function(){var e=new P;var t=e.m.bind(e);t.stop=e.s.bind(e);return t};L.base=function(e){L._.base=e||"#";w=k()};L.exec=function(){q(true)};L.parser=function(e,t){if(!e&&!t){$=x;_=K}if(e){$=e}if(t){_=t}};L.query=function(){var r={};var e=p.href||w;e.replace(/[?&](.+?)=([^&]*)/g,function(e,t,n){r[t]=n});return r};L.stop=function(){if(b){if(s){s[n](f,y);s[n](u,y);c[n](v,B)}m[o]("stop");b=false}};L.start=function(e){if(!b){if(s){if(document.readyState==="interactive"||document.readyState==="complete"){O(e)}else{document.onreadystatechange=function(){if(document.readyState==="interactive"){setTimeout(function(){O(e)},1)}}}}b=true}};L.base();L.parser();return L}();