-
Notifications
You must be signed in to change notification settings - Fork 7
/
FormGenerator.js
107 lines (91 loc) · 3.29 KB
/
FormGenerator.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
import jsonld from 'jsonld';
import Constants from '../constants/Constants';
import DefaultFormGenerator from './DefaultFormGenerator';
import FormUtils from '../util/FormUtils';
import Logger from '../util/Logger';
import JsonLdFramingUtils from '../util/JsonLdFramingUtils';
import JsonLdObjectUtils from '../util/JsonLdObjectUtils';
import JsonLdObjectMap from '../util/JsonLdObjectMap';
export default class FormGenerator {
/**
* Generates a default form with one-step wizard.
*
* @param intl Preferred language of questions
*
* @return Form definition (a one-step wizard with one question) and form data
*/
static constructDefaultForm(intl) {
const defaultFormData = DefaultFormGenerator.generateForm();
const [formQuestions, form] = FormGenerator._constructFormQuestions(defaultFormData, intl);
return [formQuestions, form];
}
/**
* Generates form definition from the specified data-enriched template.
*
* @param structure The form structure in JSON-LD
* @param intl Preferred language of questions
*
* @return Promise with generated form definition and form data
*/
static constructForm(structure, intl) {
return new Promise((resolve) =>
jsonld.flatten(structure, {}, null, (err, structure) => {
let formProperties;
let form;
if (err) {
Logger.error(err);
}
try {
const [formQuestions, rootForm] = FormGenerator._constructFormQuestions(structure, intl);
form = rootForm;
formProperties = {
formQuestions
};
} catch (e) {
const [formQuestions, rootForm] = FormGenerator.constructDefaultForm(intl);
form = rootForm;
formProperties = {
formQuestions
};
}
return resolve([formProperties, form]);
})
);
}
static _constructFormQuestions(structure, intl) {
let form;
let formElements;
let id2ObjectMap;
let formQuestions = [];
if (structure['@graph'][0]['@id'] !== undefined) {
id2ObjectMap = JsonLdFramingUtils.expandStructure(structure); //TODO make as callback
Object.keys(id2ObjectMap).map((key) => {
JsonLdObjectMap.putObject(key, id2ObjectMap[key]);
});
} else {
console.warn('default form is constructed.');
}
form = structure['@graph'].find((item) => FormUtils.isForm(item));
formElements = form[Constants.HAS_SUBQUESTION];
if (!formElements) {
Logger.error('Could not find any questions in the received data.');
throw 'No questions in the form';
}
formQuestions = formElements.filter((item) => {
if (FormUtils.isWizardStep(item) && !FormUtils.isHidden(item)) {
return true;
}
Logger.warn('Item is not a wizard step: ' + item);
return false;
});
if (!formQuestions.length) {
Logger.log('Could not find any wizard steps in the received data. Building form without steps');
form[Constants.HAS_SUBQUESTION].forEach((question) => formQuestions.push(question));
}
// sort by label
formQuestions.sort(JsonLdObjectUtils.getCompareLocalizedLabelFunction(intl));
// sort by property
JsonLdObjectUtils.orderPreservingToplogicalSort(formQuestions, Constants.HAS_PRECEDING_QUESTION);
return [formQuestions, { root: form }];
}
}