-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumeratedValue.js
70 lines (61 loc) · 1.58 KB
/
EnumeratedValue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* EnumeratedValue class. Wraps a key, value, and map of valid key-value pairs.
*/
export default class EnumeratedValue
{
map;
selected;
updateFunction;
owner;
constructor(map, valueUpdatedFunc=null, owner=null)
{
if (Object.keys(map).length <= 0) {
throw new Error("EnumeratedValue: 'map' argument must contain at minimum 1 key-value pair.");
}
this.owner = owner;
this.map = map;
this.selected = Object.keys(this.map).at(0);
this.updateFunction = valueUpdatedFunc;
}
get value()
{
return this.map[this.selected];
}
/**
* value setter, passes changes to the EnumeratedValue up to it's owner's updateFunction
* so you can define how an object that has this as a member behaves when this value
* changes.
*/
set value(value)
{
this.selected = value;
if (this.owner != null)
this.updateFunction.call(this.owner, this.value);
}
setValue(value)
{
if (!(value in this.map))
throw new Error('Cannot set value of EnumeratedValue to invalid value.');
this.value = value;
}
nextValue()
{
let flag = false;
for (const prop in this.map)
{
if (flag)
{
this.value = prop;
return;
}
else if (prop == this.selected)
{
flag = true;
}
}
if (flag)
{
this.setValue(Object.keys(this.map).at(0));
}
}
}