I created this type for use in a "Wizard"-style user interaction. In each step, the user chooses an option called a Modifier and each of those options contains subselections that must be made.
These Modifiers are stored with two parallel structures, one of the fixed values and a second of the selectable ones using this type. When the uses chooses a suboption, the chosen value is merged into the fixed values and when completed, the values are used to modify the wizard model.
/**
* A portion of a modifier that consists of a set of possible values and a number of them
* that will be selected. After selection, the selected values will be added to the set values
* of the modifier.
*/
export class SelectableModifier {
constructor(numSelectionsNeeded:number, options:Array<any>, selectionTime:string) {
this._numSelectionsNeeded = numSelectionsNeeded;
this._options = options;
this._selectionTime = selectionTime;
}
/**
* The number of options that need to be selected.
*/
private _numSelectionsNeeded:number;
/**
* The available options
*/
private _options:Array<any>;
private _selectionTime:string;
/**
* Choose from this selection, decomposing it into the selected options.
*
* @param chosenIndices
*/
public makeSelection(chosenIndices:Array<Number>):Array<any> {
if (chosenIndices.length != this._numSelectionsNeeded) {
throw "The selection requires that " + this._numSelectionsNeeded + " selections be made but " + chosenIndices.length + " were instead."
}
return this._options.filter((element, index)=> {
return chosenIndices.indexOf(index) !== -1;
});
}
get numSelectionsNeeded():number {
return this._numSelectionsNeeded;
}
get options():Array<any> {
return this._options;
}
get selectionTime():string {
return this._selectionTime;
}
}
any
in favor of using a generic class. \$\endgroup\$