calcite-tree-item.js 19 KB

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