calcite-tree_2.cjs.entry.js 29 KB

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