calcite-tree_2.entry.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-1f9b54dc.js';
  7. import { f as focusElement, n as nodeListToArray, l as getRootNode, g as getSlotted, a as getElementDir, t as toAriaBoolean, m as filterDirectChildren } from './dom-8f0a9ff2.js';
  8. import { C as CSS_UTILITY } from './resources-9c476cb6.js';
  9. import { c as connectConditionalSlotComponent, d as disconnectConditionalSlotComponent } from './conditionalSlot-a3e8c63a.js';
  10. import { u as updateHostInteraction } from './interactive-5db230e8.js';
  11. import './guid-9f15e57a.js';
  12. import './observers-9f44e9b3.js';
  13. var TreeSelectionMode;
  14. (function (TreeSelectionMode) {
  15. TreeSelectionMode["Single"] = "single";
  16. TreeSelectionMode["Multi"] = "multi";
  17. TreeSelectionMode["None"] = "none";
  18. TreeSelectionMode["Children"] = "children";
  19. TreeSelectionMode["MultiChildren"] = "multi-children";
  20. TreeSelectionMode["Ancestors"] = "ancestors";
  21. })(TreeSelectionMode || (TreeSelectionMode = {}));
  22. function isTreeItem(element) {
  23. return element === null || element === void 0 ? void 0 : element.matches("calcite-tree-item");
  24. }
  25. function getEnabledSiblingItem(el, direction) {
  26. const directionProp = direction === "down" ? "nextElementSibling" : "previousElementSibling";
  27. let currentEl = el;
  28. let enabledEl = null;
  29. while (isTreeItem(currentEl)) {
  30. if (!currentEl.disabled) {
  31. enabledEl = currentEl;
  32. break;
  33. }
  34. currentEl = currentEl[directionProp];
  35. }
  36. return enabledEl;
  37. }
  38. const treeCss = "@keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in-down{0%{opacity:0;transform:translate3D(0, -5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;transform:translate3D(0, 5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-scale{0%{opacity:0;transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;animation-fill-mode:both;animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{animation-name:in}.calcite-animate__in-down{animation-name:in-down}.calcite-animate__in-up{animation-name:in-up}.calcite-animate__in-scale{animation-name:in-scale}@media (prefers-reduced-motion: reduce){:root{--calcite-internal-duration-factor:0.01}}:root{--calcite-floating-ui-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{display:block}:host(:focus){outline:2px solid transparent;outline-offset:2px}";
  39. const Tree = class {
  40. constructor(hostRef) {
  41. registerInstance(this, hostRef);
  42. this.calciteTreeSelect = createEvent(this, "calciteTreeSelect", 6);
  43. //--------------------------------------------------------------------------
  44. //
  45. // Properties
  46. //
  47. //--------------------------------------------------------------------------
  48. /** Displays indentation guide lines. */
  49. this.lines = false;
  50. /**
  51. * Display input
  52. *
  53. * @deprecated Use `selectionMode="ancestors"` for checkbox input.
  54. */
  55. this.inputEnabled = false;
  56. /** Specifies the size of the component. */
  57. this.scale = "m";
  58. /**
  59. * Customize how the component's selection works.
  60. *
  61. * @default "single"
  62. * @see [TreeSelectionMode](https://github.com/Esri/calcite-components/blob/master/src/components/tree/interfaces.ts#L5)
  63. */
  64. this.selectionMode = TreeSelectionMode.Single;
  65. }
  66. //--------------------------------------------------------------------------
  67. //
  68. // Lifecycle
  69. //
  70. //--------------------------------------------------------------------------
  71. componentWillRender() {
  72. var _a;
  73. const parent = (_a = this.el.parentElement) === null || _a === void 0 ? void 0 : _a.closest("calcite-tree");
  74. this.lines = parent ? parent.lines : this.lines;
  75. this.scale = parent ? parent.scale : this.scale;
  76. this.selectionMode = parent ? parent.selectionMode : this.selectionMode;
  77. this.child = !!parent;
  78. }
  79. render() {
  80. return (h(Host, { "aria-multiselectable": this.child
  81. ? undefined
  82. : (this.selectionMode === TreeSelectionMode.Multi ||
  83. this.selectionMode === TreeSelectionMode.MultiChildren).toString(), role: !this.child ? "tree" : undefined, tabIndex: this.getRootTabIndex() }, h("slot", null)));
  84. }
  85. //--------------------------------------------------------------------------
  86. //
  87. // Event Listeners
  88. //
  89. //--------------------------------------------------------------------------
  90. onFocus() {
  91. if (!this.child) {
  92. const focusTarget = this.el.querySelector("calcite-tree-item[selected]:not([disabled])") || this.el.querySelector("calcite-tree-item:not([disabled])");
  93. focusElement(focusTarget);
  94. }
  95. }
  96. onFocusIn(event) {
  97. const focusedFromRootOrOutsideTree = event.relatedTarget === this.el || !this.el.contains(event.relatedTarget);
  98. if (focusedFromRootOrOutsideTree) {
  99. // gives user the ability to tab into external elements (modifying tabindex property will not work in firefox)
  100. this.el.removeAttribute("tabindex");
  101. }
  102. }
  103. onFocusOut(event) {
  104. const willFocusOutsideTree = !this.el.contains(event.relatedTarget);
  105. if (willFocusOutsideTree) {
  106. this.el.tabIndex = this.getRootTabIndex();
  107. }
  108. }
  109. onClick(event) {
  110. const target = event.target;
  111. const childItems = nodeListToArray(target.querySelectorAll("calcite-tree-item"));
  112. if (this.child) {
  113. return;
  114. }
  115. if (!this.child) {
  116. event.preventDefault();
  117. event.stopPropagation();
  118. }
  119. if (this.selectionMode === TreeSelectionMode.Ancestors && !this.child) {
  120. this.updateAncestorTree(event);
  121. return;
  122. }
  123. const isNoneSelectionMode = this.selectionMode === TreeSelectionMode.None;
  124. const shouldSelect = this.selectionMode !== null &&
  125. (!target.hasChildren ||
  126. (target.hasChildren &&
  127. (this.selectionMode === TreeSelectionMode.Children ||
  128. this.selectionMode === TreeSelectionMode.MultiChildren)));
  129. const shouldModifyToCurrentSelection = !isNoneSelectionMode &&
  130. event.detail.modifyCurrentSelection &&
  131. (this.selectionMode === TreeSelectionMode.Multi ||
  132. this.selectionMode === TreeSelectionMode.MultiChildren);
  133. const shouldSelectChildren = this.selectionMode === TreeSelectionMode.MultiChildren ||
  134. this.selectionMode === TreeSelectionMode.Children;
  135. const shouldClearCurrentSelection = !shouldModifyToCurrentSelection &&
  136. (((this.selectionMode === TreeSelectionMode.Single ||
  137. this.selectionMode === TreeSelectionMode.Multi) &&
  138. childItems.length <= 0) ||
  139. this.selectionMode === TreeSelectionMode.Children ||
  140. this.selectionMode === TreeSelectionMode.MultiChildren);
  141. const shouldExpandTarget = this.selectionMode === TreeSelectionMode.Children ||
  142. this.selectionMode === TreeSelectionMode.MultiChildren;
  143. if (!this.child) {
  144. const targetItems = [];
  145. if (shouldSelect) {
  146. targetItems.push(target);
  147. }
  148. if (shouldSelectChildren) {
  149. childItems.forEach((treeItem) => {
  150. targetItems.push(treeItem);
  151. });
  152. }
  153. if (shouldClearCurrentSelection) {
  154. const selectedItems = nodeListToArray(this.el.querySelectorAll("calcite-tree-item[selected]"));
  155. selectedItems.forEach((treeItem) => {
  156. if (!targetItems.includes(treeItem)) {
  157. treeItem.selected = false;
  158. }
  159. });
  160. }
  161. if (shouldExpandTarget && !event.detail.forceToggle) {
  162. target.expanded = true;
  163. }
  164. if (shouldModifyToCurrentSelection) {
  165. window.getSelection().removeAllRanges();
  166. }
  167. if ((shouldModifyToCurrentSelection && target.selected) ||
  168. (shouldSelectChildren && event.detail.forceToggle)) {
  169. targetItems.forEach((treeItem) => {
  170. if (!treeItem.disabled) {
  171. treeItem.selected = false;
  172. }
  173. });
  174. }
  175. else if (!isNoneSelectionMode) {
  176. targetItems.forEach((treeItem) => {
  177. if (!treeItem.disabled) {
  178. treeItem.selected = true;
  179. }
  180. });
  181. }
  182. }
  183. const selected = isNoneSelectionMode
  184. ? [target]
  185. : nodeListToArray(this.el.querySelectorAll("calcite-tree-item")).filter((i) => i.selected);
  186. this.calciteTreeSelect.emit({ selected });
  187. event.stopPropagation();
  188. }
  189. keyDownHandler(event) {
  190. var _a;
  191. const root = this.el.closest("calcite-tree:not([child])");
  192. const target = event.target;
  193. if (!(root === this.el && target.tagName === "CALCITE-TREE-ITEM" && this.el.contains(target))) {
  194. return;
  195. }
  196. if (event.key === "ArrowDown") {
  197. const next = getEnabledSiblingItem(target.nextElementSibling, "down");
  198. if (next) {
  199. next.focus();
  200. event.preventDefault();
  201. }
  202. return;
  203. }
  204. if (event.key === "ArrowUp") {
  205. const previous = getEnabledSiblingItem(target.previousElementSibling, "up");
  206. if (previous) {
  207. previous.focus();
  208. event.preventDefault();
  209. }
  210. }
  211. if (event.key === "ArrowLeft" && !target.disabled) {
  212. // When focus is on an open node, closes the node.
  213. if (target.hasChildren && target.expanded) {
  214. target.expanded = false;
  215. event.preventDefault();
  216. return;
  217. }
  218. // When focus is on a child node that is also either an end node or a closed node, moves focus to its parent node.
  219. const parentItem = target.parentElement.closest("calcite-tree-item");
  220. if (parentItem && (!target.hasChildren || target.expanded === false)) {
  221. parentItem.focus();
  222. event.preventDefault();
  223. return;
  224. }
  225. // When focus is on a root node that is also either an end node or a closed node, does nothing.
  226. return;
  227. }
  228. if (event.key === "ArrowRight" && !target.disabled) {
  229. if (target.hasChildren) {
  230. if (target.expanded && getRootNode(this.el).activeElement === target) {
  231. // When focus is on an open node, moves focus to the first child node.
  232. (_a = getEnabledSiblingItem(target.querySelector("calcite-tree-item"), "down")) === null || _a === void 0 ? void 0 : _a.focus();
  233. event.preventDefault();
  234. }
  235. else {
  236. // When focus is on a closed node, opens the node; focus does not move.
  237. target.expanded = true;
  238. event.preventDefault();
  239. }
  240. }
  241. return;
  242. }
  243. }
  244. updateAncestorTree(event) {
  245. const item = event.target;
  246. if (item.disabled) {
  247. return;
  248. }
  249. const ancestors = [];
  250. let parent = item.parentElement.closest("calcite-tree-item");
  251. while (parent) {
  252. ancestors.push(parent);
  253. parent = parent.parentElement.closest("calcite-tree-item");
  254. }
  255. const childItems = Array.from(item.querySelectorAll("calcite-tree-item:not([disabled])"));
  256. const childItemsWithNoChildren = childItems.filter((child) => !child.hasChildren);
  257. const childItemsWithChildren = childItems.filter((child) => child.hasChildren);
  258. const futureSelected = item.hasChildren
  259. ? !(item.selected || item.indeterminate)
  260. : !item.selected;
  261. childItemsWithNoChildren.forEach((el) => {
  262. el.selected = futureSelected;
  263. el.indeterminate = false;
  264. });
  265. function updateItemState(childItems, item) {
  266. const selected = childItems.filter((child) => child.selected);
  267. const unselected = childItems.filter((child) => !child.selected);
  268. item.selected = selected.length === childItems.length;
  269. item.indeterminate = selected.length > 0 && unselected.length > 0;
  270. }
  271. childItemsWithChildren.forEach((el) => {
  272. const directChildItems = Array.from(el.querySelectorAll(":scope > calcite-tree > calcite-tree-item"));
  273. updateItemState(directChildItems, el);
  274. });
  275. if (item.hasChildren) {
  276. updateItemState(childItems, item);
  277. }
  278. else {
  279. item.selected = futureSelected;
  280. item.indeterminate = false;
  281. }
  282. ancestors.forEach((ancestor) => {
  283. const descendants = nodeListToArray(ancestor.querySelectorAll("calcite-tree-item"));
  284. const activeDescendants = descendants.filter((el) => el.selected);
  285. if (activeDescendants.length === 0) {
  286. ancestor.selected = false;
  287. ancestor.indeterminate = false;
  288. return;
  289. }
  290. const indeterminate = activeDescendants.length < descendants.length;
  291. ancestor.indeterminate = indeterminate;
  292. ancestor.selected = !indeterminate;
  293. });
  294. this.calciteTreeSelect.emit({
  295. selected: nodeListToArray(this.el.querySelectorAll("calcite-tree-item")).filter((i) => i.selected)
  296. });
  297. }
  298. // --------------------------------------------------------------------------
  299. //
  300. // Private Methods
  301. //
  302. //--------------------------------------------------------------------------
  303. getRootTabIndex() {
  304. return !this.child ? 0 : -1;
  305. }
  306. get el() { return getElement(this); }
  307. };
  308. Tree.style = treeCss;
  309. const CSS = {
  310. checkboxLabel: "checkbox-label",
  311. checkbox: "checkbox",
  312. chevron: "chevron",
  313. nodeContainer: "node-container",
  314. childrenContainer: "children-container",
  315. bulletPointIcon: "bullet-point",
  316. checkmarkIcon: "checkmark"
  317. };
  318. const SLOTS = {
  319. children: "children"
  320. };
  321. const ICONS = {
  322. bulletPoint: "bullet-point",
  323. checkmark: "check",
  324. chevronRight: "chevron-right",
  325. blank: "blank"
  326. };
  327. const treeItemCss = "@keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in-down{0%{opacity:0;transform:translate3D(0, -5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;transform:translate3D(0, 5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-scale{0%{opacity:0;transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;animation-fill-mode:both;animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{animation-name:in}.calcite-animate__in-down{animation-name:in-down}.calcite-animate__in-up{animation-name:in-up}.calcite-animate__in-scale{animation-name:in-scale}@media (prefers-reduced-motion: reduce){:root{--calcite-internal-duration-factor:0.01}}:root{--calcite-floating-ui-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host{display:block;max-inline-size:100%;cursor:pointer;color:var(--calcite-ui-text-3)}:host([calcite-hydrated-hidden]){visibility:hidden !important;pointer-events:none}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host([scale=s]){font-size:var(--calcite-font-size--2);line-height:1rem}:host([scale=s]) .node-container{--calcite-tree-padding-y:0.25rem}:host([scale=s]) .node-container .checkbox,:host([scale=s]) .node-container .chevron,:host([scale=s]) .node-container .checkmark,:host([scale=s]) .node-container .bullet-point{margin-inline:0.25rem}:host([scale=m]){font-size:var(--calcite-font-size--1);line-height:1rem}:host([scale=m]) .node-container{--calcite-tree-padding-y:0.5rem}:host([scale=m]) .node-container .checkbox,:host([scale=m]) .node-container .chevron,:host([scale=m]) .node-container .checkmark,:host([scale=m]) .node-container .bullet-point{margin-inline:0.5rem}:host([scale=l]){font-size:var(--calcite-font-size-0);line-height:1.25rem}:host([scale=l]) .node-container{--calcite-tree-padding-y:0.75rem}:host([scale=l]) .node-container .checkbox,:host([scale=l]) .node-container .chevron,:host([scale=l]) .node-container .checkmark,:host([scale=l]) .node-container .bullet-point{margin-inline:0.75rem}:host([lines]) .children-container:after{position:absolute;inset-block-start:0px;z-index:1;inline-size:1px;transition-property:color, background-color, border-color, text-decoration-color, fill, stroke;transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);transition-duration:150ms;transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s;block-size:96%;content:\"\";background-color:var(--calcite-ui-border-2)}:host(:not([lines])) .node-container:after{display:none}::slotted(*){min-inline-size:0px;max-inline-size:100%;overflow-wrap:break-word;color:inherit;text-decoration:none !important}::slotted(*):hover{text-decoration:none !important}::slotted(a){inline-size:100%;text-decoration-line:none}:host{outline-color:transparent}:host(:focus:not([disabled])){outline:2px solid transparent;outline:2px solid var(--calcite-ui-brand);outline-offset:-2px}.checkbox{line-height:0}.checkbox-label{pointer-events:none;display:flex;align-items:center}.checkbox:focus{outline:2px solid transparent;outline-offset:2px}.children-container{position:relative;block-size:0px;overflow:hidden;margin-inline-start:1.25rem;transform:scaleY(0);opacity:0;transition:var(--calcite-animation-timing) cubic-bezier(0.215, 0.44, 0.42, 0.88), opacity var(--calcite-animation-timing) cubic-bezier(0.215, 0.44, 0.42, 0.88), all var(--calcite-animation-timing) ease-in-out;transform-origin:top}:host([expanded])>.children-container{transform:scaleY(1);opacity:1;block-size:auto}.node-container{position:relative;display:flex;align-items:center;padding-block:var(--calcite-tree-padding-y);padding-inline:0}.node-container .checkmark,.node-container .bullet-point{opacity:0;transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s;color:var(--calcite-ui-border-1)}.node-container:hover .checkmark,.node-container:hover .bullet-point,:host([selected]) .node-container:hover .checkmark,:host([selected]) .node-container:hover .bullet-point,:host(:focus:not([disabled])) .node-container .checkmark,:host(:focus:not([disabled])) .node-container .bullet-point{opacity:1}:host([selected])>.node-container,:host([selected])>.node-container:hover{font-weight:var(--calcite-font-weight-medium);color:var(--calcite-ui-text-1)}:host([selected])>.node-container .bullet-point,:host([selected])>.node-container .checkmark,:host([selected])>.node-container:hover .bullet-point,:host([selected])>.node-container:hover .checkmark{opacity:1;color:var(--calcite-ui-brand)}:host([selection-mode=none]:not([has-children])):host([scale=s])>.node-container{padding-inline-start:0.5rem}:host([selection-mode=none]:not([has-children])):host([scale=m])>.node-container{padding-inline-start:1rem}:host([selection-mode=none]:not([has-children])):host([scale=l])>.node-container{padding-inline-start:1.5rem}:host(:not([has-children])):host([scale=s])>.node-container[data-selection-mode=ancestors] .checkbox{padding-inline-start:1.25rem}:host(:not([has-children])):host([scale=m])>.node-container[data-selection-mode=ancestors] .checkbox{padding-inline-start:1.5rem}:host(:not([has-children])):host([scale=l])>.node-container[data-selection-mode=ancestors] .checkbox{padding-inline-start:1.75rem}:host([has-children])>.node-container[data-selection-mode=ancestors] .checkbox{margin-inline-start:0}:host([has-children])>.node-container .bullet-point,:host([has-children])>.node-container .checkmark{display:none}:host([has-children][expanded]:not([selected]):not([selection-mode=none]))>.node-container ::slotted(*){font-weight:var(--calcite-font-weight-medium);color:var(--calcite-ui-text-1)}:host([has-children][selected])>.node-container[data-selection-mode=children],:host([has-children][selected])>.node-container[data-selection-mode=multi-children]{color:var(--calcite-ui-brand)}.chevron{position:relative;align-self:center;color:var(--calcite-ui-text-3);transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s;flex:0 0 auto;transform:rotate(0deg)}.calcite--rtl .chevron{transform:rotate(180deg)}:host([expanded])>.node-container>.chevron{transform:rotate(90deg)}:host([selected]) .checkmark,:host([selected]) .bullet-point{color:var(--calcite-ui-brand)}";
  328. const TreeItem = class {
  329. constructor(hostRef) {
  330. registerInstance(this, hostRef);
  331. this.calciteInternalTreeItemSelect = createEvent(this, "calciteInternalTreeItemSelect", 6);
  332. //--------------------------------------------------------------------------
  333. //
  334. // Properties
  335. //
  336. //--------------------------------------------------------------------------
  337. /**
  338. * When `true`, interaction is prevented and the component is displayed with lower opacity.
  339. */
  340. this.disabled = false;
  341. /** When `true`, the component is selected. */
  342. this.selected = false;
  343. /** When `true`, the component is expanded. */
  344. this.expanded = false;
  345. /**
  346. * @internal
  347. */
  348. this.parentExpanded = false;
  349. /**
  350. * @internal
  351. */
  352. this.depth = -1;
  353. /**
  354. * @internal
  355. */
  356. this.hasChildren = null;
  357. this.iconClickHandler = (event) => {
  358. event.stopPropagation();
  359. this.expanded = !this.expanded;
  360. };
  361. this.childrenClickHandler = (event) => event.stopPropagation();
  362. //--------------------------------------------------------------------------
  363. //
  364. // Private Methods
  365. //
  366. //--------------------------------------------------------------------------
  367. this.updateParentIsExpanded = (el, expanded) => {
  368. const items = getSlotted(el, SLOTS.children, {
  369. all: true,
  370. selector: "calcite-tree-item"
  371. });
  372. items.forEach((item) => (item.parentExpanded = expanded));
  373. };
  374. this.updateAncestorTree = () => {
  375. var _a;
  376. if (this.selected && this.selectionMode === TreeSelectionMode.Ancestors) {
  377. const ancestors = [];
  378. let parent = this.parentTreeItem;
  379. while (parent) {
  380. ancestors.push(parent);
  381. parent = (_a = parent.parentElement) === null || _a === void 0 ? void 0 : _a.closest("calcite-tree-item");
  382. }
  383. ancestors.forEach((item) => (item.indeterminate = true));
  384. return;
  385. }
  386. };
  387. }
  388. expandedHandler(newValue) {
  389. this.updateParentIsExpanded(this.el, newValue);
  390. }
  391. getselectionMode() {
  392. this.isSelectionMultiLike =
  393. this.selectionMode === TreeSelectionMode.Multi ||
  394. this.selectionMode === TreeSelectionMode.MultiChildren;
  395. }
  396. //--------------------------------------------------------------------------
  397. //
  398. // Lifecycle
  399. //
  400. //--------------------------------------------------------------------------
  401. connectedCallback() {
  402. var _a;
  403. this.parentTreeItem = (_a = this.el.parentElement) === null || _a === void 0 ? void 0 : _a.closest("calcite-tree-item");
  404. if (this.parentTreeItem) {
  405. const { expanded } = this.parentTreeItem;
  406. this.updateParentIsExpanded(this.parentTreeItem, expanded);
  407. }
  408. connectConditionalSlotComponent(this);
  409. }
  410. disconnectedCallback() {
  411. disconnectConditionalSlotComponent(this);
  412. }
  413. componentWillRender() {
  414. var _a;
  415. this.hasChildren = !!this.el.querySelector("calcite-tree");
  416. this.depth = 0;
  417. let parentTree = this.el.closest("calcite-tree");
  418. if (!parentTree) {
  419. return;
  420. }
  421. this.selectionMode = parentTree.selectionMode;
  422. this.scale = parentTree.scale || "m";
  423. this.lines = parentTree.lines;
  424. let nextParentTree;
  425. while (parentTree) {
  426. nextParentTree = (_a = parentTree.parentElement) === null || _a === void 0 ? void 0 : _a.closest("calcite-tree");
  427. if (nextParentTree === parentTree) {
  428. break;
  429. }
  430. else {
  431. parentTree = nextParentTree;
  432. this.depth = this.depth + 1;
  433. }
  434. }
  435. }
  436. componentDidLoad() {
  437. this.updateAncestorTree();
  438. }
  439. componentDidRender() {
  440. updateHostInteraction(this, () => this.parentExpanded || this.depth === 1);
  441. }
  442. render() {
  443. const rtl = getElementDir(this.el) === "rtl";
  444. const showBulletPoint = this.selectionMode === TreeSelectionMode.Single ||
  445. this.selectionMode === TreeSelectionMode.Children;
  446. const showCheckmark = this.selectionMode === TreeSelectionMode.Multi ||
  447. this.selectionMode === TreeSelectionMode.MultiChildren;
  448. const showBlank = this.selectionMode === TreeSelectionMode.None && !this.hasChildren;
  449. const chevron = this.hasChildren ? (h("calcite-icon", { class: {
  450. [CSS.chevron]: true,
  451. [CSS_UTILITY.rtl]: rtl
  452. }, "data-test-id": "icon", icon: ICONS.chevronRight, onClick: this.iconClickHandler, scale: "s" })) : null;
  453. const defaultSlotNode = h("slot", { key: "default-slot" });
  454. const checkbox = this.selectionMode === TreeSelectionMode.Ancestors ? (h("label", { class: CSS.checkboxLabel, key: "checkbox-label" }, h("calcite-checkbox", { checked: this.selected, class: CSS.checkbox, "data-test-id": "checkbox", indeterminate: this.hasChildren && this.indeterminate, scale: this.scale, tabIndex: -1 }), defaultSlotNode)) : null;
  455. const selectedIcon = showBulletPoint
  456. ? ICONS.bulletPoint
  457. : showCheckmark
  458. ? ICONS.checkmark
  459. : showBlank
  460. ? ICONS.blank
  461. : null;
  462. const itemIndicator = selectedIcon ? (h("calcite-icon", { class: {
  463. [CSS.bulletPointIcon]: selectedIcon === ICONS.bulletPoint,
  464. [CSS.checkmarkIcon]: selectedIcon === ICONS.checkmark,
  465. [CSS_UTILITY.rtl]: rtl
  466. }, icon: selectedIcon, scale: "s" })) : null;
  467. const hidden = !(this.parentExpanded || this.depth === 1);
  468. return (h(Host, { "aria-expanded": this.hasChildren ? toAriaBoolean(this.expanded) : undefined, "aria-hidden": toAriaBoolean(hidden), "aria-selected": this.selected ? "true" : showCheckmark ? "false" : undefined, "calcite-hydrated-hidden": hidden, role: "treeitem" }, h("div", { class: {
  469. [CSS.nodeContainer]: true,
  470. [CSS_UTILITY.rtl]: rtl
  471. }, "data-selection-mode": this.selectionMode, ref: (el) => (this.defaultSlotWrapper = el) }, chevron, itemIndicator, checkbox ? checkbox : defaultSlotNode), h("div", { class: {
  472. [CSS.childrenContainer]: true,
  473. [CSS_UTILITY.rtl]: rtl
  474. }, "data-test-id": "calcite-tree-children", onClick: this.childrenClickHandler, ref: (el) => (this.childrenSlotWrapper = el), role: this.hasChildren ? "group" : undefined }, h("slot", { name: SLOTS.children }))));
  475. }
  476. //--------------------------------------------------------------------------
  477. //
  478. // Event Listeners
  479. //
  480. //--------------------------------------------------------------------------
  481. onClick(event) {
  482. // Solve for if the item is clicked somewhere outside the slotted anchor.
  483. // Anchor is triggered anywhere you click
  484. const [link] = filterDirectChildren(this.el, "a");
  485. if (link && event.composedPath()[0].tagName.toLowerCase() !== "a") {
  486. const target = link.target === "" ? "_self" : link.target;
  487. window.open(link.href, target);
  488. }
  489. this.calciteInternalTreeItemSelect.emit({
  490. modifyCurrentSelection: this.selectionMode === TreeSelectionMode.Ancestors || this.isSelectionMultiLike,
  491. forceToggle: false
  492. });
  493. }
  494. keyDownHandler(event) {
  495. let root;
  496. switch (event.key) {
  497. case " ":
  498. this.calciteInternalTreeItemSelect.emit({
  499. modifyCurrentSelection: this.isSelectionMultiLike,
  500. forceToggle: false
  501. });
  502. event.preventDefault();
  503. break;
  504. case "Enter":
  505. // activates a node, i.e., performs its default action. For parent nodes, one possible default action is to open or close the node. In single-select trees where selection does not follow focus (see note below), the default action is typically to select the focused node.
  506. const link = nodeListToArray(this.el.children).find((el) => el.matches("a"));
  507. if (link) {
  508. link.click();
  509. this.selected = true;
  510. }
  511. else {
  512. this.calciteInternalTreeItemSelect.emit({
  513. modifyCurrentSelection: this.isSelectionMultiLike,
  514. forceToggle: false
  515. });
  516. }
  517. event.preventDefault();
  518. break;
  519. case "Home":
  520. root = this.el.closest("calcite-tree:not([child])");
  521. const firstNode = root.querySelector("calcite-tree-item");
  522. firstNode === null || firstNode === void 0 ? void 0 : firstNode.focus();
  523. break;
  524. case "End":
  525. root = this.el.closest("calcite-tree:not([child])");
  526. let currentNode = root.children[root.children.length - 1]; // last child
  527. let currentTree = nodeListToArray(currentNode.children).find((el) => el.matches("calcite-tree"));
  528. while (currentTree) {
  529. currentNode = currentTree.children[root.children.length - 1];
  530. currentTree = nodeListToArray(currentNode.children).find((el) => el.matches("calcite-tree"));
  531. }
  532. currentNode === null || currentNode === void 0 ? void 0 : currentNode.focus();
  533. break;
  534. }
  535. }
  536. get el() { return getElement(this); }
  537. static get watchers() { return {
  538. "expanded": ["expandedHandler"],
  539. "selectionMode": ["getselectionMode"]
  540. }; }
  541. };
  542. TreeItem.style = treeItemCss;
  543. export { Tree as calcite_tree, TreeItem as calcite_tree_item };