Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

event-delegate.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('event-delegate', function(Y) {
  9. /**
  10. * Adds event delegation support to the library.
  11. *
  12. * @module event
  13. * @submodule event-delegate
  14. */
  15. var toArray = Y.Array,
  16. YLang = Y.Lang,
  17. isString = YLang.isString,
  18. isObject = YLang.isObject,
  19. isArray = YLang.isArray,
  20. selectorTest = Y.Selector.test,
  21. detachCategories = Y.Env.evt.handles;
  22. /**
  23. * <p>Sets up event delegation on a container element. The delegated event
  24. * will use a supplied selector or filtering function to test if the event
  25. * references at least one node that should trigger the subscription
  26. * callback.</p>
  27. *
  28. * <p>Selector string filters will trigger the callback if the event originated
  29. * from a node that matches it or is contained in a node that matches it.
  30. * Function filters are called for each Node up the parent axis to the
  31. * subscribing container node, and receive at each level the Node and the event
  32. * object. The function should return true (or a truthy value) if that Node
  33. * should trigger the subscription callback. Note, it is possible for filters
  34. * to match multiple Nodes for a single event. In this case, the delegate
  35. * callback will be executed for each matching Node.</p>
  36. *
  37. * <p>For each matching Node, the callback will be executed with its 'this'
  38. * object set to the Node matched by the filter (unless a specific context was
  39. * provided during subscription), and the provided event's
  40. * <code>currentTarget</code> will also be set to the matching Node. The
  41. * containing Node from which the subscription was originally made can be
  42. * referenced as <code>e.container</code>.
  43. *
  44. * @method delegate
  45. * @param type {String} the event type to delegate
  46. * @param fn {Function} the callback function to execute. This function
  47. * will be provided the event object for the delegated event.
  48. * @param el {String|node} the element that is the delegation container
  49. * @param spec {string|Function} a selector that must match the target of the
  50. * event or a function to test target and its parents for a match
  51. * @param context optional argument that specifies what 'this' refers to.
  52. * @param args* 0..n additional arguments to pass on to the callback function.
  53. * These arguments will be added after the event object.
  54. * @return {EventHandle} the detach handle
  55. * @for YUI
  56. */
  57. function delegate(type, fn, el, filter) {
  58. var args = toArray(arguments, 0, true),
  59. query = isString(el) ? el : null,
  60. typeBits, synth, container, categories, cat, i, len, handles, handle;
  61. // Support Y.delegate({ click: fnA, key: fnB }, context, filter, ...);
  62. // and Y.delegate(['click', 'key'], fn, context, filter, ...);
  63. if (isObject(type)) {
  64. handles = [];
  65. if (isArray(type)) {
  66. for (i = 0, len = type.length; i < len; ++i) {
  67. args[0] = type[i];
  68. handles.push(Y.delegate.apply(Y, args));
  69. }
  70. } else {
  71. // Y.delegate({'click', fn}, context, filter) =>
  72. // Y.delegate('click', fn, context, filter)
  73. args.unshift(null); // one arg becomes two; need to make space
  74. for (i in type) {
  75. if (type.hasOwnProperty(i)) {
  76. args[0] = i;
  77. args[1] = type[i];
  78. handles.push(Y.delegate.apply(Y, args));
  79. }
  80. }
  81. }
  82. return new Y.EventHandle(handles);
  83. }
  84. typeBits = type.split(/\|/);
  85. if (typeBits.length > 1) {
  86. cat = typeBits.shift();
  87. args[0] = type = typeBits.shift();
  88. }
  89. synth = Y.Node.DOM_EVENTS[type];
  90. if (isObject(synth) && synth.delegate) {
  91. handle = synth.delegate.apply(synth, arguments);
  92. }
  93. if (!handle) {
  94. if (!type || !fn || !el || !filter) {
  95. return;
  96. }
  97. container = (query) ? Y.Selector.query(query, null, true) : el;
  98. if (!container && isString(el)) {
  99. handle = Y.on('available', function () {
  100. Y.mix(handle, Y.delegate.apply(Y, args), true);
  101. }, el);
  102. }
  103. if (!handle && container) {
  104. args.splice(2, 2, container); // remove the filter
  105. handle = Y.Event._attach(args, { facade: false });
  106. handle.sub.filter = filter;
  107. handle.sub._notify = delegate.notifySub;
  108. }
  109. }
  110. if (handle && cat) {
  111. categories = detachCategories[cat] || (detachCategories[cat] = {});
  112. categories = categories[type] || (categories[type] = []);
  113. categories.push(handle);
  114. }
  115. return handle;
  116. }
  117. /**
  118. Overrides the <code>_notify</code> method on the normal DOM subscription to
  119. inject the filtering logic and only proceed in the case of a match.
  120. This method is hosted as a private property of the `delegate` method
  121. (e.g. `Y.delegate.notifySub`)
  122. @method notifySub
  123. @param thisObj {Object} default 'this' object for the callback
  124. @param args {Array} arguments passed to the event's <code>fire()</code>
  125. @param ce {CustomEvent} the custom event managing the DOM subscriptions for
  126. the subscribed event on the subscribing node.
  127. @return {Boolean} false if the event was stopped
  128. @private
  129. @static
  130. @since 3.2.0
  131. **/
  132. delegate.notifySub = function (thisObj, args, ce) {
  133. // Preserve args for other subscribers
  134. args = args.slice();
  135. if (this.args) {
  136. args.push.apply(args, this.args);
  137. }
  138. // Only notify subs if the event occurred on a targeted element
  139. var currentTarget = delegate._applyFilter(this.filter, args, ce),
  140. //container = e.currentTarget,
  141. e, i, len, ret;
  142. if (currentTarget) {
  143. // Support multiple matches up the the container subtree
  144. currentTarget = toArray(currentTarget);
  145. // The second arg is the currentTarget, but we'll be reusing this
  146. // facade, replacing the currentTarget for each use, so it doesn't
  147. // matter what element we seed it with.
  148. e = args[0] = new Y.DOMEventFacade(args[0], ce.el, ce);
  149. e.container = Y.one(ce.el);
  150. for (i = 0, len = currentTarget.length; i < len && !e.stopped; ++i) {
  151. e.currentTarget = Y.one(currentTarget[i]);
  152. ret = this.fn.apply(this.context || e.currentTarget, args);
  153. if (ret === false) { // stop further notifications
  154. break;
  155. }
  156. }
  157. return ret;
  158. }
  159. };
  160. /**
  161. Compiles a selector string into a filter function to identify whether
  162. Nodes along the parent axis of an event's target should trigger event
  163. notification.
  164. This function is memoized, so previously compiled filter functions are
  165. returned if the same selector string is provided.
  166. This function may be useful when defining synthetic events for delegate
  167. handling.
  168. Hosted as a property of the `delegate` method (e.g. `Y.delegate.compileFilter`).
  169. @method compileFilter
  170. @param selector {String} the selector string to base the filtration on
  171. @return {Function}
  172. @since 3.2.0
  173. @static
  174. **/
  175. delegate.compileFilter = Y.cached(function (selector) {
  176. return function (target, e) {
  177. return selectorTest(target._node, selector, e.currentTarget._node);
  178. };
  179. });
  180. /**
  181. Walks up the parent axis of an event's target, and tests each element
  182. against a supplied filter function. If any Nodes, including the container,
  183. satisfy the filter, the delegated callback will be triggered for each.
  184. Hosted as a protected property of the `delegate` method (e.g.
  185. `Y.delegate._applyFilter`).
  186. @method _applyFilter
  187. @param filter {Function} boolean function to test for inclusion in event
  188. notification
  189. @param args {Array} the arguments that would be passed to subscribers
  190. @param ce {CustomEvent} the DOM event wrapper
  191. @return {Node|Node[]|undefined} The Node or Nodes that satisfy the filter
  192. @protected
  193. **/
  194. delegate._applyFilter = function (filter, args, ce) {
  195. var e = args[0],
  196. container = ce.el, // facadeless events in IE, have no e.currentTarget
  197. target = e.target || e.srcElement,
  198. match = [],
  199. isContainer = false;
  200. // Resolve text nodes to their containing element
  201. if (target.nodeType === 3) {
  202. target = target.parentNode;
  203. }
  204. // passing target as the first arg rather than leaving well enough alone
  205. // making 'this' in the filter function refer to the target. This is to
  206. // support bound filter functions.
  207. args.unshift(target);
  208. if (isString(filter)) {
  209. while (target) {
  210. isContainer = (target === container);
  211. if (selectorTest(target, filter, (isContainer ?null: container))) {
  212. match.push(target);
  213. }
  214. if (isContainer) {
  215. break;
  216. }
  217. target = target.parentNode;
  218. }
  219. } else {
  220. // filter functions are implementer code and should receive wrappers
  221. args[0] = Y.one(target);
  222. args[1] = new Y.DOMEventFacade(e, container, ce);
  223. while (target) {
  224. // filter(target, e, extra args...) - this === target
  225. if (filter.apply(args[0], args)) {
  226. match.push(target);
  227. }
  228. if (target === container) {
  229. break;
  230. }
  231. target = target.parentNode;
  232. args[0] = Y.one(target);
  233. }
  234. args[1] = e; // restore the raw DOM event
  235. }
  236. if (match.length <= 1) {
  237. match = match[0]; // single match or undefined
  238. }
  239. // remove the target
  240. args.shift();
  241. return match;
  242. };
  243. /**
  244. * Sets up event delegation on a container element. The delegated event
  245. * will use a supplied filter to test if the callback should be executed.
  246. * This filter can be either a selector string or a function that returns
  247. * a Node to use as the currentTarget for the event.
  248. *
  249. * The event object for the delegated event is supplied to the callback
  250. * function. It is modified slightly in order to support all properties
  251. * that may be needed for event delegation. 'currentTarget' is set to
  252. * the element that matched the selector string filter or the Node returned
  253. * from the filter function. 'container' is set to the element that the
  254. * listener is delegated from (this normally would be the 'currentTarget').
  255. *
  256. * Filter functions will be called with the arguments that would be passed to
  257. * the callback function, including the event object as the first parameter.
  258. * The function should return false (or a falsey value) if the success criteria
  259. * aren't met, and the Node to use as the event's currentTarget and 'this'
  260. * object if they are.
  261. *
  262. * @method delegate
  263. * @param type {string} the event type to delegate
  264. * @param fn {function} the callback function to execute. This function
  265. * will be provided the event object for the delegated event.
  266. * @param el {string|node} the element that is the delegation container
  267. * @param filter {string|function} a selector that must match the target of the
  268. * event or a function that returns a Node or false.
  269. * @param context optional argument that specifies what 'this' refers to.
  270. * @param args* 0..n additional arguments to pass on to the callback function.
  271. * These arguments will be added after the event object.
  272. * @return {EventHandle} the detach handle
  273. * @for YUI
  274. */
  275. Y.delegate = Y.Event.delegate = delegate;
  276. }, '3.4.0' ,{requires:['node-base']});