calcite-tree_2.entry.js 30 KB

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