diff --git a/Makefile b/Makefile index 49e025d..67b0ad9 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,15 @@ MIN_NAME=stativus-min.js +EVENTED_NAME=stativus-evt-min.js -minified : stativus.js +all: minified evented + +minified: stativus.js @@rm -f ./libs/$(MIN_NAME) - @@uglifyjs --define DEBUG_MODE=false ./stativus.js > ./libs/$(MIN_NAME) - @@echo 'Minified version built' \ No newline at end of file + @@uglifyjs --define DEBUG_MODE=false --define EVENTABLE=false ./stativus.js > ./libs/$(MIN_NAME) + @@echo 'Basic Minified version built' + +evented: stativus.js + @@rm -f ./libs/$(EVENTED_NAME) + @@uglifyjs --define DEBUG_MODE=false --define EVENTABLE=true ./stativus.js > ./libs/$(EVENTED_NAME) + @@echo 'Minified Evented version built' + \ No newline at end of file diff --git a/libs/stativus-evt-min.js b/libs/stativus-evt-min.js new file mode 100644 index 0000000..ae1b418 --- /dev/null +++ b/libs/stativus-evt-min.js @@ -0,0 +1,6 @@ +/*globals Stativus DEBUG_MODE EVENTABLE exports *//** + This is the code for creating statecharts in your javascript files + + @author: Evin Grano + @version: 0.5 +*/Stativus={DEFAULT_TREE:"default",SUBSTATE_DELIM:"SUBSTATE:",version:"0.5"},Stativus.State={isState:!0,_data:null,_isNone:function(a){return a===undefined||a===null},goToState:function(a){var b=this.statechart;b&&b.goToState(a,this.globalConcurrentState,this.localConcurrentState)},goToHistoryState:function(a,b){var c=this.statechart;c&&c.goToHistoryState(a,this.globalConcurrentState,this.localConcurrentState,b)},sendEvent:function(a){var b=this.statechart;b&&b.sendEvent.apply(b,arguments)},getData:function(a){if(this._isNone(a))return a;var b=this.statechart,c=this._data[a];return this._isNone(c)&&(c=b.getData(a,this.parentState,this.globalConcurrentState)),c},setData:function(a,b){if(this._isNone(a))return b;this._data[a]=b}},Stativus.State.create=function(a){function g(){}var b,c,d,e,f;a=a||[],g.prototype=this,b=new g,b._data={};for(e=0,f=a.length||0;e0?(e=e.concat(c),f=!0):typeof c=="string"?(e.push(c),f=!0):typeof c=="object"&&(typeof c.name=="string",e.push(c.name),e.push(c),f=!0),f&&(g=e.length-1,e[g].parentState=a,e[g].globalConcurrentState=b,m.addState.apply(m,e))}),this},initStates:function(a){var b,c;this._inInitialSetup=!0;if(typeof a=="string")this.goToState(a,Stativus.DEFAULT_TREE);else if(typeof a=="object")for(b in a)a.hasOwnProperty(b)&&(c=a[b],this.goToState(c,b));return this._inInitialSetup=!1,this._flushPendingEvents(),this},goToState:function(a,b,c){var d,e=this._all_states[b],f,g,h=[],i=[],j,k,l,m,n,o,p,q,r,s,t;d=c?this._current_state[c]:this._current_state[b],n=e[a];if(this._checkAllCurrentStates(n,c||b))return;if(this._goToStateLocked){this._pendingStateTransitions.push({requestedState:a,tree:b});return}this._goToStateLocked=!0,h=this._parentStatesWithRoot(n),i=d?this._parentStatesWithRoot(d):[],k=-1;for(f=0,g=i.length;f=0)break}k<0&&(k=h.length-1),this._enterStates=h,this._enterStateMatchIndex=k,this._enterStateConcurrentTree=c,this._enterStateTree=b,this._exitStateStack=[],d&&d.substatesAreConcurrent&&this._fullExitFromSubstates(b,d);for(p=0;p"),h=g.length||0,i=g[h-1]);while(!f&&c){c[a]&&(f=c[a].apply(c,b),j=!0);if(e&&i===c.name)return[f,j];c=!f&&c.parentState?d[c.parentState]:null}return[f,j]},_checkAllCurrentStates:function(a,b){var c=this.currentState(b)||[];return c===a?!0:typeof c=="string"&&a===this._all_states[b][c]?!0:c.indexOf&&c.indexOf(a)>-1?!0:!1},_flushPendingEvents:function(){var a,b=this._pendingEvents.shift();if(!b)return;a=b.args,a.unshift(b.evt),this.sendEvent.apply(this,a)},_flushPendingStateTransitions:function(){var a=this._pendingStateTransitions.shift(),b;return a?(this.goToState(a.requestedState,a.tree),!0):!1},_parentStateObject:function(a,b){if(a&&b&&this._all_states[b]&&this._all_states[b][a])return this._all_states[b][a]},_fullEnter:function(a){if(!a)return;var b=!1;a.enterState&&a.enterState(),a.didEnterState&&a.didEnterState(),this._unwindEnterStateStack()},_fullExit:function(a){if(!a)return;var b=!1;a.exitState&&a.exitState(),a.didExitState&&a.didExitState(),this._unwindExitStateStack()},_initiateEnterStateSequence:function(){var a,b,c,d,e,f,g;a=this._enterStates,b=this._enterStateMatchIndex,c=this._enterStateConcurrentTree,d=this._enterStateTree,e=this._all_states[d],this._enterStateStack=this._enterStateStack||[],f=b-1,g=a[f],g&&this._cascadeEnterSubstates(g,a,f-1,c||d,e),this._unwindEnterStateStack(),a=null,b=null,c=null,d=null,delete this._enterStates,delete this._enterStateMatchIndex,delete this._enterStateConcurrentTree,delete this._enterStateTree},_cascadeEnterSubstates:function(a,b,c,d,e){var f,g=b.length,h,i,j=this,k,l,m,n,o;if(!a)return;m=a.name,this._enterStateStack.push(a),this._current_state[d]=a,a.localConcurrentState=d;if(a.substatesAreConcurrent){d=a.globalConcurrentState||Stativus.DEFAULT_TREE,k=[Stativus.SUBSTATE_DELIM,d,m].join("=>"),a.history=a.history||{},i=a.substates||[],i.forEach(function(a){k=d+"=>"+a,f=e[a],l=f.globalConcurrentState||Stativus.DEFAULT_TREE,o=j._active_subtrees[l]||[],o.unshift(k),j._active_subtrees[l]=o,c>-1&&b[c]===f&&(c-=1),j._cascadeEnterSubstates(f,b,c,k,e)});return}f=b[c],f?(h=e[f.parentState],h&&(h.substatesAreConcurrent?h.history[d]=f.name:h.history=f.name),c>-1&&b[c]===f&&(c-=1),this._cascadeEnterSubstates(f,b,c,d,e)):(f=e[a.initialSubstate],this._cascadeEnterSubstates(f,b,c,d,e))},_fullExitFromSubstates:function(a,b){var c,d,e,f=this;if(!a||!b||!a||!b.substates)return;d=this._all_states[a],c=this._current_state,this._exitStateStack=this._exitStateStack||[],b.substates.forEach(function(e){var g,h,i,j,k;g=[Stativus.SUBSTATE_DELIM,a,b.name,e].join("=>"),h=c[g];while(h&&h!==b){j=!1;if(!h)continue;f._exitStateStack.unshift(h),h.substatesAreConcurrent&&f._fullExitFromSubstates(a,h),i=h.parentState,h=d[i]}f._active_subtrees[a]=f._removeFromActiveTree(a,g)})},_unwindExitStateStack:function(){var a,b=!1,c;this._exitStateStack=this._exitStateStack||[],a=this._exitStateStack.shift(),a?(a.willExitState&&(c={_statechart:this,_start:a,restart:function(){var a=this._statechart;a&&a._fullExit(this._start)}},b=a.willExitState(c)),b||this._fullExit(a)):(delete this._exitStateStack,this._initiateEnterStateSequence())},_unwindEnterStateStack:function(){var a,b=!1,c,d;this._exitStateStack=this._exitStateStack||[],a=this._enterStateStack.shift(),a?(a.willEnterState&&(c={_statechart:this,_start:a,restart:function(){var a=this._statechart;a&&a._fullEnter(this._start)}},b=a.willEnterState(c)),b||this._fullEnter(a)):(delete this._enterStateStack,this._goToStateLocked=!1,d=this._flushPendingStateTransitions(),!d&&!this._inInitialSetup&&this._flushPendingEvents())},_removeFromActiveTree:function(a,b){var c=[],d=this._active_subtrees[a];return d?b?(d.forEach(function(a){a!==b&&c.push(a)}),c):d:[]},_parentStates:function(a){var b=[],c=a;b.push(c),c=this._parentStateObject(c.parentState,c.globalConcurrentState);while(c)b.push(c),c=this._parentStateObject(c.parentState,c.globalConcurrentState);return b},_parentStatesWithRoot:function(a){var b=this._parentStates(a);return b.push("root"),b}},Stativus.Statechart.tryToPerform=function(a,b){var c=[],d=arguments.length,e;if(d<2)return;for(e=2;e0?(e=e.concat(c),f=!0):typeof c=="string"?(e.push(c),f=!0):typeof c=="object"&&(typeof c.name=="string",e.push(c.name),e.push(c),f=!0),f&&(g=e.length-1,e[g].parentState=a,e[g].globalConcurrentState=b,m.addState.apply(m,e))}),this},initStates:function(a){var b,c;this._inInitialSetup=!0;if(typeof a=="string")this.goToState(a,Stativus.DEFAULT_TREE);else if(typeof a=="object")for(b in a)a.hasOwnProperty(b)&&(c=a[b],this.goToState(c,b));return this._inInitialSetup=!1,this._flushPendingEvents(),this},goToState:function(a,b,c){var d,e=this._all_states[b],f,g,h=[],i=[],j,k,l,m,n,o,p,q,r,s,t;d=c?this._current_state[c]:this._current_state[b],n=e[a];if(this._checkAllCurrentStates(n,c||b))return;if(this._goToStateLocked){this._pendingStateTransitions.push({requestedState:a,tree:b});return}this._goToStateLocked=!0,h=this._parentStatesWithRoot(n),i=d?this._parentStatesWithRoot(d):[],k=-1;for(f=0,g=i.length;f=0)break}k<0&&(k=h.length-1),this._enterStates=h,this._enterStateMatchIndex=k,this._enterStateConcurrentTree=c,this._enterStateTree=b,this._exitStateStack=[],d&&d.substatesAreConcurrent&&this._fullExitFromSubstates(b,d);for(p=0;p"),h=g.length||0,i=g[h-1]);while(!f&&c){c[a]&&(f=c[a].apply(c,b),j=!0);if(e&&i===c.name)return[f,j];c=!f&&c.parentState?d[c.parentState]:null}return[f,j]},_checkAllCurrentStates:function(a,b){var c=this.currentState(b)||[];return c===a?!0:typeof c=="string"&&a===this._all_states[b][c]?!0:c.indexOf&&c.indexOf(a)>-1?!0:!1},_flushPendingEvents:function(){var a,b=this._pendingEvents.shift();if(!b)return;a=b.args,a.unshift(b.evt),this.sendEvent.apply(this,a)},_flushPendingStateTransitions:function(){var a=this._pendingStateTransitions.shift(),b;return a?(this.goToState(a.requestedState,a.tree),!0):!1},_parentStateObject:function(a,b){if(a&&b&&this._all_states[b]&&this._all_states[b][a])return this._all_states[b][a]},_fullEnter:function(a){if(!a)return;var b=!1;a.enterState&&a.enterState(),a.didEnterState&&a.didEnterState(),this._unwindEnterStateStack()},_fullExit:function(a){if(!a)return;var b=!1;a.exitState&&a.exitState(),a.didExitState&&a.didExitState(),this._unwindExitStateStack()},_initiateEnterStateSequence:function(){var a,b,c,d,e,f,g;a=this._enterStates,b=this._enterStateMatchIndex,c=this._enterStateConcurrentTree,d=this._enterStateTree,e=this._all_states[d],this._enterStateStack=this._enterStateStack||[],f=b-1,g=a[f],g&&this._cascadeEnterSubstates(g,a,f-1,c||d,e),this._unwindEnterStateStack(),a=null,b=null,c=null,d=null,delete this._enterStates,delete this._enterStateMatchIndex,delete this._enterStateConcurrentTree,delete this._enterStateTree},_cascadeEnterSubstates:function(a,b,c,d,e){var f,g=b.length,h,i,j=this,k,l,m,n,o;if(!a)return;m=a.name,this._enterStateStack.push(a),this._current_state[d]=a,a.localConcurrentState=d;if(a.substatesAreConcurrent){d=a.globalConcurrentState||Stativus.DEFAULT_TREE,k=[Stativus.SUBSTATE_DELIM,d,m].join("=>"),a.history=a.history||{},i=a.substates||[],i.forEach(function(a){k=d+"=>"+a,f=e[a],l=f.globalConcurrentState||Stativus.DEFAULT_TREE,o=j._active_subtrees[l]||[],o.unshift(k),j._active_subtrees[l]=o,c>-1&&b[c]===f&&(c-=1),j._cascadeEnterSubstates(f,b,c,k,e)});return}f=b[c],f?(h=e[f.parentState],h&&(h.substatesAreConcurrent?h.history[d]=f.name:h.history=f.name),c-=1,c>-1&&this._cascadeEnterSubstates(f,b,c,d,e)):(f=e[a.initialSubstate],this._cascadeEnterSubstates(f,b,c,d,e))},_fullExitFromSubstates:function(a,b){var c,d,e,f=this;if(!a||!b||!a||!b.substates)return;d=this._all_states[a],c=this._current_state,this._exitStateStack=this._exitStateStack||[],b.substates.forEach(function(e){var g,h,i,j,k;g=[Stativus.SUBSTATE_DELIM,a,b.name,e].join("=>"),h=c[g];while(h&&h!==b){j=!1;if(!h)continue;f._exitStateStack.unshift(h),h.substatesAreConcurrent&&f._fullExitFromSubstates(a,h),i=h.parentState,h=d[i]}f._active_subtrees[a]=f._removeFromActiveTree(a,g)})},_unwindExitStateStack:function(){var a,b=!1,c;this._exitStateStack=this._exitStateStack||[],a=this._exitStateStack.shift(),a?(a.willExitState&&(c={_statechart:this,_start:a,restart:function(){var a=this._statechart;a&&a._fullExit(this._start)}},b=a.willExitState(c)),b||this._fullExit(a)):(delete this._exitStateStack,this._initiateEnterStateSequence())},_unwindEnterStateStack:function(){var a,b=!1,c,d;this._exitStateStack=this._exitStateStack||[],a=this._enterStateStack.shift(),a?(a.willEnterState&&(c={_statechart:this,_start:a,restart:function(){var a=this._statechart;a&&a._fullEnter(this._start)}},b=a.willEnterState(c)),b||this._fullEnter(a)):(delete this._enterStateStack,this._goToStateLocked=!1,d=this._flushPendingStateTransitions(),!d&&!this._inInitialSetup&&this._flushPendingEvents())},_removeFromActiveTree:function(a,b){var c=[],d=this._active_subtrees[a];return d?b?(d.forEach(function(a){a!==b&&c.push(a)}),c):d:[]},_parentStates:function(a){var b=[],c=a;b.push(c),c=this._parentStateObject(c.parentState,c.globalConcurrentState);while(c)b.push(c),c=this._parentStateObject(c.parentState,c.globalConcurrentState);return b},_parentStatesWithRoot:function(a){var b=this._parentStates(a);return b.push("root"),b}},Stativus.createStatechart=function(){return this.Statechart.create()},typeof window!="undefined"?window.Stativus=Stativus:typeof exports!="undefined"&&(module.exports=Stativus); \ No newline at end of file +*/Stativus={DEFAULT_TREE:"default",SUBSTATE_DELIM:"SUBSTATE:",version:"0.5"},Stativus.State={isState:!0,_data:null,_isNone:function(a){return a===undefined||a===null},goToState:function(a){var b=this.statechart;b&&b.goToState(a,this.globalConcurrentState,this.localConcurrentState)},goToHistoryState:function(a,b){var c=this.statechart;c&&c.goToHistoryState(a,this.globalConcurrentState,this.localConcurrentState,b)},sendEvent:function(a){var b=this.statechart;b&&b.sendEvent.apply(b,arguments)},getData:function(a){if(this._isNone(a))return a;var b=this.statechart,c=this._data[a];return this._isNone(c)&&(c=b.getData(a,this.parentState,this.globalConcurrentState)),c},setData:function(a,b){if(this._isNone(a))return b;this._data[a]=b}},Stativus.State.create=function(a){function g(){}var b,c,d,e,f;a=a||[],g.prototype=this,b=new g,b._data={};for(e=0,f=a.length||0;e0?(e=e.concat(c),f=!0):typeof c=="string"?(e.push(c),f=!0):typeof c=="object"&&(typeof c.name=="string",e.push(c.name),e.push(c),f=!0),f&&(g=e.length-1,e[g].parentState=a,e[g].globalConcurrentState=b,m.addState.apply(m,e))}),this},initStates:function(a){var b,c;this._inInitialSetup=!0;if(typeof a=="string")this.goToState(a,Stativus.DEFAULT_TREE);else if(typeof a=="object")for(b in a)a.hasOwnProperty(b)&&(c=a[b],this.goToState(c,b));return this._inInitialSetup=!1,this._flushPendingEvents(),this},goToState:function(a,b,c){var d,e=this._all_states[b],f,g,h=[],i=[],j,k,l,m,n,o,p,q,r,s,t;d=c?this._current_state[c]:this._current_state[b],n=e[a];if(this._checkAllCurrentStates(n,c||b))return;if(this._goToStateLocked){this._pendingStateTransitions.push({requestedState:a,tree:b});return}this._goToStateLocked=!0,h=this._parentStatesWithRoot(n),i=d?this._parentStatesWithRoot(d):[],k=-1;for(f=0,g=i.length;f=0)break}k<0&&(k=h.length-1),this._enterStates=h,this._enterStateMatchIndex=k,this._enterStateConcurrentTree=c,this._enterStateTree=b,this._exitStateStack=[],d&&d.substatesAreConcurrent&&this._fullExitFromSubstates(b,d);for(p=0;p"),h=g.length||0,i=g[h-1]);while(!f&&c){c[a]&&(f=c[a].apply(c,b),j=!0);if(e&&i===c.name)return[f,j];c=!f&&c.parentState?d[c.parentState]:null}return[f,j]},_checkAllCurrentStates:function(a,b){var c=this.currentState(b)||[];return c===a?!0:typeof c=="string"&&a===this._all_states[b][c]?!0:c.indexOf&&c.indexOf(a)>-1?!0:!1},_flushPendingEvents:function(){var a,b=this._pendingEvents.shift();if(!b)return;a=b.args,a.unshift(b.evt),this.sendEvent.apply(this,a)},_flushPendingStateTransitions:function(){var a=this._pendingStateTransitions.shift(),b;return a?(this.goToState(a.requestedState,a.tree),!0):!1},_parentStateObject:function(a,b){if(a&&b&&this._all_states[b]&&this._all_states[b][a])return this._all_states[b][a]},_fullEnter:function(a){if(!a)return;var b=!1;a.enterState&&a.enterState(),a.didEnterState&&a.didEnterState(),this._unwindEnterStateStack()},_fullExit:function(a){if(!a)return;var b=!1;a.exitState&&a.exitState(),a.didExitState&&a.didExitState(),this._unwindExitStateStack()},_initiateEnterStateSequence:function(){var a,b,c,d,e,f,g;a=this._enterStates,b=this._enterStateMatchIndex,c=this._enterStateConcurrentTree,d=this._enterStateTree,e=this._all_states[d],this._enterStateStack=this._enterStateStack||[],f=b-1,g=a[f],g&&this._cascadeEnterSubstates(g,a,f-1,c||d,e),this._unwindEnterStateStack(),a=null,b=null,c=null,d=null,delete this._enterStates,delete this._enterStateMatchIndex,delete this._enterStateConcurrentTree,delete this._enterStateTree},_cascadeEnterSubstates:function(a,b,c,d,e){var f,g=b.length,h,i,j=this,k,l,m,n,o;if(!a)return;m=a.name,this._enterStateStack.push(a),this._current_state[d]=a,a.localConcurrentState=d;if(a.substatesAreConcurrent){d=a.globalConcurrentState||Stativus.DEFAULT_TREE,k=[Stativus.SUBSTATE_DELIM,d,m].join("=>"),a.history=a.history||{},i=a.substates||[],i.forEach(function(a){k=d+"=>"+a,f=e[a],l=f.globalConcurrentState||Stativus.DEFAULT_TREE,o=j._active_subtrees[l]||[],o.unshift(k),j._active_subtrees[l]=o,c>-1&&b[c]===f&&(c-=1),j._cascadeEnterSubstates(f,b,c,k,e)});return}f=b[c],f?(h=e[f.parentState],h&&(h.substatesAreConcurrent?h.history[d]=f.name:h.history=f.name),c>-1&&b[c]===f&&(c-=1),this._cascadeEnterSubstates(f,b,c,d,e)):(f=e[a.initialSubstate],this._cascadeEnterSubstates(f,b,c,d,e))},_fullExitFromSubstates:function(a,b){var c,d,e,f=this;if(!a||!b||!a||!b.substates)return;d=this._all_states[a],c=this._current_state,this._exitStateStack=this._exitStateStack||[],b.substates.forEach(function(e){var g,h,i,j,k;g=[Stativus.SUBSTATE_DELIM,a,b.name,e].join("=>"),h=c[g];while(h&&h!==b){j=!1;if(!h)continue;f._exitStateStack.unshift(h),h.substatesAreConcurrent&&f._fullExitFromSubstates(a,h),i=h.parentState,h=d[i]}f._active_subtrees[a]=f._removeFromActiveTree(a,g)})},_unwindExitStateStack:function(){var a,b=!1,c;this._exitStateStack=this._exitStateStack||[],a=this._exitStateStack.shift(),a?(a.willExitState&&(c={_statechart:this,_start:a,restart:function(){var a=this._statechart;a&&a._fullExit(this._start)}},b=a.willExitState(c)),b||this._fullExit(a)):(delete this._exitStateStack,this._initiateEnterStateSequence())},_unwindEnterStateStack:function(){var a,b=!1,c,d;this._exitStateStack=this._exitStateStack||[],a=this._enterStateStack.shift(),a?(a.willEnterState&&(c={_statechart:this,_start:a,restart:function(){var a=this._statechart;a&&a._fullEnter(this._start)}},b=a.willEnterState(c)),b||this._fullEnter(a)):(delete this._enterStateStack,this._goToStateLocked=!1,d=this._flushPendingStateTransitions(),!d&&!this._inInitialSetup&&this._flushPendingEvents())},_removeFromActiveTree:function(a,b){var c=[],d=this._active_subtrees[a];return d?b?(d.forEach(function(a){a!==b&&c.push(a)}),c):d:[]},_parentStates:function(a){var b=[],c=a;b.push(c),c=this._parentStateObject(c.parentState,c.globalConcurrentState);while(c)b.push(c),c=this._parentStateObject(c.parentState,c.globalConcurrentState);return b},_parentStatesWithRoot:function(a){var b=this._parentStates(a);return b.push("root"),b}},Stativus.createStatechart=function(){return this.Statechart.create()},typeof window!="undefined"?window.Stativus=Stativus:typeof exports!="undefined"&&(module.exports=Stativus); \ No newline at end of file diff --git a/stativus.js b/stativus.js index cdef361..9792756 100644 --- a/stativus.js +++ b/stativus.js @@ -1,4 +1,4 @@ -/*globals Stativus DEBUG_MODE exports */ +/*globals Stativus DEBUG_MODE EVENTABLE exports */ /** This is the code for creating statecharts in your javascript files @@ -10,6 +10,11 @@ if (typeof DEBUG_MODE === "undefined"){ DEBUG_MODE = true; } +// Pre-processor for eventable code +if (typeof EVENTABLE === "undefined"){ + EVENTABLE = true; +} + Stativus = { DEFAULT_TREE: 'default', SUBSTATE_DELIM: 'SUBSTATE:', version: '0.5' }; Stativus.State = { @@ -367,9 +372,8 @@ Stativus.Statechart = { }, sendEvent: function(evt){ - var handled = false, found = false, tmp, currentStates = this._current_state, responder, - args = [], tree, len = arguments.length, i, allStates, sTree, - ss = Stativus.SUBSTATE_DELIM, aTrees, sResponder; + var args = [], len = arguments.length, i; + if (len < 1) return; for(i = 1; i < len; i++){ args[i-1] = arguments[i]; @@ -386,9 +390,37 @@ Stativus.Statechart = { return; } this._sendEventLocked = true; + + this._structureCrawl('_cascadeEvents', evt, args); + + // Now, that the states have a chance to process the first action + // we can go ahead and flush the queued events + this._sendEventLocked = false; + if (!this._inInitialSetup) this._flushPendingEvents(); + }, + + getData: function(key, stateName, tree){ + var allStates = this._all_states[tree], state; + if (!allStates) return null; + state = allStates[stateName]; + if (state && state.isState) return state.getData(key); + }, + + getState: function(name, tree){ + var allStates, ret; + tree = tree || Stativus.DEFAULT_TREE; + allStates = this._all_states[tree]; + if (!allStates) return null; + ret = allStates[name]; + return ret; + }, + + _structureCrawl: function(func, evt, args){ + var tree, currentStates = this._current_state, i, len, sResponder, tmp, + allStates, responder, aTrees, sTree, handled, found, ss = Stativus.SUBSTATE_DELIM; for(tree in currentStates){ if(!currentStates.hasOwnProperty(tree)) continue; - + handled = false; sTree = null; responder = currentStates[tree]; @@ -400,12 +432,12 @@ Stativus.Statechart = { for(i = 0, len = aTrees.length; i < len; i++){ sTree = aTrees[i]; sResponder = currentStates[sTree]; - tmp = handled ? [true, true] : this._cascadeEvents(evt, args, sResponder, allStates, sTree); + tmp = handled ? [true, true] : this[func](evt, args, sResponder, allStates, sTree); handled = tmp[0]; if (DEBUG_MODE) found = tmp[1]; } if (!handled) { - tmp = this._cascadeEvents(evt, args, responder, allStates, null); + tmp = this[func](evt, args, responder, allStates, null); handled = tmp[0]; if (DEBUG_MODE){ if (!found) found = tmp[1]; @@ -415,27 +447,6 @@ Stativus.Statechart = { if(!found) console.log(['EVENT:',evt,'with', args.length || 0, 'argument(s)','found NO state to handle the event'].join(' ')); } } - - // Now, that the states have a chance to process the first action - // we can go ahead and flush the queued events - this._sendEventLocked = false; - if (!this._inInitialSetup) this._flushPendingEvents(); - }, - - getData: function(key, stateName, tree){ - var allStates = this._all_states[tree], state; - if (!allStates) return null; - state = allStates[stateName]; - if (state && state.isState) return state.getData(key); - }, - - getState: function(name, tree){ - var allStates, ret; - tree = tree || Stativus.DEFAULT_TREE; - allStates = this._all_states[tree]; - if (!allStates) return null; - ret = allStates[name]; - return ret; }, /** @@ -593,8 +604,8 @@ Stativus.Statechart = { pState.history = cState.name; } } - index -= 1; - if (index > -1) this._cascadeEnterSubstates( cState, requiredStates, index, tree, allStates); + if (index > -1 && requiredStates[index] === cState) index -= 1; + this._cascadeEnterSubstates( cState, requiredStates, index, tree, allStates); } // now we will go into the initial substates of this state else { @@ -747,6 +758,16 @@ Stativus.Statechart = { }; +if (EVENTABLE){ + Stativus.Statechart.tryToPerform = function(selector, event){ + var args = [], len = arguments.length, i; + if (len < 2) return; + for(i = 2; i < len; i++){ + args[i-2] = arguments[i]; + } + }; +} + Stativus.createStatechart = function(){ return this.Statechart.create(); }; // TODO: Work on AMD Loading...