util.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Util = void 0;
  4. const store_1 = require("./store");
  5. var Util;
  6. (function (Util) {
  7. Util.returnTrue = () => true;
  8. Util.returnFalse = () => false;
  9. function stopPropagationCallback(e) {
  10. e.stopPropagation();
  11. }
  12. Util.stopPropagationCallback = stopPropagationCallback;
  13. function addEventListener(elem, type, handler) {
  14. if (elem.addEventListener != null) {
  15. elem.addEventListener(type, handler);
  16. }
  17. }
  18. Util.addEventListener = addEventListener;
  19. function removeEventListener(elem, type, handler) {
  20. if (elem.removeEventListener != null) {
  21. elem.removeEventListener(type, handler);
  22. }
  23. }
  24. Util.removeEventListener = removeEventListener;
  25. })(Util = exports.Util || (exports.Util = {}));
  26. (function (Util) {
  27. const rNotHTMLWhite = /[^\x20\t\r\n\f]+/g;
  28. const rNamespace = /^([^.]*)(?:\.(.+)|)/;
  29. function splitType(types) {
  30. return (types || '').match(rNotHTMLWhite) || [''];
  31. }
  32. Util.splitType = splitType;
  33. function normalizeType(type) {
  34. const parts = rNamespace.exec(type) || [];
  35. return {
  36. originType: parts[1] ? parts[1].trim() : parts[1],
  37. namespaces: parts[2]
  38. ? parts[2]
  39. .split('.')
  40. .map((ns) => ns.trim())
  41. .sort()
  42. : [],
  43. };
  44. }
  45. Util.normalizeType = normalizeType;
  46. function isValidTarget(target) {
  47. // Accepts only:
  48. // - Node
  49. // - Node.ELEMENT_NODE
  50. // - Node.DOCUMENT_NODE
  51. // - Object
  52. // - Any
  53. return target.nodeType === 1 || target.nodeType === 9 || !+target.nodeType;
  54. }
  55. Util.isValidTarget = isValidTarget;
  56. function isValidSelector(elem, selector) {
  57. if (selector) {
  58. const node = elem;
  59. return node.querySelector != null && node.querySelector(selector) != null;
  60. }
  61. return true;
  62. }
  63. Util.isValidSelector = isValidSelector;
  64. })(Util = exports.Util || (exports.Util = {}));
  65. (function (Util) {
  66. let seed = 0;
  67. const cache = new WeakMap();
  68. function ensureHandlerId(handler) {
  69. if (!cache.has(handler)) {
  70. cache.set(handler, seed);
  71. seed += 1;
  72. }
  73. return cache.get(handler);
  74. }
  75. Util.ensureHandlerId = ensureHandlerId;
  76. function getHandlerId(handler) {
  77. return cache.get(handler);
  78. }
  79. Util.getHandlerId = getHandlerId;
  80. function removeHandlerId(handler) {
  81. return cache.delete(handler);
  82. }
  83. Util.removeHandlerId = removeHandlerId;
  84. function setHandlerId(handler, id) {
  85. return cache.set(handler, id);
  86. }
  87. Util.setHandlerId = setHandlerId;
  88. })(Util = exports.Util || (exports.Util = {}));
  89. (function (Util) {
  90. function getHandlerQueue(elem, event) {
  91. const queue = [];
  92. const store = store_1.Store.get(elem);
  93. const bag = store && store.events && store.events[event.type];
  94. const handlers = (bag && bag.handlers) || [];
  95. const delegateCount = bag ? bag.delegateCount : 0;
  96. if (delegateCount > 0 &&
  97. // Support: Firefox <=42 - 66+
  98. // Suppress spec-violating clicks indicating a non-primary pointer button (trac-3861)
  99. // https://www.w3.org/TR/DOM-Level-3-Events/#event-type-click
  100. // Support: IE 11+
  101. // ...but not arrow key "clicks" of radio inputs, which can have `button` -1 (gh-2343)
  102. !(event.type === 'click' &&
  103. typeof event.button === 'number' &&
  104. event.button >= 1)) {
  105. for (let curr = event.target; curr !== elem; curr = curr.parentNode || elem) {
  106. // Don't check non-elements
  107. // Don't process clicks on disabled elements
  108. if (curr.nodeType === 1 &&
  109. !(event.type === 'click' && curr.disabled === true)) {
  110. const matchedHandlers = [];
  111. const matchedSelectors = {};
  112. for (let i = 0; i < delegateCount; i += 1) {
  113. const handleObj = handlers[i];
  114. const selector = handleObj.selector;
  115. if (selector != null && matchedSelectors[selector] == null) {
  116. const node = elem;
  117. const nodes = [];
  118. node.querySelectorAll(selector).forEach((child) => {
  119. nodes.push(child);
  120. });
  121. matchedSelectors[selector] = nodes.includes(curr);
  122. }
  123. if (matchedSelectors[selector]) {
  124. matchedHandlers.push(handleObj);
  125. }
  126. }
  127. if (matchedHandlers.length) {
  128. queue.push({ elem: curr, handlers: matchedHandlers });
  129. }
  130. }
  131. }
  132. }
  133. // Add the remaining (directly-bound) handlers
  134. if (delegateCount < handlers.length) {
  135. queue.push({ elem, handlers: handlers.slice(delegateCount) });
  136. }
  137. return queue;
  138. }
  139. Util.getHandlerQueue = getHandlerQueue;
  140. })(Util = exports.Util || (exports.Util = {}));
  141. (function (Util) {
  142. function isWindow(obj) {
  143. return obj != null && obj === obj.window;
  144. }
  145. Util.isWindow = isWindow;
  146. })(Util = exports.Util || (exports.Util = {}));
  147. (function (Util) {
  148. function contains(a, b) {
  149. const adown = a.nodeType === 9 ? a.documentElement : a;
  150. const bup = b && b.parentNode;
  151. return (a === bup ||
  152. !!(bup &&
  153. bup.nodeType === 1 &&
  154. // Support: IE 9 - 11+
  155. // IE doesn't have `contains` on SVG.
  156. (adown.contains
  157. ? adown.contains(bup)
  158. : a.compareDocumentPosition && a.compareDocumentPosition(bup) & 16)));
  159. }
  160. Util.contains = contains;
  161. })(Util = exports.Util || (exports.Util = {}));
  162. //# sourceMappingURL=util.js.map