/
document.js
257 lines (220 loc) · 6.77 KB
/
document.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import EmberObject, { computed } from "@ember/object";
import { assert } from "@ember/debug";
import { getOwner } from "@ember/application";
import Field from "ember-caluma/lib/field";
import jexl from "jexl";
import { atob } from "ember-caluma/helpers/atob";
import { inject as service } from "@ember/service";
import { intersects } from "ember-caluma/utils/jexl";
const getParentState = childStates => {
if (childStates.every(state => state === "untouched")) {
return "untouched";
}
if (childStates.some(state => state === "invalid")) {
return "invalid";
}
return childStates.every(state => state === "valid") ? "valid" : "unfinished";
};
/**
* Object which represents a document
*
* @class Document
*/
export default EmberObject.extend({
documentStore: service(),
async init() {
this._super(...arguments);
assert("The raw document `raw` must be passed", this.raw);
const fields = this.buildFields(this.raw);
fields.forEach(field => this.fields.push(field));
// automatic initialization of dynamic fields starts from the root level
if (!this.get("parentDocument")) {
await this.initializeFieldTree(fields);
}
},
async initializeFieldTree(fields) {
for (let field of fields) {
await field.question.initDynamicFields();
if (field.childDocument) {
await this.initializeFieldTree(field.childDocument.fields);
}
}
},
buildFields(rawDocument) {
return rawDocument.form.questions.edges.map(({ node: question }) => {
const answer = rawDocument.answers.edges.find(({ node: answer }) => {
return answer.question.slug === question.slug;
});
let childDocument;
if (question.__typename === "FormQuestion" && answer) {
childDocument = this.documentStore.find(answer.node.formValue, {
parentDocument: this
});
}
return Field.create(getOwner(this).ownerInjection(), {
document: this,
_question: question,
_answer: answer && answer.node,
childDocument
});
});
},
id: computed("raw.id", function() {
return atob(this.get("raw.id"));
}),
field: computed(
"raw.form.slug",
"parentDocument.{id,fields.@each.id}",
function() {
if (!this.parentDocument) return null;
try {
return this.parentDocument.fields.find(
field =>
field.id ===
`Document:${this.parentDocument.id}:Question:${this.raw.form.slug}`
);
} catch (e) {
return null;
}
}
),
questionJexl: computed(function() {
const questionJexl = new jexl.Jexl();
questionJexl.addTransform("answer", slugWithPath =>
this.findAnswer(slugWithPath)
);
questionJexl.addTransform("mapby", (arr, key) => {
return arr && arr.map ? arr.map(obj => obj[key]) : null;
});
questionJexl.addBinaryOp("intersects", 20, intersects);
return questionJexl;
}),
findAnswer(slugWithPath) {
const field = this.findField(slugWithPath);
if (!field || !field.answer) {
return null;
}
// Multiple choice questions should return an empty array if there is no answer
// otherwise `intersects` operator breaks
const emptyValue =
field.question.__typename == "MultipleChoiceQuestion" ? [] : null;
if (field.answer.value && !field.question.hidden) {
if (field.question.__typename === "TableQuestion") {
return field.getWithDefault("answer.rowDocuments", []).map(doc =>
doc.fields.reduce((obj, field) => {
return {
...obj,
[field.question.slug]: field.answer.value
};
}, {})
);
}
return field.answer.value;
}
return emptyValue;
},
findField(slugWithPath) {
const segments = slugWithPath.split(".");
const slug = segments.pop();
const doc = this.resolveDocument(segments);
let field = doc && doc.fields.find(field => field.question.slug === slug);
if (!field) {
segments.push(slug);
this._resolveError(segments, slug, doc);
}
return field;
},
_resolveError(segments, failedAtSegment, failedAtDoc) {
let path = segments.join(".");
let explanation = "";
let availableKeys = failedAtDoc.fields
.map(field => field.question.slug)
.map(slug => `"${slug}"`)
.join(", ");
if (path != failedAtSegment) {
// single quesiton, doesn't need explanation about path / segment step
explanation = ` (failed at segment "${failedAtSegment}")`;
}
throw new Error(
`Question could not be resolved: "${path}"${explanation}. Available: ${availableKeys}`
);
},
resolveDocument(segments) {
if (!segments) {
return this;
}
let _document = this;
for (let segment of segments) {
switch (segment) {
case "root":
while (_document.parentDocument) {
_document = _document.parentDocument;
}
break;
case "parent":
if (!_document.parentDocument) {
this._resolveError(segments, segment, _document);
}
_document = _document.parentDocument;
break;
default: {
let formField = _document.fields.find(
field => field.question.slug === segment
);
if (!formField) {
this._resolveError(segments, segment, _document);
}
_document = formField.childDocument;
}
}
}
return _document;
},
fields: computed(() => []).readOnly(),
childDocuments: computed(
"fields.{[],@each.hidden,childDocument}",
function() {
return this.fields
.filter(field => !field.hidden)
.map(field => field.childDocument)
.filter(Boolean);
}
),
childState: computed("childDocuments.{[],@each.state}", function() {
const childDocumentStates = this.childDocuments
.filter(Boolean)
.map(c => c.state);
if (!childDocumentStates.length) {
return null;
}
return getParentState(childDocumentStates);
}),
ownState: computed(
"fields.@each.{isNew,isValid,hidden,optional,childDocument}",
function() {
const visibleFields = this.fields
.filter(f => !f.hidden)
.filter(f => !f.childDocument);
if (!visibleFields.length) {
return null;
}
if (visibleFields.every(f => f.isNew)) {
return "untouched";
}
if (visibleFields.some(f => !f.isValid && !f.isNew)) {
return "invalid";
}
if (
visibleFields
.filter(f => !f.question.optional)
.every(f => f.isValid && !f.isNew)
) {
return "valid";
}
return "unfinished";
}
),
state: computed("childState", "ownState", function() {
return getParentState([this.childState, this.ownState].filter(Boolean));
})
});