@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,60 @@
1
+ /**
2
+ * Resets the given filter object to its default state.
3
+ *
4
+ * @param {Object} filterObject - The filter object to reset.
5
+ * @returns {Object|null} The reset filter object or null if the filter was not dirty.
6
+ */
7
+ export default function resetFilter(filterObject) {
8
+ // Return null if the filter is not dirty
9
+ if (!filterObject.dirty) {
10
+ return null;
11
+ }
12
+ const filterType = filterObject.type;
13
+
14
+ // Reset the filter state based on its type
15
+ switch (filterType) {
16
+ case "multiselect":
17
+ // For multiselect filters, set all state values to false
18
+ for (const filter in filterObject.state) {
19
+ // eslint-disable-next-line
20
+ if (filterObject.state.hasOwnProperty(filter)) {
21
+ filterObject.state[filter] = false;
22
+ }
23
+ }
24
+ break;
25
+
26
+ case "range":
27
+ // For range filters, reset state to min and max values
28
+ filterObject.state.min = filterObject.min;
29
+ filterObject.state.max = filterObject.max;
30
+ break;
31
+
32
+ case "select":
33
+ // For select filters, set all state values to false
34
+ for (const filter in filterObject.state) {
35
+ // eslint-disable-next-line
36
+ if (filterObject.state.hasOwnProperty(filter)) {
37
+ filterObject.state[filter] = false;
38
+ }
39
+ }
40
+ break;
41
+
42
+ case "spatial":
43
+ // For spatial filters, clear the geometry
44
+ filterObject.state.geometry = undefined;
45
+ break;
46
+
47
+ case "text":
48
+ // For text filters, reset state values to undefined
49
+ filterObject.keys.forEach((key) => {
50
+ filterObject.state[key] = undefined;
51
+ });
52
+ break;
53
+ }
54
+
55
+ // Remove stringifiedState and dirty properties from the filter object
56
+ delete filterObject.stringifiedState;
57
+ delete filterObject.dirty;
58
+
59
+ return filterObject;
60
+ }
@@ -0,0 +1,35 @@
1
+ import booleanIntersects from "@turf/boolean-intersects";
2
+ import booleanWithin from "@turf/boolean-within";
3
+
4
+ /**
5
+ * Checks if the item geometry intersects with the filter geometry.
6
+ *
7
+ * @param {Object} itemGeometry - The geometry of the item to check.
8
+ * @param {Object} filterGeometry - The geometry of the filter to check against.
9
+ * @returns {boolean} True if the item geometry intersects with the filter geometry, otherwise false.
10
+ */
11
+ export const intersects = (itemGeometry, filterGeometry) => {
12
+ // If no filter geometry is provided, assume it intersects
13
+ if (!filterGeometry) {
14
+ return true;
15
+ }
16
+ // Use Turf.js booleanIntersects to check for intersection
17
+ return booleanIntersects(itemGeometry, filterGeometry);
18
+ };
19
+
20
+ /**
21
+ * Checks if the item geometry is within the filter geometry.
22
+ *
23
+ * @param {Object} itemGeometry - The geometry of the item to check.
24
+ * @param {Object} filterGeometry - The geometry of the filter to check against.
25
+ * @returns {boolean} True if the item geometry is within the filter geometry, otherwise false.
26
+ */
27
+ export const within = (itemGeometry, filterGeometry) => {
28
+ // If no filter geometry is provided, assume it is within
29
+ if (!filterGeometry) {
30
+ return true;
31
+ }
32
+
33
+ // Use Turf.js booleanWithin to check if the item is within the filter
34
+ return booleanWithin(itemGeometry, filterGeometry);
35
+ };
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Calculates the tab index based on the main index and sub-index.
3
+ *
4
+ * @param {number} index - The main index.
5
+ * @param {number} subIndex - The sub-index.
6
+ * @returns {number} The calculated tab index.
7
+ */
8
+ export default function getTabIndex(index, subIndex) {
9
+ const totalComponentInAFilter = 2;
10
+
11
+ // Calculate the tab index based on the main index and sub-index
12
+ return index * totalComponentInAFilter + subIndex;
13
+ }
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Handles the toggle event for the accordion.
3
+ *
4
+ * @param {Event} event - The toggle event.
5
+ * @param {Object} config - The configuration object for item-filter.
6
+ * @param {Element} that
7
+ */
8
+ function toggleAccordion(event, config, that) {
9
+ let detailsElement;
10
+
11
+ if (event.detail) {
12
+ detailsElement = event.detail.target;
13
+ } else {
14
+ detailsElement = event.target;
15
+ }
16
+
17
+ if (detailsElement?.classList.contains("details-filter")) {
18
+ if (!detailsElement.open || config?.expandMultipleFilters) return;
19
+
20
+ that.shadowRoot
21
+ .querySelectorAll("eox-itemfilter-expandcontainer")
22
+ .forEach((container) => {
23
+ const details = container.shadowRoot.querySelector(".details-filter");
24
+ if (details && details !== detailsElement) {
25
+ details.removeAttribute("open");
26
+ }
27
+ });
28
+ } else {
29
+ if (!detailsElement?.open || config?.expandMultipleResults) return;
30
+
31
+ that.querySelectorAll("details").forEach((details) => {
32
+ if (details !== detailsElement) {
33
+ details.removeAttribute("open");
34
+ }
35
+ });
36
+ }
37
+ }
38
+
39
+ export default toggleAccordion;
package/src/main.js ADDED
@@ -0,0 +1,466 @@
1
+ import { LitElement, html, nothing } from "lit";
2
+ import { map } from "lit/directives/map.js";
3
+ import { when } from "lit/directives/when.js";
4
+ import { style } from "./style";
5
+ import { styleEOX } from "./style.eox";
6
+
7
+ import allStyle from "@eox/elements-utils/styles/dist/all.style";
8
+ import "./components/expand-container";
9
+ import "./components/itemfilter-container";
10
+ import "./components/filters/text";
11
+ import "./components/results";
12
+ import "./components/filters/selector";
13
+ import "./components/filters/range";
14
+ import "./components/filters/spatial";
15
+ import "./components/chips";
16
+ import { ELEMENT_CONFIG, ELEMENT_PROPERTIES } from "./enums";
17
+ import {
18
+ filterApplyMethod,
19
+ searchMethod,
20
+ createFilterMethod,
21
+ sortResultsMethod,
22
+ createResetMethod,
23
+ resetFilterMethod,
24
+ } from "./methods/itemfilter";
25
+ import _debounce from "lodash.debounce";
26
+ import {
27
+ getTabIndex,
28
+ isFiltersDirty,
29
+ toggleAccordion,
30
+ } from "./helpers/index.js";
31
+
32
+ /**
33
+ * EOxItemFilter is a custom web component that provides a comprehensive item filtering system.
34
+ * It includes methods for applying filters, searching, sorting results, and resetting filters.
35
+ * The component supports both inline and dropdown modes for filter display.
36
+ *
37
+ * @module EOxItemFilter
38
+ * @extends {LitElement}
39
+ */
40
+ export class EOxItemFilter extends LitElement {
41
+ // Define properties with defaults and types
42
+ static get properties() {
43
+ return {
44
+ items: { attribute: false, type: Object },
45
+ results: { state: true, attribute: false, type: Object },
46
+ filters: { state: true, attribute: false, type: Object },
47
+ selectedResult: { attribute: false, type: Object },
48
+ aggregateResults: { attribute: "aggregate-results", type: String },
49
+ autoSpreadSingle: { attribute: "auto-spread-single", type: Boolean },
50
+ enableHighlighting: { attribute: "enable-highlighting", type: Boolean },
51
+ filterProperties: { attribute: false, type: Array },
52
+ fuseConfig: { attribute: false, type: Object },
53
+ inlineMode: { attribute: "inline-mode", type: Boolean },
54
+ matchAllWhenEmpty: { attribute: "match-all-when-empty", type: Boolean },
55
+ showResults: { attribute: "show-result", type: Boolean },
56
+ idProperty: { attribute: "id-property", type: String },
57
+ titleProperty: { attribute: "title-property", type: String },
58
+ subTitleProperty: { attribute: "sub-title-property", type: String },
59
+ expandMultipleFilters: {
60
+ attribute: "enable-multiple-filter",
61
+ type: Boolean,
62
+ },
63
+ expandResults: { attribute: "expand-result", type: Boolean },
64
+ expandMultipleResults: {
65
+ attribute: "expand-multiple-results",
66
+ type: Boolean,
67
+ },
68
+ externalFilter: { attribute: false, type: Function },
69
+ unstyled: { type: Boolean },
70
+ };
71
+ }
72
+
73
+ /**
74
+ * @type Array<string>
75
+ */
76
+ #resultAggregation = [];
77
+
78
+ /**
79
+ * @type Array<object>
80
+ */
81
+ #items = [];
82
+
83
+ /**
84
+ * @type Object
85
+ */
86
+ #config = ELEMENT_CONFIG;
87
+
88
+ constructor() {
89
+ super();
90
+
91
+ /** The items to be filtered
92
+ * @type Array<Object>
93
+ */
94
+ this.items = null;
95
+
96
+ /**
97
+ * The state object containing the applied filters.
98
+ * @type Object
99
+ */
100
+ this.filters = {};
101
+
102
+ /**
103
+ * The state object containing the filtered results.
104
+ * @type Array<Object>
105
+ */
106
+ this.results = [];
107
+
108
+ /**
109
+ * The currently selected result
110
+ * @type Object
111
+ */
112
+ this.selectedResult = null;
113
+
114
+ /**
115
+ * @type Function
116
+ * @private
117
+ */
118
+ this.search = _debounce(this.searchHandler, 100, {
119
+ leading: true,
120
+ });
121
+
122
+ /**
123
+ * Aggregate results by a property key
124
+ *
125
+ * @type String
126
+ */
127
+ this.aggregateResults = undefined;
128
+
129
+ /**
130
+ * Automatically spread single item summaries
131
+ * removing the summary header
132
+ *
133
+ * @type Boolean
134
+ */
135
+ this.autoSpreadSingle = false;
136
+
137
+ /**
138
+ * Highlighting of search result character matches
139
+ *
140
+ * @type Boolean
141
+ */
142
+ this.enableHighlighting = false;
143
+
144
+ /**
145
+ * Use an external search endpoint instead of fuse search.
146
+ * Passed properties: input string, filters object
147
+ *
148
+ * @type Function
149
+ */
150
+ this.externalFilter = null;
151
+
152
+ /**
153
+ * The filter properties.
154
+ * @type Array<{key?: String, keys?: Array<String>, title: String, type: String, placeholder?: String, format?: String, state?: Object}>
155
+ */
156
+ this.filterProperties = [];
157
+
158
+ /**
159
+ * Native fuse.js config override
160
+ *
161
+ * @type Object
162
+ */
163
+ this.fuseConfig = {};
164
+
165
+ /**
166
+ * Inline mode, for rendering the itemfilter in a very condensed space.
167
+ * Expects showResults to be false
168
+ *
169
+ * @type Boolean
170
+ */
171
+ this.inlineMode = false;
172
+
173
+ /**
174
+ * Show all result items if nothing is input by the user
175
+ *
176
+ * @type Boolean
177
+ */
178
+ this.matchAllWhenEmpty = true;
179
+
180
+ /**
181
+ * Display results list
182
+ *
183
+ * @type Boolean
184
+ */
185
+ this.showResults = true;
186
+
187
+ /**
188
+ * The property of the result items used for display
189
+ *
190
+ * @type String
191
+ */
192
+ this.titleProperty = "title";
193
+
194
+ /**
195
+ * The property of the result items used for a subtitle
196
+ *
197
+ * @type String
198
+ */
199
+ this.subTitleProperty = undefined;
200
+
201
+ /**
202
+ * Unique id property of items
203
+ *
204
+ * @type String
205
+ */
206
+ this.idProperty = "id";
207
+
208
+ /**
209
+ * Allow opening multiple filter accordions in parallel
210
+ *
211
+ * @type Boolean
212
+ */
213
+ this.expandMultipleFilters = true;
214
+
215
+ /**
216
+ * Initialize result accordions expanded
217
+ *
218
+ * @type Boolean
219
+ */
220
+ this.expandResults = true;
221
+ /**
222
+ * Allow opening multiple result accordions in parallel
223
+ *
224
+ * @type Boolean
225
+ */
226
+ this.expandMultipleResults = true;
227
+
228
+ /**
229
+ * Render the element without additional styles
230
+ */
231
+ this.unstyled = false;
232
+ }
233
+
234
+ /**
235
+ * Applies the filters to the items and updates the result aggregation.
236
+ */
237
+ apply() {
238
+ this.#resultAggregation = filterApplyMethod(
239
+ this.#config,
240
+ this.#items,
241
+ this,
242
+ );
243
+ this.search();
244
+ }
245
+
246
+ /**
247
+ * Performs a search based on the current configuration and items.
248
+ * Requests an update after the search completes.
249
+ */
250
+ async searchHandler() {
251
+ await searchMethod(this.#config, this.#items, this);
252
+ /**
253
+ * Fires when the filters are changed; event detail includes `filters` and `results`
254
+ */
255
+ this.dispatchEvent(
256
+ new CustomEvent("filter", {
257
+ detail: {
258
+ results: this.results,
259
+ filters: this.filters,
260
+ },
261
+ bubbles: true,
262
+ composed: true,
263
+ }),
264
+ );
265
+ if (this.inlineMode)
266
+ this.renderRoot.querySelector("eox-itemfilter-container").updateInline();
267
+ this.requestUpdate();
268
+ }
269
+
270
+ /**
271
+ * Sorts the given items based on the current configuration.
272
+ *
273
+ * @param {Array<object>} items - The items to be sorted.
274
+ * @returns {Array<object>} - The sorted items.
275
+ */
276
+ sortResults(items) {
277
+ return sortResultsMethod(items, this.#config);
278
+ }
279
+
280
+ /**
281
+ * Creates a filter element based on the given filter object and tab index.
282
+ *
283
+ * @param {Object} filterObject - The filter object.
284
+ * @param {number} tabIndex - The tab index for the filter element.
285
+ * @returns {import("lit")} - The template result for the filter element.
286
+ * @private
287
+ */
288
+ #createFilter(filterObject, tabIndex) {
289
+ return createFilterMethod(filterObject, tabIndex, this);
290
+ }
291
+
292
+ /**
293
+ * Creates a reset element for the given filter object and tab index.
294
+ *
295
+ * @param {Object} filterObject - The filter object.
296
+ * @param {number} tabIndex - The tab index for the reset element.
297
+ * @returns {import("lit")} - The template result for the filter element.
298
+ * @private
299
+ */
300
+ #createReset(filterObject, tabIndex) {
301
+ return createResetMethod(filterObject, tabIndex, this);
302
+ }
303
+
304
+ /**
305
+ * Resets all applied filters.
306
+ */
307
+ resetFilters() {
308
+ resetFilterMethod(this);
309
+ }
310
+
311
+ /**
312
+ * Lifecycle method called after the first update.
313
+ * Initializes the configuration and items, and applies the filters.
314
+ *
315
+ * @param {Map} _changedProperties - The changed properties.
316
+ */
317
+ firstUpdated(_changedProperties) {
318
+ let config = {};
319
+ ELEMENT_PROPERTIES.map((property) => {
320
+ config = {
321
+ ...config,
322
+ [property]: this[property],
323
+ };
324
+ });
325
+ this.#config = config;
326
+ this.#items =
327
+ this.items?.map((i, index) =>
328
+ Object.assign({ id: i[this.idProperty] || `item-${index}` }, i),
329
+ ) || [];
330
+ this.apply();
331
+ }
332
+
333
+ /**
334
+ * Called when the element is updated.
335
+ * Re-Initialize itemfilter when `config` and `items` changes.
336
+ *
337
+ * @param {Map} changedProperties - The properties that have changed.
338
+ */
339
+ updated(changedProperties) {
340
+ ELEMENT_PROPERTIES.map((property) => {
341
+ if (changedProperties.has(property)) {
342
+ this.firstUpdated();
343
+ return true;
344
+ }
345
+ });
346
+ }
347
+
348
+ /**
349
+ * Updates selected result when result component triggers the "result" event.
350
+ *
351
+ * @param {{detail: Object}} evt - "result" event triggered by result component
352
+ */
353
+ updateResult(evt) {
354
+ this.selectedResult = evt.detail;
355
+ /**
356
+ * Fires when a result is selected; event detail is `selectedResult`
357
+ */
358
+ this.dispatchEvent(
359
+ new CustomEvent("select", {
360
+ detail: this.selectedResult,
361
+ bubbles: true,
362
+ composed: true,
363
+ }),
364
+ );
365
+ this.requestUpdate();
366
+ }
367
+
368
+ /**
369
+ * Renders the HTML template for the component.
370
+ */
371
+ render() {
372
+ return html`
373
+ <style>
374
+ ${style}
375
+ ${!this.unstyled && styleEOX}
376
+ ${!this.unstyled && allStyle}
377
+ ${this.styleOverride}
378
+ </style>
379
+ <form
380
+ id="itemfilter"
381
+ class=${this.inlineMode ? "inline" : nothing}
382
+ @submit="${(evt) => evt.preventDefault()}"
383
+ >
384
+ ${when(
385
+ this.filterProperties,
386
+ () => html`
387
+ <eox-itemfilter-container
388
+ .filters=${this.filters}
389
+ .filterProperties=${this.filterProperties}
390
+ .inlineMode=${this.inlineMode || false}
391
+ @reset=${() => this.resetFilters()}
392
+ @filter=${() => this.search()}
393
+ >
394
+ <section slot="section">
395
+ ${when(
396
+ !this.inlineMode,
397
+ () => html`
398
+ <slot name="filterstitle"
399
+ ><h6 class="main-heading">Filters</h6></slot
400
+ >
401
+ `,
402
+ )}
403
+ <ul id="filters">
404
+ ${map(
405
+ Object.values(this.filters),
406
+ (filterObject, index) =>
407
+ html` <li>
408
+ <eox-itemfilter-expandcontainer
409
+ .filterObject=${filterObject}
410
+ @details-toggled=${(e) =>
411
+ toggleAccordion(e, this.#config, this)}
412
+ data-details="${filterObject.key}"
413
+ >
414
+ ${this.#createReset(
415
+ filterObject,
416
+ getTabIndex(index, 1),
417
+ )}
418
+ ${this.#createFilter(
419
+ filterObject,
420
+ getTabIndex(index, 2),
421
+ )}
422
+ </eox-itemfilter-expandcontainer>
423
+ </li>`,
424
+ )}
425
+ </ul>
426
+ ${when(
427
+ !this.inlineMode &&
428
+ this.#config.filterProperties &&
429
+ !this.inlineMode &&
430
+ this.#config.filterProperties &&
431
+ isFiltersDirty(this.filters),
432
+ () => html`
433
+ <button
434
+ type="button"
435
+ id="filter-reset"
436
+ class="outline small icon-text reset-icon"
437
+ data-cy="filter-reset"
438
+ @click=${() => this.resetFilters()}
439
+ >
440
+ Reset all
441
+ </a>
442
+ `,
443
+ )}
444
+ </section>
445
+ </eox-itemfilter-container>
446
+ `,
447
+ )}
448
+ ${when(
449
+ this.#config?.showResults && this.results,
450
+ () => html`
451
+ <eox-itemfilter-results
452
+ .config=${this.#config}
453
+ .results=${this.results}
454
+ .filters=${this.filters}
455
+ .resultAggregation=${this.#resultAggregation}
456
+ .selectedResult=${this.selectedResult}
457
+ @result=${this.updateResult}
458
+ ></eox-itemfilter-results>
459
+ `,
460
+ )}
461
+ </form>
462
+ `;
463
+ }
464
+ }
465
+
466
+ customElements.define("eox-itemfilter", EOxItemFilter);