-
Notifications
You must be signed in to change notification settings - Fork 1
/
appseeds.min.js
1 lines (1 loc) · 8.21 KB
/
appseeds.min.js
1
(function(){var t,e,s=this;t=e="undefined"!=typeof exports?exports:s.AppSeeds=s.Seeds={},e.version="0.7.0",Array.isArray||(Array.isArray=function(t){return"[object Array]"===Object.prototype.toString.call(t)}),e.PubSub=e.PS={create:function(t){var s=e.o.beget(this._instanceMethods);return e.o.mixin(s,t,{_pubsubEvents:{},_pubsubHappened:{}}),s},_instanceMethods:{pub:function(t){var e,s,r,i,n,a,u,o,h,c=this._parseEventNamespace(t);for(s=0,r=c.length;r>s;s++){for(e=this._pubsubEvents[h=c[s]]||[],a=Array.prototype.slice.call(arguments,1),i=0,n=e.length;n>i;i++)u=e[i],o=u[0].apply(u[1]||this,a),u[2]&&o!==!1&&this.unsub(h,u[0]);this._pubsubHappened[h]=a}return this},_parseEventNamespace:function(t){var e,s,r=[],i="";for(s=0;t.length>s;s++)":"===(e=t.charAt(s))&&r.push(i),i+=e;return r.push(i),r},sub:function(t,e,s,r){var i,n,a,u,o,h=t.split(/\s+/);for(r=r||{once:!1,recoup:!1},a=0,u=h.length;u>a;a++)n=this._pubsubEvents[i=h[a]],n?n.push([e,s,r.once]):this._pubsubEvents[i]=[[e,s,r.once]],r.recoup&&(o=this._pubsubHappened[i],o&&e.apply(s||this,o));return this},unsub:function(t,e){var s,r,i,n,a,u=t.split(/\s+/);for(n=0;u.length>n;n++){for(r=this._pubsubEvents[s=u[n]],i=[],a=0;e&&r.length>a;a++)r[a][0]!==e&&i.push(r[a]);this._pubsubEvents[s]=i}return this},once:function(t,e,s){return this.sub(t,e,s,{once:!0})},recoup:function(t,e,s){return this.sub(t,e,s,{recoup:!0})},schedule:function(){return e.Lambda?e.Lambda.create.apply(e.Lambda,[this.pub,this].concat(Array.prototype.slice.call(arguments))):null}},PUBLIC_API:["pub","sub","unsub","once","recoup"]},e.StateManager=e.SM={create:function(t){var s=e.o.beget(this._instanceMethods),r=e.o.facade(e.PS.create(),e.PS.PUBLIC_API,s);return e.o.mixin(s,r,{_status:e.StateManager.STATUS_READY,root:"root",_states:{},_actions:{},_queue:{}}),s.state(s.root,{parent:null,context:{},defaultSubstate:null}),s.context=s.state(s.root).context,s.current=s.root,t=t||{},("string"==typeof t||Array.isArray(t))&&s.add(t),s},STATUS_READY:1,STATUS_TRANSITIONING:2,STATUS_ASYNC:3,ASYNC:!1,_instanceMethods:{root:"root",current:null,_states:{},context:null,_status:null,_queue:null,state:function(t,e){return void 0!==e&&(this._states[t]=e),this._states[t]},children:function(t){var e,s=[];for(e in this._states)this._states.hasOwnProperty(e)&&this._states[e].parent===t&&s.push(e);return s},_toRoot:function(t){for(var e=[];t;)e.push(t),t=this.state(t).parent;return e},_lca:function(t,e){var s,r,i,n,a=this._toRoot(t),u=this._toRoot(e);for(i=0;a.length>i;i++){for(r=-1,n=0;u.length>n;n++)if(u[n]===a[i]){r=n;break}if(-1!==r){s=a[i],a=a.slice(0,i),u=u.slice(0,r).reverse();break}}return{exits:a,entries:u,lca:s}},_getStatePairs:function(t){var e,s,r=t.split("->");switch(r.length){case 0:s=[];break;case 1:e=this.root,s=r[0].split(/\s+/);break;case 2:e=r[0].replace(/^\s\s*/,"").replace(/\s\s*$/,""),s=r[1].split(/\s+/);break;default:this.pub("error","String "+t+" is an invalid state pair and has been dropped."),s=[]}var i,n=[],a=/^!/;for(i=0;s.length>i;i++)s[i]&&n.push([e,s[i].replace(a,""),a.test(s[i])]);return n},go:function(t){var s=this.state(t),r=Array.prototype.slice.call(arguments,1)||[];if(void 0===s)return this.pub("error","State "+t+" not defined"),void 0;if(this.current!==t&&this._status===e.SM.STATUS_READY){var i=this._lca(this.current,t);r.unshift(i),this._walk.apply(this,r)}return this},_walk:function(t){var s,r=Array.prototype.slice.call(arguments,1);for(this._status=e.SM.STATUS_TRANSITIONING,s=0;t.exits.length>s;s++){if(this.current=t.exits[s],this.context=this.state(t.exits[s]).context,"function"==typeof this.context.exit&&this.context.exit.call(this)===e.SM.ASYNC)return this._status=e.SM.STATUS_ASYNC,this._queue={exits:t.exits.slice(s+1),entries:t.entries,lca:t.lca,args:r},this;this.pub("exit",this.current)}for(this.current=t.lca,this.context=this.state(this.current).context,s=0;t.entries.length>s;s++){if(this.current=t.entries[s],this.context=this.state(t.entries[s]).context,"function"==typeof this.context.enter&&this.context.enter.call(this)===e.SM.ASYNC)return this._status=e.SM.STATUS_ASYNC,this._queue={exits:[],entries:t.entries.slice(s+1),lca:t.entries[s],args:r},this;this.pub("enter",this.current)}this._status=e.SM.STATUS_READY;var i=this.state(this.current).defaultSubstate;i?(r.unshift(i),this.go.apply(this,r)):("function"==typeof this.context.stay&&this.context.stay.apply(this,r),r.unshift("stay",this.current),this.pub.apply(this,r))},resume:function(){var t=this._queue.args;delete this._queue.args,this._status===e.SM.STATUS_ASYNC?(t.unshift(this._queue),this._walk.apply(this,t)):this.pub("error","State manager is not paused.")},add:function(t){var e,s,r,i;if(arguments.length>1)for(e=0;arguments.length>e;e++)this.add(arguments[e]);else if(Array.isArray(t))for(e=0;t.length>e;e++)this.add(t[e]);else if("string"==typeof t){var n=this._getStatePairs(t);for(e=0;n.length>e;e++){if(s=n[e][0],r=n[e][1],i=n[e][2],!this.state(s))return this.pub("error","State "+s+" is not included in the tree. State not added."),void 0;this.state(r)&&this.pub("error","State "+r+" is already defined. New state not added."),this.state(r,{context:{},defaultSubstate:null,parent:s}),i&&(this.state(s).defaultSubstate&&this.pub("error","State "+s+" already has a default substate "+this.state(s).defaultSubstate+". It will be overwritten with "+r),this.state(s).defaultSubstate=r)}}else if("object"==typeof t)for(e in t)t.hasOwnProperty(e)&&this.add(e+" -> "+t[e]);return this},act:function(t){if(this._status!==e.SM.ASYNC){var s=this.context;this._act(this.current,arguments),this.context=s}else this.pub("error","State manager is paused, can't perform action "+t);return this},action:function(t){var e=this;return this._actions[t]||(this._actions[t]=function(){return e.act.apply(e,[t].concat(Array.prototype.slice.call(arguments)))}),this._actions[t]},_act:function(t,e){this.context=this.state(t).context;var s=this.context[e[0]];if("function"==typeof s){var r=s.apply(this,Array.prototype.slice.call(e,1));if(this.pub("act",e[0],t),r===!1)return}var i=this.state(t).parent;i&&this._act(i,e)},when:function(t,e,s){var r;if("object"==typeof t)for(r in t)this.when(r,t[r]);else{var i=t.split(/\s+/);for(r=0;i.length>r;r++){var n=i[r];if(n){var a=this.state(n);if(!a)return this.pub("error","State "+n+" doesn't exist. Actions not added."),void 0;if("function"==typeof e)e={stay:e};else if("string"==typeof e){var u=e;e={},e[u]=s}a.context||(a.context={});for(r in e)e.hasOwnProperty(r)&&(a.context[r]=e[r])}}}return this}}},e.Lambda={create:function(t,s){var r=function(){return r.reset.apply(r,arguments)};return e.o.mixin(r,this._instanceMethods,{callback:t,args:Array.prototype.slice.call(arguments,2),thisArg:s||this,limit:null,_lastCalled:null,_timerId:null,period:null,type:null}),r},_instanceMethods:{run:function(){var t=(new Date).getTime();return(!this.limit||!this._lastCalled||t-this._lastCalled>this.limit)&&(this.callback.apply(this.thisArg,arguments.length?arguments:this.args),this._lastCalled=t),this},stop:function(){return this._timerId&&("delay"===this.type?window.clearTimeout(this._timerId):"interval"===this.type&&window.clearInterval(this._timerId)),this},reset:function(){this.stop();var t=this,e=function(){t.run()};return"delay"===this.type?this._timerId=window.setTimeout(e,this.period):"interval"===this.type?this._timerId=window.setInterval(e,this.period):this.run.apply(this,arguments),this},delay:function(t){return this.type="delay",this.period=t,this},delayed:function(t){return this.delay(t).reset()},repeat:function(t){return this.type="interval",this.period=t,this},repeated:function(t){return this.repeat(t).reset()},throttle:function(t){return this.limit=t,this}}},e.o={guid:function(){var t=function(){return(0|65536*(1+Math.random())).toString(16).substring(1)};return t()+t()+"-"+t()+"-"+t()+"-"+t()+"-"+t()+t()+t()},mixin:function(t){var e,s;for(e=1;arguments.length>e;e++){var r=arguments[e];if(r)for(s in r)r.hasOwnProperty(s)&&(t[s]=r[s])}return t},beget:function(t){var e=function(){};return e.prototype=t,new e},facade:function(t,s,r){var i,n={};if(Array.isArray(s))for(i=0;s.length>i;i++)n[s[i]]=e.o.bind(t,s[i],r||n);else if("object"==typeof s)for(i in s)s.hasOwnProperty(i)&&(n[s[i]]=e.o.bind(t,i,r||n));return n},bind:function(t,e,s){return function(){var r=t[e].apply(t,arguments);return r===t?s:r}}},e.f=function(){return e.Lambda.create.apply(e.Lambda,arguments)}})(this);