3ad084162861e0f5fa6a060aa6c7143d29b7b79bc5f0a28b392e425ac149fd7c48ff5691fd584e97e38afb215494f10283cc441ad3ea08a4df215c221c9691 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.listen = 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}],3:[function(require,module,exports){
  68. /**
  69. * Check if argument is a HTML element.
  70. *
  71. * @param {Object} value
  72. * @return {Boolean}
  73. */
  74. exports.node = function(value) {
  75. return value !== undefined
  76. && value instanceof HTMLElement
  77. && value.nodeType === 1;
  78. };
  79. /**
  80. * Check if argument is a list of HTML elements.
  81. *
  82. * @param {Object} value
  83. * @return {Boolean}
  84. */
  85. exports.nodeList = function(value) {
  86. var type = Object.prototype.toString.call(value);
  87. return value !== undefined
  88. && (type === '[object NodeList]' || type === '[object HTMLCollection]')
  89. && ('length' in value)
  90. && (value.length === 0 || exports.node(value[0]));
  91. };
  92. /**
  93. * Check if argument is a string.
  94. *
  95. * @param {Object} value
  96. * @return {Boolean}
  97. */
  98. exports.string = function(value) {
  99. return typeof value === 'string'
  100. || value instanceof String;
  101. };
  102. /**
  103. * Check if argument is a function.
  104. *
  105. * @param {Object} value
  106. * @return {Boolean}
  107. */
  108. exports.fn = function(value) {
  109. var type = Object.prototype.toString.call(value);
  110. return type === '[object Function]';
  111. };
  112. },{}],4:[function(require,module,exports){
  113. var is = require('./is');
  114. var delegate = require('delegate');
  115. /**
  116. * Validates all params and calls the right
  117. * listener function based on its target type.
  118. *
  119. * @param {String|HTMLElement|HTMLCollection|NodeList} target
  120. * @param {String} type
  121. * @param {Function} callback
  122. * @return {Object}
  123. */
  124. function listen(target, type, callback) {
  125. if (!target && !type && !callback) {
  126. throw new Error('Missing required arguments');
  127. }
  128. if (!is.string(type)) {
  129. throw new TypeError('Second argument must be a String');
  130. }
  131. if (!is.fn(callback)) {
  132. throw new TypeError('Third argument must be a Function');
  133. }
  134. if (is.node(target)) {
  135. return listenNode(target, type, callback);
  136. }
  137. else if (is.nodeList(target)) {
  138. return listenNodeList(target, type, callback);
  139. }
  140. else if (is.string(target)) {
  141. return listenSelector(target, type, callback);
  142. }
  143. else {
  144. throw new TypeError('First argument must be a String, HTMLElement, HTMLCollection, or NodeList');
  145. }
  146. }
  147. /**
  148. * Adds an event listener to a HTML element
  149. * and returns a remove listener function.
  150. *
  151. * @param {HTMLElement} node
  152. * @param {String} type
  153. * @param {Function} callback
  154. * @return {Object}
  155. */
  156. function listenNode(node, type, callback) {
  157. node.addEventListener(type, callback);
  158. return {
  159. destroy: function() {
  160. node.removeEventListener(type, callback);
  161. }
  162. }
  163. }
  164. /**
  165. * Add an event listener to a list of HTML elements
  166. * and returns a remove listener function.
  167. *
  168. * @param {NodeList|HTMLCollection} nodeList
  169. * @param {String} type
  170. * @param {Function} callback
  171. * @return {Object}
  172. */
  173. function listenNodeList(nodeList, type, callback) {
  174. Array.prototype.forEach.call(nodeList, function(node) {
  175. node.addEventListener(type, callback);
  176. });
  177. return {
  178. destroy: function() {
  179. Array.prototype.forEach.call(nodeList, function(node) {
  180. node.removeEventListener(type, callback);
  181. });
  182. }
  183. }
  184. }
  185. /**
  186. * Add an event listener to a selector
  187. * and returns a remove listener function.
  188. *
  189. * @param {String} selector
  190. * @param {String} type
  191. * @param {Function} callback
  192. * @return {Object}
  193. */
  194. function listenSelector(selector, type, callback) {
  195. return delegate(document.body, selector, type, callback);
  196. }
  197. module.exports = listen;
  198. },{"./is":3,"delegate":2}]},{},[4])(4)
  199. });