dom-2ec8c9ed.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. 'use strict';
  7. const resources = require('./resources-b5a5f8a7.js');
  8. const guid = require('./guid-f4f03a7a.js');
  9. /**
  10. * This helper will guarantee an ID on the provided element.
  11. *
  12. * If it already has an ID, it will be preserved, otherwise a unique one will be generated and assigned.
  13. *
  14. * @param el
  15. * @returns {string} The element's ID.
  16. */
  17. function ensureId(el) {
  18. if (!el) {
  19. return "";
  20. }
  21. return (el.id = el.id || `${el.tagName.toLowerCase()}-${guid.guid()}`);
  22. }
  23. function nodeListToArray(nodeList) {
  24. return Array.isArray(nodeList) ? nodeList : Array.from(nodeList);
  25. }
  26. function getThemeName(el) {
  27. const closestElWithTheme = closestElementCrossShadowBoundary(el, `.${resources.CSS_UTILITY.darkTheme}, .${resources.CSS_UTILITY.lightTheme}`);
  28. return (closestElWithTheme === null || closestElWithTheme === void 0 ? void 0 : closestElWithTheme.classList.contains("calcite-theme-dark")) ? "dark" : "light";
  29. }
  30. function getElementDir(el) {
  31. const prop = "dir";
  32. const selector = `[${prop}]`;
  33. const closest = closestElementCrossShadowBoundary(el, selector);
  34. return closest ? closest.getAttribute(prop) : "ltr";
  35. }
  36. function getElementProp(el, prop, fallbackValue) {
  37. const selector = `[${prop}]`;
  38. const closest = el.closest(selector);
  39. return closest ? closest.getAttribute(prop) : fallbackValue;
  40. }
  41. function getRootNode(el) {
  42. return el.getRootNode();
  43. }
  44. function getHost(root) {
  45. return root.host || null;
  46. }
  47. /**
  48. * This helper queries an element's rootNode and any ancestor rootNodes.
  49. *
  50. * If both an 'id' and 'selector' are supplied, 'id' will take precedence over 'selector'.
  51. *
  52. * @param element
  53. * @param root0
  54. * @param root0.selector
  55. * @param root0.id
  56. * @returns {Element} The element.
  57. */
  58. function queryElementRoots(element, { selector, id }) {
  59. // Gets the rootNode and any ancestor rootNodes (shadowRoot or document) of an element and queries them for a selector.
  60. // Based on: https://stackoverflow.com/q/54520554/194216
  61. function queryFrom(el) {
  62. if (!el) {
  63. return null;
  64. }
  65. if (el.assignedSlot) {
  66. el = el.assignedSlot;
  67. }
  68. const rootNode = getRootNode(el);
  69. const found = id
  70. ? "getElementById" in rootNode
  71. ? /*
  72. Check to make sure 'getElementById' exists in cases where element is no longer connected to the DOM and getRootNode() returns the element.
  73. https://github.com/Esri/calcite-components/pull/4280
  74. */
  75. rootNode.getElementById(id)
  76. : null
  77. : selector
  78. ? rootNode.querySelector(selector)
  79. : null;
  80. const host = getHost(rootNode);
  81. return found ? found : host ? queryFrom(host) : null;
  82. }
  83. return queryFrom(element);
  84. }
  85. function closestElementCrossShadowBoundary(element, selector) {
  86. // based on https://stackoverflow.com/q/54520554/194216
  87. function closestFrom(el) {
  88. return el ? el.closest(selector) || closestFrom(getHost(getRootNode(el))) : null;
  89. }
  90. return closestFrom(element);
  91. }
  92. /**
  93. * This utility helps invoke a callback as it traverses a node and its ancestors until reaching the root document.
  94. *
  95. * Returning early or undefined in `onVisit` will continue traversing up the DOM tree. Otherwise, traversal will halt with the returned value as the result of the function
  96. *
  97. * @param element
  98. * @param onVisit
  99. */
  100. function walkUpAncestry(element, onVisit) {
  101. return visit(element, onVisit);
  102. }
  103. function visit(node, onVisit) {
  104. if (!node) {
  105. return;
  106. }
  107. const result = onVisit(node);
  108. if (result !== undefined) {
  109. return result;
  110. }
  111. const { parentNode } = node;
  112. return visit(parentNode instanceof ShadowRoot ? parentNode.host : parentNode, onVisit);
  113. }
  114. function containsCrossShadowBoundary(element, maybeDescendant) {
  115. return !!walkUpAncestry(maybeDescendant, (node) => (node === element ? true : undefined));
  116. }
  117. function isCalciteFocusable(el) {
  118. return typeof (el === null || el === void 0 ? void 0 : el.setFocus) === "function";
  119. }
  120. async function focusElement(el) {
  121. if (!el) {
  122. return;
  123. }
  124. return isCalciteFocusable(el) ? el.setFocus() : el.focus();
  125. }
  126. const defaultSlotSelector = ":not([slot])";
  127. function getSlotted(element, slotName, options) {
  128. if (slotName && !Array.isArray(slotName) && typeof slotName !== "string") {
  129. options = slotName;
  130. slotName = null;
  131. }
  132. const slotSelector = slotName
  133. ? Array.isArray(slotName)
  134. ? slotName.map((name) => `[slot="${name}"]`).join(",")
  135. : `[slot="${slotName}"]`
  136. : defaultSlotSelector;
  137. if (options === null || options === void 0 ? void 0 : options.all) {
  138. return queryMultiple(element, slotSelector, options);
  139. }
  140. return querySingle(element, slotSelector, options);
  141. }
  142. function getDirectChildren(el, selector) {
  143. return el ? Array.from(el.children || []).filter((child) => child === null || child === void 0 ? void 0 : child.matches(selector)) : [];
  144. }
  145. function queryMultiple(element, slotSelector, options) {
  146. let matches = slotSelector === defaultSlotSelector
  147. ? getDirectChildren(element, defaultSlotSelector)
  148. : Array.from(element.querySelectorAll(slotSelector));
  149. matches = options && options.direct === false ? matches : matches.filter((el) => el.parentElement === element);
  150. matches = (options === null || options === void 0 ? void 0 : options.matches) ? matches.filter((el) => el === null || el === void 0 ? void 0 : el.matches(options.matches)) : matches;
  151. const selector = options === null || options === void 0 ? void 0 : options.selector;
  152. return selector
  153. ? matches
  154. .map((item) => Array.from(item.querySelectorAll(selector)))
  155. .reduce((previousValue, currentValue) => [...previousValue, ...currentValue], [])
  156. .filter((match) => !!match)
  157. : matches;
  158. }
  159. function querySingle(element, slotSelector, options) {
  160. let match = slotSelector === defaultSlotSelector
  161. ? getDirectChildren(element, defaultSlotSelector)[0] || null
  162. : element.querySelector(slotSelector);
  163. match = options && options.direct === false ? match : (match === null || match === void 0 ? void 0 : match.parentElement) === element ? match : null;
  164. match = (options === null || options === void 0 ? void 0 : options.matches) ? ((match === null || match === void 0 ? void 0 : match.matches(options.matches)) ? match : null) : match;
  165. const selector = options === null || options === void 0 ? void 0 : options.selector;
  166. return selector ? match === null || match === void 0 ? void 0 : match.querySelector(selector) : match;
  167. }
  168. function filterDirectChildren(el, selector) {
  169. return Array.from(el.children).filter((child) => child.matches(selector));
  170. }
  171. // set a default icon from a defined set or allow an override with an icon name string
  172. function setRequestedIcon(iconObject, iconValue, matchedValue) {
  173. if (typeof iconValue === "string" && iconValue !== "") {
  174. return iconValue;
  175. }
  176. else if (iconValue === "") {
  177. return iconObject[matchedValue];
  178. }
  179. }
  180. function intersects(rect1, rect2) {
  181. return !(rect2.left > rect1.right ||
  182. rect2.right < rect1.left ||
  183. rect2.top > rect1.bottom ||
  184. rect2.bottom < rect1.top);
  185. }
  186. /**
  187. * This helper makes sure that boolean aria attributes are properly converted to a string.
  188. *
  189. * It should only be used for aria attributes that require a string value of "true" or "false".
  190. *
  191. * @param value
  192. * @returns {string} The string conversion of a boolean value ("true" | "false").
  193. */
  194. function toAriaBoolean(value) {
  195. return Boolean(value).toString();
  196. }
  197. /**
  198. * This helper returns true if the pointer event fired from the primary button of the device.
  199. *
  200. * See https://www.w3.org/TR/pointerevents/#the-button-property.
  201. *
  202. * @param event
  203. * @returns {boolean}
  204. */
  205. function isPrimaryPointerButton(event) {
  206. return !!(event.isPrimary && event.button === 0);
  207. }
  208. exports.closestElementCrossShadowBoundary = closestElementCrossShadowBoundary;
  209. exports.containsCrossShadowBoundary = containsCrossShadowBoundary;
  210. exports.ensureId = ensureId;
  211. exports.filterDirectChildren = filterDirectChildren;
  212. exports.focusElement = focusElement;
  213. exports.getElementDir = getElementDir;
  214. exports.getElementProp = getElementProp;
  215. exports.getRootNode = getRootNode;
  216. exports.getSlotted = getSlotted;
  217. exports.getThemeName = getThemeName;
  218. exports.intersects = intersects;
  219. exports.isCalciteFocusable = isCalciteFocusable;
  220. exports.isPrimaryPointerButton = isPrimaryPointerButton;
  221. exports.nodeListToArray = nodeListToArray;
  222. exports.queryElementRoots = queryElementRoots;
  223. exports.setRequestedIcon = setRequestedIcon;
  224. exports.toAriaBoolean = toAriaBoolean;