-
Notifications
You must be signed in to change notification settings - Fork 0
/
form_value.js
63 lines (52 loc) · 1.47 KB
/
form_value.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
'use strict';
const NamedObjectMap = require('./named_object_map');
class FormValue {
constructor(form, value) {
this.form = form;
this.fieldValues = new NamedObjectMap();
if (value === true) {
this.isValid = true;
this.state = 'Rejected';
for (const field of this.form.fields) {
this.constructFieldValues({}, field);
}
} else if (value === false) {
this.isValid = true;
this.state = 'NotAttached';
for (const field of this.form.fields) {
this.constructFieldValues({}, field);
}
} else {
this.state = 'Filled';
for (const field of this.form.fields) {
this.constructFieldValues(value, field);
}
this.isValid = [...this.fieldValues.values()].
every(fieldValue => fieldValue.isValid);
}
Object.freeze(this);
}
constructFieldValues(rawValue, field) {
const fieldValue = field.constructValue(rawValue[field.name]);
this.fieldValues.add(fieldValue);
fieldValue.insertionFields.
map(insertionField =>
this.constructFieldValues(rawValue, insertionField));
}
get name() {
return this.form.name;
}
get value() {
if (this.state === 'NotAttached') {
return null;
} else if (this.state === 'Rejected') {
return true;
}
const value = {};
for (const fieldValue of this.fieldValues.values()) {
value[fieldValue.name] = fieldValue.value;
}
return value;
}
}
module.exports = FormValue;