-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsd.js
93 lines (86 loc) · 2.73 KB
/
jsd.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
async function loadJson(file) {
const response = await fetch(file);
const json = await response.json();
return json;
}
function renderStyle(element, json)
{
const keys = Object.keys(json);
let styleStr="";
for(let i in keys) {
const key = keys[i];
const data = json[key];
if(key === ".") continue;
else {
const dataKeys = Object.keys(data);
styleStr += key + "{";
for(let j in dataKeys)
{
const dk = dataKeys[j];
const ddk = data[dk];
if(ddk instanceof Array) {
styleStr += dk + ":" + ddk.join(" ") + ";";
}
else {
styleStr += dk + ":" + ddk.toString() + ";";
}
}
styleStr += "}";
}
}
element.innerHTML = styleStr;
}
function render(element, json,clear, id) {
if (clear) element.innerHTML="";
if(id !== undefined) element.id = id;
const keys = Object.keys(json);
for(let i in keys) {
const key = keys[i];
const data = json[key];
if(key === "." || key === id) continue;
else if(key === "class") {
if(data instanceof Array) {
element.className=data.join(" ");
}
else {
element.className=data.toString();
}
}
else if(key === "_") element.innerText=data.toString();
else if(data instanceof Array) {
for(let j = 0; j < data.length; ++j) {
const child = document.createElement(data[j]["."]);
element.appendChild(child);
if(data[j]["."] === "style") {
renderStyle(child, data[j]);
}
else{
render(child, data[j], clear);
}
}
}
else if(typeof(data) === "object") {
const child = document.createElement(data["."]);
element.appendChild(child);
if(data["."] === "style") {
renderStyle(child, data);
}
else{
render(child, data, clear, key);
}
}
else element.setAttribute(key,data.toString());
}
}
function renderHead(json, clear) {
if(json.head!==undefined) render(document.getElementsByTagName("head")[0], json.head, clear);
}
function renderBody(json, clear) {
if(json.body!==undefined) render(document.getElementsByTagName("body")[0], json.body, clear);
}
async function renderJSON(pageSrc, clear = false){
const page = await loadJson(pageSrc);
renderHead(page, clear);
renderBody(page, clear);
document.dispatchEvent(new Event("onJsonLoad"));
};