@eox/itemfilter 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/README.md +4 -0
  2. package/dist/eox-itemfilter.js +1156 -1276
  3. package/dist/eox-itemfilter.umd.cjs +110 -112
  4. package/package.json +8 -13
  5. package/src/components/chips.js +131 -0
  6. package/src/components/expand-container.js +94 -0
  7. package/src/components/filters/range.js +114 -0
  8. package/src/components/filters/selector.js +245 -0
  9. package/src/components/filters/spatial.js +192 -0
  10. package/src/components/filters/text.js +114 -0
  11. package/src/components/itemfilter-container.js +289 -0
  12. package/src/components/results.js +156 -0
  13. package/src/enums/config.js +104 -0
  14. package/src/enums/index.js +1 -0
  15. package/src/helpers/chip-items.js +19 -0
  16. package/src/helpers/filter-client.js +179 -0
  17. package/src/helpers/filter-external.js +18 -0
  18. package/src/helpers/get-value.js +12 -0
  19. package/src/helpers/highlighting.js +79 -0
  20. package/src/helpers/index.js +11 -0
  21. package/src/helpers/is-filters-dirty.js +13 -0
  22. package/src/helpers/map-layers.js +42 -0
  23. package/src/helpers/reset-filter.js +60 -0
  24. package/src/helpers/spatial.js +35 -0
  25. package/src/helpers/tab-index.js +13 -0
  26. package/src/helpers/toggle-accordion.js +39 -0
  27. package/src/main.js +466 -0
  28. package/src/methods/chips/handler.js +129 -0
  29. package/src/methods/chips/index.js +5 -0
  30. package/src/methods/container/handler.js +70 -0
  31. package/src/methods/container/index.js +10 -0
  32. package/src/methods/container/init-popver.js +34 -0
  33. package/src/methods/container/reset-filter.js +14 -0
  34. package/src/methods/container/reset-search.js +16 -0
  35. package/src/methods/container/search-filter.js +27 -0
  36. package/src/methods/filters/index.js +20 -0
  37. package/src/methods/filters/range.js +78 -0
  38. package/src/methods/filters/selector.js +209 -0
  39. package/src/methods/filters/spatial.js +114 -0
  40. package/src/methods/filters/text.js +32 -0
  41. package/src/methods/itemfilter/create-filter.js +83 -0
  42. package/src/methods/itemfilter/create-reset.js +39 -0
  43. package/src/methods/itemfilter/filter-apply.js +140 -0
  44. package/src/methods/itemfilter/index.js +6 -0
  45. package/src/methods/itemfilter/reset-filter.js +13 -0
  46. package/src/methods/itemfilter/search.js +23 -0
  47. package/src/methods/itemfilter/sort-results.js +14 -0
  48. package/src/methods/results/aggregate-results.js +43 -0
  49. package/src/methods/results/create.js +111 -0
  50. package/src/methods/results/index.js +2 -0
  51. package/src/style.eox.js +457 -0
  52. package/src/style.js +26 -0
  53. package/test/_mockMap.js +0 -20
  54. package/test/cases/filters/filter-keys.js +0 -22
  55. package/test/cases/filters/index.js +0 -3
  56. package/test/cases/general/accordion.js +0 -60
  57. package/test/cases/general/disable-highlight.js +0 -21
  58. package/test/cases/general/external-filter.js +0 -35
  59. package/test/cases/general/highlight-result.js +0 -16
  60. package/test/cases/general/index.js +0 -13
  61. package/test/cases/general/multiple-filters.js +0 -21
  62. package/test/cases/general/nested-property.js +0 -25
  63. package/test/cases/general/search-bar.js +0 -12
  64. package/test/cases/general/search-result.js +0 -14
  65. package/test/cases/general/spatial-filter.js +0 -22
  66. package/test/cases/general/subtitle.js +0 -15
  67. package/test/cases/general/validation.js +0 -22
  68. package/test/cases/state/custom-order.js +0 -24
  69. package/test/cases/state/index.js +0 -6
  70. package/test/cases/state/render-checkbox.js +0 -17
  71. package/test/cases/state/result-pre-checked.js +0 -20
  72. package/test/cases/state/results-with-pre-set-filters.js +0 -30
  73. package/test/filters.cy.js +0 -54
  74. package/test/general.cy.js +0 -110
  75. package/test/state.cy.js +0 -74
  76. package/test/testItems.json +0 -440
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Handles the click event on a chip, highlighting the clicked chip and updating the component.
3
+ *
4
+ * @param {Event} evt - The click event.
5
+ * @param {Object} EOxItemFilterChips - The EOxItemFilterChips component instance.
6
+ */
7
+ export function handleChipClickMethod(evt, EOxItemFilterChips) {
8
+ // Remove the 'highlighted' class from all chips
9
+ EOxItemFilterChips.renderRoot.querySelectorAll(".chip").forEach((chip) => {
10
+ chip.classList.remove("highlighted");
11
+ });
12
+ // Add the 'highlighted' class to the clicked chip
13
+ evt.target.classList.add("highlighted");
14
+ EOxItemFilterChips.requestUpdate();
15
+ }
16
+
17
+ /**
18
+ * Handles the close event on a chip, removing the chip from the controller.
19
+ *
20
+ * @param {Event} event - The close event.
21
+ * @param {number} index - The index of the chip to be removed.
22
+ * @param {Object} EOxItemFilterChips - The EOxItemFilterChips component instance.
23
+ */
24
+ export function handleCloseMethod(event, index, EOxItemFilterChips) {
25
+ // Remove the chip from the controller
26
+ EOxItemFilterChips.controller.remove(event, index);
27
+ }
28
+
29
+ /**
30
+ * Handles keyboard events for the EOxItemFilterChips component.
31
+ *
32
+ * @param {Event} event - The keyboard event.
33
+ * @param {Object} EOxItemFilterChips - The EOxItemFilterChips component instance.
34
+ */
35
+ export function keyboardEventListenerMethod(event, EOxItemFilterChips) {
36
+ const { code, target } = event;
37
+
38
+ // Ignore are events that don't come from seach input
39
+ if (target.id !== "eox-itemfilter-input-search") return;
40
+
41
+ // If the parent element is hidden and the key is one of the specified keys, do nothing
42
+ if (
43
+ EOxItemFilterChips.parentElement.classList.contains("hidden") &&
44
+ ["ArrowLeft", "ArrowRight", "Backspace"].includes(code)
45
+ )
46
+ return;
47
+
48
+ // Prevent default action for the Space key
49
+ if (code === "Space") event.preventDefault();
50
+
51
+ // Stop propagation for keys other than Escape, Space, and Enter
52
+ if (!["Escape", "Space", "Enter"].includes(code)) event.stopPropagation();
53
+
54
+ // Handle specific keyboard keys
55
+ if (["ArrowLeft", "ArrowRight", "Escape", "Backspace"].includes(code))
56
+ handleKeyboard(code, event.target.value ?? "", EOxItemFilterChips);
57
+ }
58
+
59
+ /**
60
+ * Handles specific keyboard keys to navigate and manipulate chips.
61
+ *
62
+ * @param {string} key - The key pressed.
63
+ * @param {string} textValue - The current value of the input field.
64
+ * @param {Object} that - The EOxItemFilterChips component instance.
65
+ */
66
+ function handleKeyboard(key, textValue, that) {
67
+ const highlightedChip = that.renderRoot.querySelector(".chip.highlighted");
68
+ // Handle Escape key or text value changes
69
+ if (key === "Escape" || textValue) {
70
+ if (highlightedChip) {
71
+ highlightedChip.classList.remove("highlighted");
72
+ }
73
+ }
74
+
75
+ // Handle Backspace key with no text value
76
+ if (key === "Backspace" && !textValue) {
77
+ if (that.items.length) {
78
+ if (highlightedChip) {
79
+ highlightedChip.querySelector(".chip-item-close").click();
80
+ that.items.splice(
81
+ Array.from(that.renderRoot.querySelectorAll(".chip")).indexOf(
82
+ highlightedChip,
83
+ ),
84
+ 1,
85
+ );
86
+ }
87
+ const lastChip =
88
+ that.renderRoot.querySelectorAll(".chip")[
89
+ that.renderRoot.querySelectorAll(".chip").length - 1
90
+ ];
91
+ if (!lastChip.classList.contains("highlighted")) {
92
+ lastChip.classList.add("highlighted");
93
+ }
94
+ that.requestUpdate();
95
+ }
96
+ that._dispatchEvent();
97
+ }
98
+
99
+ // Handle ArrowLeft and ArrowRight keys with no text value
100
+ if ((key === "ArrowLeft" || key === "ArrowRight") && !textValue) {
101
+ if (that.renderRoot.querySelectorAll(".chip").length < 1) {
102
+ return;
103
+ }
104
+ let highlightedChipIndex = 0;
105
+ const highlightedChip = that.renderRoot.querySelector(".chip.highlighted");
106
+ if (highlightedChip) {
107
+ highlightedChipIndex = Array.from(
108
+ that.renderRoot.querySelectorAll(".chip"),
109
+ ).indexOf(highlightedChip);
110
+ highlightedChip.classList.remove("highlighted");
111
+ }
112
+ highlightedChipIndex =
113
+ highlightedChipIndex + (key === "ArrowLeft" ? -1 : +1);
114
+ if (key === "ArrowLeft" && highlightedChipIndex < 0) {
115
+ highlightedChipIndex =
116
+ that.renderRoot.querySelectorAll(".chip").length - 1;
117
+ }
118
+ if (
119
+ key === "ArrowRight" &&
120
+ highlightedChipIndex >
121
+ that.renderRoot.querySelectorAll(".chip").length - 1
122
+ ) {
123
+ highlightedChipIndex = 0;
124
+ }
125
+ Array.from(that.renderRoot.querySelectorAll(".chip"))[
126
+ highlightedChipIndex
127
+ ].classList.add("highlighted");
128
+ }
129
+ }
@@ -0,0 +1,5 @@
1
+ export {
2
+ handleChipClickMethod,
3
+ keyboardEventListenerMethod,
4
+ handleCloseMethod,
5
+ } from "./handler";
@@ -0,0 +1,70 @@
1
+ import resetSearchMethod from "./reset-search.js";
2
+
3
+ /**
4
+ * Handles the click event on the form, stopping propagation if in inline mode.
5
+ *
6
+ * @param {Event} event - The click event.
7
+ * @param {Object} EOxItemFilterContainer - The EOxItemFilterContainer component instance.
8
+ */
9
+ export function handleFormClickMethod(event, EOxItemFilterContainer) {
10
+ if (EOxItemFilterContainer.inlineMode) event.stopPropagation();
11
+ }
12
+
13
+ /**
14
+ * Shows the dropdown when the component is focused, if in inline mode.
15
+ *
16
+ * @param {Object} EOxItemFilterContainer - The EOxItemFilterContainer component instance.
17
+ */
18
+ export function handleShowDropdownOnFocusMethod(EOxItemFilterContainer) {
19
+ if (EOxItemFilterContainer.inlineMode) {
20
+ EOxItemFilterContainer.showDropdown = true;
21
+ }
22
+ }
23
+
24
+ /**
25
+ * Toggles the dropdown visibility when an event occurs, stopping propagation if in inline mode.
26
+ *
27
+ * @param {Event} event - The event that triggers the toggle.
28
+ * @param {Object} EOxItemFilterContainer - The EOxItemFilterContainer component instance.
29
+ */
30
+ export function handleToggleDropdownMethod(event, EOxItemFilterContainer) {
31
+ if (EOxItemFilterContainer.inlineMode) {
32
+ event.stopPropagation();
33
+ EOxItemFilterContainer.showDropdown = true;
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Handles the keydown event, resetting the search and hiding the dropdown if Escape is pressed in inline mode.
39
+ *
40
+ * @param {Event} event - The keydown event.
41
+ * @param {Object} EOxItemFilterContainer - The EOxItemFilterContainer component instance.
42
+ */
43
+ export function handleKeyDownMethod(event, EOxItemFilterContainer) {
44
+ if (
45
+ EOxItemFilterContainer.inlineMode &&
46
+ event.key === "Escape" &&
47
+ EOxItemFilterContainer.showDropdown
48
+ ) {
49
+ resetSearchMethod(EOxItemFilterContainer);
50
+ EOxItemFilterContainer.showDropdown = false;
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Handles clicks outside the component, resetting the search and hiding the dropdown if in inline mode.
56
+ *
57
+ * @param {Event} event - The click event.
58
+ * @param {Object} EOxItemFilterContainer - The EOxItemFilterContainer component instance.
59
+ */
60
+ export function handleClickOutsideMethod(event, EOxItemFilterContainer) {
61
+ if (
62
+ EOxItemFilterContainer.inlineMode &&
63
+ event.target.tagName !== "DROPDOWN-FORM" &&
64
+ event.target.tagName !== "EOX-ITEMFILTER" &&
65
+ EOxItemFilterContainer.showDropdown
66
+ ) {
67
+ resetSearchMethod(EOxItemFilterContainer);
68
+ EOxItemFilterContainer.showDropdown = false;
69
+ }
70
+ }
@@ -0,0 +1,10 @@
1
+ export { default as searchFilterMethod } from "./search-filter";
2
+ export { default as resetFilterMethod } from "./reset-filter";
3
+ export { default as initPopoverMethod } from "./init-popver";
4
+ export {
5
+ handleFormClickMethod,
6
+ handleShowDropdownOnFocusMethod,
7
+ handleToggleDropdownMethod,
8
+ handleKeyDownMethod,
9
+ handleClickOutsideMethod,
10
+ } from "./handler";
@@ -0,0 +1,34 @@
1
+ import { autoUpdate, computePosition } from "@floating-ui/dom";
2
+
3
+ /**
4
+ * Init popover autoUpdate and position
5
+ *
6
+ * @param {Object} EOxItemFilterContainer - The EOxItemFilterContainer component instance.
7
+ * @return {Function}
8
+ */
9
+ function initPopover(EOxItemFilterContainer) {
10
+ const trigger = EOxItemFilterContainer.renderRoot.querySelector(
11
+ ".inline-container-wrapper",
12
+ );
13
+ const dropdown = EOxItemFilterContainer.renderRoot.querySelector("[popover]");
14
+ const updatePosition = () => {
15
+ // Only compute position if popover is open
16
+ if (dropdown.matches(":popover-open")) {
17
+ computePosition(trigger, dropdown, { strategy: "fixed" }).then(
18
+ ({ x, y }) => {
19
+ Object.assign(dropdown.style, {
20
+ left: `${x}px`,
21
+ top: `${y}px`,
22
+ width: `${trigger.getBoundingClientRect().width}px`,
23
+ });
24
+ },
25
+ );
26
+ }
27
+ };
28
+
29
+ return autoUpdate(trigger, dropdown, updatePosition, {
30
+ animationFrame: true,
31
+ });
32
+ }
33
+
34
+ export default initPopover;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Resets a specific filter in the EOxItemFilterContainer based on the event target.
3
+ *
4
+ * @param {Event} event - The event that triggers the filter reset.
5
+ * @param {Object} EOxItemFilterContainer - The EOxItemFilterContainer component instance.
6
+ */
7
+ function resetFilterMethod(event, EOxItemFilterContainer) {
8
+ const filterKey = event.target.getAttribute("data-close").replace("|", "-");
9
+ EOxItemFilterContainer.querySelector(`#filter-${filterKey}`).reset();
10
+ EOxItemFilterContainer.dispatchEvent(new CustomEvent("filter"));
11
+ EOxItemFilterContainer.requestUpdate();
12
+ }
13
+
14
+ export default resetFilterMethod;
@@ -0,0 +1,16 @@
1
+ import searchFilterMethod from "./search-filter.js";
2
+
3
+ /**
4
+ * Resets the search input in the EOxItemFilterContainer and triggers a search filter update.
5
+ *
6
+ * @param {Object} EOxItemFilterContainer - The EOxItemFilterContainer component instance.
7
+ */
8
+ function resetSearchMethod(EOxItemFilterContainer) {
9
+ EOxItemFilterContainer.renderRoot.querySelector(
10
+ "#eox-itemfilter-input-search",
11
+ ).value = "";
12
+ const event = { target: { value: "" } };
13
+ searchFilterMethod(event, EOxItemFilterContainer);
14
+ }
15
+
16
+ export default resetSearchMethod;
@@ -0,0 +1,27 @@
1
+ import Fuse from "fuse.js";
2
+
3
+ /**
4
+ * Filters the items in the EOxItemFilterContainer based on the search input.
5
+ *
6
+ * @param {Event} event - The input event that triggers the search.
7
+ * @param {Object} EOxItemFilterContainer - The EOxItemFilterContainer component instance.
8
+ */
9
+ function searchFilterMethod(event, EOxItemFilterContainer) {
10
+ // Create a Fuse instance for fuzzy searching with the filter properties
11
+ const fuse = new Fuse(EOxItemFilterContainer.filterProperties, {
12
+ keys: ["title"],
13
+ });
14
+ const inputText = event.target.value;
15
+ const result = fuse.search(inputText);
16
+ const matches = result.map((res) => res.item.key || res.item.keys.join("|"));
17
+
18
+ // Update the display of filter elements based on the search results
19
+ Object.keys(EOxItemFilterContainer.filters).forEach((filter) => {
20
+ EOxItemFilterContainer.querySelector(
21
+ `[data-details="${filter}"]`,
22
+ ).parentElement.style.display =
23
+ matches.includes(filter) || !inputText ? "" : "none";
24
+ });
25
+ }
26
+
27
+ export default searchFilterMethod;
@@ -0,0 +1,20 @@
1
+ export { resetTextMethod, textInputHandlerMethod } from "./text";
2
+ export {
3
+ rangeLabelMethod,
4
+ resetRangeMethod,
5
+ rangeInputHandlerMethod,
6
+ } from "./range";
7
+ export {
8
+ resetSelectorMethod,
9
+ removeItemSelectorMethod,
10
+ toggleItemSelectorMethod,
11
+ handleInputSelectorMethod,
12
+ handleKeyDownSelectorMethod,
13
+ updatedSelectorMethod,
14
+ } from "./selector";
15
+ export {
16
+ resetSpatialMethod,
17
+ handleClickSpatialMethod,
18
+ setupSpatialMethod,
19
+ resetSpatialFilterMethod,
20
+ } from "./spatial";
@@ -0,0 +1,78 @@
1
+ import { resetFilter } from "../../helpers/index.js";
2
+ import dayjs from "dayjs";
3
+ import { html } from "lit";
4
+
5
+ /**
6
+ * Resets the range filter to its default state and requests an update.
7
+ *
8
+ * @param {import("../../components/filters/range.js").EOxItemFilterRange} EOxItemFilterRange - The EOxItemFilterRange component instance.
9
+ */
10
+ export function resetRangeMethod(EOxItemFilterRange) {
11
+ EOxItemFilterRange.filterObject = resetFilter(
12
+ EOxItemFilterRange.filterObject,
13
+ );
14
+ if (EOxItemFilterRange.filterObject) {
15
+ const eleTCRangeSlider =
16
+ EOxItemFilterRange.querySelector("tc-range-slider");
17
+ const min = EOxItemFilterRange.filterObject.min;
18
+ const max = EOxItemFilterRange.filterObject.max;
19
+ if (eleTCRangeSlider.value1 !== min) eleTCRangeSlider.value1 = min;
20
+ if (eleTCRangeSlider.value2 !== max) eleTCRangeSlider.value2 = max;
21
+ }
22
+ EOxItemFilterRange.requestUpdate();
23
+ }
24
+
25
+ /**
26
+ * Handles input changes for the range filter and updates the filter state accordingly.
27
+ *
28
+ * @param {Event} evt - The input event that triggers the update.
29
+ * @param {Object} EOxItemFilterRange - The EOxItemFilterRange component instance.
30
+ */
31
+ export function rangeInputHandlerMethod(evt, EOxItemFilterRange) {
32
+ const [min, max] = evt.detail.values;
33
+
34
+ // Check if the new min and max values are different from the current state
35
+ if (
36
+ min !== EOxItemFilterRange.filterObject.state.min ||
37
+ max !== EOxItemFilterRange.filterObject.state.max
38
+ ) {
39
+ // Update the state with the new min and max values
40
+ [
41
+ EOxItemFilterRange.filterObject.state.min,
42
+ EOxItemFilterRange.filterObject.state.max,
43
+ ] = [min, max];
44
+ EOxItemFilterRange.filterObject.dirty = true;
45
+ }
46
+
47
+ if (EOxItemFilterRange.filterObject.dirty) {
48
+ EOxItemFilterRange.filterObject.stringifiedState =
49
+ EOxItemFilterRange.filterObject.format === "date"
50
+ ? `${dayjs(min)} - ${dayjs(max)}`
51
+ : `${min} - ${max}`;
52
+ }
53
+
54
+ EOxItemFilterRange.dispatchEvent(new CustomEvent("filter"));
55
+
56
+ // Reset the component if the state matches the original min and max values, otherwise request an update
57
+ if (
58
+ min === EOxItemFilterRange.filterObject.min &&
59
+ max === EOxItemFilterRange.filterObject.max
60
+ )
61
+ EOxItemFilterRange.reset();
62
+ else EOxItemFilterRange.requestUpdate();
63
+ }
64
+
65
+ /**
66
+ * Generates the HTML for the range labels, formatted as dates if necessary.
67
+ *
68
+ * @param {number|string} val - The value of the range label.
69
+ * @param {string} pos - The position of the range label (e.g., "min" or "max").
70
+ * @param {Object} EOxItemFilterRange - The EOxItemFilterRange component instance.
71
+ * @returns {TemplateResult<1>} The HTML template for the range label.
72
+ */
73
+ export function rangeLabelMethod(val, pos, EOxItemFilterRange) {
74
+ const isDate = Boolean(EOxItemFilterRange.filterObject.format === "date");
75
+ const filteredVal = EOxItemFilterRange.filterObject.state[val];
76
+ const label = isDate ? dayjs.unix(filteredVal) : filteredVal;
77
+ return html`<div class="range-${pos}">${label}</div>`;
78
+ }
@@ -0,0 +1,209 @@
1
+ import { resetFilter } from "../../helpers/index.js";
2
+ import Fuse from "fuse.js";
3
+
4
+ /**
5
+ * Resets the selector filter to its default state and updates the component.
6
+ *
7
+ * @param {Object} EOxItemFilterSelector - The EOxItemFilterSelector component instance.
8
+ */
9
+ export function resetSelectorMethod(EOxItemFilterSelector) {
10
+ removeItemSelectorMethod(-1, EOxItemFilterSelector);
11
+
12
+ resetFilter(EOxItemFilterSelector.filterObject);
13
+ EOxItemFilterSelector.requestUpdate();
14
+ }
15
+
16
+ /**
17
+ * Removes an item from the selected items list and updates suggestions and filter list.
18
+ *
19
+ * @param {number} index - The index of the item to remove, or -1 to remove all items.
20
+ * @param {Object} EOxItemFilterSelector - The EOxItemFilterSelector component instance.
21
+ */
22
+ export function removeItemSelectorMethod(index, EOxItemFilterSelector) {
23
+ EOxItemFilterSelector.selectedItems =
24
+ index === -1
25
+ ? []
26
+ : EOxItemFilterSelector.selectedItems.filter((_, i) => i !== index);
27
+
28
+ updateSuggestions(EOxItemFilterSelector);
29
+ updateFilterList(EOxItemFilterSelector);
30
+ }
31
+
32
+ /**
33
+ * Toggles the selection of an item and updates the component.
34
+ *
35
+ * @param {Object} item - The item to toggle.
36
+ * @param {Object} EOxItemFilterSelector - The EOxItemFilterSelector component instance.
37
+ */
38
+ export function toggleItemSelectorMethod(item, EOxItemFilterSelector) {
39
+ const itemIndex = EOxItemFilterSelector.selectedItems.indexOf(item);
40
+ if (itemIndex >= 0) {
41
+ // Remove the item if it's already selected
42
+ EOxItemFilterSelector.selectedItems =
43
+ EOxItemFilterSelector.selectedItems.filter((_, i) => i !== itemIndex);
44
+ } else {
45
+ // Add the item if it's not selected
46
+ if (EOxItemFilterSelector.type === "multiselect") {
47
+ EOxItemFilterSelector.selectedItems = [
48
+ ...EOxItemFilterSelector.selectedItems,
49
+ item,
50
+ ];
51
+ } else {
52
+ EOxItemFilterSelector.selectedItems = [item];
53
+ EOxItemFilterSelector.showSuggestions = false;
54
+ }
55
+ }
56
+ // EOxItemFilterSelector.query = "";
57
+
58
+ // Update suggestions and filter list
59
+ updateSuggestions(EOxItemFilterSelector);
60
+ updateFilterList(EOxItemFilterSelector);
61
+ }
62
+
63
+ /**
64
+ * Handles input changes for the selector and updates the query and suggestions.
65
+ *
66
+ * @param {Event} event - The input event.
67
+ * @param {Object} EOxItemFilterSelector - The EOxItemFilterSelector component instance.
68
+ */
69
+ export function handleInputSelectorMethod(event, EOxItemFilterSelector) {
70
+ EOxItemFilterSelector.query = event.target.value;
71
+ EOxItemFilterSelector.showSuggestions = true;
72
+ }
73
+
74
+ /**
75
+ * Handles keydown events for the selector and navigates or selects items based on the key pressed.
76
+ *
77
+ * @param {Event} event - The keydown event.
78
+ * @param {Object} EOxItemFilterSelector - The EOxItemFilterSelector component instance.
79
+ */
80
+ export function handleKeyDownSelectorMethod(event, EOxItemFilterSelector) {
81
+ switch (event.key) {
82
+ case "ArrowDown":
83
+ // Navigate down the suggestions list
84
+ EOxItemFilterSelector.highlightedIndex = Math.min(
85
+ EOxItemFilterSelector.highlightedIndex + 1,
86
+ EOxItemFilterSelector.filteredSuggestions.length - 1,
87
+ );
88
+ break;
89
+
90
+ case "ArrowUp":
91
+ // Navigate up the suggestions list
92
+ EOxItemFilterSelector.highlightedIndex = Math.max(
93
+ EOxItemFilterSelector.highlightedIndex - 1,
94
+ 0,
95
+ );
96
+ break;
97
+
98
+ case "Enter":
99
+ // Select the highlighted item
100
+ if (EOxItemFilterSelector.highlightedIndex >= 0)
101
+ toggleItemSelectorMethod(
102
+ EOxItemFilterSelector.filteredSuggestions[
103
+ EOxItemFilterSelector.highlightedIndex
104
+ ],
105
+ EOxItemFilterSelector,
106
+ );
107
+ break;
108
+
109
+ case "Escape":
110
+ // Hide suggestions
111
+ EOxItemFilterSelector.showSuggestions = false;
112
+ break;
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Updates the Fuse.js instance and suggestions when properties change.
118
+ *
119
+ * @param {Object} changedProperties - The properties that changed.
120
+ * @param {Object} EOxItemFilterSelector - The EOxItemFilterSelector component instance.
121
+ */
122
+ export function updatedSelectorMethod(
123
+ changedProperties,
124
+ EOxItemFilterSelector,
125
+ ) {
126
+ if (changedProperties.has("suggestions") || changedProperties.has("query")) {
127
+ updateSuggestions(EOxItemFilterSelector);
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Sorts and returns the suggestions based on the filter's sort method or default string comparison.
133
+ *
134
+ * @param {Object} EOxItemFilterSelector - The EOxItemFilterSelector component instance.
135
+ * @param {Array<Object>} suggestion - The suggestions to sort.
136
+ * @returns {Array<Object>} The sorted suggestions.
137
+ */
138
+ function getSortedSuggestions(EOxItemFilterSelector, suggestion) {
139
+ const sortCallback =
140
+ EOxItemFilterSelector.filterObject?.sort || ((a, b) => a.localeCompare(b));
141
+
142
+ return suggestion.sort(sortCallback).map((i) => i);
143
+ }
144
+
145
+ /**
146
+ * Updates the filtered suggestions based on the current query.
147
+ *
148
+ * @param {Object} EOxItemFilterSelector - The EOxItemFilterSelector component instance.
149
+ */
150
+ function updateSuggestions(EOxItemFilterSelector) {
151
+ let filteredSuggestion;
152
+ if (EOxItemFilterSelector.query) {
153
+ const fuse = new Fuse(EOxItemFilterSelector.suggestions, {
154
+ threshold: 0.4,
155
+ });
156
+ filteredSuggestion = fuse
157
+ .search(EOxItemFilterSelector.query)
158
+ .map((result) => result.item);
159
+ }
160
+ EOxItemFilterSelector.filteredSuggestions = getSortedSuggestions(
161
+ EOxItemFilterSelector,
162
+ filteredSuggestion || EOxItemFilterSelector.suggestions,
163
+ );
164
+
165
+ // if `filterKeys` is defined, force filter keys
166
+ if (EOxItemFilterSelector.filterObject?.filterKeys) {
167
+ EOxItemFilterSelector.filteredSuggestions =
168
+ EOxItemFilterSelector.filterObject?.filterKeys.map((k) => `${k}`);
169
+ EOxItemFilterSelector.filterObject.state =
170
+ EOxItemFilterSelector.filterObject?.filterKeys
171
+ .map((k) => `${k}`)
172
+ .reduce((acc, curr) => {
173
+ if (!(curr in acc)) {
174
+ acc[curr] = undefined;
175
+ }
176
+ return acc;
177
+ }, EOxItemFilterSelector.filterObject.state);
178
+ }
179
+
180
+ EOxItemFilterSelector.highlightedIndex = -1;
181
+ }
182
+
183
+ /**
184
+ * Updates the filter list based on the selected items.
185
+ *
186
+ * @param {Object} EOxItemFilterSelector - The EOxItemFilterSelector component instance.
187
+ */
188
+ function updateFilterList(EOxItemFilterSelector) {
189
+ // Update the state of the filter object based on the selected items
190
+ Object.keys(EOxItemFilterSelector.filterObject.state).forEach((k) => {
191
+ EOxItemFilterSelector.filterObject.state[k] =
192
+ EOxItemFilterSelector.selectedItems.includes(k);
193
+ });
194
+
195
+ // Update the stringified state of the filter object
196
+ EOxItemFilterSelector.filterObject.stringifiedState =
197
+ Object.keys(EOxItemFilterSelector.filterObject.state)
198
+ .filter((k) => EOxItemFilterSelector.filterObject.state[k])
199
+ .join(", ") || "";
200
+
201
+ // Mark the filter as dirty if there are selected items
202
+ EOxItemFilterSelector.filterObject.dirty = Boolean(
203
+ EOxItemFilterSelector.filterObject.stringifiedState.length > 0,
204
+ );
205
+
206
+ // Dispatch a custom event named "filter" and request an update
207
+ EOxItemFilterSelector.dispatchEvent(new CustomEvent("filter"));
208
+ EOxItemFilterSelector.requestUpdate();
209
+ }