utils.js 1.5 KB

1234567891011121314151617181920212223242526272829303132
  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. import { nodeListToArray } from "../../utils/dom";
  7. import { ComboboxChildSelector } from "./resources";
  8. import { Build } from "@stencil/core";
  9. export function getAncestors(element) {
  10. var _a, _b;
  11. const parent = (_a = element.parentElement) === null || _a === void 0 ? void 0 : _a.closest(ComboboxChildSelector);
  12. const grandparent = (_b = parent === null || parent === void 0 ? void 0 : parent.parentElement) === null || _b === void 0 ? void 0 : _b.closest(ComboboxChildSelector);
  13. return [parent, grandparent].filter((el) => el);
  14. }
  15. export function getItemAncestors(item) {
  16. var _a;
  17. return (((_a = item.ancestors) === null || _a === void 0 ? void 0 : _a.filter((el) => el.nodeName === "CALCITE-COMBOBOX-ITEM")) || []);
  18. }
  19. export function getItemChildren(item) {
  20. return nodeListToArray(item.querySelectorAll("calcite-combobox-item"));
  21. }
  22. export function hasActiveChildren(node) {
  23. const items = nodeListToArray(node.querySelectorAll("calcite-combobox-item"));
  24. return items.filter((item) => item.selected).length > 0;
  25. }
  26. export function getDepth(element) {
  27. if (!Build.isBrowser) {
  28. return 0;
  29. }
  30. const result = document.evaluate("ancestor::calcite-combobox-item | ancestor::calcite-combobox-item-group", element, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  31. return result.snapshotLength;
  32. }