delegate.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.delegate = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. var DOCUMENT_NODE_TYPE = 9;
  3. /**
  4. * A polyfill for Element.matches()
  5. */
  6. if (typeof Element !== 'undefined' && !Element.prototype.matches) {
  7. var proto = Element.prototype;
  8. proto.matches = proto.matchesSelector ||
  9. proto.mozMatchesSelector ||
  10. proto.msMatchesSelector ||
  11. proto.oMatchesSelector ||
  12. proto.webkitMatchesSelector;
  13. }
  14. /**
  15. * Finds the closest parent that matches a selector.
  16. *
  17. * @param {Element} element
  18. * @param {String} selector
  19. * @return {Function}
  20. */
  21. function closest (element, selector) {
  22. while (element && element.nodeType !== DOCUMENT_NODE_TYPE) {
  23. if (element.matches(selector)) return element;
  24. element = element.parentNode;
  25. }
  26. }
  27. module.exports = closest;
  28. },{}],2:[function(require,module,exports){
  29. var closest = require('./closest');
  30. /**
  31. * Delegates event to a selector.
  32. *
  33. * @param {Element} element
  34. * @param {String} selector
  35. * @param {String} type
  36. * @param {Function} callback
  37. * @param {Boolean} useCapture
  38. * @return {Object}
  39. */
  40. function delegate(element, selector, type, callback, useCapture) {
  41. var listenerFn = listener.apply(this, arguments);
  42. element.addEventListener(type, listenerFn, useCapture);
  43. return {
  44. destroy: function() {
  45. element.removeEventListener(type, listenerFn, useCapture);
  46. }
  47. }
  48. }
  49. /**
  50. * Finds closest match and invokes callback.
  51. *
  52. * @param {Element} element
  53. * @param {String} selector
  54. * @param {String} type
  55. * @param {Function} callback
  56. * @return {Function}
  57. */
  58. function listener(element, selector, type, callback) {
  59. return function(e) {
  60. e.delegateTarget = closest(e.target, selector);
  61. if (e.delegateTarget) {
  62. callback.call(element, e);
  63. }
  64. }
  65. }
  66. module.exports = delegate;
  67. },{"./closest":1}]},{},[2])(2)
  68. });