Requires

Provides

Action.js

Action is a class that adds some feature to widget by mixing up in runtime

License:
Public domain (http://unlicense.org).
Authors:
Yaroslaff Fedin
  1. 23
  2. 24
  3. 25
  4. 26
  5. 27
  6. 28
  7. 29
  8. 30
  9. 31
  10. 32
  11. 33
  12. 34
  13. 35
  14. 36
  15. 37
  16. 38
  17. 39
  18. 40
  19. 41
  20. 42
  21. 43
  22. 44
  23. 45
  24. 46
  25. 47
  26. 48
  27. 49
  28. 50
  29. 51
  30. 52
  31. 53
  32. 54
  33. 55
  34. 56
  35. 57
  36. 58
  37. 59
  38. 60
  39. 61
  40. 62
  41. 63
  42. 64
  43. 65
  44. 66
  45. 67
  46. 68
  47. 69
  48. 70
  49. 71
  50. 72
  51. 73
  52. 74
  53. 75
  54. 76
  55. 77
  56. 78
  57. 79
  58. 80
  59. 81
  60. 82
  61. 83
  62. 84
  63. 85
  64. 86
  65. 87
  66. 88
  67. 89
  68. 90
  69. 91
  70. 92
  71. 93
  72. 94
  73. 95
  74. 96
  75. 97
  76. 98
  77. 99
  78. 100
  79. 101
  80. 102
  81. 103
  82. 104
  83. 105
  84. 106
  85. 107
  86. 108
  87. 109
  88. 110
  89. 111
  90. 112
  91. 113
  92. 114
  93. 115
  94. 116
  95. 117
  96. 118
  97. 119
  98. 120
  99. 121
  100. 122
  101. 123
  102. 124
  103. 125
  104. 126
  105. 127
  106. 128
  107. 129
  108. 130
  109. 131
  110. 132
  111. 133
  112. 134
  113. 135
  114. 136
  115. 137
  116. 138
  117. 139
  118. 140
LSD.Action = function(options, name) { var target, state; var self = { options: options, enable: function() { if (self.enabled) return false; this.commit(target, state, arguments, target); if (options.events) target.addEvents(target.events[options.events]); if (self.enabled == null) target.addEvents(events); self.enabled = true; return true; }, disable: function() { if (!self.enabled) return false; this.revert(target, state, arguments, target); if (options.events) target.removeEvents(target.events[options.events]); if (self.enabled != null) target.removeEvents(events); self.enabled = false; return true; }, commit: function(target, state, args, bind) { if (state) target[state.enabler](); var result = options.enable.apply(bind || this, [target].concat(args)); return result; }, revert: function(target, state, args, bind) { if (state) target[state.disabler](); return options.disable.apply(bind || this, [target].concat(args)); }, perform: function(target, state, args) { var method = (!options.getState || !options.getState.apply(this, [target].concat(args))) ? 'commit' : 'revert'; return this[method].apply(this, arguments); }, use: function(widget, state) { var widgets = Array.prototype.slice.call(arguments, 0); var state = widgets.pop(); self[state ? 'enable' : 'disable'].apply(self, widgets); }, watch: function(widget, state) { if (!self[state ? 'enable' : 'disable'](widget)) //try enable the action options[state ? 'enable' : 'disable'].call(target, widget); //just fire the callback }, inject: function() { self.enable(); if (state) self[state.enabler](); }, attach: function(widget) { target = widget; state = name && widget.options.states && widget.options.states[name]; if (state) { events[state.enabler] = options.enable.bind(target); events[state.disabler] = options.disabler.bind(target); } target.addEvents(events); if (options.uses) { target.use(options.uses, self.use); } else if (options.watches) { target.watch(options.watches, self.watch); } else if (!state || (name && target[name])) target.onDOMInject(self.inject); }, detach: function(widget) { target.removeEvents(events); if (options.watches) target.unwatch(options.watches, self.watch); if (self.enabled) self.disable(); if (state) { self[state.disabler](); delete events[state.enabler], events[state.disabler]; } target = state = null; }, store: function(key, value) { if (!this.storage) this.storage = {}; if (!key.indexOf && (typeof key !== 'number')) key = $uid(key); this.storage[key] = value; }, retrieve: function(key) { if (!this.storage) return; if (!key.indexOf && (typeof key !== 'number')) key = $uid(key); return this.storage[key]; } }; for (var methods = ['enable', 'disable'], i, method; method = methods[i++];) { var fn = options[method]; if (fn && !fn.call) { var types = fn; options[method] = function(target) { var callback = types[typeOf(target)]; if (callback) return callback.apply(this, arguments); } } } var events = { enable: self.enable, disable: self.disable, detach: self.disable }; return self; }; LSD.Action.build = function(curry) { return function(options, name) { return new LSD.Action(Object.append({}, options, curry), name); }; };