-
Notifications
You must be signed in to change notification settings - Fork 6
/
api.html
362 lines (360 loc) · 16.5 KB
/
api.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>DBView</title>
<link href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/svgs/solid/folder-tree.svg" rel="shortcut icon" color="#c0392b">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/css/font-awesome.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jstree.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/themes/default/style.min.css" rel="stylesheet">
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-3 vh-100 overflow-auto">
<div id="jstree"></div>
</div>
<div id="data" class="col-5 vh-100 overflow-auto">
<div id="status">/</div>
<pre id="json"></pre>
<button id="update" type="button" class="btn btn-primary" disabled>Update</button>
</div>
<div id="docs" class="col-4 vh-100 overflow-auto"></div>
</div>
</div>
<script type="module">
const doc_base = document.location.pathname.substring(0, document.location.pathname.lastIndexOf('.'))
const api_base = doc_base
import { JSONEditor } from 'https://cdn.jsdelivr.net/npm/[email protected]/index.min.js'
const jeditor = new JSONEditor({
target: document.getElementById("json"),
props: {
mainMenuBar: false,
navigationBar: false,
statusBar: false,
mode: 'text',
content: {
text: '"Select nodes on the tree view"',
},
onChange: function (update) {
var node = $("#jstree").jstree("get_selected", true);
if (node.length == 1 && node_contains_config(node[0])) {
document.getElementById("update").disabled = false;
}
}
}
});
function strip_xml(parent, nodes) {
var to_remove = [];
for (var i = 0; nodes && i < nodes.length; i++) {
if (nodes[i].nodeName == "NODE" || nodes[i].nodeName == "VALUE") {
strip_xml(nodes[i], nodes[i].childNodes);
}
else {
to_remove.push(nodes[i]);
}
}
for (var i = 0; i < to_remove.length; i++) {
parent.removeChild(to_remove[i]);
}
}
function xmlPath(node) {
if (node.parentNode && node.parentNode.getAttribute("name") != null) {
return xmlPath(node.parentNode) + "/" + node.getAttribute("name");
}
return node.getAttribute("name");
}
function create_template(node) {
if (node.childNodes && node.childNodes.length > 0) {
var template = {};
var children = node.childNodes;
for (var i = 0; i < children.length; i++) {
var child = children[i];
var name = child.getAttribute("name");
var mode = child.getAttribute("mode");
var def = child.getAttribute("default");
if (name && (mode == "w" || mode == "rw")) {
if (def) {
template[name] = def;
} else {
template[name] = "";
}
}
}
} else {
template = "";
}
return template;
}
function traverse(xmlDoc, nodes) {
var txt = "";
var list_entry = false;
for (var i = 0; nodes && i < nodes.length; i++) {
if (nodes[i].nodeName == "NODE") {
var name = nodes[i].getAttribute("name");
var mode = nodes[i].getAttribute("mode");
txt += "<ul>";
txt += "<li ";
var data = {}
data.list = false;
if (nodes[i].hasChildNodes() && nodes[i].childNodes[0].getAttribute("name") == "*") {
data.icon = "fa fa-list";
data.list = true;
data.template = create_template(nodes[i].childNodes[0]);
}
else if (list_entry) {
data.icon = "fa fa-file";
}
else if (!nodes[i].hasChildNodes() || nodes[i].childNodes[0].nodeName == "VALUE") {
if (mode == "w" || mode == "rw")
data.icon = "fa fa-leaf text-success";
else
data.icon = "fa fa-leaf text-secondary";
}
else {
data.icon = "fa fa-folder";
}
// data.disabled = true;
if (data.length != 0) {
txt += " data-jstree='" + JSON.stringify(data) + "'";
}
if (mode) {
txt += " mode=\"" + mode + "\"";
}
var help = nodes[i].getAttribute("help");
if (help) {
txt += " title=\"" + help + "\"";
}
var pattern = nodes[i].getAttribute("pattern");
if (pattern) {
txt += " pattern=\"" + pattern + "\"";
}
var range = nodes[i].getAttribute("range");
if (range) {
txt += " range=\"" + range + "\"";
}
var children = nodes[i].childNodes;
var values = "";
for (var j = 0; j < children.length; j++) {
var child = children[j];
if (child.nodeName == "VALUE") {
var cname = child.getAttribute("name");
if (values != "")
values += ",";
values += cname;
}
}
if (values != "") {
txt += " values=\"" + values + "\"";
}
txt += ">";
txt += name;
if (nodes[i].hasChildNodes()) {
txt += traverse(xmlDoc, nodes[i].childNodes);
}
txt += "</li>";
txt += "</ul>";
}
}
return txt;
}
function node_is_list(node) {
if (node && node.li_attr && node.li_attr["data-jstree"]) {
var json = JSON.parse(node.li_attr["data-jstree"]);
if (json && json.list)
return true;
}
return false;
}
function node_contains_config(node) {
var mode = node.li_attr.mode;
if (mode == "w" || mode == "rw") {
return true;
}
for (var i = 0; i < node.children.length; i++) {
if (node_contains_config($('#jstree').jstree(true).get_json(node.children[i])))
return true;
}
return false;
}
function action_get(node) {
var path = "/" + $('#jstree').jstree(true).get_path(node, "/");
var json = $.ajax({
headers: {
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Accept': 'application/json',
'X-JSON-Types': 'on',
'X-JSON-Array': 'on',
},
url: api_base + path + '?with-defaults=report-all',
async: false
}).responseJSON;
if (json == undefined) {
jeditor.set({text: '"Not found"'})
}
else {
var data = JSON.stringify(json, null, " ")
jeditor.set({text: data})
jeditor.path = path;
if (node_contains_config(node)) {
jeditor.updateProps({readOnly: false});
} else {
jeditor.updateProps({readOnly: true});
}
}
}
function generate_docs(node, depth) {
var html = '';
html += '<div class="text-left">'
html += '<div class="ms-' + depth + '">'
html += '<h4>' + node.text + '</h4>'
if (node.li_attr.title != undefined)
html += '<p>' + node.li_attr.title + '</p>'
if (node.li_attr.pattern != undefined)
html += '<p>pattern = ' + node.li_attr.pattern + '</p>'
if (node.li_attr.range != undefined)
html += '<p>range = ' + node.li_attr.range + '</p>'
if (node.li_attr.values != undefined)
html += '<p>values = ' + node.li_attr.values + '</p>'
html += '</div>'
html += '</div>'
for (var i = 0; i < node.children.length; i++) {
var child = $('#jstree').jstree(true).get_node(node.children[i]);
html += generate_docs(child, depth + 1);
}
return html;
}
function custom_menu(node) {
var items = {};
if (node_is_list(node) && node_contains_config(node)) {
items['newItem'] = {
label: "New Entry",
icon: "fa fa-plus",
action: function () {
var path = "/" + $('#jstree').jstree(true).get_path(node, "/") + '/*';
jeditor.path = path;
document.getElementById("status").innerHTML = path;
document.getElementById("docs").innerHTML = generate_docs(node, 0);
var json = JSON.parse(node.li_attr["data-jstree"]);
if (json && json.template) {
var data = JSON.stringify(json.template, null, " ")
jeditor.set({text: data})
} else {
jeditor.set({text: "\"\""})
}
jeditor.updateProps({readOnly: false});
document.getElementById("update").disabled = false;
}
}
}
return items;
}
$(function () {
$.ajax({
url: doc_base + '.xml',
success: function (xml) {
if (xml.documentElement.childNodes) {
strip_xml(xml.documentElement, xml.documentElement.childNodes)
document.getElementById("jstree").innerHTML = traverse(xml, xml.documentElement.childNodes);
$('#jstree').jstree({
core : {
"check_callback": true,
},
plugins: [
"contextmenu"
],
contextmenu: {
items: custom_menu,
select_node: false
}
}).on('changed.jstree', function (e, data) {
if (data.action == "select_node") {
var path = "/" + $('#jstree').jstree(true).get_path(data.node, "/");
document.getElementById("status").innerHTML = path;
document.getElementById("docs").innerHTML = generate_docs(data.node, 0);
action_get(data.node);
}
}).on('before_open.jstree', function (e, data) {
if (node_is_list(data.node)) {
var child = $('#jstree').jstree(true).get_node(data.node.children[0]);
$('#jstree').jstree(true).hide_node(child);
}
}).on('open_node.jstree', function (e, data) {
var icon = $('#' + data.node.id).find('i.jstree-icon.jstree-themeicon').first();
if (icon && icon.length > 0 && icon[0].classList.contains('fa-folder'))
icon.removeClass('fa-folder').addClass('fa-folder-open');
if (node_is_list(data.node)) {
var name = $('#jstree').jstree(true).get_text(data.node);
var path = $('#jstree').jstree(true).get_path(data.node, "/");
var json = $.ajax({
headers: {
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Accept': 'application/json',
'X-JSON-Types': 'on',
'X-JSON-Array': 'on',
},
url: api_base + '/' + path + "/",
async: false,
}).responseJSON;
var entries = json[name];
if (entries.length > 0) {
var child = $('#jstree').jstree(true).get_node(data.node.children[0]);
for (var i = 0; i < entries.length; i++) {
var config = {text: entries[i], icon: "fa fa-file"};
var node = $('#jstree').jstree(true).create_node(data.node, config);
$('#jstree').jstree(true).copy_node(child.children, node);
}
} else {
$('#jstree').jstree(true).show_node(data.node.children[0]);
}
}
return true;
}).on('after_close.jstree', function (e, data) {
var icon = $('#' + data.node.id).find('i.jstree-icon.jstree-themeicon').first();
if (icon && icon[0].classList.contains('fa-folder-open'))
icon.removeClass('fa-folder-open').addClass('fa-folder');
if (node_is_list(data.node)) {
for (var i = 1; i < data.node.children.length; i++) {
$('#jstree').jstree(true).delete_node(data.node.children[i]);
}
$('#jstree').jstree(true).show_node(data.node.children[0]);
}
})
}
}
});
});
$('#update').on('click', function(event) {
this.blur();
var json = JSON.parse(jeditor.get().text);
var url = api_base;
const path = jeditor.path;
if (path.endsWith("/*")) {
var key = json[Object.keys(json)[0]];
url += path.substring(0, path.lastIndexOf('*')) + key;
} else {
url += path.substring(0, path.lastIndexOf('/'));
}
var data = JSON.stringify(json, null, " ");
$.ajax({
type: "POST",
url: url,
dataType: "json",
contentType: "application/json",
data: data,
error: function(e) {
if (e.status == 200 || e.status == 201) {
document.getElementById("update").disabled = true;
$('#jstree').jstree("refresh");
} else {
alert(e.statusText);
}
},
})
});
</script>
</body>
</html>