|
11 | 11 | * or submit itself to any jurisdiction. |
12 | 12 | */ |
13 | 13 |
|
14 | | -import { FilterModel } from '../common/FilterModel.js'; |
15 | 14 | import { ObservableBasedSelectionDropdownModel } from '../../detector/ObservableBasedSelectionDropdownModel.js'; |
16 | 15 |
|
17 | 16 | /** |
18 | 17 | * Return the option value corresponding to a given magnets current level |
19 | 18 | * |
20 | 19 | * @param {MagnetsCurrentLevels} currentLevels the current levels |
21 | | - * @return {string} the option's value |
| 20 | + * @return {object<value: string>} the option's value |
22 | 21 | */ |
23 | | -const magnetsCurrentLevelsToOptionValue = ({ l3, dipole }) => `${l3}kA/${dipole}kA`; |
| 22 | +const magnetsCurrentLevelsToKey = ({ l3, dipole }) => ({ value: `${l3}kA/${dipole}kA` }); |
| 23 | + |
| 24 | +/** |
| 25 | + * Return the magnets current lever based on a key string |
| 26 | + * |
| 27 | + * @param {object} option string containing the current levels |
| 28 | + * @param {string} option.value string containing the current levels |
| 29 | + * @return {MagnetsCurrentLevels} |
| 30 | + */ |
| 31 | +const keyToMagnetsCurrentLevels = (value) => { |
| 32 | + const [l3, dipole] = value.split('/').map((str) => parseFloat(str.slice(0, -2))); |
| 33 | + return { l3, dipole }; |
| 34 | +}; |
24 | 35 |
|
25 | 36 | /** |
26 | 37 | * AliceL3AndDipoleFilteringModel |
27 | 38 | */ |
28 | | -export class MagnetsFilteringModel extends FilterModel { |
| 39 | +export class MagnetsFilteringModel extends ObservableBasedSelectionDropdownModel { |
29 | 40 | /** |
30 | 41 | * Constructor |
31 | 42 | * |
32 | 43 | * @param {ObservableData<RemoteData<MagnetsCurrentLevels[], ApiError>>} magnetsCurrentLevels$ observable remote data of magnets current |
33 | 44 | * levels |
34 | 45 | */ |
35 | 46 | constructor(magnetsCurrentLevels$) { |
36 | | - super(); |
37 | | - this._selectionDropdownModel = new ObservableBasedSelectionDropdownModel( |
38 | | - magnetsCurrentLevels$, |
39 | | - (magnetsCurrentLevels) => ({ value: magnetsCurrentLevelsToOptionValue(magnetsCurrentLevels) }), |
40 | | - { multiple: false }, |
41 | | - ); |
42 | | - this._addSubmodel(this._selectionDropdownModel); |
43 | | - |
44 | | - this._valueToFilteringParamsMap = new Map(); |
45 | | - magnetsCurrentLevels$.observe(() => { |
46 | | - magnetsCurrentLevels$.getCurrent().match({ |
47 | | - |
48 | | - /** |
49 | | - * Fill map indexing current level by their corresponding value |
50 | | - * |
51 | | - * @param {MagnetsCurrentLevels[]} currentLevels the current levels to map |
52 | | - * @return {void} |
53 | | - */ |
54 | | - Success: (currentLevels) => { |
55 | | - this._valueToFilteringParamsMap = new Map(currentLevels.map(({ l3, dipole }) => [ |
56 | | - magnetsCurrentLevelsToOptionValue({ l3, dipole }), |
57 | | - { l3, dipole }, |
58 | | - ])); |
59 | | - }, |
60 | | - Other: () => { |
61 | | - this._valueToFilteringParamsMap = new Map(); |
62 | | - }, |
63 | | - }); |
64 | | - }); |
65 | | - } |
66 | | - |
67 | | - /** |
68 | | - * @inheritDoc |
69 | | - */ |
70 | | - reset() { |
71 | | - this._selectionDropdownModel.reset(); |
72 | | - } |
73 | | - |
74 | | - /** |
75 | | - * @inheritDoc |
76 | | - */ |
77 | | - get isEmpty() { |
78 | | - return this._selectionDropdownModel.isEmpty; |
| 47 | + super(magnetsCurrentLevels$, magnetsCurrentLevelsToKey, { multiple: false }); |
79 | 48 | } |
80 | 49 |
|
81 | 50 | /** |
82 | 51 | * @inheritDoc |
83 | 52 | */ |
84 | 53 | get normalized() { |
85 | | - return this._valueToFilteringParamsMap.get(this._selectionDropdownModel.selected[0]) ?? null; |
| 54 | + const [selectedOption] = this.selected; |
| 55 | + return keyToMagnetsCurrentLevels(selectedOption); |
86 | 56 | } |
87 | 57 |
|
88 | 58 | /** |
89 | | - * Return the underlying selection dropdown model |
| 59 | + * Sets selected options based on a comma-seperated string. |
| 60 | + * Accounts for the options being either RemoteData or an array. |
90 | 61 | * |
91 | | - * @return {SelectionDropdownModel} the dropdown model |
| 62 | + * @return {string} |
92 | 63 | */ |
93 | | - get selectionDropdownModel() { |
94 | | - return this._selectionDropdownModel; |
| 64 | + set normalized(value) { |
| 65 | + super.normalized = magnetsCurrentLevelsToKey(value).value; |
95 | 66 | } |
96 | 67 | } |
0 commit comments