UNPKG

stateman

Version:

A tiny foundation that providing nested state-based routing for complex web application.

1 lines 14.4 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.StateMan=e():t.StateMan=e()}(this,function(){return function(t){function e(i){if(n[i])return n[i].exports;var r=n[i]={exports:{},id:i,loaded:!1};return t[i].call(r.exports,r,r.exports,e),r.loaded=!0,r.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e,n){var i;"object"==typeof window?(i=n(1),i.History=n(4),i.util=n(3),i.isServer=!1):(i=n(7),i.isServer=!0),i.State=n(2),t.exports=i},function(t,e,n){function i(t){return this instanceof i==!1?new i(t):(t=t||{},o.call(this,t),t.history&&(this.history=t.history),this._stashCallback=[],this.current=this.active=this,void this.on("end",function(t){var e=this.current;document.title=e.getTitle(t)||h}))}var r=n(2),a=n(4),o=n(6),s=n(3),h=document.title,c=(r.prototype.state,s.inherit(i,o.prototype));s.extend(c,{start:function(t,e){return this._startCallback=e,this.history||(this.history=new a(t)),this.history.isStart||(this.history.on("change",s.bind(this._afterPathChange,this)),this.history.start()),this},stop:function(){this.history.stop()},go:function(t,e,n){e=e||{};var i;if("string"==typeof t&&(i=t,t=this.state(t)),!t)return this._notfound({state:i});if("function"==typeof e&&(n=e,e={}),e.encode!==!1){var r=t.encode(e.param);e.path=r,this.nav(r,{silent:!0,replace:e.replace})}return this._go(t,e,n),this},nav:function(t,e,n){return"function"==typeof e&&(n=e,e={}),e=e||{},e.path=t,this.history.nav(t,s.extend({silent:!0},e)),e.silent||this._afterPathChange(s.cleanPath(t),e,n),this},_afterPathChange:function(t,e,n){this.emit("history:change",t);var i=this.decode(t);return e=e||{},e.path=t,i?(e.param=i.param,e.firstTime&&!n&&(n=this._startCallback,delete this._startCallback),void this._go(i.state,e,n)):this._notfound(e)},_notfound:function(t){return this.emit("notfound",t)},_go:function(t,e,n){function i(t){r=!0,t!==!1&&h.emit("end",e),h.pending=null,h._popStash(e)}var r;if(t.hasNext&&this.strict)return this._notfound({name:t.name});e.param=e.param||{};var a=this.current,o=(this._findBase(a,t),this.path),h=this;"function"==typeof n&&this._stashCallback.push(n),e.previous=a,e.current=t,a!==t&&(e.stop=function(){i(!1),h.nav(o?o:"/",{silent:!0})},h.emit("begin",e)),r!==!0&&(e.phase="permission",this._walk(a,t,e,!0,s.bind(function(n){return n===!1?(o&&this.nav(o,{silent:!0}),i(!1,2),this.emit("abort",e)):(this.pending&&this.pending.stop(),this.pending=e,this.path=e.path,this.current=e.current,this.param=e.param,this.previous=e.previous,e.phase="navigation",void this._walk(a,t,e,!1,s.bind(function(t){return t===!1?(this.current=this.active,i(!1),this.emit("abort",e)):(this.active=e.current,e.phase="completion",i())},this)))},this)))},_popStash:function(t){var e=this._stashCallback,n=e.length;if(this._stashCallback=[],n)for(var i=0;n>i;i++)e[i].call(this,t)},_walk:function(t,e,n,i,r){var a=this._findBase(t,e),o=this;n.backward=!0,this._transit(t,a,n,i,function(t){return t===!1?r(t):(n.backward=!1,void o._walkUpdate(o,a,n,i,function(t){return t===!1?r(t):void o._transit(a,e,n,i,r)}))})},_transit:function(t,e,n,i,r){if(t===e)return r();var a,o=t.name.length>e.name.length,h=o?"leave":"enter";i&&(h="can"+h.replace(/^\w/,function(t){return t.toUpperCase()}));var c=s.bind(function(i){return a===e||i===!1?r(i):(a=a?this._computeNext(a,e):o?t:this._computeNext(t,e),o&&a===e||!a?r(i):void this._moveOn(a,h,n,c))},this);c()},_moveOn:function(t,e,n,i){function r(t){a||(o=!1,a=!0,i(t))}var a=!1,o=!1;n.async=function(){return o=!0,r},n.stop=function(){r(!1)},this.active=t;var h=t[e]?t[e](n):!0;return"enter"===e&&(t.visited=!0),s.isPromise(h)?this._wrapPromise(h,r):void(o||r(h))},_wrapPromise:function(t,e){return t.then(e,function(t){if(t instanceof Error)throw t;e(!1)})},_computeNext:function(t,e){var n=t.name,i=e.name,r=i.split("."),a=n.split("."),o=r.length,s=a.length;return""===n&&(s=0),""===i&&(o=0),o>s?a[s]=r[s]:a.pop(),this.state(a.join("."))},_findQuery:function(t){var e=t&&t.split("&"),n={};if(e)for(var i=e.length,r=0;i>r;r++){var a=e[r].split("=");n[a[0]]=a[1]}return n},_sortState:function(t,e){return(e.priority||0)-(t.priority||0)},_findBase:function(t,e){if(!t||!e||t==this||e==this)return this;for(var n,i=t,r=e;i&&r;){for(n=r;n;){if(i===n)return n;n=n.parent}i=i.parent}},_walkUpdate:function(t,e,n,i,r){for(var a=i?"canUpdate":"update",o=t,s=this,h=[],c=e;c!==this;)h.push(c),c=c.parent;var u=function(t){return t===!1?r(!1):h.length?(o=h.pop(),void s._moveOn(o,a,n,u)):r()};s._moveOn(o,a,n,u)}},!0),t.exports=i},function(t,e,n){function i(t){this._states={},this._pending=!1,this.visited=!1,t&&this.config(t)}var r=n(3);i.rCache={},r.extend(r.emitable(i),{getTitle:function(t){for(var e,n=this;n;){if(e=n.title)return"function"==typeof e?n.title(t):n.title;n=n.parent}return e},state:function(t,e){if("object"===r.typeOf(t)){var n=r.values(t,!0);n.sort(function(t,e){return r.countDot(t)-r.countDot(e)});for(var a=0,o=n.length;o>a;a++){var s=n[a];this.state(s,t[s])}return this}var h,c,u=this,f=this._states,a=0;"string"==typeof t&&(t=t.split("."));var p=t.length,l=[];do{if(c=t[a],h=f[c],l.push(c),!h){if(!e)return;h=f[c]=new i,r.extend(h,{parent:u,manager:u.manager||u,name:l.join("."),currentName:c}),u.hasNext=!0,h.configUrl()}u=h,f=h._states}while(++a<p);return e?(h.config(e),this):u},config:function(t){t=this._getConfig(t);for(var e in t){var n=t[e];switch(e){case"url":"string"==typeof n&&(this.url=n,this.configUrl());break;case"events":this.on(n);break;default:this[e]=n}}},_getConfig:function(t){return"function"==typeof t?{enter:t}:t},configUrl:function(){for(var t="",e=this;e;){if(t=("string"==typeof e.url?e.url:e.currentName||"")+"/"+t,0===t.indexOf("^/")){t=t.slice(1);break}e=e.parent}this.pattern=r.cleanPath("/"+t);var n=this.pattern.split("?");this.pattern=n[0],r.extend(this,r.normalize(this.pattern),!0)},encode:function(t){var e=this;t=t||{};var n="%",i=e.matches.replace(/\(([\w-]+)\)/g,function(e,i){var r=t[i],a=typeof r;return("boolean"===a||"number"===a)&&(r=""+r),r=r||"",n+=i+"%",r})+"?";for(var a in t)-1===n.indexOf("%"+a+"%")&&(i+=a+"="+t[a]+"&");return r.cleanPath(i.replace(/(?:\?|&)$/,""))},decode:function(t){var e=this.regexp.exec(t),n=this.keys;if(e){for(var i={},r=0,a=n.length;a>r;r++)i[n[r]]=e[r+1];return i}return!1},async:function(){throw new Error("please use option.async instead")}}),t.exports=i},function(t,e){function n(t){var e=0,n=[],r=0,a="";t=i.cleanPath(t);var o=t.replace(/\:([\w-]+)(?:\(([^\/]+?)\))?|(?:\(([^\/]+)\))|(\*{2,})|(\*(?!\*))/g,function(i,o,s,h,c,u,f){return f>e&&(a+=t.slice(e,f)),e=f+i.length,o?(a+="("+o+")",n.push(o),"("+(s||"[\\w-]+")+")"):(a+="("+r+")",n.push(r++),h?"("+h+")":c?"(.*)":u?"([^\\/]*)":void 0)});return e!==t.length&&(a+=t.slice(e)),{regexp:new RegExp("^"+o+"/?$"),keys:n,matches:a||t}}var i=t.exports={},r=[].slice,a={}.toString;i.extend=function(t,e,n){for(var i in e)(n||void 0===t[i])&&(t[i]=e[i]);return t};var o=/\./g;i.countDot=function(t){var e=t.match(o);return e?e.length:0},i.values=function(t,e){var n=[];for(var i in t)t.hasOwnProperty(i)&&n.push(e?i:t[i]);return n},i.inherit=function(t,e){function n(){}return n.prototype=e,t.prototype=new n,t.prototype.constructor=t,e},i.slice=function(t,e){return r.call(t,e)},i.typeOf=function(t){return null==t?String(t):a.call(t).slice(8,-1).toLowerCase()},i.eql=function(t,e){var n=i.typeOf(t),r=i.typeOf(e);if(n!==r)return!1;if("object"===n){for(var a in t)if(t[a]!==e[a])return!1;return!0}return t===e},i.emitable=function(){function t(t){var e=(t||"").split(":");return{event:e[0],namespace:e[1]}}var e={once:function(t,e){var n=function(){e.apply(this,arguments),this.off(t,n)};return this.on(t,n)},on:function(e,n){if("object"==typeof e){for(var i in e)this.on(i,e[i]);return this}var r=t(e);if(e=r.event,e&&"function"==typeof n){var a=this._handles||(this._handles={}),o=a[e]||(a[e]=[]);n._ns=r.namespace,o.push(n)}return this},off:function(e,n){var i=t(e);e=i.event,e&&this._handles||(this._handles={});var r=this._handles,a=r[e];if(a)if(n||i.namespace){for(var o=0,s=a.length;s>o;o++)if(!(n&&n!==a[o]||i.namespace&&a[o]._ns!==i.namespace))return a.splice(o,1),this}else r[e]=[];return this},emit:function(e){var n=t(e);e=n.event;var r,a=i.slice(arguments,1),o=this._handles;if(!o||!(r=o[e]))return this;for(var s=0,h=r.length;h>s;s++){var c=r[s];n.namespace&&c._ns!==n.namespace||c.apply(this,a)}return this}};return function(t){return t="function"==typeof t?t.prototype:t,i.extend(t,e)}}(),i.bind=function(t,e){return function(){return t.apply(e,arguments)}};var s=/\/+/g,h=/\/$/;i.cleanPath=function(t){return("/"+t).replace(s,"/").replace(h,"")||"/"},i.log=function(t,e){"undefined"!=typeof console&&console[e||"log"](t)},i.isPromise=function(t){return!!t&&("object"==typeof t||"function"==typeof t)&&"function"==typeof t.then},i.normalize=n},function(t,e,n){function i(t){t=t||{},this.location=t.location||r.location,this.html5=t.html5,this.mode=t.html5&&r.history?h:s,r.hash||(this.mode=o),t.mode&&(this.mode=t.mode),this.prefix="#"+(t.prefix||""),this.rPrefix=new RegExp(this.prefix+"(.*)$"),this.interval=t.interval||66,this.root=t.root||"/",this.rRoot=new RegExp("^"+this.root),this.autolink=t.autolink!==!1,this.autofix=t.autofix!==!1,this.curPath=void 0}var r=n(5),a=n(3),o=3,s=1,h=2;a.extend(a.emitable(i),{start:function(t){var e=this.getPath();if(this._checkPath=a.bind(this.checkPath,this),!this.isStart){switch(this.isStart=!0,this.mode===o&&this._fixHashProbelm(e),this.mode){case s:r.on(window,"hashchange",this._checkPath);break;case h:r.on(window,"popstate",this._checkPath);break;case o:this._checkLoop()}this.autolink&&this._autolink(),this.autofix&&this._fixInitState(),this.curPath=e,this.emit("change",e,{firstTime:!0})}},stop:function(){r.off(window,"hashchange",this._checkPath),r.off(window,"popstate",this._checkPath),clearTimeout(this.tid),this.isStart=!1,this._checkPath=null},checkPath:function(){var t=this.getPath(),e=this.curPath;t===e&&this.iframe&&(t=this.getPath(this.iframe.location)),t!==e&&(this.iframe&&this.nav(t,{silent:!0}),this.curPath=t,this.emit("change",t))},getPath:function(t){t=t||this.location;var e;return this.mode!==h?(e=t.href.match(this.rPrefix),a.cleanPath(e&&e[1]?e[1]:"")):a.cleanPath((t.pathname+t.search||"").replace(this.rRoot,"/"))},nav:function(t,e){var n=this.iframe;e=e||{},t=a.cleanPath(t),this.curPath!=t&&(this.curPath=t,this.mode!==h?(this._setHash(this.location,t,e.replace),n&&this.getPath(n.location)!==t&&(e.replace||n.document.open().close(),this._setHash(this.iframe.location,t,e.replace))):this._changeState(this.location,e.title||"",a.cleanPath(this.root+t),e.replace),e.silent||this.emit("change",t))},_autolink:function(){if(this.mode===h){var t=this;r.on(document.body,"click",function(e){var n=e.target||e.srcElement;if("a"===n.tagName.toLowerCase()){var i=r.isSameDomain(n.href)&&(r.getHref(n)||"").match(t.rPrefix),a=i&&i[1]?i[1]:"";if(a)return e.preventDefault&&e.preventDefault(),t.nav(a),e.returnValue=!1}})}},_setHash:function(t,e,n){var i=t.href.replace(/(javascript:|#).*$/,"");n?t.replace(i+this.prefix+e):t.hash=this.prefix+e},_checkLoop:function(){var t=this;this.tid=setTimeout(function(){t._checkPath(),t._checkLoop()},this.interval)},_fixInitState:function(){var t,e,n=a.cleanPath(this.location.pathname);this.mode!==h&&this.html5?(e=n.replace(this.rRoot,""),e&&this.location.replace(this.root+this.prefix+a.cleanPath(e))):this.mode===h&&(t=this.location.hash.replace(this.prefix,""),t&&this._changeState(this.location,document.title,a.cleanPath(this.root+t)))},_changeState:function(t,e,n,i){var r=t.history||window.history;return r[i?"replaceState":"pushState"]({},e,n)},_fixHashProbelm:function(t){var e=document.createElement("iframe"),n=document.body;e.src="javascript:;",e.style.display="none",e.tabIndex=-1,e.title="",this.iframe=n.insertBefore(e,n.firstChild).contentWindow,this.iframe.document.open().close(),this.iframe.location.hash="#"+t}}),t.exports=i},function(t,e){var n=window,i=document;t.exports={hash:"onhashchange"in n&&(!i.documentMode||i.documentMode>7),history:n.history&&"onpopstate"in n,location:n.location,isSameDomain:function(t){var e=t.match(/^.*?:\/\/([^\/]*)/);return e?e[0]==this.location.origin:!0},getHref:function(t){return"href"in t?t.getAttribute("href",2):t.getAttribute("href")},on:"addEventListener"in n?function(t,e,n){return t.addEventListener(e,n)}:function(t,e,n){return t.attachEvent("on"+e,n)},off:"removeEventListener"in n?function(t,e,n){return t.removeEventListener(e,n)}:function(t,e,n){return t.detachEvent("on"+e,n)}}},function(t,e,n){function i(t){t=t||{},this._states={},this.strict=t.strict,this.title=t.title,t.routes&&this.state(t.routes)}var r=n(2),a=n(3),o=r.prototype.state;a.extend(a.emitable(i),{name:"",root:!0,state:function(t){var e=this.active,n=a.slice(arguments,1);return"string"==typeof t&&e&&(t=t.replace("~",e.name),e.parent&&(t=t.replace("^",e.parent.name||""))),n.unshift(t),o.apply(this,n)},decode:function(t,e){var n=t.split("?"),i=this._findQuery(n[1]);t=n[0];var r=this._findState(this,t);return r&&a.extend(r.param,i),r},encode:function(t,e,n){var i=this.state(t),r=this.history;if(i){var a=i.encode(e);return n&&2!==r.mode?r.prefix+a:a}},is:function(t,e,n){if(!t)return!1;t=t.name||t;var i=this.current,r=i.name,o=n?r===t:0===(r+".").indexOf(t+".");return o&&(!e||a.eql(e,this.param))},_wrapPromise:function(t,e){return t.then(e,function(){e(!1)})},_findQuery:function(t){var e=t&&t.split("&"),n={};if(e)for(var i=e.length,r=0;i>r;r++){var a=e[r].split("=");n[a[0]]=a[1]}return n},_findState:function(t,e){var n,i,r=t._states;if(t.hasNext)for(var o=a.values(r).sort(this._sortState),s=o.length,h=0;s>h;h++)if(n=this._findState(o[h],e))return n;return i=t.regexp&&t.decode(e),i?{state:t,param:i}:!1},_sortState:function(t,e){return(e.priority||0)-(t.priority||0)},_findBase:function(t,e){if(!t||!e||t==this||e==this)return this;for(var n,i=t,r=e;i&&r;){for(n=r;n;){if(i===n)return n;n=n.parent}i=i.parent}}},!0),t.exports=i},function(t,e,n){function i(t){return this instanceof i==!1?new i(t):void a.apply(this,arguments)}var r=n(3),a=n(6),o=r.inherit(i,a.prototype);r.extend(o,{exec:function(t){var e=this.decode(t);if(e){var n=e.param;for(var i in n)"string"==typeof n[i]&&(n[i]=decodeURIComponent(n[i]));var r=[],a=e.state;for(this.current=a;a&&!a.root;)r.unshift(a),a=a.parent;return{states:r,param:n}}}}),t.exports=i}])});