riot-route
Advanced tools
Comparing version 2.3.11 to 2.3.12
@@ -18,2 +18,3 @@ ;define(function(require, exports, module) { | ||
POPSTATE = 'popstate', | ||
HASHCHANGE = 'hashchange', | ||
TRIGGER = 'trigger', | ||
@@ -29,2 +30,3 @@ MAX_EMIT_STACK_LEVEL = 3, | ||
routeFound = false, | ||
debouncedEmit, | ||
base, current, parser, secondParser, emitStack = [], emitStackLevel = 0 | ||
@@ -55,2 +57,28 @@ | ||
/** | ||
* Simple/cheap debounce implementation | ||
* @param {function} fn - callback | ||
* @param {number} delay - delay in seconds | ||
* @returns {function} debounced function | ||
*/ | ||
function debounce(fn, delay) { | ||
var t | ||
return function () { | ||
clearTimeout(t) | ||
t = setTimeout(fn, delay) | ||
} | ||
} | ||
/** | ||
* Set the window listeners to trigger the routes | ||
* @param {boolean} autoExec - see route.start | ||
*/ | ||
function start(autoExec) { | ||
debouncedEmit = debounce(emit, 1) | ||
win[ADD_EVENT_LISTENER](POPSTATE, debouncedEmit) | ||
win[ADD_EVENT_LISTENER](HASHCHANGE, debouncedEmit) | ||
doc[ADD_EVENT_LISTENER](clickEvent, click) | ||
if (autoExec) emit(true) | ||
} | ||
/** | ||
* Router class | ||
@@ -147,2 +175,3 @@ */ | ||
* @param {string} title - page title | ||
* @returns {boolean} - route not found flag | ||
*/ | ||
@@ -190,3 +219,3 @@ function go(path, title) { | ||
var args = (filter == '@' ? parser : secondParser)(normalize(path), normalize(filter)) | ||
if (args) { | ||
if (typeof args != 'undefined') { | ||
this[TRIGGER].apply(null, [filter].concat(args)) | ||
@@ -268,3 +297,4 @@ return routeFound = true // exit from loop | ||
if (started) { | ||
win[REMOVE_EVENT_LISTENER](POPSTATE, emit) | ||
win[REMOVE_EVENT_LISTENER](POPSTATE, debouncedEmit) | ||
win[REMOVE_EVENT_LISTENER](HASHCHANGE, debouncedEmit) | ||
doc[REMOVE_EVENT_LISTENER](clickEvent, click) | ||
@@ -282,7 +312,10 @@ central[TRIGGER]('stop') | ||
if (!started) { | ||
win[ADD_EVENT_LISTENER](POPSTATE, emit) | ||
doc[ADD_EVENT_LISTENER](clickEvent, click) | ||
if (document.readyState == 'complete') start(autoExec) | ||
// the timeout is needed to solve | ||
// a weird safari bug https://github.com/riot/route/issues/33 | ||
else win[ADD_EVENT_LISTENER]('load', function() { | ||
setTimeout(function() { start(autoExec) }, 1) | ||
}) | ||
started = true | ||
} | ||
if (autoExec) emit(true) | ||
} | ||
@@ -289,0 +322,0 @@ |
@@ -1,1 +0,1 @@ | ||
define(function(t,e,n){"use strict";var i=t("riot-observable");var r=/^.+?\/+[^\/]+/,f="EventListener",o="remove"+f,s="add"+f,u="hasAttribute",a="replace",c="popstate",h="trigger",l=3,p=window,d=document,v=p.history.location||p.location,m=q.prototype,b=d&&d.ontouchstart?"touchstart":"click",g=false,w=i(),y=false,$,x,A,K,N=[],E=0;function O(t){return t.split(/[\/?#]/)}function k(t,e){var n=new RegExp("^"+e[a](/\*/g,"([^/?#]+?)")[a](/\.\./,".*")+"$"),i=t.match(n);if(i)return i.slice(1)}function q(){this.$=[];i(this);w.on("stop",this.s.bind(this));w.on("emit",this.e.bind(this))}function D(t){return t[a](/^\/|\/$/,"")}function L(t){return typeof t=="string"}function P(t){return(t||v.href)[a](r,"")}function R(t){return $[0]=="#"?(t||v.href).split($)[1]||"":P(t)[a]($,"")}function S(t){var e=E==0;if(l<=E)return;E++;N.push(function(){var e=R();if(t||e!=x){w[h]("emit",e);x=e}});if(e){while(N.length){N[0]();N.shift()}E=0}}function _(t){if(t.which!=1||t.metaKey||t.ctrlKey||t.shiftKey||t.defaultPrevented)return;var e=t.target;while(e&&e.nodeName!="A")e=e.parentNode;if(!e||e.nodeName!="A"||e[u]("download")||!e[u]("href")||e.target&&e.target!="_self"||e.href.indexOf(v.href.match(r)[0])==-1)return;if(e.href!=v.href){if(e.href.split("#")[0]==v.href.split("#")[0]||$!="#"&&P(e.href).indexOf($)!==0||!j(R(e.href),e.title||d.title))return}t.preventDefault()}function j(t,e){e=e||d.title;history.pushState(null,e,$+D(t));d.title=e;y=false;S();return y}m.m=function(t,e){if(L(t)&&(!e||L(e)))j(t,e);else if(e)this.r(t,e);else this.r("@",t)};m.s=function(){this.off("*");this.$=[]};m.e=function(t){this.$.concat("@").some(function(e){var n=(e=="@"?A:K)(D(t),D(e));if(n){this[h].apply(null,[e].concat(n));return y=true}},this)};m.r=function(t,e){if(t!="@"){t="/"+D(t);this.$.push(t)}this.on(t,e)};var z=new q;var B=z.m.bind(z);B.create=function(){var t=new q;t.m.stop=t.s.bind(t);return t.m.bind(t)};B.base=function(t){$=t||"#";x=R()};B.exec=function(){S(true)};B.parser=function(t,e){if(!t&&!e){A=O;K=k}if(t)A=t;if(e)K=e};B.query=function(){var t={};v.href[a](/[?&](.+?)=([^&]*)/g,function(e,n,i){t[n]=i});return t};B.stop=function(){if(g){p[o](c,S);d[o](b,_);w[h]("stop");g=false}};B.start=function(t){if(!g){p[s](c,S);d[s](b,_);g=true}if(t)S(true)};B.base();B.parser();n.exports=B}); | ||
define(function(t,e,n){"use strict";var i=t("riot-observable");var r=/^.+?\/+[^\/]+/,f="EventListener",o="remove"+f,u="add"+f,s="hasAttribute",c="replace",a="popstate",h="hashchange",l="trigger",p=3,d=window,m=document,v=d.history.location||d.location,g=L.prototype,y=m&&m.ontouchstart?"touchstart":"click",b=false,w=i(),$=false,x,A,K,N,T,E=[],O=0;function S(t){return t.split(/[\/?#]/)}function k(t,e){var n=new RegExp("^"+e[c](/\*/g,"([^/?#]+?)")[c](/\.\./,".*")+"$"),i=t.match(n);if(i)return i.slice(1)}function q(t,e){var n;return function(){clearTimeout(n);n=setTimeout(t,e)}}function D(t){x=q(z,1);d[u](a,x);d[u](h,x);m[u](y,B);if(t)z(true)}function L(){this.$=[];i(this);w.on("stop",this.s.bind(this));w.on("emit",this.e.bind(this))}function P(t){return t[c](/^\/|\/$/,"")}function R(t){return typeof t=="string"}function _(t){return(t||v.href)[c](r,"")}function j(t){return A[0]=="#"?(t||v.href).split(A)[1]||"":_(t)[c](A,"")}function z(t){var e=O==0;if(p<=O)return;O++;E.push(function(){var e=j();if(t||e!=K){w[l]("emit",e);K=e}});if(e){while(E.length){E[0]();E.shift()}O=0}}function B(t){if(t.which!=1||t.metaKey||t.ctrlKey||t.shiftKey||t.defaultPrevented)return;var e=t.target;while(e&&e.nodeName!="A")e=e.parentNode;if(!e||e.nodeName!="A"||e[s]("download")||!e[s]("href")||e.target&&e.target!="_self"||e.href.indexOf(v.href.match(r)[0])==-1)return;if(e.href!=v.href){if(e.href.split("#")[0]==v.href.split("#")[0]||A!="#"&&_(e.href).indexOf(A)!==0||!C(j(e.href),e.title||m.title))return}t.preventDefault()}function C(t,e){e=e||m.title;history.pushState(null,e,A+P(t));m.title=e;$=false;z();return $}g.m=function(t,e){if(R(t)&&(!e||R(e)))C(t,e);else if(e)this.r(t,e);else this.r("@",t)};g.s=function(){this.off("*");this.$=[]};g.e=function(t){this.$.concat("@").some(function(e){var n=(e=="@"?N:T)(P(t),P(e));if(typeof n!="undefined"){this[l].apply(null,[e].concat(n));return $=true}},this)};g.r=function(t,e){if(t!="@"){t="/"+P(t);this.$.push(t)}this.on(t,e)};var F=new L;var G=F.m.bind(F);G.create=function(){var t=new L;t.m.stop=t.s.bind(t);return t.m.bind(t)};G.base=function(t){A=t||"#";K=j()};G.exec=function(){z(true)};G.parser=function(t,e){if(!t&&!e){N=S;T=k}if(t)N=t;if(e)T=e};G.query=function(){var t={};v.href[c](/[?&](.+?)=([^&]*)/g,function(e,n,i){t[n]=i});return t};G.stop=function(){if(b){d[o](a,x);d[o](h,x);m[o](y,B);w[l]("stop");b=false}};G.start=function(t){if(!b){if(document.readyState=="complete")D(t);else d[u]("load",function(){setTimeout(function(){D(t)},1)});b=true}};G.base();G.parser();n.exports=G}); |
@@ -16,2 +16,3 @@ ;(function(riot) { if (!window) return; | ||
POPSTATE = 'popstate', | ||
HASHCHANGE = 'hashchange', | ||
TRIGGER = 'trigger', | ||
@@ -27,2 +28,3 @@ MAX_EMIT_STACK_LEVEL = 3, | ||
routeFound = false, | ||
debouncedEmit, | ||
base, current, parser, secondParser, emitStack = [], emitStackLevel = 0 | ||
@@ -53,2 +55,28 @@ | ||
/** | ||
* Simple/cheap debounce implementation | ||
* @param {function} fn - callback | ||
* @param {number} delay - delay in seconds | ||
* @returns {function} debounced function | ||
*/ | ||
function debounce(fn, delay) { | ||
var t | ||
return function () { | ||
clearTimeout(t) | ||
t = setTimeout(fn, delay) | ||
} | ||
} | ||
/** | ||
* Set the window listeners to trigger the routes | ||
* @param {boolean} autoExec - see route.start | ||
*/ | ||
function start(autoExec) { | ||
debouncedEmit = debounce(emit, 1) | ||
win[ADD_EVENT_LISTENER](POPSTATE, debouncedEmit) | ||
win[ADD_EVENT_LISTENER](HASHCHANGE, debouncedEmit) | ||
doc[ADD_EVENT_LISTENER](clickEvent, click) | ||
if (autoExec) emit(true) | ||
} | ||
/** | ||
* Router class | ||
@@ -145,2 +173,3 @@ */ | ||
* @param {string} title - page title | ||
* @returns {boolean} - route not found flag | ||
*/ | ||
@@ -188,3 +217,3 @@ function go(path, title) { | ||
var args = (filter == '@' ? parser : secondParser)(normalize(path), normalize(filter)) | ||
if (args) { | ||
if (typeof args != 'undefined') { | ||
this[TRIGGER].apply(null, [filter].concat(args)) | ||
@@ -266,3 +295,4 @@ return routeFound = true // exit from loop | ||
if (started) { | ||
win[REMOVE_EVENT_LISTENER](POPSTATE, emit) | ||
win[REMOVE_EVENT_LISTENER](POPSTATE, debouncedEmit) | ||
win[REMOVE_EVENT_LISTENER](HASHCHANGE, debouncedEmit) | ||
doc[REMOVE_EVENT_LISTENER](clickEvent, click) | ||
@@ -280,7 +310,10 @@ central[TRIGGER]('stop') | ||
if (!started) { | ||
win[ADD_EVENT_LISTENER](POPSTATE, emit) | ||
doc[ADD_EVENT_LISTENER](clickEvent, click) | ||
if (document.readyState == 'complete') start(autoExec) | ||
// the timeout is needed to solve | ||
// a weird safari bug https://github.com/riot/route/issues/33 | ||
else win[ADD_EVENT_LISTENER]('load', function() { | ||
setTimeout(function() { start(autoExec) }, 1) | ||
}) | ||
started = true | ||
} | ||
if (autoExec) emit(true) | ||
} | ||
@@ -287,0 +320,0 @@ |
@@ -139,2 +139,3 @@ ;(function() { | ||
POPSTATE = 'popstate', | ||
HASHCHANGE = 'hashchange', | ||
TRIGGER = 'trigger', | ||
@@ -150,2 +151,3 @@ MAX_EMIT_STACK_LEVEL = 3, | ||
routeFound = false, | ||
debouncedEmit, | ||
base, current, parser, secondParser, emitStack = [], emitStackLevel = 0 | ||
@@ -176,2 +178,28 @@ | ||
/** | ||
* Simple/cheap debounce implementation | ||
* @param {function} fn - callback | ||
* @param {number} delay - delay in seconds | ||
* @returns {function} debounced function | ||
*/ | ||
function debounce(fn, delay) { | ||
var t | ||
return function () { | ||
clearTimeout(t) | ||
t = setTimeout(fn, delay) | ||
} | ||
} | ||
/** | ||
* Set the window listeners to trigger the routes | ||
* @param {boolean} autoExec - see route.start | ||
*/ | ||
function start(autoExec) { | ||
debouncedEmit = debounce(emit, 1) | ||
win[ADD_EVENT_LISTENER](POPSTATE, debouncedEmit) | ||
win[ADD_EVENT_LISTENER](HASHCHANGE, debouncedEmit) | ||
doc[ADD_EVENT_LISTENER](clickEvent, click) | ||
if (autoExec) emit(true) | ||
} | ||
/** | ||
* Router class | ||
@@ -268,2 +296,3 @@ */ | ||
* @param {string} title - page title | ||
* @returns {boolean} - route not found flag | ||
*/ | ||
@@ -311,3 +340,3 @@ function go(path, title) { | ||
var args = (filter == '@' ? parser : secondParser)(normalize(path), normalize(filter)) | ||
if (args) { | ||
if (typeof args != 'undefined') { | ||
this[TRIGGER].apply(null, [filter].concat(args)) | ||
@@ -389,3 +418,4 @@ return routeFound = true // exit from loop | ||
if (started) { | ||
win[REMOVE_EVENT_LISTENER](POPSTATE, emit) | ||
win[REMOVE_EVENT_LISTENER](POPSTATE, debouncedEmit) | ||
win[REMOVE_EVENT_LISTENER](HASHCHANGE, debouncedEmit) | ||
doc[REMOVE_EVENT_LISTENER](clickEvent, click) | ||
@@ -403,7 +433,10 @@ central[TRIGGER]('stop') | ||
if (!started) { | ||
win[ADD_EVENT_LISTENER](POPSTATE, emit) | ||
doc[ADD_EVENT_LISTENER](clickEvent, click) | ||
if (document.readyState == 'complete') start(autoExec) | ||
// the timeout is needed to solve | ||
// a weird safari bug https://github.com/riot/route/issues/33 | ||
else win[ADD_EVENT_LISTENER]('load', function() { | ||
setTimeout(function() { start(autoExec) }, 1) | ||
}) | ||
started = true | ||
} | ||
if (autoExec) emit(true) | ||
} | ||
@@ -410,0 +443,0 @@ |
@@ -1,1 +0,1 @@ | ||
(function(){"use strict";var t=function(t){t=t||{};var e={},n=function(t,e){t.replace(/\S+/g,e)},r=function(e,n){Object.defineProperty(t,e,{value:n,enumerable:false,writable:false,configurable:false})};r("on",function(r,i){if(typeof i!="function")return t;n(r,function(t,n){(e[t]=e[t]||[]).push(i);i.typed=n>0});return t});r("off",function(r,i){if(r=="*")e={};else{n(r,function(t){if(i){var n=e[t];for(var r=0,f;f=n&&n[r];++r){if(f==i)n.splice(r--,1)}}else delete e[t]})}return t});r("one",function(e,n){function r(){t.off(e,r);n.apply(t,arguments)}return t.on(e,r)});r("trigger",function(r){var i=arguments.length-1,f=new Array(i);for(var u=0;u<i;u++){f[u]=arguments[u+1]}n(r,function(n){var r=(e[n]||[]).slice(0);for(var i=0,u;u=r[i];++i){if(u.busy)return;u.busy=1;try{u.apply(t,u.typed?[n].concat(f):f)}catch(o){}if(r[i]!==u){i--}u.busy=0}if(e.all&&n!="all")t.trigger.apply(t,["all",n].concat(f))});return t});return t};var e=/^.+?\/+[^\/]+/,n="EventListener",r="remove"+n,i="add"+n,f="hasAttribute",u="replace",o="popstate",a="trigger",s=3,c=window,l=document,h=c.history.location||c.location,p=O.prototype,d=l&&l.ontouchstart?"touchstart":"click",v=false,y=t(),g=false,m,b,w,$,x=[],A=0;function K(t){return t.split(/[\/?#]/)}function N(t,e){var n=new RegExp("^"+e[u](/\*/g,"([^/?#]+?)")[u](/\.\./,".*")+"$"),r=t.match(n);if(r)return r.slice(1)}function O(){this.$=[];t(this);y.on("stop",this.s.bind(this));y.on("emit",this.e.bind(this))}function E(t){return t[u](/^\/|\/$/,"")}function P(t){return typeof t=="string"}function S(t){return(t||h.href)[u](e,"")}function j(t){return m[0]=="#"?(t||h.href).split(m)[1]||"":S(t)[u](m,"")}function k(t){var e=A==0;if(s<=A)return;A++;x.push(function(){var e=j();if(t||e!=b){y[a]("emit",e);b=e}});if(e){while(x.length){x[0]();x.shift()}A=0}}function q(t){if(t.which!=1||t.metaKey||t.ctrlKey||t.shiftKey||t.defaultPrevented)return;var n=t.target;while(n&&n.nodeName!="A")n=n.parentNode;if(!n||n.nodeName!="A"||n[f]("download")||!n[f]("href")||n.target&&n.target!="_self"||n.href.indexOf(h.href.match(e)[0])==-1)return;if(n.href!=h.href){if(n.href.split("#")[0]==h.href.split("#")[0]||m!="#"&&S(n.href).indexOf(m)!==0||!D(j(n.href),n.title||l.title))return}t.preventDefault()}function D(t,e){e=e||l.title;history.pushState(null,e,m+E(t));l.title=e;g=false;k();return g}p.m=function(t,e){if(P(t)&&(!e||P(e)))D(t,e);else if(e)this.r(t,e);else this.r("@",t)};p.s=function(){this.off("*");this.$=[]};p.e=function(t){this.$.concat("@").some(function(e){var n=(e=="@"?w:$)(E(t),E(e));if(n){this[a].apply(null,[e].concat(n));return g=true}},this)};p.r=function(t,e){if(t!="@"){t="/"+E(t);this.$.push(t)}this.on(t,e)};var L=new O;var R=L.m.bind(L);R.create=function(){var t=new O;t.m.stop=t.s.bind(t);return t.m.bind(t)};R.base=function(t){m=t||"#";b=j()};R.exec=function(){k(true)};R.parser=function(t,e){if(!t&&!e){w=K;$=N}if(t)w=t;if(e)$=e};R.query=function(){var t={};h.href[u](/[?&](.+?)=([^&]*)/g,function(e,n,r){t[n]=r});return t};R.stop=function(){if(v){c[r](o,k);l[r](d,q);y[a]("stop");v=false}};R.start=function(t){if(!v){c[i](o,k);l[i](d,q);v=true}if(t)k(true)};R.base();R.parser();window.route=R})(); | ||
(function(){"use strict";var t=function(t){t=t||{};var e={},n=function(t,e){t.replace(/\S+/g,e)},r=function(e,n){Object.defineProperty(t,e,{value:n,enumerable:false,writable:false,configurable:false})};r("on",function(r,i){if(typeof i!="function")return t;n(r,function(t,n){(e[t]=e[t]||[]).push(i);i.typed=n>0});return t});r("off",function(r,i){if(r=="*")e={};else{n(r,function(t){if(i){var n=e[t];for(var r=0,f;f=n&&n[r];++r){if(f==i)n.splice(r--,1)}}else delete e[t]})}return t});r("one",function(e,n){function r(){t.off(e,r);n.apply(t,arguments)}return t.on(e,r)});r("trigger",function(r){var i=arguments.length-1,f=new Array(i);for(var u=0;u<i;u++){f[u]=arguments[u+1]}n(r,function(n){var r=(e[n]||[]).slice(0);for(var i=0,u;u=r[i];++i){if(u.busy)return;u.busy=1;try{u.apply(t,u.typed?[n].concat(f):f)}catch(o){}if(r[i]!==u){i--}u.busy=0}if(e.all&&n!="all")t.trigger.apply(t,["all",n].concat(f))});return t});return t};var e=/^.+?\/+[^\/]+/,n="EventListener",r="remove"+n,i="add"+n,f="hasAttribute",u="replace",o="popstate",a="hashchange",c="trigger",s=3,l=window,h=document,p=l.history.location||l.location,d=P.prototype,v=h&&h.ontouchstart?"touchstart":"click",m=false,y=t(),g=false,b,w,$,x,A,K=[],N=0;function O(t){return t.split(/[\/?#]/)}function S(t,e){var n=new RegExp("^"+e[u](/\*/g,"([^/?#]+?)")[u](/\.\./,".*")+"$"),r=t.match(n);if(r)return r.slice(1)}function T(t,e){var n;return function(){clearTimeout(n);n=setTimeout(t,e)}}function E(t){b=T(L,1);l[i](o,b);l[i](a,b);h[i](v,R);if(t)L(true)}function P(){this.$=[];t(this);y.on("stop",this.s.bind(this));y.on("emit",this.e.bind(this))}function j(t){return t[u](/^\/|\/$/,"")}function k(t){return typeof t=="string"}function q(t){return(t||p.href)[u](e,"")}function D(t){return w[0]=="#"?(t||p.href).split(w)[1]||"":q(t)[u](w,"")}function L(t){var e=N==0;if(s<=N)return;N++;K.push(function(){var e=D();if(t||e!=$){y[c]("emit",e);$=e}});if(e){while(K.length){K[0]();K.shift()}N=0}}function R(t){if(t.which!=1||t.metaKey||t.ctrlKey||t.shiftKey||t.defaultPrevented)return;var n=t.target;while(n&&n.nodeName!="A")n=n.parentNode;if(!n||n.nodeName!="A"||n[f]("download")||!n[f]("href")||n.target&&n.target!="_self"||n.href.indexOf(p.href.match(e)[0])==-1)return;if(n.href!=p.href){if(n.href.split("#")[0]==p.href.split("#")[0]||w!="#"&&q(n.href).indexOf(w)!==0||!_(D(n.href),n.title||h.title))return}t.preventDefault()}function _(t,e){e=e||h.title;history.pushState(null,e,w+j(t));h.title=e;g=false;L();return g}d.m=function(t,e){if(k(t)&&(!e||k(e)))_(t,e);else if(e)this.r(t,e);else this.r("@",t)};d.s=function(){this.off("*");this.$=[]};d.e=function(t){this.$.concat("@").some(function(e){var n=(e=="@"?x:A)(j(t),j(e));if(typeof n!="undefined"){this[c].apply(null,[e].concat(n));return g=true}},this)};d.r=function(t,e){if(t!="@"){t="/"+j(t);this.$.push(t)}this.on(t,e)};var z=new P;var B=z.m.bind(z);B.create=function(){var t=new P;t.m.stop=t.s.bind(t);return t.m.bind(t)};B.base=function(t){w=t||"#";$=D()};B.exec=function(){L(true)};B.parser=function(t,e){if(!t&&!e){x=O;A=S}if(t)x=t;if(e)A=e};B.query=function(){var t={};p.href[u](/[?&](.+?)=([^&]*)/g,function(e,n,r){t[n]=r});return t};B.stop=function(){if(m){l[r](o,b);l[r](a,b);h[r](v,R);y[c]("stop");m=false}};B.start=function(t){if(!m){if(document.readyState=="complete")E(t);else l[i]("load",function(){setTimeout(function(){E(t)},1)});m=true}};B.base();B.parser();window.route=B})(); |
@@ -8,4 +8,4 @@ # Router API | ||
- replacable parser | ||
- compatible with IE9 and higher | ||
- use a [polyfill](https://github.com/devote/HTML5-History-API) for ie9 support and earlier. Because ie. | ||
## Setup routing | ||
@@ -12,0 +12,0 @@ |
@@ -15,2 +15,3 @@ /** | ||
POPSTATE = 'popstate', | ||
HASHCHANGE = 'hashchange', | ||
TRIGGER = 'trigger', | ||
@@ -26,2 +27,3 @@ MAX_EMIT_STACK_LEVEL = 3, | ||
routeFound = false, | ||
debouncedEmit, | ||
base, current, parser, secondParser, emitStack = [], emitStackLevel = 0 | ||
@@ -52,2 +54,28 @@ | ||
/** | ||
* Simple/cheap debounce implementation | ||
* @param {function} fn - callback | ||
* @param {number} delay - delay in seconds | ||
* @returns {function} debounced function | ||
*/ | ||
function debounce(fn, delay) { | ||
var t | ||
return function () { | ||
clearTimeout(t) | ||
t = setTimeout(fn, delay) | ||
} | ||
} | ||
/** | ||
* Set the window listeners to trigger the routes | ||
* @param {boolean} autoExec - see route.start | ||
*/ | ||
function start(autoExec) { | ||
debouncedEmit = debounce(emit, 1) | ||
win[ADD_EVENT_LISTENER](POPSTATE, debouncedEmit) | ||
win[ADD_EVENT_LISTENER](HASHCHANGE, debouncedEmit) | ||
doc[ADD_EVENT_LISTENER](clickEvent, click) | ||
if (autoExec) emit(true) | ||
} | ||
/** | ||
* Router class | ||
@@ -144,2 +172,3 @@ */ | ||
* @param {string} title - page title | ||
* @returns {boolean} - route not found flag | ||
*/ | ||
@@ -187,3 +216,3 @@ function go(path, title) { | ||
var args = (filter == '@' ? parser : secondParser)(normalize(path), normalize(filter)) | ||
if (args) { | ||
if (typeof args != 'undefined') { | ||
this[TRIGGER].apply(null, [filter].concat(args)) | ||
@@ -265,3 +294,4 @@ return routeFound = true // exit from loop | ||
if (started) { | ||
win[REMOVE_EVENT_LISTENER](POPSTATE, emit) | ||
win[REMOVE_EVENT_LISTENER](POPSTATE, debouncedEmit) | ||
win[REMOVE_EVENT_LISTENER](HASHCHANGE, debouncedEmit) | ||
doc[REMOVE_EVENT_LISTENER](clickEvent, click) | ||
@@ -279,7 +309,10 @@ central[TRIGGER]('stop') | ||
if (!started) { | ||
win[ADD_EVENT_LISTENER](POPSTATE, emit) | ||
doc[ADD_EVENT_LISTENER](clickEvent, click) | ||
if (document.readyState == 'complete') start(autoExec) | ||
// the timeout is needed to solve | ||
// a weird safari bug https://github.com/riot/route/issues/33 | ||
else win[ADD_EVENT_LISTENER]('load', function() { | ||
setTimeout(function() { start(autoExec) }, 1) | ||
}) | ||
started = true | ||
} | ||
if (autoExec) emit(true) | ||
} | ||
@@ -286,0 +319,0 @@ |
{ | ||
"name": "riot-route", | ||
"version": "2.3.11", | ||
"version": "2.3.12", | ||
"description": "Simple client-side router", | ||
@@ -24,18 +24,18 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"riot-observable": "^2.3.1" | ||
"riot-observable": "^2.3.11" | ||
}, | ||
"devDependencies": { | ||
"chokidar-cli": "^1.1.1", | ||
"coveralls": "^2.11.4", | ||
"eslint": "^1.9.0", | ||
"chokidar-cli": "^1.2.0", | ||
"coveralls": "^2.11.6", | ||
"eslint": "^1.10.3", | ||
"expect.js": "^0.3.1", | ||
"karma": "^0.13.15", | ||
"karma-coverage": "^0.5.3", | ||
"karma-mocha": "^0.2.0", | ||
"karma-browserstack-launcher": "^0.1.6", | ||
"karma-mocha-reporter": "^1.1.1", | ||
"karma-mocha": "^0.2.1", | ||
"karma-browserstack-launcher": "^0.1.7", | ||
"karma-mocha-reporter": "^1.1.3", | ||
"karma-phantomjs-launcher": "^0.2.1", | ||
"mocha": "^2.3.3", | ||
"phantomjs": "^1.9.18", | ||
"uglify-js": "^2.5.0" | ||
"mocha": "^2.3.4", | ||
"phantomjs": "^1.9.19", | ||
"uglify-js": "^2.6.1" | ||
}, | ||
@@ -42,0 +42,0 @@ "author": "Muut, Inc. and other contributors", |
@@ -6,2 +6,2 @@ var route = require('riot-route') | ||
}) | ||
route.exec() | ||
route.start(true) |
@@ -15,2 +15,16 @@ function fireEvent(node, eventName) { | ||
/** | ||
* Simple serial runner with intervals | ||
* @param { array } tasks - functions | ||
* @param { number } interval - msec | ||
*/ | ||
function serial(tasks, interval) { | ||
function runner() { | ||
var task | ||
if (task = tasks.shift()) task() | ||
if (tasks.length) setTimeout(runner, interval) | ||
} | ||
runner() | ||
} | ||
describe('Core specs', function() { | ||
@@ -51,3 +65,2 @@ | ||
}) | ||
// start router | ||
@@ -71,2 +84,11 @@ route.start() | ||
it('autostart triggers the route only once', function() { | ||
route.stop() | ||
route(function() { | ||
counter++ | ||
}) | ||
route.start(true) | ||
expect(counter).to.be(1) | ||
}) | ||
it('detects the params', function() { | ||
@@ -83,3 +105,3 @@ route(function(first, second) { | ||
it('detects link clicked', function() { | ||
it('detects link clicked', function(done) { | ||
route(function(first, second) { | ||
@@ -90,5 +112,8 @@ counter++ | ||
}) | ||
fireEvent($('.tag-c'), 'click') | ||
fireEvent($('.tag-d'), 'click') | ||
expect(counter).to.be(2) | ||
serial([ | ||
function(){ fireEvent($('.tag-c'), 'click') }, | ||
function(){ fireEvent($('.tag-d'), 'click') }, | ||
function(){ expect(counter).to.be(2) }, | ||
done | ||
], 10) | ||
}) | ||
@@ -241,2 +266,3 @@ | ||
it('start with autoExec option', function() { | ||
route.stop() | ||
route.base('/') | ||
@@ -315,2 +341,14 @@ route(function() { | ||
it('custom parser2', function() { | ||
route.parser(function(path) { | ||
return path | ||
}) | ||
route(function(first) { | ||
counter++ | ||
}) | ||
route('test') | ||
route('/') | ||
expect(counter).to.be(2) | ||
}) | ||
it('custom second parser', function() { | ||
@@ -317,0 +355,0 @@ route.base('/') |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
32
98262
2105
Updatedriot-observable@^2.3.11