-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonform.js
150 lines (139 loc) · 4.96 KB
/
jsonform.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
class JsonForm {
constructor(jsonObj) {
this.form = document.createElement('form');
for(var key in jsonObj) {
this.form.appendChild( new FormField(jsonObj[key], key) );
}
return this.form;
}
}
class FormField {
constructor(jsonObj, name) {
this.TYPES = ["text", "number", "select", "radio", "checkbox"]
if( this.isNull(jsonObj, "jsonObj is undefined") ||
this.isNull(jsonObj.type, "jsonObj.type is undefined") ||
this.isNull(name, "name is undefined")
) { return; }
if(this.TYPES.indexOf(jsonObj.type) < 0) {
console.error("valid types are ["+this.TYPES+"] but found type ["+jsonObj.type+"]");
return;
}
switch(jsonObj.type) {
case "text":
case "number":
return this.addElToDiv(this.createInput(jsonObj), name);
break;
case "select":
return this.addElToDiv(this.createSelect(jsonObj), name);
break;
case "radio":
case "checkbox":
return this.addElToDiv(this.createChoice(jsonObj), name);
break;
default:
console.error("There seems to be a problem with this javascript library.")
return;
}
}
addElToDiv(element, name) {
if (element == null) {
return;
}
var div = document.createElement('div');
div.classList.add("formfield");
var label = document.createElement('label');
label.innerText = name;
if(element.id != undefined && element.id.length > 0)
label.htmlFor = element.id;
div.appendChild(label);
div.appendChild(element);
return div;
}
createInput(json) {
var input = document.createElement('input');
input.type = json.type;
input.required = json.required == true || json.required == "true"
if(json.placeholder)
input.placeholder = json.placeholder;
if(json.default)
input.value = json.default;
if(json.id)
input.id = json.id;
return input;
}
createSelect(json) {
var selector = document.createElement('select');
var defaultOption = document.createElement('option');
defaultOption.disabled = true;
defaultOption.selected = true;
if(json.placeholder)
defaultOption.innerText = json.placeholder;
selector.appendChild(defaultOption);
if(json.required == true || json.required == "true"){
selector.required = true
if(json.options == null || typeof(json.options) == "string" || json.options.length < 1) {
console.error("select must contain options")
return;
}
}
if(json.id)
selector.id = json.id;
if(json.options){
for(var i in json.options) {
var optEl = document.createElement('option');
optEl.value = json.options[i];
optEl.innerText = json.options[i];
if(json.default == json.options[i])
optEl.selected = true;
selector.appendChild(optEl);
}
}
return selector;
}
createChoice(json) {
if(this.isNull(json.options, "options must not be null.") || this.isNull(json.name, "name property must not be null.")) {
return;
}
if(typeof(json.options) == "string" || json.options.length < 1) {
console.error("options must be a non-empty array");
return;
}
let outSpan = document.createElement('span');
outSpan.classList.add('container');
if(json.id)
outSpan.id = json.id;
for(var i in json.options){
let opt = json.options[i];
let span = document.createElement('span');
let label = document.createElement('label');
label.innerText = opt;
label.htmlFor = json.name + "-" + opt;
let btn = document.createElement('input');
btn.type = json.type;
btn.id = label.htmlFor;
btn.name = json.name;
if(json.required == true || json.required == "true")
btn.required = true;
if(json.default == opt)
btn.checked = true;
else if(json.default != null && typeof(json.default) == "object"){
// for multi-select
if(json.default.indexOf(opt) >= 0)
btn.checked = true;
}
span.appendChild(btn);
span.appendChild(label);
outSpan.appendChild(span);
}
return outSpan;
}
isNull(val, err="") {
if(err.length > 0 && val == undefined) {
console.error(err);
return true;
} else if(val == undefined) {
return true;
}
return false;
}
}