Eventful.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. var Eventful = (function () {
  2. function Eventful(eventProcessors) {
  3. if (eventProcessors) {
  4. this._$eventProcessor = eventProcessors;
  5. }
  6. }
  7. Eventful.prototype.on = function (event, query, handler, context) {
  8. if (!this._$handlers) {
  9. this._$handlers = {};
  10. }
  11. var _h = this._$handlers;
  12. if (typeof query === 'function') {
  13. context = handler;
  14. handler = query;
  15. query = null;
  16. }
  17. if (!handler || !event) {
  18. return this;
  19. }
  20. var eventProcessor = this._$eventProcessor;
  21. if (query != null && eventProcessor && eventProcessor.normalizeQuery) {
  22. query = eventProcessor.normalizeQuery(query);
  23. }
  24. if (!_h[event]) {
  25. _h[event] = [];
  26. }
  27. for (var i = 0; i < _h[event].length; i++) {
  28. if (_h[event][i].h === handler) {
  29. return this;
  30. }
  31. }
  32. var wrap = {
  33. h: handler,
  34. query: query,
  35. ctx: (context || this),
  36. callAtLast: handler.zrEventfulCallAtLast
  37. };
  38. var lastIndex = _h[event].length - 1;
  39. var lastWrap = _h[event][lastIndex];
  40. (lastWrap && lastWrap.callAtLast)
  41. ? _h[event].splice(lastIndex, 0, wrap)
  42. : _h[event].push(wrap);
  43. return this;
  44. };
  45. Eventful.prototype.isSilent = function (eventName) {
  46. var _h = this._$handlers;
  47. return !_h || !_h[eventName] || !_h[eventName].length;
  48. };
  49. Eventful.prototype.off = function (eventType, handler) {
  50. var _h = this._$handlers;
  51. if (!_h) {
  52. return this;
  53. }
  54. if (!eventType) {
  55. this._$handlers = {};
  56. return this;
  57. }
  58. if (handler) {
  59. if (_h[eventType]) {
  60. var newList = [];
  61. for (var i = 0, l = _h[eventType].length; i < l; i++) {
  62. if (_h[eventType][i].h !== handler) {
  63. newList.push(_h[eventType][i]);
  64. }
  65. }
  66. _h[eventType] = newList;
  67. }
  68. if (_h[eventType] && _h[eventType].length === 0) {
  69. delete _h[eventType];
  70. }
  71. }
  72. else {
  73. delete _h[eventType];
  74. }
  75. return this;
  76. };
  77. Eventful.prototype.trigger = function (eventType) {
  78. var args = [];
  79. for (var _i = 1; _i < arguments.length; _i++) {
  80. args[_i - 1] = arguments[_i];
  81. }
  82. if (!this._$handlers) {
  83. return this;
  84. }
  85. var _h = this._$handlers[eventType];
  86. var eventProcessor = this._$eventProcessor;
  87. if (_h) {
  88. var argLen = args.length;
  89. var len = _h.length;
  90. for (var i = 0; i < len; i++) {
  91. var hItem = _h[i];
  92. if (eventProcessor
  93. && eventProcessor.filter
  94. && hItem.query != null
  95. && !eventProcessor.filter(eventType, hItem.query)) {
  96. continue;
  97. }
  98. switch (argLen) {
  99. case 0:
  100. hItem.h.call(hItem.ctx);
  101. break;
  102. case 1:
  103. hItem.h.call(hItem.ctx, args[0]);
  104. break;
  105. case 2:
  106. hItem.h.call(hItem.ctx, args[0], args[1]);
  107. break;
  108. default:
  109. hItem.h.apply(hItem.ctx, args);
  110. break;
  111. }
  112. }
  113. }
  114. eventProcessor && eventProcessor.afterTrigger
  115. && eventProcessor.afterTrigger(eventType);
  116. return this;
  117. };
  118. Eventful.prototype.triggerWithContext = function (type) {
  119. var args = [];
  120. for (var _i = 1; _i < arguments.length; _i++) {
  121. args[_i - 1] = arguments[_i];
  122. }
  123. if (!this._$handlers) {
  124. return this;
  125. }
  126. var _h = this._$handlers[type];
  127. var eventProcessor = this._$eventProcessor;
  128. if (_h) {
  129. var argLen = args.length;
  130. var ctx = args[argLen - 1];
  131. var len = _h.length;
  132. for (var i = 0; i < len; i++) {
  133. var hItem = _h[i];
  134. if (eventProcessor
  135. && eventProcessor.filter
  136. && hItem.query != null
  137. && !eventProcessor.filter(type, hItem.query)) {
  138. continue;
  139. }
  140. switch (argLen) {
  141. case 0:
  142. hItem.h.call(ctx);
  143. break;
  144. case 1:
  145. hItem.h.call(ctx, args[0]);
  146. break;
  147. case 2:
  148. hItem.h.call(ctx, args[0], args[1]);
  149. break;
  150. default:
  151. hItem.h.apply(ctx, args.slice(1, argLen - 1));
  152. break;
  153. }
  154. }
  155. }
  156. eventProcessor && eventProcessor.afterTrigger
  157. && eventProcessor.afterTrigger(type);
  158. return this;
  159. };
  160. return Eventful;
  161. }());
  162. export default Eventful;