calcite-tree_2.cjs.entry.js 30 KB

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