|
1 | 1 | import Component from '@glimmer/component'; |
2 | | -import { tracked } from '@glimmer/tracking'; |
3 | 2 | import { inject as service } from '@ember/service'; |
4 | | -import { set, action } from '@ember/object'; |
5 | | -import { filter, gt } from '@ember/object/computed'; |
| 3 | +import { tracked } from '@glimmer/tracking'; |
| 4 | +import { action } from '@ember/object'; |
6 | 5 | import { isArray } from '@ember/array'; |
7 | | -import { later } from '@ember/runloop'; |
8 | 6 | import getUrlParam from '../utils/get-url-param'; |
9 | 7 |
|
10 | 8 | export default class FiltersPickerComponent extends Component { |
11 | | - /** |
12 | | - * Inject router to handle param changes via `transitionTo` |
13 | | - * |
14 | | - * @memberof FiltersPickerComponent |
15 | | - */ |
16 | 9 | @service hostRouter; |
17 | | - |
18 | | - /** |
19 | | - * Array of filters created from columns argument. |
20 | | - * |
21 | | - * @memberof FiltersPickerComponent |
22 | | - */ |
23 | 10 | @tracked filters = []; |
24 | 11 |
|
25 | | - /** |
26 | | - * Filters which are active and should be applied. |
27 | | - * |
28 | | - * @memberof FiltersPickerComponent |
29 | | - */ |
30 | | - @filter('filters.@each.isFilterActive', (filter) => filter.isFilterActive === true) activeFilters; |
31 | | - |
32 | | - /** |
33 | | - * Computed property that determines if any filters are set. |
34 | | - * |
35 | | - * @memberof FiltersPickerComponent |
36 | | - */ |
37 | | - @gt('activeFilters.length', 0) hasFilters; |
38 | | - |
39 | | - /** |
40 | | - * Creates an instance of FiltersPickerComponent. |
41 | | - * @memberof FiltersPickerComponent |
42 | | - */ |
| 12 | + get activeFilters() { |
| 13 | + return this.filters.filter((f) => f.isFilterActive); |
| 14 | + } |
| 15 | + |
| 16 | + get hasFilters() { |
| 17 | + return this.activeFilters.length > 0; |
| 18 | + } |
| 19 | + |
43 | 20 | constructor() { |
44 | 21 | super(...arguments); |
45 | | - this.updateFilters(); |
| 22 | + |
| 23 | + this.#rebuildFilters(); // initial state |
| 24 | + |
| 25 | + // Refresh whenever the route (→ query-params) changes |
| 26 | + this._routeHandler = () => this.#rebuildFilters(); |
| 27 | + this.hostRouter.on('routeDidChange', this._routeHandler); |
46 | 28 | } |
47 | 29 |
|
48 | | - /** |
49 | | - * Creates and updates filters via map |
50 | | - * |
51 | | - * @param {null|Function} onColumn |
52 | | - * @memberof FiltersPickerComponent |
53 | | - */ |
54 | | - @action updateFilters(onColumn) { |
55 | | - this.filters = this.args.columns |
56 | | - .filter((column) => column.filterable) |
57 | | - .map((column, trueIndex) => { |
58 | | - // add true index to column |
59 | | - column = { ...column, trueIndex }; |
60 | | - |
61 | | - // set the column param |
62 | | - column.param = column.filterParam ?? column.valuePath; |
63 | | - |
64 | | - // get the active param if any and update filter |
65 | | - const activeParam = getUrlParam(column.param); |
66 | | - |
67 | | - // update if an activeParam exists |
68 | | - if (activeParam) { |
69 | | - column.isFilterActive = true; |
70 | | - |
71 | | - if (isArray(activeParam) && activeParam.length === 0) { |
72 | | - column.isFilterActive = false; |
73 | | - } |
74 | | - |
75 | | - column.filterValue = activeParam; |
76 | | - } |
| 30 | + willDestroy() { |
| 31 | + super.willDestroy(...arguments); |
| 32 | + this.hostRouter.off('routeDidChange', this._routeHandler); |
| 33 | + } |
| 34 | + |
| 35 | + #readUrlValue(param) { |
| 36 | + const raw = getUrlParam(param); // string | string[] | undefined |
| 37 | + if (isArray(raw)) { |
| 38 | + return raw.length ? raw : undefined; |
| 39 | + } |
| 40 | + return raw === '' ? undefined : raw; |
| 41 | + } |
| 42 | + |
| 43 | + #rebuildFilters(onColumn) { |
| 44 | + const cols = this.args.columns ?? []; |
| 45 | + |
| 46 | + this.filters = cols |
| 47 | + .filter((c) => c.filterable) |
| 48 | + .map((column, index) => { |
| 49 | + const param = column.filterParam ?? column.valuePath; |
| 50 | + const value = this.#readUrlValue(param); |
| 51 | + const active = value != null; // null & undefined only |
| 52 | + |
| 53 | + const filterCol = { |
| 54 | + ...column, |
| 55 | + trueIndex: index, |
| 56 | + param, |
| 57 | + filterValue: value, |
| 58 | + isFilterActive: active, |
| 59 | + }; |
77 | 60 |
|
78 | | - // callback to modify column from hook |
79 | 61 | if (typeof onColumn === 'function') { |
80 | | - onColumn(column, trueIndex, activeParam); |
| 62 | + onColumn(filterCol, index, value); |
81 | 63 | } |
82 | 64 |
|
83 | | - return column; |
| 65 | + return filterCol; |
84 | 66 | }); |
85 | 67 |
|
86 | 68 | return this; |
87 | 69 | } |
88 | 70 |
|
89 | | - /** |
90 | | - * Triggers the apply callback for the filters picker. |
91 | | - * |
92 | | - * @memberof FiltersPickerComponent |
93 | | - */ |
94 | 71 | @action applyFilters() { |
95 | | - const { onApply } = this.args; |
96 | | - |
97 | | - // run `onApply()` callback |
98 | | - if (typeof onApply === 'function') { |
99 | | - onApply(); |
| 72 | + if (typeof this.args.onApply === 'function') { |
| 73 | + this.args.onApply(); |
100 | 74 | } |
101 | | - |
102 | | - // manually run update filters after apply with slight 300ms delay to update activeFilters |
103 | | - later( |
104 | | - this, |
105 | | - () => { |
106 | | - this.updateFilters(); |
107 | | - }, |
108 | | - 150 |
109 | | - ); |
110 | 75 | } |
111 | 76 |
|
112 | | - /** |
113 | | - * Updates an individual filter/column value. |
114 | | - * |
115 | | - * @param {String} key |
116 | | - * @param {*} value |
117 | | - * @memberof FiltersPickerComponent |
118 | | - */ |
119 | 77 | @action updateFilterValue({ param }, value) { |
120 | | - const { onChange } = this.args; |
121 | | - |
122 | | - // run `onChange()` callback |
123 | | - if (typeof onChange === 'function') { |
124 | | - onChange(param, value); |
| 78 | + if (typeof this.args.onChange === 'function') { |
| 79 | + this.args.onChange(param, value); |
125 | 80 | } |
126 | 81 | } |
127 | 82 |
|
128 | | - /** |
129 | | - * Callback to clear a single filter/column value. |
130 | | - * |
131 | | - * @param {String} key |
132 | | - * @memberof FiltersPickerComponent |
133 | | - */ |
134 | 83 | @action clearFilterValue({ param }) { |
135 | | - const { onFilterClear } = this.args; |
136 | | - |
137 | | - // update filters |
138 | | - this.updateFilters((column) => { |
139 | | - if (column.param !== param) { |
140 | | - return; |
141 | | - } |
142 | | - |
143 | | - // clear column values |
144 | | - set(column, 'filterValue', undefined); |
145 | | - set(column, 'isFilterActive', false); |
146 | | - }); |
147 | | - |
148 | | - // run `onFilterClear()` callback |
149 | | - if (typeof onFilterClear === 'function') { |
150 | | - onFilterClear(param); |
| 84 | + if (typeof this.args.onFilterClear === 'function') { |
| 85 | + this.args.onFilterClear(param); |
151 | 86 | } |
152 | 87 | } |
153 | 88 |
|
154 | | - /** |
155 | | - * Used to clear all filter/column values and URL params. |
156 | | - * |
157 | | - * @memberof FiltersPickerComponent |
158 | | - */ |
159 | | - @action clearFilters() { |
160 | | - const { onClear } = this.args; |
161 | | - const currentRouteName = this.hostRouter.currentRouteName; |
162 | | - const currentQueryParams = { ...this.hostRouter.currentRoute.queryParams }; |
163 | | - |
164 | | - // update filters |
165 | | - this.updateFilters((column) => { |
166 | | - const paramKey = column.filterParam ?? column.valuePath; |
167 | | - delete currentQueryParams[paramKey]; |
168 | | - delete currentQueryParams[`${paramKey}[]`]; |
169 | | - |
170 | | - // reset column values |
171 | | - set(column, 'filterValue', undefined); |
172 | | - set(column, 'isFilterActive', false); |
173 | | - }); |
174 | | - |
175 | | - // transition to cleared params with router service |
176 | | - this.hostRouter.transitionTo(currentRouteName, { queryParams: currentQueryParams }); |
177 | | - |
178 | | - // run `onClear()` callback |
179 | | - if (typeof onClear === 'function') { |
180 | | - onClear(...arguments); |
| 89 | + @action async clearFilters(...args) { |
| 90 | + if (typeof this.args.onClear === 'function') { |
| 91 | + this.args.onClear(...args); |
| 92 | + } |
| 93 | + |
| 94 | + // Build a clean query-param bag |
| 95 | + const qp = { ...this.hostRouter.currentRoute.queryParams }; |
| 96 | + (this.args.columns ?? []) |
| 97 | + .filter((c) => c.filterable) |
| 98 | + .forEach((c) => { |
| 99 | + const key = c.filterParam ?? c.valuePath; |
| 100 | + delete qp[key]; |
| 101 | + delete qp[`${key}[]`]; |
| 102 | + }); |
| 103 | + |
| 104 | + // Transition – routeDidChange listener will rebuild afterwards |
| 105 | + try { |
| 106 | + await this.hostRouter.transitionTo(this.hostRouter.currentRouteName, { |
| 107 | + queryParams: qp, |
| 108 | + }); |
| 109 | + } catch (error) { |
| 110 | + // Ignore only the "transition aborted" case |
| 111 | + if (error?.name !== 'TransitionAborted') { |
| 112 | + throw error; // real error → rethrow |
| 113 | + } |
181 | 114 | } |
182 | 115 | } |
183 | 116 | } |
0 commit comments