forked from mtrpcic/pathjs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpath.min.js
1 lines (1 loc) · 3.32 KB
/
path.min.js
1
var Path={version:"0.8.4.1",map:function(e){if(Path.routes.defined.hasOwnProperty(e)){return Path.routes.defined[e]}else{return new Path.core.route(e)}},root:function(e){Path.routes.root=e},rescue:function(e){Path.routes.rescue=e},history:{initial:{},pushState:function(e,t,n){if(Path.history.supported){if(Path.dispatch(n)){history.pushState(e,t,n)}}else{if(Path.history.fallback){window.location.hash="#"+n}}},popState:function(e){var t=!Path.history.initial.popped&&location.href==Path.history.initial.URL;Path.history.initial.popped=true;if(t)return;Path.dispatch(document.location.pathname)},listen:function(e){Path.history.supported=!!(window.history&&window.history.pushState);Path.history.fallback=e;if(Path.history.supported){Path.history.initial.popped="state"in window.history,Path.history.initial.URL=location.href;window.onpopstate=Path.history.popState;if(window.location.hash===""){if(Path.routes.root!==null){Path.history.pushState({},"",Path.routes.root)}}}else{if(Path.history.fallback){for(route in Path.routes.defined){if(route.charAt(0)!="#"){Path.routes.defined["#"+route]=Path.routes.defined[route];Path.routes.defined["#"+route].path="#"+route}}Path.listen()}}}},match:function(e,t){function n(e,t){var n=t.split("/");var r=/\/?([^\/]+)/;var i=true;var s={};var o,u;for(;i&&n.length&&e.length&&(o=r.exec(e));e=e.substring(o[0].length)){var u=n.shift();if(u.charAt(0)==":"){s[u.substring(1)]=o[1]}else if(u!==o[1]){i=false}}return i&&0===n.length&&0===e.length?s:false}var r={},i=null,s,o,u,a,f;for(i in Path.routes.defined){if(i!==null&&i!==undefined){i=Path.routes.defined[i];s=i.partition();for(a=0;a<s.length;a++){o=s[a];f=e;r=n(f,o);if(r){if(t){i.params=r}return i}else{r={}}}}}return null},dispatch:function(e){var t,n;if(Path.routes.current!==e){Path.routes.previous=Path.routes.current;Path.routes.current=e;n=Path.match(e,true);if(Path.routes.previous){t=Path.match(Path.routes.previous);if(t!==null&&t.do_exit!==null){t.do_exit()}}if(n!==null){n.run();return true}else{if(Path.routes.rescue!==null){Path.routes.rescue()}}}},listen:function(){var e=function(){Path.dispatch(window.location.hash)};if(window.location.hash===""){if(Path.routes.root!==null){window.location.hash=Path.routes.root}}if("onhashchange"in window&&(!document.documentMode||document.documentMode>=8)){window.onhashchange=e}else{setInterval(e,50)}if(window.location.hash!==""){Path.dispatch(window.location.hash)}},core:{route:function(e){this.path=e;this.action=null;this.do_enter=[];this.do_exit=null;this.params={};Path.routes.defined[e]=this}},routes:{current:null,root:null,rescue:null,previous:null,defined:{}}};Path.core.route.prototype={to:function(e){this.action=e;return this},enter:function(e){if(e instanceof Array){this.do_enter=this.do_enter.concat(e)}else{this.do_enter.push(e)}return this},exit:function(e){this.do_exit=e;return this},partition:function(){var e=[],t=[],n=/\(([^}]+?)\)/g,r,i;while(r=n.exec(this.path)){e.push(r[1])}t.push(this.path.split("(")[0]);for(i=0;i<e.length;i++){t.push(t[t.length-1]+e[i])}return t},run:function(){var e=false,t,n,r;if(Path.routes.defined[this.path].hasOwnProperty("do_enter")){if(Path.routes.defined[this.path].do_enter.length>0){for(t=0;t<Path.routes.defined[this.path].do_enter.length;t++){n=Path.routes.defined[this.path].do_enter[t].apply(this);if(n===false){e=true;break}}}}if(!e){Path.routes.defined[this.path].action(this.params)}}}