calcite-tree-item.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client/index.js';
  7. import { T as TreeSelectionMode } from './interfaces2.js';
  8. import { b as getSlotted, c as getElementDir, C as CSS_UTILITY, t as toAriaBoolean, m as filterDirectChildren, n as nodeListToArray } from './dom.js';
  9. import { c as connectConditionalSlotComponent, d as disconnectConditionalSlotComponent } from './conditionalSlot.js';
  10. import { u as updateHostInteraction } from './interactive.js';
  11. import { d as defineCustomElement$3 } from './checkbox.js';
  12. import { d as defineCustomElement$2 } from './icon.js';
  13. const CSS = {
  14. checkboxLabel: "checkbox-label",
  15. checkbox: "checkbox",
  16. chevron: "chevron",
  17. nodeContainer: "node-container",
  18. childrenContainer: "children-container",
  19. bulletPointIcon: "bullet-point",
  20. checkmarkIcon: "checkmark"
  21. };
  22. const SLOTS = {
  23. children: "children"
  24. };
  25. const ICONS = {
  26. bulletPoint: "bullet-point",
  27. checkmark: "check",
  28. chevronRight: "chevron-right",
  29. blank: "blank"
  30. };
  31. 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)}";
  32. const TreeItem = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  33. constructor() {
  34. super();
  35. this.__registerHost();
  36. this.__attachShadow();
  37. this.calciteInternalTreeItemSelect = createEvent(this, "calciteInternalTreeItemSelect", 6);
  38. //--------------------------------------------------------------------------
  39. //
  40. // Properties
  41. //
  42. //--------------------------------------------------------------------------
  43. /**
  44. * When `true`, interaction is prevented and the component is displayed with lower opacity.
  45. */
  46. this.disabled = false;
  47. /** When `true`, the component is selected. */
  48. this.selected = false;
  49. /** When `true`, the component is expanded. */
  50. this.expanded = false;
  51. /**
  52. * @internal
  53. */
  54. this.parentExpanded = false;
  55. /**
  56. * @internal
  57. */
  58. this.depth = -1;
  59. /**
  60. * @internal
  61. */
  62. this.hasChildren = null;
  63. this.iconClickHandler = (event) => {
  64. event.stopPropagation();
  65. this.expanded = !this.expanded;
  66. };
  67. this.childrenClickHandler = (event) => event.stopPropagation();
  68. //--------------------------------------------------------------------------
  69. //
  70. // Private Methods
  71. //
  72. //--------------------------------------------------------------------------
  73. this.updateParentIsExpanded = (el, expanded) => {
  74. const items = getSlotted(el, SLOTS.children, {
  75. all: true,
  76. selector: "calcite-tree-item"
  77. });
  78. items.forEach((item) => (item.parentExpanded = expanded));
  79. };
  80. this.updateAncestorTree = () => {
  81. var _a;
  82. if (this.selected && this.selectionMode === TreeSelectionMode.Ancestors) {
  83. const ancestors = [];
  84. let parent = this.parentTreeItem;
  85. while (parent) {
  86. ancestors.push(parent);
  87. parent = (_a = parent.parentElement) === null || _a === void 0 ? void 0 : _a.closest("calcite-tree-item");
  88. }
  89. ancestors.forEach((item) => (item.indeterminate = true));
  90. return;
  91. }
  92. };
  93. }
  94. expandedHandler(newValue) {
  95. this.updateParentIsExpanded(this.el, newValue);
  96. }
  97. getselectionMode() {
  98. this.isSelectionMultiLike =
  99. this.selectionMode === TreeSelectionMode.Multi ||
  100. this.selectionMode === TreeSelectionMode.MultiChildren;
  101. }
  102. //--------------------------------------------------------------------------
  103. //
  104. // Lifecycle
  105. //
  106. //--------------------------------------------------------------------------
  107. connectedCallback() {
  108. var _a;
  109. this.parentTreeItem = (_a = this.el.parentElement) === null || _a === void 0 ? void 0 : _a.closest("calcite-tree-item");
  110. if (this.parentTreeItem) {
  111. const { expanded } = this.parentTreeItem;
  112. this.updateParentIsExpanded(this.parentTreeItem, expanded);
  113. }
  114. connectConditionalSlotComponent(this);
  115. }
  116. disconnectedCallback() {
  117. disconnectConditionalSlotComponent(this);
  118. }
  119. componentWillRender() {
  120. var _a;
  121. this.hasChildren = !!this.el.querySelector("calcite-tree");
  122. this.depth = 0;
  123. let parentTree = this.el.closest("calcite-tree");
  124. if (!parentTree) {
  125. return;
  126. }
  127. this.selectionMode = parentTree.selectionMode;
  128. this.scale = parentTree.scale || "m";
  129. this.lines = parentTree.lines;
  130. let nextParentTree;
  131. while (parentTree) {
  132. nextParentTree = (_a = parentTree.parentElement) === null || _a === void 0 ? void 0 : _a.closest("calcite-tree");
  133. if (nextParentTree === parentTree) {
  134. break;
  135. }
  136. else {
  137. parentTree = nextParentTree;
  138. this.depth = this.depth + 1;
  139. }
  140. }
  141. }
  142. componentDidLoad() {
  143. this.updateAncestorTree();
  144. }
  145. componentDidRender() {
  146. updateHostInteraction(this, () => this.parentExpanded || this.depth === 1);
  147. }
  148. render() {
  149. const rtl = getElementDir(this.el) === "rtl";
  150. const showBulletPoint = this.selectionMode === TreeSelectionMode.Single ||
  151. this.selectionMode === TreeSelectionMode.Children;
  152. const showCheckmark = this.selectionMode === TreeSelectionMode.Multi ||
  153. this.selectionMode === TreeSelectionMode.MultiChildren;
  154. const showBlank = this.selectionMode === TreeSelectionMode.None && !this.hasChildren;
  155. const chevron = this.hasChildren ? (h("calcite-icon", { class: {
  156. [CSS.chevron]: true,
  157. [CSS_UTILITY.rtl]: rtl
  158. }, "data-test-id": "icon", icon: ICONS.chevronRight, onClick: this.iconClickHandler, scale: "s" })) : null;
  159. const defaultSlotNode = h("slot", { key: "default-slot" });
  160. 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;
  161. const selectedIcon = showBulletPoint
  162. ? ICONS.bulletPoint
  163. : showCheckmark
  164. ? ICONS.checkmark
  165. : showBlank
  166. ? ICONS.blank
  167. : null;
  168. const itemIndicator = selectedIcon ? (h("calcite-icon", { class: {
  169. [CSS.bulletPointIcon]: selectedIcon === ICONS.bulletPoint,
  170. [CSS.checkmarkIcon]: selectedIcon === ICONS.checkmark,
  171. [CSS_UTILITY.rtl]: rtl
  172. }, icon: selectedIcon, scale: "s" })) : null;
  173. const hidden = !(this.parentExpanded || this.depth === 1);
  174. return (h(Host, { "aria-expanded": this.hasChildren ? toAriaBoolean(this.expanded) : undefined, "aria-hidden": toAriaBoolean(hidden), "aria-selected": this.selected ? "true" : showCheckmark ? "false" : undefined, "calcite-hydrated-hidden": hidden, role: "treeitem" }, h("div", { class: {
  175. [CSS.nodeContainer]: true,
  176. [CSS_UTILITY.rtl]: rtl
  177. }, "data-selection-mode": this.selectionMode, ref: (el) => (this.defaultSlotWrapper = el) }, chevron, itemIndicator, checkbox ? checkbox : defaultSlotNode), h("div", { class: {
  178. [CSS.childrenContainer]: true,
  179. [CSS_UTILITY.rtl]: rtl
  180. }, "data-test-id": "calcite-tree-children", onClick: this.childrenClickHandler, ref: (el) => (this.childrenSlotWrapper = el), role: this.hasChildren ? "group" : undefined }, h("slot", { name: SLOTS.children }))));
  181. }
  182. //--------------------------------------------------------------------------
  183. //
  184. // Event Listeners
  185. //
  186. //--------------------------------------------------------------------------
  187. onClick(event) {
  188. // Solve for if the item is clicked somewhere outside the slotted anchor.
  189. // Anchor is triggered anywhere you click
  190. const [link] = filterDirectChildren(this.el, "a");
  191. if (link && event.composedPath()[0].tagName.toLowerCase() !== "a") {
  192. const target = link.target === "" ? "_self" : link.target;
  193. window.open(link.href, target);
  194. }
  195. this.calciteInternalTreeItemSelect.emit({
  196. modifyCurrentSelection: this.selectionMode === TreeSelectionMode.Ancestors || this.isSelectionMultiLike,
  197. forceToggle: false
  198. });
  199. }
  200. keyDownHandler(event) {
  201. let root;
  202. switch (event.key) {
  203. case " ":
  204. this.calciteInternalTreeItemSelect.emit({
  205. modifyCurrentSelection: this.isSelectionMultiLike,
  206. forceToggle: false
  207. });
  208. event.preventDefault();
  209. break;
  210. case "Enter":
  211. // 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.
  212. const link = nodeListToArray(this.el.children).find((el) => el.matches("a"));
  213. if (link) {
  214. link.click();
  215. this.selected = true;
  216. }
  217. else {
  218. this.calciteInternalTreeItemSelect.emit({
  219. modifyCurrentSelection: this.isSelectionMultiLike,
  220. forceToggle: false
  221. });
  222. }
  223. event.preventDefault();
  224. break;
  225. case "Home":
  226. root = this.el.closest("calcite-tree:not([child])");
  227. const firstNode = root.querySelector("calcite-tree-item");
  228. firstNode === null || firstNode === void 0 ? void 0 : firstNode.focus();
  229. break;
  230. case "End":
  231. root = this.el.closest("calcite-tree:not([child])");
  232. let currentNode = root.children[root.children.length - 1]; // last child
  233. let currentTree = nodeListToArray(currentNode.children).find((el) => el.matches("calcite-tree"));
  234. while (currentTree) {
  235. currentNode = currentTree.children[root.children.length - 1];
  236. currentTree = nodeListToArray(currentNode.children).find((el) => el.matches("calcite-tree"));
  237. }
  238. currentNode === null || currentNode === void 0 ? void 0 : currentNode.focus();
  239. break;
  240. }
  241. }
  242. get el() { return this; }
  243. static get watchers() { return {
  244. "expanded": ["expandedHandler"],
  245. "selectionMode": ["getselectionMode"]
  246. }; }
  247. static get style() { return treeItemCss; }
  248. }, [1, "calcite-tree-item", {
  249. "disabled": [516],
  250. "selected": [1540],
  251. "expanded": [1540],
  252. "parentExpanded": [4, "parent-expanded"],
  253. "depth": [1538],
  254. "hasChildren": [1540, "has-children"],
  255. "lines": [1540],
  256. "inputEnabled": [4, "input-enabled"],
  257. "scale": [1537],
  258. "indeterminate": [516],
  259. "selectionMode": [1537, "selection-mode"]
  260. }, [[0, "click", "onClick"], [0, "keydown", "keyDownHandler"]]]);
  261. function defineCustomElement$1() {
  262. if (typeof customElements === "undefined") {
  263. return;
  264. }
  265. const components = ["calcite-tree-item", "calcite-checkbox", "calcite-icon"];
  266. components.forEach(tagName => { switch (tagName) {
  267. case "calcite-tree-item":
  268. if (!customElements.get(tagName)) {
  269. customElements.define(tagName, TreeItem);
  270. }
  271. break;
  272. case "calcite-checkbox":
  273. if (!customElements.get(tagName)) {
  274. defineCustomElement$3();
  275. }
  276. break;
  277. case "calcite-icon":
  278. if (!customElements.get(tagName)) {
  279. defineCustomElement$2();
  280. }
  281. break;
  282. } });
  283. }
  284. defineCustomElement$1();
  285. const CalciteTreeItem = TreeItem;
  286. const defineCustomElement = defineCustomElement$1;
  287. export { CalciteTreeItem, defineCustomElement };