calcite-value-list.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 } from '@stencil/core/internal/client';
  7. import { S as Sortable } from './sortable.esm.js';
  8. import { m as mutationObserverCallback, d as deselectRemovedItems, a as deselectSiblingItems, s as selectSiblings, h as handleFilter, g as getItemData, k as keyDownHandler, o as moveItemIndex, i as initialize, b as initializeObserver, c as cleanUpObserver, j as calciteListFocusOutHandler, r as removeItem, e as calciteListItemChangeHandler, f as calciteListItemValueChangeHandler, 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. const valueListCss = "@-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{position:relative;-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;-ms-flex-negative:0;flex-shrink:0;-ms-flex-positive:0;flex-grow:0;-ms-flex-direction:column;flex-direction:column;-ms-flex-align:stretch;align-items:stretch;background-color:transparent;font-size:var(--calcite-font-size--1);color:var(--calcite-ui-text-2)}:host *{-webkit-box-sizing:border-box;box-sizing:border-box}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}: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;-webkit-box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);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-bottom:0.25rem;display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center;-ms-flex-pack:end;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);-webkit-box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);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:-webkit-sticky;position:sticky;top:0px;z-index:10}calcite-filter{margin-bottom:1px}";
  26. const ValueList = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  27. constructor() {
  28. super();
  29. this.__registerHost();
  30. this.__attachShadow();
  31. this.calciteListChange = createEvent(this, "calciteListChange", 7);
  32. this.calciteListOrderChange = createEvent(this, "calciteListOrderChange", 7);
  33. // --------------------------------------------------------------------------
  34. //
  35. // Properties
  36. //
  37. // --------------------------------------------------------------------------
  38. /**
  39. * When true, prevents user interaction. This state shows list items grayed out and with lower opacity.
  40. */
  41. this.disabled = false;
  42. /**
  43. * When true, list items are sortable via a draggable button.
  44. */
  45. this.dragEnabled = false;
  46. /**
  47. * When true, an input appears at the top of the list that can be used by end users to filter list items.
  48. */
  49. this.filterEnabled = false;
  50. /**
  51. * When true, content is waiting to be loaded. This state shows a busy indicator.
  52. */
  53. this.loading = false;
  54. /**
  55. * Similar to standard radio buttons and checkboxes.
  56. * When true, a user can select multiple list items at a time.
  57. * When false, only a single list item can be selected at a time,
  58. * and selecting a new list item will deselect any other selected list items.
  59. */
  60. this.multiple = false;
  61. /**
  62. * When true and single-selection is enabled, the selection changes when navigating list items via the keyboard.
  63. */
  64. this.selectionFollowsFocus = false;
  65. // --------------------------------------------------------------------------
  66. //
  67. // Private Properties
  68. //
  69. // --------------------------------------------------------------------------
  70. this.selectedValues = new Map();
  71. this.dataForFilter = [];
  72. this.lastSelectedItem = null;
  73. this.mutationObserver = createObserver("mutation", mutationObserverCallback.bind(this));
  74. this.setFilterEl = (el) => {
  75. this.filterEl = el;
  76. };
  77. this.deselectRemovedItems = deselectRemovedItems.bind(this);
  78. this.deselectSiblingItems = deselectSiblingItems.bind(this);
  79. this.selectSiblings = selectSiblings.bind(this);
  80. this.handleFilter = handleFilter.bind(this);
  81. this.getItemData = getItemData.bind(this);
  82. this.keyDownHandler = (event) => {
  83. const handleElement = event
  84. .composedPath()
  85. .find((item) => { var _a; return ((_a = item.dataset) === null || _a === void 0 ? void 0 : _a.jsHandle) !== undefined; });
  86. const item = event
  87. .composedPath()
  88. .find((item) => { var _a; return ((_a = item.tagName) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === "calcite-value-list-item"; });
  89. // Only trigger keyboard sorting when the internal drag handle is focused and activated
  90. if (!handleElement || !item.handleActivated) {
  91. keyDownHandler.call(this, event);
  92. return;
  93. }
  94. const { items } = this;
  95. if ((event.key !== "ArrowUp" && event.key !== "ArrowDown") || items.length <= 1) {
  96. return;
  97. }
  98. event.preventDefault();
  99. const { el } = this;
  100. const nextIndex = moveItemIndex(this, item, event.key === "ArrowUp" ? "up" : "down");
  101. if (nextIndex === items.length - 1) {
  102. el.appendChild(item);
  103. }
  104. else {
  105. const itemAtNextIndex = el.children[nextIndex];
  106. const insertionReferenceItem = itemAtNextIndex === item.nextElementSibling
  107. ? itemAtNextIndex.nextElementSibling
  108. : itemAtNextIndex;
  109. el.insertBefore(item, insertionReferenceItem);
  110. }
  111. this.items = this.getItems();
  112. this.calciteListOrderChange.emit(this.items.map(({ value }) => value));
  113. requestAnimationFrame(() => handleElement.focus());
  114. item.handleActivated = true;
  115. };
  116. }
  117. // --------------------------------------------------------------------------
  118. //
  119. // Lifecycle
  120. //
  121. // --------------------------------------------------------------------------
  122. connectedCallback() {
  123. initialize.call(this);
  124. initializeObserver.call(this);
  125. }
  126. componentDidLoad() {
  127. this.setUpDragAndDrop();
  128. }
  129. componentDidRender() {
  130. updateHostInteraction(this);
  131. }
  132. disconnectedCallback() {
  133. cleanUpObserver.call(this);
  134. this.cleanUpDragAndDrop();
  135. }
  136. calciteListFocusOutHandler(event) {
  137. calciteListFocusOutHandler.call(this, event);
  138. }
  139. calciteListItemRemoveHandler(event) {
  140. removeItem.call(this, event);
  141. }
  142. calciteListItemChangeHandler(event) {
  143. calciteListItemChangeHandler.call(this, event);
  144. }
  145. calciteListItemPropsChangeHandler(event) {
  146. event.stopPropagation();
  147. this.setUpFilter();
  148. }
  149. calciteListItemValueChangeHandler(event) {
  150. calciteListItemValueChangeHandler.call(this, event);
  151. }
  152. // --------------------------------------------------------------------------
  153. //
  154. // Private Methods
  155. //
  156. // --------------------------------------------------------------------------
  157. getItems() {
  158. return Array.from(this.el.querySelectorAll("calcite-value-list-item"));
  159. }
  160. setUpItems() {
  161. setUpItems.call(this, "calcite-value-list-item");
  162. }
  163. setUpFilter() {
  164. if (this.filterEnabled) {
  165. this.dataForFilter = this.getItemData();
  166. }
  167. }
  168. setUpDragAndDrop() {
  169. this.cleanUpDragAndDrop();
  170. if (!this.dragEnabled) {
  171. return;
  172. }
  173. this.sortable = Sortable.create(this.el, {
  174. dataIdAttr: "id",
  175. handle: `.${CSS.handle}`,
  176. draggable: "calcite-value-list-item",
  177. group: this.group,
  178. onSort: () => {
  179. this.items = Array.from(this.el.querySelectorAll("calcite-value-list-item"));
  180. const values = this.items.map((item) => item.value);
  181. this.calciteListOrderChange.emit(values);
  182. }
  183. });
  184. }
  185. cleanUpDragAndDrop() {
  186. var _a;
  187. (_a = this.sortable) === null || _a === void 0 ? void 0 : _a.destroy();
  188. this.sortable = null;
  189. }
  190. // --------------------------------------------------------------------------
  191. //
  192. // Public Methods
  193. //
  194. // --------------------------------------------------------------------------
  195. /** Returns the currently selected items */
  196. async getSelectedItems() {
  197. return this.selectedValues;
  198. }
  199. /** Sets focus on the component. */
  200. async setFocus(focusId) {
  201. return setFocus.call(this, focusId);
  202. }
  203. // --------------------------------------------------------------------------
  204. //
  205. // Render Methods
  206. //
  207. // --------------------------------------------------------------------------
  208. getIconType() {
  209. let type = null;
  210. if (this.dragEnabled) {
  211. type = ICON_TYPES.grip;
  212. }
  213. return type;
  214. }
  215. render() {
  216. return h(List, { onKeyDown: this.keyDownHandler, props: this });
  217. }
  218. get el() { return this; }
  219. static get style() { return valueListCss; }
  220. }, [1, "calcite-value-list", {
  221. "disabled": [516],
  222. "dragEnabled": [516, "drag-enabled"],
  223. "filterEnabled": [516, "filter-enabled"],
  224. "filterPlaceholder": [513, "filter-placeholder"],
  225. "group": [1],
  226. "loading": [516],
  227. "multiple": [516],
  228. "selectionFollowsFocus": [4, "selection-follows-focus"],
  229. "selectedValues": [32],
  230. "dataForFilter": [32],
  231. "getSelectedItems": [64],
  232. "setFocus": [64]
  233. }, [[0, "focusout", "calciteListFocusOutHandler"], [0, "calciteListItemRemove", "calciteListItemRemoveHandler"], [0, "calciteListItemChange", "calciteListItemChangeHandler"], [0, "calciteListItemPropsChange", "calciteListItemPropsChangeHandler"], [0, "calciteListItemValueChange", "calciteListItemValueChangeHandler"]]]);
  234. function defineCustomElement$1() {
  235. if (typeof customElements === "undefined") {
  236. return;
  237. }
  238. const components = ["calcite-value-list", "calcite-filter", "calcite-icon", "calcite-input", "calcite-loader", "calcite-progress", "calcite-scrim"];
  239. components.forEach(tagName => { switch (tagName) {
  240. case "calcite-value-list":
  241. if (!customElements.get(tagName)) {
  242. customElements.define(tagName, ValueList);
  243. }
  244. break;
  245. case "calcite-filter":
  246. if (!customElements.get(tagName)) {
  247. defineCustomElement$7();
  248. }
  249. break;
  250. case "calcite-icon":
  251. if (!customElements.get(tagName)) {
  252. defineCustomElement$6();
  253. }
  254. break;
  255. case "calcite-input":
  256. if (!customElements.get(tagName)) {
  257. defineCustomElement$5();
  258. }
  259. break;
  260. case "calcite-loader":
  261. if (!customElements.get(tagName)) {
  262. defineCustomElement$4();
  263. }
  264. break;
  265. case "calcite-progress":
  266. if (!customElements.get(tagName)) {
  267. defineCustomElement$3();
  268. }
  269. break;
  270. case "calcite-scrim":
  271. if (!customElements.get(tagName)) {
  272. defineCustomElement$2();
  273. }
  274. break;
  275. } });
  276. }
  277. defineCustomElement$1();
  278. const CalciteValueList = ValueList;
  279. const defineCustomElement = defineCustomElement$1;
  280. export { CalciteValueList, defineCustomElement };