Requires

Provides

Class.States.js

A mutator that adds some basic state definition capabilities.

License:
MIT-style license.
  1. 26
  2. 27
  3. 28
  4. 29
  5. 30
  6. 31
  7. 32
  8. 33
  9. 34
  10. 35
  11. 36
  12. 37
  13. 38
  14. 39
  15. 40
  16. 41
  17. 42
Class.Stateful = function(states) { var proto = { options: { states: {} }, setStateTo: function(state, to) { return this[this.options.states[state][to ? 'enabler' : 'disabler']](); } }; Object.each(states, function(methods, state) { var options = Array.link(methods, { enabler: Type.isString, disabler: Type.isString, toggler: Type.isString, reflect: function(value){ return value != null; } });

enable reflection by default

  1. 45
  2. 46
  3. 47
  4. 48
  5. 49
  6. 50
  7. 51
  8. 52
  9. 53
  10. 54
  11. 55
  12. 56
  13. 57
  14. 58
  15. 59
  16. 60
  17. 61
  18. 62
  19. 63
  20. 64
  21. 65
  22. 66
  23. 67
  24. 68
  25. 69
  26. 70
  27. 71
  28. 72
  29. 73
  30. 74
  31. 75
  32. 76
  33. 77
  34. 78
  35. 79
  36. 80
  37. 81
  38. 82
  39. 83
if (options.reflect == null) options.reflect = true; proto.options.states[state] = options; proto[options.enabler] = function() { if (this[state]) return false; this[state] = true; if (Class.hasParent(this)) this.parent.apply(this, arguments); this.fireEvent(options.enabler, arguments); if (this.onStateChange && options.reflect) this.onStateChange(state, true, arguments); return true; }; proto[options.disabler] = function() { if (!this[state]) return false; this[state] = false; if (Class.hasParent(this)) this.parent.apply(this, arguments); this.fireEvent(options.disabler, arguments); if (this.onStateChange && options.reflect) this.onStateChange(state, false, arguments); return true; }; if (options.toggler) proto[options.toggler] = function() { return this[this[state] ? options.disabler : options.enabler].apply(this, arguments); }; }); return new Class(proto); }; Class.Mutators.States = function(states) { this.implement('Includes', Class.Stateful(states)); }; Class.Mutators.Stateful = function(states) { this.implement('Implements', Class.Stateful(states)); };