resources-3e3255c7.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 { f as focusElement, g as getSlotted, t as toAriaBoolean } from './dom-8f0a9ff2.js';
  7. import { g as getRoundRobinIndex } from './array-03a17827.js';
  8. import { d as debounce } from './debounce-d85a6654.js';
  9. import { h, H as Host } from './index-1f9b54dc.js';
  10. const CSS$2 = {
  11. heading: "heading",
  12. container: "container",
  13. indented: "container--indented"
  14. };
  15. const SLOTS$2 = {
  16. parentItem: "parent-item"
  17. };
  18. const HEADING_LEVEL = 3;
  19. function mutationObserverCallback() {
  20. this.setUpItems();
  21. this.setUpFilter();
  22. this.deselectRemovedItems();
  23. }
  24. const SUPPORTED_ARROW_KEYS = ["ArrowUp", "ArrowDown"];
  25. // --------------------------------------------------------------------------
  26. //
  27. // Lifecycle
  28. //
  29. // --------------------------------------------------------------------------
  30. function initialize() {
  31. this.setUpItems();
  32. this.setUpFilter();
  33. this.emitCalciteListChange = debounce(internalCalciteListChangeEvent.bind(this), 0);
  34. }
  35. function initializeObserver() {
  36. var _a;
  37. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true, subtree: true });
  38. }
  39. function cleanUpObserver() {
  40. var _a;
  41. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  42. }
  43. // --------------------------------------------------------------------------
  44. //
  45. // Listeners
  46. //
  47. // --------------------------------------------------------------------------
  48. function calciteListItemChangeHandler(event) {
  49. const { selectedValues } = this;
  50. const { item, value, selected, shiftPressed } = event.detail;
  51. if (selected) {
  52. if (this.multiple && shiftPressed) {
  53. this.selectSiblings(item);
  54. }
  55. if (!this.multiple) {
  56. this.deselectSiblingItems(item);
  57. }
  58. selectedValues.set(value, item);
  59. }
  60. else {
  61. selectedValues.delete(value);
  62. if (this.multiple && shiftPressed) {
  63. this.selectSiblings(item, true);
  64. }
  65. }
  66. if (!this.multiple) {
  67. toggleSingleSelectItemTabbing(item, selected);
  68. if (selected) {
  69. focusElement(item);
  70. }
  71. }
  72. this.lastSelectedItem = item;
  73. this.emitCalciteListChange();
  74. }
  75. function calciteInternalListItemValueChangeHandler(event) {
  76. const oldValue = event.detail.oldValue;
  77. const selectedValues = this.selectedValues;
  78. if (selectedValues.has(oldValue)) {
  79. const item = selectedValues.get(oldValue);
  80. selectedValues.delete(oldValue);
  81. selectedValues.set(event.detail.newValue, item);
  82. }
  83. event.stopPropagation();
  84. }
  85. // --------------------------------------------------------------------------
  86. //
  87. // Private Methods
  88. //
  89. // --------------------------------------------------------------------------
  90. function isValidNavigationKey(key) {
  91. return !!SUPPORTED_ARROW_KEYS.find((k) => k === key);
  92. }
  93. function calciteListFocusOutHandler(event) {
  94. const { el, items, multiple, selectedValues } = this;
  95. if (multiple) {
  96. return;
  97. }
  98. const focusedInside = el.contains(event.relatedTarget);
  99. if (focusedInside) {
  100. return;
  101. }
  102. filterOutDisabled(items).forEach((item) => {
  103. toggleSingleSelectItemTabbing(item, selectedValues.size === 0 ? item.contains(event.target) || event.target === item : item.selected);
  104. });
  105. }
  106. function keyDownHandler(event) {
  107. const { key, target } = event;
  108. if (!isValidNavigationKey(key)) {
  109. return;
  110. }
  111. const { items, multiple, selectionFollowsFocus } = this;
  112. const { length: totalItems } = items;
  113. const currentIndex = items.indexOf(target);
  114. if (!totalItems || currentIndex === -1) {
  115. return;
  116. }
  117. event.preventDefault();
  118. const index = moveItemIndex(this, target, key === "ArrowUp" ? "up" : "down");
  119. const item = items[index];
  120. items.forEach((i) => toggleSingleSelectItemTabbing(i, i === item));
  121. if (!multiple && selectionFollowsFocus) {
  122. item.selected = true;
  123. }
  124. focusElement(item);
  125. }
  126. function moveItemIndex(list, item, direction) {
  127. const { items } = list;
  128. const { length: totalItems } = items;
  129. const currentIndex = items.indexOf(item);
  130. const directionFactor = direction === "up" ? -1 : 1;
  131. let moveOffset = 1;
  132. let index = getRoundRobinIndex(currentIndex + directionFactor * moveOffset++, totalItems);
  133. const firstMovedIndex = index;
  134. while (items[index].disabled) {
  135. index = getRoundRobinIndex(currentIndex + directionFactor * moveOffset++, totalItems);
  136. if (index === firstMovedIndex) {
  137. break;
  138. }
  139. }
  140. return index;
  141. }
  142. function getItemIndex(list, item) {
  143. const { items } = list;
  144. return items.indexOf(item);
  145. }
  146. function filterOutDisabled(items) {
  147. return items.filter((item) => !item.disabled);
  148. }
  149. function internalCalciteListChangeEvent() {
  150. this.calciteListChange.emit(this.selectedValues);
  151. }
  152. function removeItem(event) {
  153. if (event.defaultPrevented) {
  154. return;
  155. }
  156. const item = event.target;
  157. const selectedValues = this.selectedValues;
  158. if (item.parentElement.tagName === "CALCITE-PICK-LIST-GROUP" && item.slot === SLOTS$2.parentItem) {
  159. item.parentElement.remove();
  160. Array.from(item.parentElement.children).forEach((item) => selectedValues.delete(item.value));
  161. }
  162. else {
  163. item.remove();
  164. selectedValues.delete(item.value);
  165. }
  166. this.emitCalciteListChange();
  167. }
  168. function toggleSingleSelectItemTabbing(item, selectable) {
  169. if (item.disabled) {
  170. return;
  171. }
  172. // using attribute intentionally
  173. if (selectable) {
  174. item.removeAttribute("tabindex");
  175. }
  176. else {
  177. item.setAttribute("tabindex", "-1");
  178. }
  179. }
  180. async function setFocus(focusId) {
  181. var _a;
  182. if (this.filterEnabled && focusId === "filter") {
  183. await focusElement(this.filterEl);
  184. return;
  185. }
  186. const { items, multiple, selectionFollowsFocus } = this;
  187. if (items.length === 0) {
  188. return;
  189. }
  190. if (multiple) {
  191. return (_a = filterOutDisabled(items)[0]) === null || _a === void 0 ? void 0 : _a.setFocus();
  192. }
  193. const filtered = filterOutDisabled(items);
  194. const focusTarget = filtered.find((item) => item.selected) || filtered[0];
  195. if (selectionFollowsFocus && focusTarget) {
  196. focusTarget.selected = true;
  197. }
  198. return focusTarget.setFocus();
  199. }
  200. function setUpItems(tagName) {
  201. this.items = Array.from(this.el.querySelectorAll(tagName));
  202. let hasSelected = false;
  203. const { items } = this;
  204. items.forEach((item) => {
  205. item.icon = this.getIconType();
  206. if (!this.multiple) {
  207. item.disableDeselect = true;
  208. toggleSingleSelectItemTabbing(item, false);
  209. }
  210. if (item.selected) {
  211. hasSelected = true;
  212. toggleSingleSelectItemTabbing(item, true);
  213. this.selectedValues.set(item.value, item);
  214. }
  215. });
  216. const [first] = items;
  217. if (!hasSelected && first && !first.disabled) {
  218. toggleSingleSelectItemTabbing(first, true);
  219. }
  220. }
  221. function deselectRemovedItems() {
  222. const selectedValues = this.selectedValues;
  223. const itemValues = this.items.map(({ value }) => value);
  224. selectedValues.forEach((selectedItem) => {
  225. if (!itemValues.includes(selectedItem.value)) {
  226. this.selectedValues.delete(selectedItem.value);
  227. }
  228. });
  229. }
  230. function deselectSiblingItems(item) {
  231. this.items.forEach((currentItem) => {
  232. if (currentItem.value !== item.value) {
  233. currentItem.toggleSelected(false);
  234. if (this.selectedValues.has(currentItem.value)) {
  235. this.selectedValues.delete(currentItem.value);
  236. }
  237. }
  238. });
  239. }
  240. function selectSiblings(item, deselect = false) {
  241. if (!this.lastSelectedItem) {
  242. return;
  243. }
  244. const { items } = this;
  245. const start = items.findIndex((currentItem) => {
  246. return currentItem.value === this.lastSelectedItem.value;
  247. });
  248. const end = items.findIndex((currentItem) => {
  249. return currentItem.value === item.value;
  250. });
  251. items.slice(Math.min(start, end), Math.max(start, end)).forEach((currentItem) => {
  252. currentItem.toggleSelected(!deselect);
  253. if (!deselect) {
  254. this.selectedValues.set(currentItem.value, currentItem);
  255. }
  256. else {
  257. this.selectedValues.delete(currentItem.value);
  258. }
  259. });
  260. }
  261. let groups;
  262. function handleFilter(event) {
  263. const { filteredItems } = event.currentTarget;
  264. const values = filteredItems.map((item) => item.value);
  265. let hasSelectedMatch = false;
  266. if (!groups) {
  267. groups = new Set();
  268. }
  269. const matchedItems = this.items.filter((item) => {
  270. const parent = item.parentElement;
  271. const grouped = parent.matches("calcite-pick-list-group");
  272. if (grouped) {
  273. groups.add(parent);
  274. }
  275. const matches = values.includes(item.value);
  276. item.hidden = !matches;
  277. if (!hasSelectedMatch) {
  278. hasSelectedMatch = matches && item.selected;
  279. }
  280. return matches;
  281. });
  282. groups.forEach((group) => {
  283. const hasAtLeastOneMatch = matchedItems.some((item) => group.contains(item));
  284. group.hidden = !hasAtLeastOneMatch;
  285. if (!hasAtLeastOneMatch) {
  286. return;
  287. }
  288. const parentItem = getSlotted(group, "parent-item");
  289. if (parentItem) {
  290. parentItem.hidden = false;
  291. if (matchedItems.includes(parentItem)) {
  292. Array.from(group.children).forEach((child) => (child.hidden = false));
  293. }
  294. }
  295. });
  296. groups.clear();
  297. if (matchedItems.length > 0 && !hasSelectedMatch && !this.multiple) {
  298. toggleSingleSelectItemTabbing(matchedItems[0], true);
  299. }
  300. }
  301. function getItemData() {
  302. return this.items.map((item) => ({
  303. label: item.label,
  304. description: item.description,
  305. metadata: item.metadata,
  306. value: item.value
  307. }));
  308. }
  309. const CSS$1 = {
  310. sticky: "sticky-pos"
  311. };
  312. var ICON_TYPES;
  313. (function (ICON_TYPES) {
  314. ICON_TYPES["circle"] = "circle";
  315. ICON_TYPES["square"] = "square";
  316. ICON_TYPES["grip"] = "grip";
  317. })(ICON_TYPES || (ICON_TYPES = {}));
  318. const SLOTS$1 = {
  319. menuActions: "menu-actions"
  320. };
  321. const List = ({ props: { disabled, loading, filterEnabled, dataForFilter, handleFilter, filterPlaceholder, setFilterEl, dragEnabled, storeAssistiveEl }, ...rest }) => {
  322. const defaultSlot = h("slot", null);
  323. return (h(Host, { "aria-busy": toAriaBoolean(loading), role: "menu", ...rest },
  324. h("section", null,
  325. dragEnabled ? (h("span", { "aria-live": "assertive", class: "assistive-text", ref: storeAssistiveEl })) : null,
  326. h("header", { class: { [CSS$1.sticky]: true } },
  327. filterEnabled ? (h("calcite-filter", { "aria-label": filterPlaceholder, disabled: loading || disabled, items: dataForFilter, onCalciteFilterChange: handleFilter, placeholder: filterPlaceholder, ref: setFilterEl })) : null,
  328. h("slot", { name: SLOTS$1.menuActions })),
  329. loading ? h("calcite-scrim", { loading: loading }) : null,
  330. defaultSlot)));
  331. };
  332. const CSS = {
  333. actions: "actions",
  334. actionsEnd: "actions--end",
  335. actionsStart: "actions--start",
  336. description: "description",
  337. handle: "handle",
  338. handleActivated: "handle--activated",
  339. highlight: "highlight",
  340. icon: "icon",
  341. iconDot: "icon-dot",
  342. label: "label",
  343. remove: "remove",
  344. title: "title",
  345. textContainer: "text-container"
  346. };
  347. const ICONS = {
  348. checked: "check",
  349. remove: "x"
  350. };
  351. const SLOTS = {
  352. actionsEnd: "actions-end",
  353. actionsStart: "actions-start"
  354. };
  355. const TEXT = {
  356. remove: "Remove"
  357. };
  358. export { CSS as C, HEADING_LEVEL as H, ICON_TYPES as I, List as L, SLOTS as S, TEXT as T, deselectSiblingItems as a, getItemData as b, moveItemIndex as c, deselectRemovedItems as d, initializeObserver as e, cleanUpObserver as f, getItemIndex as g, handleFilter as h, initialize as i, calciteListFocusOutHandler as j, keyDownHandler as k, calciteListItemChangeHandler as l, mutationObserverCallback as m, calciteInternalListItemValueChangeHandler as n, setUpItems as o, setFocus as p, SLOTS$2 as q, removeItem as r, selectSiblings as s, CSS$2 as t, ICONS as u };