-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
380 lines (270 loc) · 11.1 KB
/
script.js
File metadata and controls
380 lines (270 loc) · 11.1 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
https://JSON-Form-Transformer.brewhog.repl.co
REQUIREMENTS
- Bulma is required for this to work properly. Current version used when developing was 0.9.4
FORM REQUIREMENTS FROM THE JSON OBJECT
- "testName" field is required. This is used for the test name
- If an object is chosen as a value, you have a few optional parameters to use
-
- Goal for this project is to be able to convert back and forth from JSON -> HTML Forms
- You should also have the ability to create a data structure from UI elements (Form Creator) - Long Term
- (High Priority) Take a JSON object and turn it in to a form
- Everything should be completely heirarchical
*/
let exampleJSON = {
"testTitle": "Example Test",
"Session": {
"sessionId": "ID of a session",
"resumed": "true",
"closed": "false",
"subSession": {
"testing": "Aha"
}
},
"ArraysSelectDropdowns": [ // Arrays are treated as dropdowns
"First option",
"Bugwah"
],
"assignee": "Justin Emilio",
}
class jsonForm {
constructor(parentId, initJSON) {
this.json = {};
if (typeof initJSON != "undefined") {
console.log("Init JSON produced:");
console.log(initJSON);
this.json = initJSON;
}
this.parentId = parentId;
this.parentEl = document.getElementById(this.parentId);
this.parentEl.id = "json-form-parent";
this.outputLeft = null;
this.outputRight = null;
this.outputEl = this.createParentOutputEl(this.parentEl);
this.processForm();
//console.log("Parent element: ",this.parentEl);
}
//Initialize the HTML and create all the proper elements in the DOM
createParentOutputEl(el) {
//Give reference to 'this' for any out of scope functions
let self = this;
let output = document.createElement("div");
el.appendChild(output);
output.id = "json-form-output";
output.classList.add("columns");
self.outputLeft = document.createElement("div");
self.outputLeft.id = "json-form-output-left";
self.outputLeft.classList.add("column");
self.outputLeft.innerText = "JSON Input";
self.outputRight = document.createElement("div");
self.outputRight.id = "json-form-output-right";
self.outputRight.classList.add("column");
self.outputRight.innerText = "Form Output";
output.appendChild(self.outputLeft);
output.appendChild(self.outputRight);
let jsonTextArea = this.createJSONTextArea(self.outputLeft);
return output;
}
createJSONTextArea(el) {
//Give reference to 'this' for any out of scope functions
let self = this;
let br = document.createElement("br");
let ta = document.createElement("textarea");
ta.style.width = "100%";
ta.style.minHeight = "400px";
ta.style.border = "0px solid white";
ta.style.backgroundColor = "lightgray";
ta.style.paddingLeft = "3px";
ta.setAttribute("spellcheck", "false");
let taMessage = document.createElement("span");
taMessage.style.color = "red";
el.appendChild(br);
el.appendChild(ta);
el.appendChild(taMessage);
ta.innerHTML = JSON.stringify(self.json, undefined, 4);
ta.addEventListener('keydown', function(e) {
function isValidJSON(jsonObj) {
try {
JSON.parse(jsonObj);
return true;
} catch (e) {
return false;
}
}
if (e.key == 'Tab') {
e.preventDefault();
var start = this.selectionStart;
var end = this.selectionEnd;
// set textarea value to: text before caret + tab + text after caret
this.value = this.value.substring(0, start) +
"\t" + this.value.substring(end);
// put caret at right position again
this.selectionStart =
this.selectionEnd = start + 1;
}
if (e.key == 'Enter') {
if (isValidJSON(this.value)) {
let oLen = this.value.length;
e.preventDefault();
let newVal = JSON.parse(this.value);
self.json = newVal;
this.value = JSON.stringify(newVal, undefined, 4);
}
}
});
ta.addEventListener('keyup', function(e) {
function isValidJSON(jsonObj) {
try {
JSON.parse(jsonObj);
return true;
} catch (e) {
return false;
}
}
if (isValidJSON(ta.value)) {
self.json = JSON.parse(ta.value);
ta.style.boxShadow = "3px 3px green";
ta.style.backgroundColor = "lightgray";
//this.innerHTML = JSON.stringify(JSON.parse(this.value),undefined,4);
taMessage.style.color = "green";
taMessage.innerText = "Valid JSON";
//console.log(self);
self.processForm();
} else {
ta.style.boxShadow = "7px 7px red";
ta.style.backgroundColor = "#fce6e3";
taMessage.style.color = "red";
taMessage.innerText = "Not valid JSON. Please try again.";
}
});
return ta;
}
processObjectItemForForm(name, obj, parentEl) {
for (let prop in obj) {
if (typeof obj[prop] == "object") { continue; }
let newContainer = document.createElement("div");
newContainer.style.width = "100%";
newContainer.style.paddingLeft = "15px";
let newLabel = document.createElement("span");
newLabel.innerText = prop;
let newInput = null;
switch (obj[prop]) {
case "true":
newInput = document.createElement('input');
newInput.setAttribute('type', 'checkbox');
newInput.checked = true;
newInput.id = prop;
newLabel.style.paddingLeft = "3px";
newContainer.appendChild(newInput);
newContainer.appendChild(newLabel);
break;
case "false":
newInput = document.createElement('input');
newInput.setAttribute('type', 'checkbox');
newInput.checked = false;
newInput.id = prop;
newLabel.style.paddingLeft = "3px";
newContainer.appendChild(newInput);
newContainer.appendChild(newLabel);
break;
default:
newInput = document.createElement("input");
newInput.classList.add("input");
newInput.classList.add("is-small");
newInput.id = prop;
newInput.placeholder = obj[prop];
newContainer.appendChild(newLabel);
newContainer.appendChild(newInput);
}
parentEl.appendChild(newContainer);
}
}
processArrayForForm(name, items, parentEl) {
//give reference to 'this' for any out of scope functions
let self = this;
console.log("Process Array called");
let newContainer = document.createElement("div");
let newLabel = document.createElement("span");
newLabel.innerText = name;
let newInput = document.createElement("select");
newInput.classList.add("select");
newInput.classList.add("is-small");
newInput.id = name;
for (let i = 0; i < items.length; i++) {
let opt = document.createElement("option");
opt.text = items[i];
opt.value = items[i];
newInput.add(opt);
}
let br = document.createElement("br");
newContainer.appendChild(br);
newContainer.appendChild(newLabel);
newContainer.appendChild(br);
newContainer.appendChild(newInput);
parentEl.appendChild(newContainer);
}
processItemForForm(name, item, parentEl) {
//give reference to 'this' for any out of scope functions
let self = this;
console.log("Process Item called");
let newContainer = document.createElement("div");
let newLabel = document.createElement("span");
newLabel.innerText = name;
let newInput = document.createElement("input");
newInput.classList.add("input");
newInput.classList.add("is-small");
newInput.id = name;
newInput.placeholder = item;
newContainer.appendChild(newLabel);
newContainer.appendChild(newInput);
parentEl.appendChild(newContainer);
}
loopSubObj(obj, level, parentEl) {
//give reference to 'this' for any out of scope functions
let self = this;
let currLevel = level + 1;
for (let prop in obj) {
if (Array.isArray(obj[prop])) {
//console.log("| " + "--".repeat(level) + "> " + prop + " = Array");
} else {
//console.log("| " + "--".repeat(level) + "> " + prop + " = " + typeof obj[prop]);
}
if (typeof obj[prop] == 'object') {
if (Array.isArray(obj[prop])) {
//console.log("Arrays are not supported");
let newEl = document.createElement("div");
parentEl.appendChild(newEl);
self.processArrayForForm(prop, obj[prop], newEl);
continue;
} else {
let newTitle = document.createElement("span");
newTitle.style.fontSize = "18pt";
newTitle.innerText = prop;
parentEl.appendChild(newTitle);
let newEl = document.createElement("div");
newEl.style.paddingLeft = "15px";
parentEl.appendChild(newEl);
self.processObjectItemForForm(prop, obj[prop], newEl);
self.loopSubObj(obj[prop], currLevel, newEl);
}
} else {
//Item is not an object, and not an array
//You can process this item and put on a form (If it's valid)
if (level == 0) {
self.processItemForForm(prop, obj[prop], parentEl);
}
}
}
}
processForm() {
//give reference to 'this' for any out of scope functions
let self = this;
let currLevel = 0;
self.outputRight.innerHTML = "";
//Loop recursively through all properties
self.loopSubObj(self.json, currLevel, self.outputRight);
//console.log("Process Form", self.outputRight.innerHTML);
}
}
let newForm = new jsonForm("json-form-output", exampleJSON);
//newForm.parentEl.innerText = "Haha. This is a test";