Skip to content

Commit 7db974b

Browse files
author
NarrowsProjects
committed
feat: add normalize setter to SelectionModel and FilteringModel
1 parent ca5d12b commit 7db974b

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

lib/public/components/Filters/common/FilterModel.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ export class FilterModel extends Observable {
5757
throw new Error('Abstract function call');
5858
}
5959

60+
/**
61+
* Sets filters from normalised values to submodels in needed.
62+
*
63+
* @param {string|number|object|string[]|number[]|null} _value The value used to set filters
64+
* @return {void} the normalized value
65+
* @abstract
66+
*/
67+
set normalized(_value) {
68+
throw new Error('Abstract function call');
69+
}
70+
6071
/**
6172
* Returns the observable notified any time there is a visual change which has no impact on the actual filter value
6273
*

lib/public/components/common/selection/SelectionModel.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ export class SelectionModel extends Observable {
4242
super();
4343
const { availableOptions = [], defaultSelection = [], multiple = true, allowEmpty = true } = configuration || {};
4444

45+
/**
46+
* @type {SelectionOption[]}
47+
* @protected
48+
*/
49+
this._selectionBacklog = [];
50+
4551
/**
4652
* @type {RemoteData<SelectionOption[]>|SelectionOption[]}
4753
* @protected
@@ -243,14 +249,20 @@ export class SelectionModel extends Observable {
243249
}
244250

245251
/**
246-
* Defines the list of available options
252+
* Defines the list of available options and if there is a selection backlog, these will be applied
247253
*
248254
* @param {RemoteData<SelectionOption[], *>|SelectionOption[]} availableOptions the new available options
249255
* @return {void}
250256
*/
251257
setAvailableOptions(availableOptions) {
252258
this._availableOptions = availableOptions;
253259
this.visualChange$.notify();
260+
261+
if (this._selectionBacklog.length) {
262+
this.selectedOptions = this._selectionBacklog;
263+
this._selectionBacklog = [];
264+
this.notify();
265+
}
254266
}
255267

256268
/**
@@ -315,12 +327,19 @@ export class SelectionModel extends Observable {
315327
}
316328

317329
/**
318-
* Define (overrides) the list of currently selected options
330+
* Define (overrides) the list of currently selected options.
331+
* Invalid selection options are excluded
319332
*
320333
* @param {SelectionOption[]} selected the list of selected options
321334
*/
322335
set selectedOptions(selected) {
323-
this._selectedOptions = selected;
336+
let { options } = this;
337+
338+
if (this.options instanceof RemoteData) {
339+
options = options.isSuccess() ? options.payload : [];
340+
}
341+
342+
this._selectedOptions = options.filter((option) => selected.some(({ value }) => String(value) === String(option.value)));;
324343
}
325344

326345
/**
@@ -332,6 +351,23 @@ export class SelectionModel extends Observable {
332351
return this._defaultSelection;
333352
}
334353

354+
/**
355+
* Sets selected options based on a comma-seperated string.
356+
* Accounts for the options being either RemoteData or an array.
357+
*
358+
* @return {string}
359+
*/
360+
set normalized(value) {
361+
const options = value.split(',').map((option) => ({ value: option }));
362+
const postponeSelection = this.options instanceof RemoteData || ! this.options?.length;
363+
364+
if (postponeSelection) {
365+
this._selectionBacklog = options;
366+
} else {
367+
this.selectedOptions = options;
368+
}
369+
}
370+
335371
/**
336372
* Returns the normalized value of the selection
337373
*

0 commit comments

Comments
 (0)