element-delegate-debug.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Copyright (c) 2009, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.net/yui/license.txt
  5. version: 2.8.0r4
  6. */
  7. /**
  8. * Augments the Element Utility with a <code>delegate</code> method that
  9. * facilitates easy creation of delegated event listeners. (Note: Using CSS
  10. * selectors as the filtering criteria for delegated event listeners requires
  11. * inclusion of the Selector Utility.)
  12. *
  13. * @module element-delegate
  14. * @title Element Event Delegation Module
  15. * @namespace YAHOO.util
  16. * @requires element, event-delegate
  17. */
  18. (function () {
  19. var Event = YAHOO.util.Event,
  20. delegates = [],
  21. specialTypes = {
  22. mouseenter: true,
  23. mouseleave: true
  24. };
  25. YAHOO.lang.augmentObject(YAHOO.util.Element.prototype, {
  26. /**
  27. * Appends a delegated event listener. Delegated event listeners
  28. * receive two arguments by default: the DOM event and the element
  29. * specified by the filtering function or CSS selector.
  30. * (Note: Using the delegate method requires the element-delegate
  31. * module. Using CSS selectors as the filtering criteria for delegated
  32. * event listeners requires inclusion of the Selector Utility.)
  33. * @method delegate
  34. * @param {String} type The name of the event to listen for
  35. * @param {Function} fn The handler to call when the event fires
  36. * @param {Function|string} filter Function or CSS selector used to
  37. * determine for what element(s) the event listener should be called.
  38. * When a function is specified, the function should return an
  39. * HTML element. Using a CSS Selector requires the inclusion of the
  40. * CSS Selector Utility.
  41. * @param {Any} obj A variable to pass to the handler
  42. * @param {Object} scope The object to use for the scope of the handler
  43. * @return {boolean} Returns true if the delegated event listener
  44. * was added successfully
  45. * @for Element
  46. */
  47. delegate: function (type, fn, filter, obj, overrideContext) {
  48. if (YAHOO.lang.isString(filter) && !YAHOO.util.Selector) {
  49. YAHOO.log("Using a CSS selector to define the filtering criteria for a delegated listener requires the Selector Utility.", "error", "Element");
  50. return false;
  51. }
  52. if (!Event._createDelegate) {
  53. YAHOO.log("Using delegate functionality requires the event-delegate module.", "error", "Element");
  54. return false;
  55. }
  56. var sType = Event._getType(type),
  57. el = this.get("element"),
  58. fnDelegate,
  59. fnMouseDelegate,
  60. fnWrapper = function (e) {
  61. return fnDelegate.call(el, e);
  62. };
  63. if (specialTypes[type]) {
  64. if (!Event._createMouseDelegate) {
  65. YAHOO.log("Delegating a " + type + " event requires the event-mouseleave module.", "error", "Element");
  66. return false;
  67. }
  68. fnMouseDelegate = Event._createMouseDelegate(fn, obj, overrideContext);
  69. fnDelegate = Event._createDelegate(function (event, matchedEl, container) {
  70. return fnMouseDelegate.call(matchedEl, event, container);
  71. }, filter, obj, overrideContext);
  72. }
  73. else {
  74. fnDelegate = Event._createDelegate(fn, filter, obj, overrideContext);
  75. }
  76. delegates.push([el, sType, fn, fnWrapper]);
  77. return this.on(sType, fnWrapper);
  78. },
  79. /**
  80. * Remove a delegated event listener
  81. * @method removeDelegate
  82. * @param {String} type The name of the event to listen for
  83. * @param {Function} fn The function call when the event fires
  84. * @return {boolean} Returns true if the unbind was successful, false
  85. * otherwise.
  86. * @for Element
  87. */
  88. removeDelegate: function (type, fn) {
  89. var sType = Event._getType(type),
  90. index = Event._getCacheIndex(delegates, this.get("element"), sType, fn),
  91. returnVal,
  92. cacheItem;
  93. if (index >= 0) {
  94. cacheItem = delegates[index];
  95. }
  96. if (cacheItem) {
  97. returnVal = this.removeListener(cacheItem[1], cacheItem[3]);
  98. if (returnVal) {
  99. delete delegates[index][2];
  100. delete delegates[index][3];
  101. delegates.splice(index, 1);
  102. }
  103. }
  104. return returnVal;
  105. }
  106. });
  107. }());
  108. YAHOO.register("element-delegate", YAHOO.util.Element, {version: "2.8.0r4", build: "2449"});