-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcodegenerator.js
48 lines (43 loc) · 1.61 KB
/
codegenerator.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
function isNameCorrect(name) {
return /^@?[a-zA-Z_]\w*(\.@?[a-zA-Z_]\w*)*$/.test(name);
}
function isListQuestionType(el) {
return ["paneldynamic", "matrixdynamic", "matrixdropdown"].indexOf(el.getType()) > -1;
}
function getNestedItemClassName(name) {
return name + "Item";
}
function getElementTypeName(el) {
if (isListQuestionType(el)) return "IList<" + getNestedItemClassName(el.name) + ">";
if (el.getType() === "text") {
if (el.inputType === "number") return "int";
if (el.inputType === "date" || el.inputType === "localedate") return "DateTime";
}
if (el.getType() === "boolean") return "bool"
return "string";
}
function getCodePadding() { return " "; }
function generateClassByElements(className, elements, lines) {
lines.push("public class " + className + " {");
elements.forEach(el => {
if (el.isQuestion || el.getType() === "matrixdropdowncolumn") {
lines.push(getCodePadding() + getElementTypeName(el) + " " + el.name + " { get; set; }")
}
});
lines.push("}")
}
function generateDomainModelsCode(survey) {
const lines = [];
generateClassByElements(survey.name, survey.getAllQuestions(), lines);
survey.getAllQuestions().forEach(q => {
if (!isListQuestionType(q)) return;
if (q.getType() === "paneldynamic") {
lines.push("");
generateClassByElements(getNestedItemClassName(q.name), q.elements, lines);
} else {
lines.push("");
generateClassByElements(getNestedItemClassName(q.name), q.columns, lines);
}
});
return lines.join("\n");
}