forked from Lab1not/coranos.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoranos-common.js
131 lines (110 loc) · 3.25 KB
/
coranos-common.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
const hide = (id) => {
document.getElementById(id).style = 'display:none;';
}
const show = (id) => {
document.getElementById(id).style = 'display:block;';
}
const html = (id, html) => {
const elt = document.getElementById(id);
if (html !== undefined) {
elt.innerHTML = html;
}
return elt.innerHTML;
}
const class_attr = (id, cl) => {
const elt = document.getElementById(id);
if (cl !== undefined) {
elt.className = cl;
}
return elt.className;
}
const value = (id, value) => {
const elt = document.getElementById(id);
if (value !== undefined) {
elt.value = value;
}
return elt.value;
}
const xlink_href = (id, value) => {
const elt = document.getElementById(id);
if (elt == null) {
return null;
}
// console.log('xlink_href',id,value,elt);
if (value !== null) {
elt.setAttribute('xlink:href', value);
}
return elt.getAttribute('xlink:href');
}
// fill in the URL parameters into the form.
const fillInUrlParameters = () => {
const url = new URL(window.location.href);
url.searchParams.forEach((value, name) => {
// console.log('nv',name,value);
const fields = document.getElementsByName(name);
fields.forEach((field) => {
// console.log('set',field.name,field.id,value);
field.value = value;
});
});
}
const addChildElement = (parent, childType, childText, styleCallback) => {
const child = document.createElement(childType);
parent.appendChild(child);
if (childText !== undefined) {
if (childText instanceof HTMLImageElement) {
child.appendChild(childText);
} else if (childText instanceof Object) {
createTableInElt(child, childText, styleCallback);
} else {
child.appendChild(document.createTextNode(childText));
}
}
if(styleCallback != undefined) {
styleCallback(child);
}
return child;
}
const createTable = (id, json, styleCallback) => {
const elt = document.getElementById(id);
createTableInElt(elt, json, styleCallback);
}
const createTableInElt = (elt, json, styleCallback) => {
if (json.length == 0) {
return;
}
while (!(json[0] instanceof Object)) {
const child = json.shift();
elt.appendChild(document.createTextNode(child));
}
const table = addChildElement(elt, 'table', undefined, styleCallback);
table.className = 'cb w100pct';
const tableHeaderRow = addChildElement(table, 'tr', undefined, styleCallback);
tableHeaderRow.className = 'cb';
const keys = [];
for (const [key, value] of Object.entries(json[0])) {
const tableDataHeader = addChildElement(tableHeaderRow, 'th', key, styleCallback);
tableDataHeader.className = 'cb';
keys.push(key);
}
json.forEach((jsonElt, jsonEltIx) => {
const tableDataRow = addChildElement(table, 'tr');
tableDataRow.className = 'cb';
keys.forEach((key) => {
const value = jsonElt[key];
const tableDataCell = addChildElement(tableDataRow, 'td', value, styleCallback);
tableDataCell.className = 'cb';
});
});
}
const loadJson = (url, callback) => {
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
callback(JSON.parse(this.response));
}
}
xhttp.responseType = 'text';
xhttp.open('GET', url, true);
xhttp.send();
}