-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_builder.js
137 lines (120 loc) · 3.31 KB
/
json_builder.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
"use strict"
var params = [];
var arr = [];
var table = document.getElementsByTagName("tbody")[0];
var headers = table.children[0];
var obj_submit = document.getElementById("object_submit");
var output = document.getElementById("output");
var addColCell = document.getElementById("addcol");
var addRowButton = document.getElementById("addrow");
var headerOffset = table.parentElement.offsetTop;
//document.body.addEventListener("scroll", () => temp(), false);
function temp() {
if (document.body.scrollTop > headerOffset){
headers.classList.add("sticky");
table.style.paddingTop = "13px";
} else {
headers.classList.remove("sticky");
}
};
function addSubmission(name){
var elem = document.createElement("input");
elem.appendChild(document.createTextNode(name));
elem.type = "text";
obj_submit.appendChild(elem);
}
function addRow(){
var row = document.createElement("tr");
row.contenteditable = "false";
//row.contenteditable = "true";
for (let i=0; i<headers.children.length-1; i++) {
var cell = document.createElement("td");
cell.appendChild(document.createTextNode(""));
cell.contenteditable = "true";
row.appendChild(cell);
}
table.appendChild(row);
return row;
}
function removeCol(){
}
function promptCol() {
var name = prompt("Enter a unique parameter name");
if (name === "" || name === null)
return;
addCol(name);
}
function addCol(name){
addColCell.id = "addcol-sm";
addRowButton.id = "addrow-vis";
for (let i=0; i<headers.children.length-1; i++) {
if (headers.children[i].innerHTML === name){
alert("'" + name + "' is already a parameter");
return;
}
}
var elem = document.createElement("th");
elem.appendChild(document.createTextNode(name));
headers.insertBefore(elem, headers.children[headers.children.length-1]);
for (let i=1; i<table.children.length; i++){
var cell = document.createElement("td");
cell.contenteditable="true";
cell.appendChild(document.createTextNode(""));
table.children[i].appendChild(cell);
}
}
function updateJSON(){
if (table.children.length < 2)
return output.innerHTML = "[]";
var names=headers.children;
var len = names.length-1;
var json = "[ " + rowToJSON(table.children[1]);
for (let row = 2; row < table.children.length; row++){
json += ", " + rowToJSON(table.children[row]);
}
output.style.display = "block";
output.value = json + " ]";
}
function rowToJSON(row){
if (row.children.length < 1)
return "{}";
var json = "{ " + getKeyValue(row, 0);
for (let i=1; i<row.children.length; i++) {
json += ", " + getKeyValue(row, i);
}
return json + " }";
}
function getKeyValue(row, i){
var out = "\"" + headers.children[i].innerHTML + "\": \"";
if (row.children[i].innerHTML !== "<br>")
out += row.children[i].innerHTML;
return out + "\"";
}
function JSONToTable() {
try {
var out = JSON.parse(document.getElementById("json-in").value);
} catch (e) {
console.error("JSON input could not be parsed");
return;
}
clearTable();
for (let x in out[0]) {
addCol(x);
}
for (let i in out){
let obj = out[i];
let row = addRow();
let col = 0;
for (let v in obj) {
row.children[col++].innerHTML = obj[v];
}
}
}
function clearTable() {
for (let i=headers.children.length-2; i>=0; i--){
headers.removeChild(headers.children[i]);
}
for(let i=table.children.length-1; i>=1; i--){
table.removeChild(table.children[i]);
}
}