1 // ========================================================================== 2 // Project: SC.Statechart - A Statechart Framework for SproutCore 3 // Copyright: ©2010, 2011 Michael Cohen, and contributors. 4 // Portions @2011 Apple Inc. All rights reserved. 5 // License: Licensed under MIT license (see license.js) 6 // ========================================================================== 7 8 /*globals SC */ 9 10 SC.StatechartMonitor = SC.Object.extend({ 11 12 statechart: null, 13 14 sequence: null, 15 16 init: function() { 17 sc_super(); 18 this.reset(); 19 }, 20 21 reset: function() { 22 this.propertyWillChange('length'); 23 this.sequence = []; 24 this.propertyDidChange('length'); 25 }, 26 27 length: function() { 28 return this.sequence.length; 29 }.property(), 30 31 pushEnteredState: function(state) { 32 this.propertyWillChange('length'); 33 this.sequence.push({ action: 'entered', state: state }); 34 this.propertyDidChange('length'); 35 }, 36 37 pushExitedState: function(state) { 38 this.propertyWillChange('length'); 39 this.sequence.push({ action: 'exited', state: state }); 40 this.propertyDidChange('length'); 41 }, 42 43 matchSequence: function() { 44 return SC.StatechartSequenceMatcher.create({ 45 statechartMonitor: this 46 }); 47 }, 48 49 matchEnteredStates: function() { 50 var expected = SC.A(arguments.length === 1 ? arguments[0] : arguments), 51 actual = this.getPath('statechart.enteredStates'), 52 matched = 0, 53 statechart = this.get('statechart'); 54 55 if (expected.length !== actual.length) return NO; 56 57 expected.forEach(function(item) { 58 if (SC.typeOf(item) === SC.T_STRING) item = statechart.getState(item); 59 if (!item) return; 60 if (statechart.stateIsEntered(item) && item.get('isEnteredState')) matched += 1; 61 }); 62 63 return matched === actual.length; 64 }, 65 66 toString: function() { 67 var seq = "", 68 i = 0, 69 len = 0, 70 item = null; 71 72 seq += "["; 73 74 len = this.sequence.length; 75 for (i = 0; i < len; i += 1) { 76 item = this.sequence[i]; 77 seq += "%@ %@".fmt(item.action, item.state.get('fullPath')); 78 if (i < len - 1) seq += ", "; 79 } 80 81 seq += "]"; 82 83 return seq; 84 } 85 86 });