aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/core/state-chart.js
blob: 1e7265d83f253581c45f588f629d37b4968f0e09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/* <copyright>
 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
 (c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
 </copyright> */
 /**
	@module montage/core/state-chart
    @requires montage
*/
var Montage = require("montage").Montage;

/**
    @class module:montage/core/state-chart.State
    @extends module:montage/core/core.Montage
*/
var State = exports.State = Montage.create(Montage, /** @lends module:montage/core/state-chart.State# */{

    _stateChart: {
        enumerable: false,
        value: null
    },
/**
    Initializes a State object with a set of options.
    @function
    @param {String} options The options for the new State.
    @returns {State}
    */
    init: {
        value: function(options) {

            this.substates = {};
            this.enterState = null;
            this.exitState = null;

            var keys = Object.keys(options),
                i = 0,
                iKey,
                iOption;

            for (; (iKey = keys[i]); i++) {

                iOption = options[iKey];

                if (iOption.prototype === State.prototype) {
                    iOption.name = iKey;
                    iOption.parentState = this;
                    this.substates[iKey] = iOption;
                }

                // TODO is it worth guarding against any old property being added to this state?
                if (typeof iOption === "string" && "initialSubstate" !== iKey) {
                    // this is a simple gotoState transition syntax
                    this[iKey] = this._encloseGotoState(iOption);
                } else {
                    this[iKey] = iOption;
                }
            }

            return this;
        }
    },
/**
        The name of the state.
        @type {Property}
        @default {String} null
    */
    name: {
        enumerable: false,
        value: null
    },
    _initialSubstate: {
        enumerable: false,
        value: null
    },

/**
        The intitial substate.
        @type {Function}
        @default {String} null
    */
    initialSubstate: {
        get: function() {
            if (typeof this._initialSubstate === "string") {
                this._initialSubstate = this[this._initialSubstate];
            }
            return this._initialSubstate;
        },
        set: function(value) {
            this._initialSubstate = value;
        }
    },
/**
        The set of substates.
        @type {Property}
        @default {String} null
    */
    substates: {
        enumerable: false,
        value: null
    },
/**
       The state's parent state.
        @type {Property}
        @default {String} null
    */
    parentState: {
        enumerable: false,
        value: null
    },

    _path: {
        enumerable: false,
        value: null
    },

/**
        @type {Function}
        @default {String} null
    */
    path: {
        // TODO add dependency on parentState, remember to clear cached value
        enumerable: false,
        get: function() {
            if (!this._path) {

                if (this.parentState && this.parentState.path) {
                    this._path = this.parentState.path + "." + this.name;
                } else {
                    this._path = this.name;
                }
            }
            return this._path;
        }
    },
/**
        @type {Property}
        @default {String} null
    */
    enterState: {
        enumerable: false,
        value: null
    },
/**
        @type {Property}
        @default {String} null
    */
    exitState: {
        enumerable: false,
        value: null
    },
/**
    @function
    @param {String} otherState
    @returns !!this.path.match(new RegExp(".?" + otherState + ".?"))
    */
    isInState: {
        enumerable: false,
        value: function(otherState) {

            if (typeof otherState !== "string") {
                otherState = otherState.name;
            }

            return !!this.path.match(new RegExp(".?" + otherState + ".?"));
        }
    },
/**
  @private
*/
    _encloseGotoState: {
        value: function(state) {
            return (function(stateChart, owner) {
                // Not relying on original implementation to save an extra function call, despite duplicated code
                return this._stateChart._gotoState(state, owner);
            });
        }
    },
/**
    @function
    @param {Property} state
    @param {Property} owner
    @returns this._stateChart._gotoState(state, owner)
    */
    gotoState: {
        value: function(state, owner) {
            return this._stateChart._gotoState(state, owner);
        }
    },
/**
  @private
*/
    _performAction: {
        enumerable: null,
        value: function(actionName, stateChart, owner) {
            if (this[actionName]) {
                // TODO what should the context be inside the action function: state or stateChart?
                // state makes sense but requires that authors know that when building a stateChart
                // it's easy to get annoyed with having to remember which one you're in when building
                // a relatively complex stateChart
                // we could just .call(this._stateChart, args) etc...
                this[actionName](stateChart, owner);

            } else if (this.parentState) {
                this.parentState._performAction(actionName, stateChart, owner);
            } else {
                throw "Action '" + actionName + "' not available";
            }
        }
    },
/**
    @function
    @returns "[State " + this.path + " ]"
    */
    toString: {
        enumerable: false,
        value: function() {
            return "[State " + this.path + " ]";
        }
    }

});
/**
    @class module:montage/core/state-chart.StateChart
*/
var StateChart = exports.StateChart = Montage.create(Montage,/** @lends module:montage/core/state-chart.StateChart# */ {
/**
        @type {Property}
        @default {String} null
    */
    delegate: {
        enumerable: false,
        value: null
    },

    // When the ownerStateProperty is set, we expect all operations of this stateChart to rely on the owner property.
    // That is, many objects can rely on this stateChart to manage the flow of state but keep track of their own
    // currentState (exposed as ownerStateProperty and recorded at "_" + ownerStateProperty);
    // When this is set, the statechart itself is completely stateless and an object must take ownership of it
    // prior to performing any actions
/**
        @type {Property}
        @default {String} null
    */
    ownerStateProperty: {
        enumerable: false,
        value: null
    },
/**
        @type {Property}
        @default {String} null
    */
    rootState: {
        enumerable: false,
        value: null
    },

    _currentState: {
        enumerable: false,
        value: null
    },
/**
    The current state.
    @function
    @returns The current state.
    */
    currentState: {
        get: function() {
            return this.ownerStateProperty ? null : this._currentState;
        }
    },
/**
    Initializes a StateChart with a State object, and returns the StateChart.
    @function
    @param {String} state TODO
    @returns {StateChart}
    */
    initWithState: {
        value: function(state) {

            this._states = {};

            this.rootState = state;
            this.rootState._stateChart = this;
            this._prepareState(this.rootState);

            this.enterDefaultState();

            return this;
        }
    },

    _defaultState: {
        enumerable: false,
        value: null
    },
/**
    The default state.
    @function
    @returns this._defaultState
    */
    defaultState: {
        enumerable: false,
        get: function() {
            if (!this._defaultState) {

                var deepestState, nextState;
                deepestState = nextState = this.rootState;

                while ((nextState = nextState.initialSubstate)) {
                    deepestState = nextState;
                }

                this._defaultState = deepestState;
            }

            return this._defaultState;
        }
    },
/**
    @function
    @returns this.defaultState
    */
    enterDefaultState: {
        enumerable: false,
        value: function() {
            if (this.ownerStateProperty && !this.owner) {
                throw "This stateChart has been configured to require an owner to execute this function";
            }

            var owner = this.ownerStateProperty ? this.owner : this,
                // Using internal ownerStateProperty normally reserved for writing, seeing as many
                // components trigger this enterDefaultState, when their ownerStateProperty.get is invoked the first
                // time. So we need to stop the infinite loop
                currentState = this.ownerStateProperty ? owner["_" + this.ownerStateProperty] : owner.currentState;

            if (currentState) {
                throw "Cannot enter default state from '" + currentState.name + "'";
            }

            // We could probably use gotoState for this but I want to minimize the callbacks
            // as we're not really transitioning for the first setup of the state
            // we're just getting there, I don't think we need to make it look like
            // a "transition" to the delegate
            var deepestState, nextState;
            deepestState = nextState = this.rootState;

            while ((nextState = nextState.initialSubstate)) {

                if (deepestState.enterState) {
                    deepestState.enterState(this, owner);
                }

                deepestState = nextState;

                if (nextState.initialSubstate && deepestState.exitState) {
                    deepestState.exitState(this, owner);
                }

            }

            if (this.ownerStateProperty) {
                owner["_" + this.ownerStateProperty] = this.defaultState;
            } else {
                this._currentState = this.defaultState;
            }

            return this.defaultState;
        }
    },
/**
  @private
*/
    _prepareState: {
        enumerable: false,
        value: function(state) {
            state._stateChart = this;

            // Keep a record of all states other than the root
            if (state.name) {
                this._states[state.name] = state;
            }

            var substateName;
            for (substateName in state.substates) {
                this._prepareState(state.substates[substateName]);
            }
        }
    },
/**
  @private
*/
    _states: {
        enumerable: false,
        value: null
    },
/**
    @function
    @param {Property} stateName TODO
    @returns {Array} this._states[stateName]
    */
    stateWithName: {
        enumerable: false,
        value: function(stateName) {
            return this._states[stateName];
        }
    },
/**
    @function
    @param {String} action TODO
    @param {String} owner TODO
    */
    performAction: {
        value: function(action, owner) {

            if (this.ownerStateProperty && !owner) {
                throw "This stateChart has been configured to require an owner to execute this function";
            }

            owner = this.ownerStateProperty ? owner : this;

            var currentState = this.ownerStateProperty ? owner[this.ownerStateProperty] : owner.currentState;

            if (!currentState) {
                throw "Cannot perform action '" + action + "' without a currentState";
            }

            currentState._performAction(action, this, owner);

            // After performing the action, possibly with state transitions, clear out the owner
            this.owner = null;
        }
    },
/**
  @private
*/
    _gotoState: {
        value: function(state, owner) {

            if (this.ownerStateProperty && !owner) {
                throw "This stateChart has been configured to require an owner to execute this function";
            }

            owner = this.ownerStateProperty ? owner : this;

            var fromState = this.ownerStateProperty ? owner[this.ownerStateProperty] : owner.currentState,
                fromStateName = fromState.name,
                stateName = state,
                currentPath,
                destinationPath,
                i,
                interiorDestinationPath,
                interiorDestinationPathCount,
                lastCommonIndex,
                nextIndex,
                destinationPathCount,
                searchCount,
                iState,
                oldState,
                delegateWillExit = false,
                delegateWillEnter = false,
                delegateDidExit = false,
                delegateDidEnter = false;

            if (typeof stateName === "string") {
                state = this._states[state];
            } else {
                stateName = state.name;
            }

            //same state
            if (stateName === fromStateName) {
                return;
            }

            if (this.delegate) {
                delegateWillExit = typeof this.delegate.stateChartWillExitState === "function";
                delegateWillEnter = typeof this.delegate.stateChartWillEnterState === "function";
                delegateDidExit = typeof this.delegate.stateChartDidExitState === "function";
                delegateDidEnter = typeof this.delegate.stateChartDidEnterState === "function";
            }

            if (this.delegate && typeof this.delegate.stateChartShouldGoFromStateToState === "function") {
                if (!this.delegate.stateChartShouldGoFromStateToState(this, fromState, state)) {
                    return;
                }
            }

            if (this.delegate && typeof this.delegate.stateChartWillGoFromStateToState === "function") {
                this.delegate.stateChartWillGoFromStateToState(this, fromState, state);
            }

            currentPath = fromState.path;
            destinationPath = state.path;

            //state is inside this state
            if ((new RegExp(currentPath)).test(destinationPath)) {

                interiorDestinationPath = destinationPath.replace(new RegExp(currentPath + ".?"), "").split(".");
                interiorDestinationPathCount = interiorDestinationPath.length;
                i = 0;

                for (; i < interiorDestinationPathCount; i++) {
                    iState = this._states[interiorDestinationPath[i]];

                    if (delegateWillEnter) {
                        this.delegate.stateChartWillEnterState(this, iState);
                    }

                    if (typeof iState.enterState === "function") {
                        iState.enterState(this, owner);
                    }

                    if (delegateDidEnter) {
                        this.delegate.stateChartDidEnterState(this, iState);
                    }
                }

            }
            // state is outside this state, need to find where they fork
            else {
                currentPath = currentPath.split(".");
                destinationPath = destinationPath.split(".");

                lastCommonIndex = -1;
                destinationPathCount = destinationPath.length;
                searchCount = Math.min(currentPath.length, destinationPathCount);

                while (lastCommonIndex < searchCount) {
                    nextIndex = lastCommonIndex + 1;
                    if (currentPath[nextIndex] !== destinationPath[nextIndex]) {
                        break;
                    }
                    lastCommonIndex++;
                }

                // exit from the currentState to just before the common path
                for (i = currentPath.length - 1; i > lastCommonIndex; i--) {
                    iState = this._states[currentPath[i]];

                    if (delegateWillExit) {
                        this.delegate.stateChartWillExitState(this, iState);
                    }

                    if (typeof iState.exitState === "function") {
                        iState.exitState(this, owner);
                    }

                    if (delegateDidExit) {
                        this.delegate.stateChartDidExitState(this, iState);
                    }
                }

                // We don't want to enter the root state
                lastCommonIndex = lastCommonIndex < 0 ? 0 : lastCommonIndex;

                // enter from the common path to the new state at the end
                for (i = lastCommonIndex; i < destinationPathCount; i++) {
                    iState = this._states[destinationPath[i]];

                    if (delegateWillEnter) {
                        this.delegate.stateChartWillEnterState(this, iState);
                    }

                    // Update the currentState as we enter each state
                    // TODO is this really necessary, is it best done at this point? what about exit?
                    if (this.ownerStateProperty) {
                        owner["_" + this.ownerStateProperty] = iState;
                    } else {
                        this._currentState = iState;
                    }

                    if (typeof iState.enterState === "function") {
                        iState.enterState(this, owner);
                    }

                    if (delegateDidEnter) {
                        this.delegate.stateChartDidEnterState(this, iState);
                    }
                }
            }

            oldState = fromState;

            if (this.delegate && typeof this.delegate.stateChartDidGoFromStateToState === "function") {
                this.delegate.stateChartDidGoFromStateToState(this, oldState, state);
            }

            if (typeof owner.transitionedFromStateToState === "function") {
                owner.transitionedFromStateToState(this, oldState, state);
            }

        }
    }

});