calcite-value-list.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 } from '@stencil/core/internal/client/index.js';
  7. import { S as Sortable } from './sortable.esm.js';
  8. import { o as getItemIndex, m as mutationObserverCallback, d as deselectRemovedItems, a as deselectSiblingItems, s as selectSiblings, h as handleFilter, g as getItemData, k as keyDownHandler, p as moveItemIndex, i as initialize, b as initializeObserver, c as cleanUpObserver, j as calciteListFocusOutHandler, r as removeItem, e as calciteListItemChangeHandler, f as calciteInternalListItemValueChangeHandler, l as setUpItems, n as setFocus, L as List } from './shared-list-render.js';
  9. import { c as createObserver } from './observers.js';
  10. import { u as updateHostInteraction } from './interactive.js';
  11. import { d as defineCustomElement$7 } from './filter.js';
  12. import { d as defineCustomElement$6 } from './icon.js';
  13. import { d as defineCustomElement$5 } from './input.js';
  14. import { d as defineCustomElement$4 } from './loader.js';
  15. import { d as defineCustomElement$3 } from './progress.js';
  16. import { d as defineCustomElement$2 } from './scrim.js';
  17. const CSS = {
  18. container: "container",
  19. handle: "handle"
  20. };
  21. var ICON_TYPES;
  22. (function (ICON_TYPES) {
  23. ICON_TYPES["grip"] = "grip";
  24. })(ICON_TYPES || (ICON_TYPES = {}));
  25. function getScreenReaderText(item, status, valueList) {
  26. const { items, intlDragHandleIdle, intlDragHandleActive, intlDragHandleChange, intlDragHandleCommit } = valueList;
  27. const total = items.length;
  28. const position = getItemIndex(valueList, item) + 1;
  29. if (status === "idle") {
  30. const idleText = intlDragHandleIdle
  31. ? replacePlaceholders(intlDragHandleIdle, item.label, position, total)
  32. : `${item.label}, press space and use arrow keys to reorder content. Current position ${position} of ${total}.`;
  33. return idleText;
  34. }
  35. else if (status === "active") {
  36. const activeText = intlDragHandleActive
  37. ? replacePlaceholders(intlDragHandleActive, item.label, position, total)
  38. : `Reordering ${item.label}, current position ${position} of ${total}.`;
  39. return activeText;
  40. }
  41. else if (status === "change") {
  42. const changeText = intlDragHandleChange
  43. ? replacePlaceholders(intlDragHandleChange, item.label, position, total)
  44. : `${item.label}, new position ${position} of ${total}. Press space to confirm.`;
  45. return changeText;
  46. }
  47. else {
  48. const commitText = intlDragHandleCommit
  49. ? replacePlaceholders(intlDragHandleCommit, item.label, position, total)
  50. : `${item.label}, current position ${position} of ${total}.`;
  51. return commitText;
  52. }
  53. }
  54. function getHandleAndItemElement(event) {
  55. const handle = event
  56. .composedPath()
  57. .find((item) => { var _a; return ((_a = item.dataset) === null || _a === void 0 ? void 0 : _a.jsHandle) !== undefined; });
  58. const item = event
  59. .composedPath()
  60. .find((item) => { var _a; return ((_a = item.tagName) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === "calcite-value-list-item"; });
  61. return { handle, item };
  62. }
  63. function replacePlaceholders(text, label, position, total) {
  64. const replacePosition = text.replace("${position}", position.toString());
  65. const replaceLabel = replacePosition.replace("${item.label}", label);
  66. return replaceLabel.replace("${total}", total.toString());
  67. }
  68. const valueListCss = "@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{position:relative;box-sizing:border-box;display:flex;flex-shrink:0;flex-grow:0;flex-direction:column;align-items:stretch;background-color:transparent;font-size:var(--calcite-font-size--1);color:var(--calcite-ui-text-2)}:host *{box-sizing:border-box}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}calcite-value-list-item:last-of-type{--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow)}:host([filter-enabled]) header{margin-block-end:0.25rem;display:flex;align-items:center;justify-content:flex-end;background-color:var(--calcite-ui-foreground-1);--tw-shadow:0 1px 0 var(--calcite-ui-border-3);--tw-shadow-colored:0 1px 0 var(--tw-shadow-color);box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow)}:host([filter-enabled]) header.sticky-pos{position:sticky;inset-block-start:0px;z-index:300}calcite-filter{margin-block-end:1px}";
  69. const ValueList = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  70. constructor() {
  71. super();
  72. this.__registerHost();
  73. this.__attachShadow();
  74. this.calciteListChange = createEvent(this, "calciteListChange", 6);
  75. this.calciteListOrderChange = createEvent(this, "calciteListOrderChange", 6);
  76. // --------------------------------------------------------------------------
  77. //
  78. // Properties
  79. //
  80. // --------------------------------------------------------------------------
  81. /**
  82. * When `true`, interaction is prevented and the component is displayed with lower opacity.
  83. */
  84. this.disabled = false;
  85. /**
  86. * When `true`, `calcite-value-list-item`s are sortable via a draggable button.
  87. */
  88. this.dragEnabled = false;
  89. /**
  90. * When `true`, an input appears at the top of the component that can be used by end users to filter list items.
  91. */
  92. this.filterEnabled = false;
  93. /**
  94. * When `true`, a busy indicator is displayed.
  95. */
  96. this.loading = false;
  97. /**
  98. * Similar to standard radio buttons and checkboxes.
  99. * When `true`, a user can select multiple `calcite-value-list-item`s at a time.
  100. * When `false`, only a single `calcite-value-list-item` can be selected at a time,
  101. * and a new selection will deselect previous selections.
  102. */
  103. this.multiple = false;
  104. /**
  105. * When `true` and single-selection is enabled, the selection changes when navigating `calcite-value-list-item`s via keyboard.
  106. */
  107. this.selectionFollowsFocus = false;
  108. // --------------------------------------------------------------------------
  109. //
  110. // Private Properties
  111. //
  112. // --------------------------------------------------------------------------
  113. this.selectedValues = new Map();
  114. this.dataForFilter = [];
  115. this.lastSelectedItem = null;
  116. this.mutationObserver = createObserver("mutation", mutationObserverCallback.bind(this));
  117. this.setFilterEl = (el) => {
  118. this.filterEl = el;
  119. };
  120. this.deselectRemovedItems = deselectRemovedItems.bind(this);
  121. this.deselectSiblingItems = deselectSiblingItems.bind(this);
  122. this.selectSiblings = selectSiblings.bind(this);
  123. this.handleFilter = handleFilter.bind(this);
  124. this.getItemData = getItemData.bind(this);
  125. this.keyDownHandler = (event) => {
  126. if (event.defaultPrevented) {
  127. return;
  128. }
  129. const { handle, item } = getHandleAndItemElement(event);
  130. if (handle && !item.handleActivated && event.key === " ") {
  131. this.updateScreenReaderText(getScreenReaderText(item, "commit", this));
  132. }
  133. if (!handle || !item.handleActivated) {
  134. keyDownHandler.call(this, event);
  135. return;
  136. }
  137. const { items } = this;
  138. if (event.key === " ") {
  139. this.updateScreenReaderText(getScreenReaderText(item, "active", this));
  140. }
  141. if ((event.key !== "ArrowUp" && event.key !== "ArrowDown") || items.length <= 1) {
  142. return;
  143. }
  144. event.preventDefault();
  145. const { el } = this;
  146. const nextIndex = moveItemIndex(this, item, event.key === "ArrowUp" ? "up" : "down");
  147. if (nextIndex === items.length - 1) {
  148. el.appendChild(item);
  149. }
  150. else {
  151. const itemAtNextIndex = el.children[nextIndex];
  152. const insertionReferenceItem = itemAtNextIndex === item.nextElementSibling
  153. ? itemAtNextIndex.nextElementSibling
  154. : itemAtNextIndex;
  155. el.insertBefore(item, insertionReferenceItem);
  156. }
  157. this.items = this.getItems();
  158. this.calciteListOrderChange.emit(this.items.map(({ value }) => value));
  159. requestAnimationFrame(() => handle === null || handle === void 0 ? void 0 : handle.focus());
  160. item.handleActivated = true;
  161. this.updateHandleAriaLabel(handle, getScreenReaderText(item, "change", this));
  162. };
  163. this.storeAssistiveEl = (el) => {
  164. this.assistiveTextEl = el;
  165. };
  166. this.handleFocusIn = (event) => {
  167. const { handle, item } = getHandleAndItemElement(event);
  168. if (!(item === null || item === void 0 ? void 0 : item.handleActivated) && item && handle) {
  169. this.updateHandleAriaLabel(handle, getScreenReaderText(item, "idle", this));
  170. }
  171. };
  172. }
  173. // --------------------------------------------------------------------------
  174. //
  175. // Lifecycle
  176. //
  177. // --------------------------------------------------------------------------
  178. connectedCallback() {
  179. initialize.call(this);
  180. initializeObserver.call(this);
  181. }
  182. componentDidLoad() {
  183. this.setUpDragAndDrop();
  184. }
  185. componentDidRender() {
  186. updateHostInteraction(this);
  187. }
  188. disconnectedCallback() {
  189. cleanUpObserver.call(this);
  190. this.cleanUpDragAndDrop();
  191. }
  192. calciteListFocusOutHandler(event) {
  193. calciteListFocusOutHandler.call(this, event);
  194. }
  195. calciteListItemRemoveHandler(event) {
  196. removeItem.call(this, event);
  197. }
  198. calciteListItemChangeHandler(event) {
  199. calciteListItemChangeHandler.call(this, event);
  200. }
  201. calciteInternalListItemPropsChangeHandler(event) {
  202. event.stopPropagation();
  203. this.setUpFilter();
  204. }
  205. calciteInternalListItemValueChangeHandler(event) {
  206. calciteInternalListItemValueChangeHandler.call(this, event);
  207. event.stopPropagation();
  208. }
  209. // --------------------------------------------------------------------------
  210. //
  211. // Private Methods
  212. //
  213. // --------------------------------------------------------------------------
  214. getItems() {
  215. return Array.from(this.el.querySelectorAll("calcite-value-list-item"));
  216. }
  217. setUpItems() {
  218. setUpItems.call(this, "calcite-value-list-item");
  219. }
  220. setUpFilter() {
  221. if (this.filterEnabled) {
  222. this.dataForFilter = this.getItemData();
  223. }
  224. }
  225. setUpDragAndDrop() {
  226. this.cleanUpDragAndDrop();
  227. if (!this.dragEnabled) {
  228. return;
  229. }
  230. this.sortable = Sortable.create(this.el, {
  231. dataIdAttr: "id",
  232. handle: `.${CSS.handle}`,
  233. draggable: "calcite-value-list-item",
  234. group: this.group,
  235. onSort: () => {
  236. this.items = Array.from(this.el.querySelectorAll("calcite-value-list-item"));
  237. const values = this.items.map((item) => item.value);
  238. this.calciteListOrderChange.emit(values);
  239. }
  240. });
  241. }
  242. cleanUpDragAndDrop() {
  243. var _a;
  244. (_a = this.sortable) === null || _a === void 0 ? void 0 : _a.destroy();
  245. this.sortable = null;
  246. }
  247. handleBlur() {
  248. if (this.dragEnabled) {
  249. this.updateScreenReaderText("");
  250. }
  251. }
  252. // --------------------------------------------------------------------------
  253. //
  254. // Public Methods
  255. //
  256. // --------------------------------------------------------------------------
  257. /** Returns the currently selected items */
  258. async getSelectedItems() {
  259. return this.selectedValues;
  260. }
  261. /**
  262. * Sets focus on the component.
  263. *
  264. * @param focusId
  265. */
  266. async setFocus(focusId) {
  267. return setFocus.call(this, focusId);
  268. }
  269. // --------------------------------------------------------------------------
  270. //
  271. // Render Methods
  272. //
  273. // --------------------------------------------------------------------------
  274. getIconType() {
  275. let type = null;
  276. if (this.dragEnabled) {
  277. type = ICON_TYPES.grip;
  278. }
  279. return type;
  280. }
  281. updateScreenReaderText(text) {
  282. this.assistiveTextEl.textContent = text;
  283. }
  284. updateHandleAriaLabel(handleElement, text) {
  285. handleElement.ariaLabel = text;
  286. }
  287. render() {
  288. return (h(List, { onBlur: this.handleBlur, onFocusin: this.handleFocusIn, onKeyDown: this.keyDownHandler, props: this }));
  289. }
  290. get el() { return this; }
  291. static get style() { return valueListCss; }
  292. }, [1, "calcite-value-list", {
  293. "disabled": [516],
  294. "dragEnabled": [516, "drag-enabled"],
  295. "filterEnabled": [516, "filter-enabled"],
  296. "filterPlaceholder": [513, "filter-placeholder"],
  297. "group": [513],
  298. "loading": [516],
  299. "multiple": [516],
  300. "selectionFollowsFocus": [516, "selection-follows-focus"],
  301. "intlDragHandleIdle": [1, "intl-drag-handle-idle"],
  302. "intlDragHandleActive": [1, "intl-drag-handle-active"],
  303. "intlDragHandleChange": [1, "intl-drag-handle-change"],
  304. "intlDragHandleCommit": [1, "intl-drag-handle-commit"],
  305. "selectedValues": [32],
  306. "dataForFilter": [32],
  307. "getSelectedItems": [64],
  308. "setFocus": [64]
  309. }, [[0, "focusout", "calciteListFocusOutHandler"], [0, "calciteListItemRemove", "calciteListItemRemoveHandler"], [0, "calciteListItemChange", "calciteListItemChangeHandler"], [0, "calciteInternalListItemPropsChange", "calciteInternalListItemPropsChangeHandler"], [0, "calciteInternalListItemValueChange", "calciteInternalListItemValueChangeHandler"]]]);
  310. function defineCustomElement$1() {
  311. if (typeof customElements === "undefined") {
  312. return;
  313. }
  314. const components = ["calcite-value-list", "calcite-filter", "calcite-icon", "calcite-input", "calcite-loader", "calcite-progress", "calcite-scrim"];
  315. components.forEach(tagName => { switch (tagName) {
  316. case "calcite-value-list":
  317. if (!customElements.get(tagName)) {
  318. customElements.define(tagName, ValueList);
  319. }
  320. break;
  321. case "calcite-filter":
  322. if (!customElements.get(tagName)) {
  323. defineCustomElement$7();
  324. }
  325. break;
  326. case "calcite-icon":
  327. if (!customElements.get(tagName)) {
  328. defineCustomElement$6();
  329. }
  330. break;
  331. case "calcite-input":
  332. if (!customElements.get(tagName)) {
  333. defineCustomElement$5();
  334. }
  335. break;
  336. case "calcite-loader":
  337. if (!customElements.get(tagName)) {
  338. defineCustomElement$4();
  339. }
  340. break;
  341. case "calcite-progress":
  342. if (!customElements.get(tagName)) {
  343. defineCustomElement$3();
  344. }
  345. break;
  346. case "calcite-scrim":
  347. if (!customElements.get(tagName)) {
  348. defineCustomElement$2();
  349. }
  350. break;
  351. } });
  352. }
  353. defineCustomElement$1();
  354. const CalciteValueList = ValueList;
  355. const defineCustomElement = defineCustomElement$1;
  356. export { CalciteValueList, defineCustomElement };