@eox/itemfilter 1.3.1 → 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 +984 -1111
  3. package/dist/eox-itemfilter.umd.cjs +106 -108
  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 -532
@@ -0,0 +1,114 @@
1
+ import { getMapLayers, resetFilter } from "../../helpers/index.js";
2
+
3
+ /**
4
+ * Resets the spatial filter to its default state and updates the component.
5
+ *
6
+ * @param {Object} EOxItemFilterSpatial - The EOxItemFilterSpatial component instance.
7
+ */
8
+ export function resetSpatialMethod(EOxItemFilterSpatial) {
9
+ resetFilter(EOxItemFilterSpatial.filterObject);
10
+
11
+ const spatialFilter = EOxItemFilterSpatial.renderRoot.querySelector(
12
+ "eox-itemfilter-spatial-filter",
13
+ );
14
+ spatialFilter.reset();
15
+ EOxItemFilterSpatial.requestUpdate();
16
+ }
17
+
18
+ /**
19
+ * Resets the spatial filter map and re-initializes it.
20
+ *
21
+ * @param {Object} EOxItemFilterSpatial - The EOxItemFilterSpatial component instance.
22
+ */
23
+ export function resetSpatialFilterMethod(EOxItemFilterSpatial) {
24
+ EOxItemFilterSpatial.renderRoot.querySelector("#eox-map").innerHTML = "";
25
+ setupSpatialMethod(EOxItemFilterSpatial);
26
+ }
27
+
28
+ /**
29
+ * Handles the click event for spatial mode buttons and updates the filter state.
30
+ *
31
+ * @param {string} mode - The selected spatial mode.
32
+ * @param {Object} EOxItemFilterSpatial - The EOxItemFilterSpatial component instance.
33
+ */
34
+ export function handleClickSpatialMethod(mode, EOxItemFilterSpatial) {
35
+ EOxItemFilterSpatial.filterObject.state.mode = mode;
36
+ const event = new CustomEvent("filter", {
37
+ detail: {
38
+ [EOxItemFilterSpatial.filterObject.key]: {},
39
+ },
40
+ });
41
+ EOxItemFilterSpatial.dispatchEvent(event);
42
+ }
43
+
44
+ /**
45
+ * Sets up the spatial filter map and its interactions.
46
+ *
47
+ * @param {Object} that - The EOxItemFilterSpatial component instance.
48
+ */
49
+ export function setupSpatialMethod(that) {
50
+ const parentEOxMap = that.renderRoot.querySelector("#eox-map");
51
+ if (parentEOxMap.innerHTML === "")
52
+ parentEOxMap.innerHTML = `<eox-map part="map" style="height: 400px"></eox-map>`; // Initialize the map element if it is not already initialized
53
+
54
+ // Create the feature URL if geometry is provided
55
+ const url = that.geometry && createFeatureUrl(that.geometry);
56
+ const mapLayers = getMapLayers(that.geometry, url);
57
+
58
+ that.eoxMap = that.renderRoot.querySelector("eox-map");
59
+
60
+ // Set up the map layers and interactions after a short delay
61
+ setTimeout(() => {
62
+ that.eoxMap.layers = mapLayers;
63
+
64
+ // Function to update the geometry filter
65
+ const updateGeometryFilter = (feature) => {
66
+ const event = new CustomEvent("filter", {
67
+ detail: {
68
+ geometry: {
69
+ type: "Polygon",
70
+ coordinates: feature
71
+ .getGeometry()
72
+ .clone()
73
+ .transform("EPSG:3857", "EPSG:4326")
74
+ .getCoordinates(),
75
+ },
76
+ },
77
+ });
78
+ that.dispatchEvent(event);
79
+ };
80
+
81
+ // Set up draw end interaction
82
+ that.eoxMap.interactions["drawInteraction"].on("drawend", (e) => {
83
+ updateGeometryFilter(e.feature);
84
+ that.eoxMap.removeInteraction("drawInteraction");
85
+ });
86
+
87
+ // Set up modify end interaction
88
+ that.eoxMap.interactions["drawInteraction_modify"].on("modifyend", (e) => {
89
+ updateGeometryFilter(e.features.getArray()[0]);
90
+ });
91
+ }, 1000);
92
+ }
93
+
94
+ /**
95
+ * Creates a URL for a GeoJSON feature collection from the given geometry.
96
+ *
97
+ * @param {Object} geometry - The geometry to include in the feature collection.
98
+ * @returns {string} The URL for the GeoJSON feature collection.
99
+ */
100
+ function createFeatureUrl(geometry) {
101
+ const featureString = encodeURIComponent(
102
+ JSON.stringify({
103
+ type: "FeatureCollection",
104
+ features: [
105
+ {
106
+ type: "Feature",
107
+ properties: null,
108
+ geometry,
109
+ },
110
+ ],
111
+ }),
112
+ );
113
+ return `data:text/json,${featureString}`;
114
+ }
@@ -0,0 +1,32 @@
1
+ import { resetFilter } from "../../helpers/index.js";
2
+
3
+ /**
4
+ * Resets the text filter to its default state and updates the component.
5
+ *
6
+ * @param {Object} EOxItemFilterText - The EOxItemFilterText component instance.
7
+ */
8
+ export function resetTextMethod(EOxItemFilterText) {
9
+ const searchInput =
10
+ EOxItemFilterText.renderRoot.querySelector("input[type='text']");
11
+ searchInput.value = "";
12
+ resetFilter(EOxItemFilterText.filterObject);
13
+ EOxItemFilterText.requestUpdate();
14
+ }
15
+
16
+ /**
17
+ * Handles input changes for the text filter and updates the filter state accordingly.
18
+ *
19
+ * @param {Object} EOxItemFilterText - The EOxItemFilterText component instance.
20
+ */
21
+ export function textInputHandlerMethod(EOxItemFilterText) {
22
+ const searchInput =
23
+ EOxItemFilterText.renderRoot.querySelector("input[type='text']");
24
+ EOxItemFilterText.isValid = searchInput.checkValidity();
25
+ EOxItemFilterText.filterObject.keys.forEach((key) => {
26
+ EOxItemFilterText.filterObject.state[key] = searchInput.value;
27
+ });
28
+ EOxItemFilterText.filterObject.dirty = true;
29
+ EOxItemFilterText.filterObject.stringifiedState = searchInput.value;
30
+ EOxItemFilterText.dispatchEvent(new CustomEvent("filter"));
31
+ if (searchInput.value === "") EOxItemFilterText.reset();
32
+ }
@@ -0,0 +1,83 @@
1
+ import { html } from "lit";
2
+ import uniq from "lodash.uniq";
3
+ import flatMap from "lodash.flatmap";
4
+
5
+ /**
6
+ * Creates a filter element based on the filter object type.
7
+ *
8
+ * @param {Object} filterObject - The filter object containing filter details.
9
+ * @param {number} tabIndex - The tab index for the filter element.
10
+ * @param {Object} EOxItemFilter - The EOxItemFilter component instance.
11
+ * @returns {TemplateResult} The HTML template for the filter element.
12
+ */
13
+ function createFilterMethod(filterObject, tabIndex, EOxItemFilter) {
14
+ // Create a unique ID for the filter element by replacing '|' with '-'
15
+ const filterId = `filter-${filterObject.key}`.replace("|", "-");
16
+
17
+ // Generate the appropriate filter element based on the filter type
18
+ switch (filterObject.type) {
19
+ case "text":
20
+ // Return a text filter element
21
+ return html`<eox-itemfilter-text
22
+ data-type="filter"
23
+ data-cy="text-filter"
24
+ slot="filter"
25
+ id="${filterId}"
26
+ .tabIndex=${tabIndex}
27
+ .filterObject=${filterObject}
28
+ .unstyled=${EOxItemFilter.unstyled}
29
+ @filter=${() => EOxItemFilter.search()}
30
+ ></eox-itemfilter-text>`;
31
+
32
+ case "multiselect":
33
+ case "select":
34
+ // Return a select or multiselect filter element
35
+ return html`
36
+ <eox-itemfilter-select
37
+ .inlineMode=${EOxItemFilter.inlineMode || false}
38
+ data-type="filter"
39
+ id="${filterId}"
40
+ .tabIndex=${tabIndex}
41
+ .filterObject=${filterObject}
42
+ slot="filter"
43
+ .suggestions="${uniq(
44
+ flatMap(EOxItemFilter.items, filterObject.key),
45
+ ).filter((i) => i)}"
46
+ type="${filterObject.type}"
47
+ .unstyled=${EOxItemFilter.unstyled}
48
+ @filter=${() => EOxItemFilter.search()}
49
+ ></eox-itemfilter-select>
50
+ `;
51
+ case "range":
52
+ // Return a range filter element
53
+ return html`
54
+ <eox-itemfilter-range
55
+ id="${filterId}"
56
+ data-type="filter"
57
+ .tabIndex=${tabIndex}
58
+ .filterObject=${filterObject}
59
+ slot="filter"
60
+ .unstyled=${EOxItemFilter.unstyled}
61
+ @filter=${() => EOxItemFilter.search()}
62
+ ></eox-itemfilter-range>
63
+ `;
64
+ case "spatial":
65
+ // Return a spatial filter element
66
+ return html`
67
+ <eox-itemfilter-spatial
68
+ id="${filterId}"
69
+ data-type="filter"
70
+ .tabIndex=${tabIndex}
71
+ .filterObject=${filterObject}
72
+ slot="filter"
73
+ @filter=${() => EOxItemFilter.search()}
74
+ ></eox-itemfilter-spatial>
75
+ `;
76
+
77
+ default:
78
+ // Return an empty template for unknown filter types
79
+ return html``;
80
+ }
81
+ }
82
+
83
+ export default createFilterMethod;
@@ -0,0 +1,39 @@
1
+ import { html, nothing } from "lit";
2
+ import { when } from "lit/directives/when.js";
3
+
4
+ /**
5
+ * Creates a reset button for the filter object if it is dirty.
6
+ *
7
+ * @param {Object} filterObject - The filter object containing filter details.
8
+ * @param {number} tabIndex - The tab index for the reset button.
9
+ * @param {Object} EOxItemFilter - The EOxItemFilter component instance.
10
+ * @returns {TemplateResult} The HTML template for the reset button.
11
+ */
12
+ function createResetMethod(filterObject, tabIndex, EOxItemFilter) {
13
+ return html`
14
+ ${when(
15
+ filterObject.dirty,
16
+ () => html`
17
+ <button
18
+ type="button"
19
+ tabindex=${tabIndex}
20
+ slot="reset-button"
21
+ class="reset-icon icon"
22
+ @click=${(e) => {
23
+ const filterElement =
24
+ e.target.parentElement.querySelector("[slot=filter]");
25
+ if (filterElement && typeof filterElement.reset === "function") {
26
+ filterElement.reset();
27
+ }
28
+ EOxItemFilter.search();
29
+ EOxItemFilter.requestUpdate();
30
+ }}
31
+ >
32
+ ${EOxItemFilter.unstyled ? "Reset" : nothing}
33
+ </button>
34
+ `,
35
+ )}
36
+ `;
37
+ }
38
+
39
+ export default createResetMethod;
@@ -0,0 +1,140 @@
1
+ import dayjs from "dayjs";
2
+ import { getValue, indexItems } from "../../helpers";
3
+ import uniq from "lodash.uniq";
4
+ import flatMap from "lodash.flatmap";
5
+
6
+ /**
7
+ * Applies filters to the provided items based on the given configuration and updates the EOxItemFilter instance.
8
+ *
9
+ * @param {Object} config - The configuration object for the filters.
10
+ * @param {Array<Object>} items - The items to be filtered.
11
+ * @param {Object} EOxItemFilter - The EOxItemFilter component instance.
12
+ * @returns {Array<string>} The aggregated results if config.aggregateResults is specified, otherwise an empty array.
13
+ */
14
+ function filterApplyMethod(config, items, EOxItemFilter) {
15
+ // build filters
16
+ let resultAggregation = [];
17
+
18
+ if (config.filterProperties.length) {
19
+ config.filterProperties.forEach((filterProperty) => {
20
+ const filterKeys = {};
21
+
22
+ // Function to parse values based on their format
23
+ const parseValue = (value) => {
24
+ return filterProperty.format === "date"
25
+ ? dayjs(value).unix()
26
+ : parseFloat(value);
27
+ };
28
+
29
+ // Iterate over items to build filter keys
30
+ items.forEach((item) => {
31
+ if (filterProperty.type === "range") {
32
+ const value = getValue(filterProperty.key, item);
33
+ if (Array.isArray(value)) {
34
+ const currentValues = [parseValue(value[0]), parseValue(value[1])];
35
+ filterKeys.min =
36
+ filterKeys.min !== undefined
37
+ ? Math.min(filterKeys.min, currentValues[0])
38
+ : currentValues[0];
39
+ filterKeys.max =
40
+ filterKeys.max !== undefined
41
+ ? Math.max(filterKeys.max, currentValues[1])
42
+ : currentValues[1];
43
+ } else {
44
+ const currentValue = parseValue(value);
45
+ filterKeys.min =
46
+ filterKeys.min !== undefined
47
+ ? Math.min(filterKeys.min, currentValue)
48
+ : currentValue;
49
+ filterKeys.max =
50
+ filterKeys.max !== undefined
51
+ ? Math.max(filterKeys.max, currentValue)
52
+ : currentValue;
53
+ }
54
+ return;
55
+ }
56
+ if (Array.isArray(item[filterProperty.key])) {
57
+ item[filterProperty.key].forEach((prop) => {
58
+ filterKeys[prop] = undefined;
59
+ });
60
+ } else {
61
+ if (filterProperty.type === "spatial") {
62
+ filterKeys.geometry = undefined;
63
+ filterKeys.mode = filterProperty.mode || "intersects";
64
+ } else {
65
+ if (filterProperty.key?.includes(".")) {
66
+ uniq(flatMap(EOxItemFilter.items, filterProperty.key))
67
+ .filter((i) => i)
68
+ .forEach((key) => {
69
+ filterKeys[key] = undefined;
70
+ });
71
+ } else filterKeys[item[filterProperty.key]] = undefined;
72
+ }
73
+ }
74
+ });
75
+
76
+ // Combine filter properties into the filter object
77
+ const filterKey = filterProperty.key || filterProperty.keys.join("|");
78
+ EOxItemFilter.filters[filterKey] = Object.assign(
79
+ {
80
+ type: filterProperty.type || "multiselect",
81
+ dirty: filterProperty.state ? false : undefined,
82
+ key: filterKey,
83
+ },
84
+ filterProperty.type === "range"
85
+ ? {
86
+ min: filterKeys.min,
87
+ max: filterKeys.max,
88
+ format: filterProperty.format,
89
+ }
90
+ : {},
91
+ filterProperty,
92
+ );
93
+ EOxItemFilter.filters[filterKey].state = Object.assign(
94
+ {},
95
+ filterKeys,
96
+ filterProperty.state,
97
+ );
98
+ });
99
+ }
100
+
101
+ // Render all items if matchAllWhenEmpty is true
102
+ if (config.matchAllWhenEmpty !== false) {
103
+ // initially render all items
104
+ EOxItemFilter.results = EOxItemFilter.sortResults(items);
105
+ EOxItemFilter.requestUpdate();
106
+ }
107
+
108
+ // Aggregate results if specified in the configuration
109
+ if (config.aggregateResults) {
110
+ resultAggregation = Array.from(
111
+ new Set(
112
+ items.reduce(
113
+ (store, item) => store.concat(item[config.aggregateResults]),
114
+ [],
115
+ ),
116
+ ),
117
+ ).sort((a, b) => a.localeCompare(b));
118
+ }
119
+
120
+ // Build Fuse.js search keys
121
+ const fuseKeys = [];
122
+ Object.values(EOxItemFilter.filters).forEach((f) => {
123
+ if (f.type === "text") {
124
+ f.keys.forEach((k) => {
125
+ if (!fuseKeys.includes(k)) {
126
+ fuseKeys.push(k);
127
+ }
128
+ });
129
+ } else if (f.type === "select" || f.type === "multiselect") {
130
+ if (!fuseKeys.includes(f.key)) {
131
+ fuseKeys.push(f.key);
132
+ }
133
+ }
134
+ });
135
+ indexItems(items, Object.assign({ keys: fuseKeys }, config.fuseConfig));
136
+
137
+ return resultAggregation;
138
+ }
139
+
140
+ export default filterApplyMethod;
@@ -0,0 +1,6 @@
1
+ export { default as filterApplyMethod } from "./filter-apply";
2
+ export { default as searchMethod } from "./search";
3
+ export { default as createFilterMethod } from "./create-filter";
4
+ export { default as sortResultsMethod } from "./sort-results";
5
+ export { default as createResetMethod } from "./create-reset";
6
+ export { default as resetFilterMethod } from "./reset-filter";
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Resets all filters in the EOxItemFilter component and triggers a search update.
3
+ *
4
+ * @param {Object} EOxItemFilter - The EOxItemFilter component instance.
5
+ */
6
+ export default function resetFilterMethod(EOxItemFilter) {
7
+ EOxItemFilter.renderRoot
8
+ .querySelectorAll("[data-type='filter']")
9
+ .forEach((filter) => {
10
+ if (typeof filter.reset === "function") filter.reset();
11
+ });
12
+ EOxItemFilter.search();
13
+ }
@@ -0,0 +1,23 @@
1
+ import { filter as filterClient } from "../../helpers/";
2
+ import { filterExternal } from "../../helpers";
3
+
4
+ /**
5
+ * Executes a search based on the provided configuration, items, and filters in the EOxItemFilter component.
6
+ *
7
+ * @param {Object} config - The configuration object for the search.
8
+ * @param {Array<Object>} items - The items to be filtered.
9
+ * @param {Object} EOxItemFilter - The EOxItemFilter component instance.
10
+ * @returns {Promise<void>} A promise that resolves when the search is complete.
11
+ */
12
+ async function searchMethod(config, items, EOxItemFilter) {
13
+ let results;
14
+ if (EOxItemFilter.externalFilter) {
15
+ results = await filterExternal(items, EOxItemFilter.filters, config);
16
+ } else {
17
+ results = await filterClient(items, EOxItemFilter.filters, config);
18
+ }
19
+
20
+ EOxItemFilter.results = EOxItemFilter.sortResults(results);
21
+ }
22
+
23
+ export default searchMethod;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Sorts the provided items based on the title property specified in the configuration.
3
+ *
4
+ * @param {Array<Object>} items - The items to be sorted.
5
+ * @param {Object} config - The configuration object containing sorting options.
6
+ * @returns {Array<Object>} The sorted array of items.
7
+ */
8
+ function sortResultsMethod(items, config) {
9
+ return [...items].sort((a, b) =>
10
+ a[config.titleProperty].localeCompare(b[config.titleProperty]),
11
+ );
12
+ }
13
+
14
+ export default sortResultsMethod;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Filters and aggregates results based on a specified property in the EOxItemFilterResults component.
3
+ *
4
+ * @param {Array<Object>} items - The items to be filtered and aggregated.
5
+ * @param {string} property - The property used for aggregation.
6
+ * @param {Object} EOxItemFilterResults - The EOxItemFilterResults component instance.
7
+ * @returns {Array<Object>} The filtered and aggregated items.
8
+ */
9
+ export function aggregateResultsMethod(items, property, EOxItemFilterResults) {
10
+ return items.filter((item) => {
11
+ // Get the aggregation property from the item
12
+ const aggregation = item[EOxItemFilterResults.config.aggregateResults];
13
+
14
+ // Get the current filter state for the aggregation property
15
+ let currentFilter;
16
+ if (
17
+ EOxItemFilterResults.filters[EOxItemFilterResults.config.aggregateResults]
18
+ ) {
19
+ currentFilter = Object.keys(
20
+ EOxItemFilterResults.filters[
21
+ EOxItemFilterResults.config.aggregateResults
22
+ ],
23
+ ).filter(
24
+ (f) =>
25
+ EOxItemFilterResults.filters[
26
+ EOxItemFilterResults.config.aggregateResults
27
+ ].state[f],
28
+ );
29
+ }
30
+
31
+ // Determine if the property is included in the current filter
32
+ const includedInCurrentFilter = currentFilter?.length
33
+ ? currentFilter.includes(property)
34
+ : true;
35
+
36
+ // Return true if the property is included in the current filter and matches the aggregation
37
+ return includedInCurrentFilter && Array.isArray(aggregation)
38
+ ? aggregation.includes(property)
39
+ : aggregation === property;
40
+ });
41
+ }
42
+
43
+ export default aggregateResultsMethod;
@@ -0,0 +1,111 @@
1
+ import { html, nothing } from "lit";
2
+ import { repeat } from "lit/directives/repeat.js";
3
+ import { when } from "lit/directives/when.js";
4
+ import { unsafeHTML } from "lit/directives/unsafe-html.js";
5
+
6
+ /**
7
+ * Creates the item details view for the aggregation property in the EOxItemFilterResults component.
8
+ *
9
+ * @param {string} aggregationProperty - The property used for aggregation.
10
+ * @param {Object} EOxItemFilterResults - The EOxItemFilterResults component instance.
11
+ * @returns {TemplateResult} The HTML template for the item details view.
12
+ */
13
+ export function createItemDetailsMethod(
14
+ aggregationProperty,
15
+ EOxItemFilterResults,
16
+ ) {
17
+ return html`
18
+ <details
19
+ class="details-results"
20
+ @toggle=${EOxItemFilterResults.handleAccordion}
21
+ ?open=${EOxItemFilterResults.config.expandResults || nothing}
22
+ >
23
+ <summary>
24
+ <span class="title">
25
+ ${aggregationProperty}
26
+ <span class="count"
27
+ >${EOxItemFilterResults.aggregateResults(
28
+ EOxItemFilterResults.results,
29
+ aggregationProperty,
30
+ ).length}</span
31
+ >
32
+ </span>
33
+ </summary>
34
+ <div>
35
+ ${createItemListMethod(aggregationProperty, EOxItemFilterResults)}
36
+ </div>
37
+ </details>
38
+ `;
39
+ }
40
+
41
+ /**
42
+ * Creates the item list view for the aggregation property in the EOxItemFilterResults component.
43
+ *
44
+ * @param {string} aggregationProperty - The property used for aggregation.
45
+ * @param {Object} EOxItemFilterResults - The EOxItemFilterResults component instance.
46
+ * @returns {TemplateResult} The HTML template for the item list view.
47
+ */
48
+ export function createItemListMethod(
49
+ aggregationProperty,
50
+ EOxItemFilterResults,
51
+ ) {
52
+ const results = EOxItemFilterResults.results;
53
+ const items = aggregationProperty
54
+ ? EOxItemFilterResults.aggregateResults(results, aggregationProperty)
55
+ : results;
56
+
57
+ const config = EOxItemFilterResults.config;
58
+
59
+ const className = (item) =>
60
+ EOxItemFilterResults.selectedResult?.[config.idProperty] ===
61
+ item[config.idProperty]
62
+ ? "highlighted"
63
+ : nothing;
64
+
65
+ return html`
66
+ <ul>
67
+ ${repeat(
68
+ items,
69
+ (item) => item.id,
70
+ (item) => html`
71
+ <li class=${className(item)}>
72
+ <span
73
+ id="${item.id}"
74
+ @click=${() => {
75
+ if (EOxItemFilterResults.selectedResult === item) {
76
+ EOxItemFilterResults.selectedResult = null;
77
+ } else {
78
+ EOxItemFilterResults.selectedResult = item;
79
+ }
80
+ EOxItemFilterResults.dispatchEvent(
81
+ new CustomEvent("result", {
82
+ detail: EOxItemFilterResults.selectedResult,
83
+ }),
84
+ );
85
+ }}
86
+ >
87
+ ${when(
88
+ config.subTitleProperty,
89
+ () => html`
90
+ <div class="title-container">
91
+ <span class="title"
92
+ >${unsafeHTML(item[config.titleProperty])}</span
93
+ >
94
+ <span class="subtitle"
95
+ >${unsafeHTML(item[config.subTitleProperty])}</span
96
+ >
97
+ </div>
98
+ `,
99
+ () => html`
100
+ <span class="title"
101
+ >${unsafeHTML(item[config.titleProperty])}</span
102
+ >
103
+ `,
104
+ )}
105
+ </span>
106
+ </li>
107
+ `,
108
+ )}
109
+ </ul>
110
+ `;
111
+ }
@@ -0,0 +1,2 @@
1
+ export { default as aggregateResultsMethod } from "./aggregate-results";
2
+ export { createItemDetailsMethod, createItemListMethod } from "./create";