Eventful.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /**
  2. * Event Mixin
  3. * @module zrender/mixin/Eventful
  4. * @author Kener (@Kener-林峰, kener.linfeng@gmail.com)
  5. * pissang (https://www.github.com/pissang)
  6. */
  7. var arrySlice = Array.prototype.slice;
  8. /**
  9. * Event dispatcher.
  10. *
  11. * @alias module:zrender/mixin/Eventful
  12. * @constructor
  13. * @param {Object} [eventProcessor] The object eventProcessor is the scope when
  14. * `eventProcessor.xxx` called.
  15. * @param {Function} [eventProcessor.normalizeQuery]
  16. * param: {string|Object} Raw query.
  17. * return: {string|Object} Normalized query.
  18. * @param {Function} [eventProcessor.filter] Event will be dispatched only
  19. * if it returns `true`.
  20. * param: {string} eventType
  21. * param: {string|Object} query
  22. * return: {boolean}
  23. * @param {Function} [eventProcessor.afterTrigger] Called after all handlers called.
  24. * param: {string} eventType
  25. */
  26. var Eventful = function (eventProcessor) {
  27. this._$handlers = {};
  28. this._$eventProcessor = eventProcessor;
  29. };
  30. Eventful.prototype = {
  31. constructor: Eventful,
  32. /**
  33. * The handler can only be triggered once, then removed.
  34. *
  35. * @param {string} event The event name.
  36. * @param {string|Object} [query] Condition used on event filter.
  37. * @param {Function} handler The event handler.
  38. * @param {Object} context
  39. */
  40. one: function (event, query, handler, context) {
  41. return on(this, event, query, handler, context, true);
  42. },
  43. /**
  44. * Bind a handler.
  45. *
  46. * @param {string} event The event name.
  47. * @param {string|Object} [query] Condition used on event filter.
  48. * @param {Function} handler The event handler.
  49. * @param {Object} [context]
  50. */
  51. on: function (event, query, handler, context) {
  52. return on(this, event, query, handler, context, false);
  53. },
  54. /**
  55. * Whether any handler has bound.
  56. *
  57. * @param {string} event
  58. * @return {boolean}
  59. */
  60. isSilent: function (event) {
  61. var _h = this._$handlers;
  62. return !_h[event] || !_h[event].length;
  63. },
  64. /**
  65. * Unbind a event.
  66. *
  67. * @param {string} [event] The event name.
  68. * If no `event` input, "off" all listeners.
  69. * @param {Function} [handler] The event handler.
  70. * If no `handler` input, "off" all listeners of the `event`.
  71. */
  72. off: function (event, handler) {
  73. var _h = this._$handlers;
  74. if (!event) {
  75. this._$handlers = {};
  76. return this;
  77. }
  78. if (handler) {
  79. if (_h[event]) {
  80. var newList = [];
  81. for (var i = 0, l = _h[event].length; i < l; i++) {
  82. if (_h[event][i].h !== handler) {
  83. newList.push(_h[event][i]);
  84. }
  85. }
  86. _h[event] = newList;
  87. }
  88. if (_h[event] && _h[event].length === 0) {
  89. delete _h[event];
  90. }
  91. } else {
  92. delete _h[event];
  93. }
  94. return this;
  95. },
  96. /**
  97. * Dispatch a event.
  98. *
  99. * @param {string} type The event name.
  100. */
  101. trigger: function (type) {
  102. var _h = this._$handlers[type];
  103. var eventProcessor = this._$eventProcessor;
  104. if (_h) {
  105. var args = arguments;
  106. var argLen = args.length;
  107. if (argLen > 3) {
  108. args = arrySlice.call(args, 1);
  109. }
  110. var len = _h.length;
  111. for (var i = 0; i < len;) {
  112. var hItem = _h[i];
  113. if (eventProcessor && eventProcessor.filter && hItem.query != null && !eventProcessor.filter(type, hItem.query)) {
  114. i++;
  115. continue;
  116. } // Optimize advise from backbone
  117. switch (argLen) {
  118. case 1:
  119. hItem.h.call(hItem.ctx);
  120. break;
  121. case 2:
  122. hItem.h.call(hItem.ctx, args[1]);
  123. break;
  124. case 3:
  125. hItem.h.call(hItem.ctx, args[1], args[2]);
  126. break;
  127. default:
  128. // have more than 2 given arguments
  129. hItem.h.apply(hItem.ctx, args);
  130. break;
  131. }
  132. if (hItem.one) {
  133. _h.splice(i, 1);
  134. len--;
  135. } else {
  136. i++;
  137. }
  138. }
  139. }
  140. eventProcessor && eventProcessor.afterTrigger && eventProcessor.afterTrigger(type);
  141. return this;
  142. },
  143. /**
  144. * Dispatch a event with context, which is specified at the last parameter.
  145. *
  146. * @param {string} type The event name.
  147. */
  148. triggerWithContext: function (type) {
  149. var _h = this._$handlers[type];
  150. var eventProcessor = this._$eventProcessor;
  151. if (_h) {
  152. var args = arguments;
  153. var argLen = args.length;
  154. if (argLen > 4) {
  155. args = arrySlice.call(args, 1, args.length - 1);
  156. }
  157. var ctx = args[args.length - 1];
  158. var len = _h.length;
  159. for (var i = 0; i < len;) {
  160. var hItem = _h[i];
  161. if (eventProcessor && eventProcessor.filter && hItem.query != null && !eventProcessor.filter(type, hItem.query)) {
  162. i++;
  163. continue;
  164. } // Optimize advise from backbone
  165. switch (argLen) {
  166. case 1:
  167. hItem.h.call(ctx);
  168. break;
  169. case 2:
  170. hItem.h.call(ctx, args[1]);
  171. break;
  172. case 3:
  173. hItem.h.call(ctx, args[1], args[2]);
  174. break;
  175. default:
  176. // have more than 2 given arguments
  177. hItem.h.apply(ctx, args);
  178. break;
  179. }
  180. if (hItem.one) {
  181. _h.splice(i, 1);
  182. len--;
  183. } else {
  184. i++;
  185. }
  186. }
  187. }
  188. eventProcessor && eventProcessor.afterTrigger && eventProcessor.afterTrigger(type);
  189. return this;
  190. }
  191. };
  192. function normalizeQuery(host, query) {
  193. var eventProcessor = host._$eventProcessor;
  194. if (query != null && eventProcessor && eventProcessor.normalizeQuery) {
  195. query = eventProcessor.normalizeQuery(query);
  196. }
  197. return query;
  198. }
  199. function on(eventful, event, query, handler, context, isOnce) {
  200. var _h = eventful._$handlers;
  201. if (typeof query === 'function') {
  202. context = handler;
  203. handler = query;
  204. query = null;
  205. }
  206. if (!handler || !event) {
  207. return eventful;
  208. }
  209. query = normalizeQuery(eventful, query);
  210. if (!_h[event]) {
  211. _h[event] = [];
  212. }
  213. for (var i = 0; i < _h[event].length; i++) {
  214. if (_h[event][i].h === handler) {
  215. return eventful;
  216. }
  217. }
  218. var wrap = {
  219. h: handler,
  220. one: isOnce,
  221. query: query,
  222. ctx: context || eventful,
  223. // FIXME
  224. // Do not publish this feature util it is proved that it makes sense.
  225. callAtLast: handler.zrEventfulCallAtLast
  226. };
  227. var lastIndex = _h[event].length - 1;
  228. var lastWrap = _h[event][lastIndex];
  229. lastWrap && lastWrap.callAtLast ? _h[event].splice(lastIndex, 0, wrap) : _h[event].push(wrap);
  230. return eventful;
  231. } // ----------------------
  232. // The events in zrender
  233. // ----------------------
  234. /**
  235. * @event module:zrender/mixin/Eventful#onclick
  236. * @type {Function}
  237. * @default null
  238. */
  239. /**
  240. * @event module:zrender/mixin/Eventful#onmouseover
  241. * @type {Function}
  242. * @default null
  243. */
  244. /**
  245. * @event module:zrender/mixin/Eventful#onmouseout
  246. * @type {Function}
  247. * @default null
  248. */
  249. /**
  250. * @event module:zrender/mixin/Eventful#onmousemove
  251. * @type {Function}
  252. * @default null
  253. */
  254. /**
  255. * @event module:zrender/mixin/Eventful#onmousewheel
  256. * @type {Function}
  257. * @default null
  258. */
  259. /**
  260. * @event module:zrender/mixin/Eventful#onmousedown
  261. * @type {Function}
  262. * @default null
  263. */
  264. /**
  265. * @event module:zrender/mixin/Eventful#onmouseup
  266. * @type {Function}
  267. * @default null
  268. */
  269. /**
  270. * @event module:zrender/mixin/Eventful#ondrag
  271. * @type {Function}
  272. * @default null
  273. */
  274. /**
  275. * @event module:zrender/mixin/Eventful#ondragstart
  276. * @type {Function}
  277. * @default null
  278. */
  279. /**
  280. * @event module:zrender/mixin/Eventful#ondragend
  281. * @type {Function}
  282. * @default null
  283. */
  284. /**
  285. * @event module:zrender/mixin/Eventful#ondragenter
  286. * @type {Function}
  287. * @default null
  288. */
  289. /**
  290. * @event module:zrender/mixin/Eventful#ondragleave
  291. * @type {Function}
  292. * @default null
  293. */
  294. /**
  295. * @event module:zrender/mixin/Eventful#ondragover
  296. * @type {Function}
  297. * @default null
  298. */
  299. /**
  300. * @event module:zrender/mixin/Eventful#ondrop
  301. * @type {Function}
  302. * @default null
  303. */
  304. var _default = Eventful;
  305. module.exports = _default;