state-jacket
Version:
An intuitive state transition system for JavaScript
158 lines (129 loc) • 4.06 kB
JavaScript
// Generated by CoffeeScript 1.6.2
/*
StateJacket JS.
An Intuitive State Transition System for JavaScript
Based on State Jacket by hopsoft
*/
(function() {
'use strict';
var StateJacket,
__slice = [].slice,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
__hasProp = {}.hasOwnProperty;
StateJacket = {};
StateJacket.Catalog = (function() {
function Catalog() {
this.isLocked = false;
this.states = {};
}
Catalog.prototype.add = function() {
var state, to, transitions;
state = arguments[0], transitions = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
if (this.isLocked) {
throw 'Cannot modify locked StateJacket.Catalog object!';
}
return this.states[String(state)] = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = transitions.length; _i < _len; _i++) {
to = transitions[_i];
_results.push(String(to));
}
return _results;
})();
};
Catalog.prototype.canTransition = function(from, to) {
var key, _ref;
key = String(from);
if (this.states[key]) {
if (_ref = String(to), __indexOf.call(this.states[key], _ref) >= 0) {
return true;
}
}
return false;
};
Catalog.prototype.transitioners = function() {
var state, states, transitions, _ref;
states = [];
_ref = this.states;
for (state in _ref) {
if (!__hasProp.call(_ref, state)) continue;
transitions = _ref[state];
if (transitions.length > 0) {
states.push(state);
}
}
return states;
};
Catalog.prototype.isTransitioner = function(state) {
var _ref;
return _ref = String(state), __indexOf.call(this.transitioners(), _ref) >= 0;
};
Catalog.prototype.terminators = function() {
var state, states, transitions, _ref;
states = [];
_ref = this.states;
for (state in _ref) {
if (!__hasProp.call(_ref, state)) continue;
transitions = _ref[state];
if (transitions.length === 0) {
states.push(state);
}
}
return states;
};
Catalog.prototype.isTerminator = function(state) {
var _ref;
return _ref = String(state), __indexOf.call(this.terminators(), _ref) >= 0;
};
Catalog.prototype.lock = function() {
var state, transition, transitions, _i, _len, _ref;
_ref = this.states;
for (state in _ref) {
if (!__hasProp.call(_ref, state)) continue;
transitions = _ref[state];
for (_i = 0, _len = transitions.length; _i < _len; _i++) {
transition = transitions[_i];
if (!(transition in this.states)) {
throw "" + transition + " is not a first class state.";
}
}
}
return this.isLocked = true;
};
Catalog.prototype.each = function(iterator) {
var state, transitions, _ref, _results;
_ref = this.states;
_results = [];
for (state in _ref) {
if (!__hasProp.call(_ref, state)) continue;
transitions = _ref[state];
_results.push(iterator(state, transitions));
}
return _results;
};
Catalog.prototype.keys = function() {
var state, _ref, _results;
_ref = this.states;
_results = [];
for (state in _ref) {
if (!__hasProp.call(_ref, state)) continue;
_results.push(state);
}
return _results;
};
Catalog.prototype.supportsState = function(state) {
return String(state) in this.states;
};
return Catalog;
})();
if (typeof module === 'object' && module.exports) {
module.exports = StateJacket;
} else if (typeof define === 'function' && this.define.amd) {
define([], function() {
return StateJacket;
});
} else {
this.StateJacket = StateJacket;
}
}).call(this);