@@ -14,11 +14,8 @@ import { Observable, RemoteData } from '/js/src/index.js';
1414import { SelectionModel } from './SelectionModel.js' ;
1515
1616/**
17- * @typedef SelectionOption A picker option, with the actual value and its string representation
18- * @property {number|string } value The id of the object this is used to see if it is checked.
19- * @property {Component } [label] The representation of the option (if null, value is used as label)
20- * @property {string } [rawLabel] The string only representation of the option, useful if the label is not a string
21- * @property {string } [selector] If the value of the option is not a valid CSS, this is used to define option's id
17+ * @typedef {SelectionModelConfiguration } FilterableRemoteSelectionModelConfiguration
18+ * @property {RemoteData<SelectionOption[], *> } [availableOptions=[]] the list of available options
2219 */
2320
2421/**
@@ -27,7 +24,7 @@ import { SelectionModel } from './SelectionModel.js';
2724export class FilterableRemoteSelectionModel extends Observable {
2825 /**
2926 * Constructor
30- * @param {SelectionModelConfiguration } [configuration] the model's configuration
27+ * @param {FilterableRemoteSelectionModelConfiguration } [configuration] the model's configuration
3128 */
3229 constructor ( configuration ) {
3330 super ( ) ;
@@ -47,6 +44,7 @@ export class FilterableRemoteSelectionModel extends Observable {
4744
4845 this . _visualChange$ = new Observable ( ) ;
4946
47+ this . _selectionModel = RemoteData . notAsked ( ) ;
5048 this . setAvailableOptions ( availableOptions ) ;
5149 }
5250
@@ -65,7 +63,7 @@ export class FilterableRemoteSelectionModel extends Observable {
6563 * @return {void }
6664 */
6765 reset ( ) {
68- this . selectionModel . match ( {
66+ this . _selectionModel . match ( {
6967 Success : ( selectionModel ) => selectionModel . reset ( ) ,
7068 Other : ( ) => {
7169 // Do nothing
@@ -80,7 +78,7 @@ export class FilterableRemoteSelectionModel extends Observable {
8078 * @return {boolean } true if the selection is empty
8179 */
8280 get isEmpty ( ) {
83- return this . selectionModel . match ( {
81+ return this . _selectionModel . match ( {
8482 Success : ( selectionModel ) => selectionModel . isEmpty ,
8583 Other : ( ) => true ,
8684 } ) ;
@@ -124,17 +122,17 @@ export class FilterableRemoteSelectionModel extends Observable {
124122 multiple : this . _multiple ,
125123 allowEmpty : this . _allowEmpty ,
126124 } ) ;
127- this . selectionModel = RemoteData . success ( selectionModel ) ;
125+ this . _selectionModel = RemoteData . success ( selectionModel ) ;
128126 selectionModel . bubbleTo ( this ) ;
129127 } ,
130128 Loading : ( ) => {
131- this . selectionModel = RemoteData . loading ( ) ;
129+ this . _selectionModel = RemoteData . loading ( ) ;
132130 } ,
133131 Failure : ( error ) => {
134- this . selectionModel = RemoteData . failure ( error ) ;
132+ this . _selectionModel = RemoteData . failure ( error ) ;
135133 } ,
136134 NotAsked : ( ) => {
137- this . selectionModel = RemoteData . notAsked ( ) ;
135+ this . _selectionModel = RemoteData . notAsked ( ) ;
138136 } ,
139137 } ) ;
140138
@@ -144,28 +142,31 @@ export class FilterableRemoteSelectionModel extends Observable {
144142 /**
145143 * Return the **values** of the currently selected options
146144 *
147- * Do not use this getter to modify the selected list but use the `selected` setter to define the new selected list and to notify observers
148- *
149145 * @return {string[]|number[] } the values of the selected options
150146 */
151147 get selected ( ) {
152- return this . selectionModel . match ( {
148+ return this . _selectionModel . match ( {
153149 Success : ( selectionModel ) => selectionModel . selected ,
154150 Other : ( ) => [ ] ,
155151 } ) ;
156152 }
157153
158154 /**
159- * If the selection allows one and only one selection, current will return the currently selected option. In any other case it will throw an
160- * error
161- *
162- * @return {string|number } the current selection
155+ * Return the list of available options
163156 */
164- get current ( ) {
165- if ( this . _allowEmpty || this . _multiple ) {
166- throw new Error ( '"current" is available only in non-multiple select that do not allow empty value' ) ;
167- }
157+ get options ( ) {
158+ return this . _selectionModel . match ( {
159+ Success : ( selectionModel ) => selectionModel . options ,
160+ Other : ( ) => [ ] ,
161+ } ) ;
162+ }
168163
169- return this . selected [ 0 ] ;
164+ /**
165+ * Return the underlying selection model
166+ *
167+ * @return {RemoteData<SelectionModel, ApiError> } the selection model
168+ */
169+ get selectionModel ( ) {
170+ return this . _selectionModel ;
170171 }
171172}
0 commit comments