-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit.html
419 lines (376 loc) · 23.9 KB
/
edit.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<html>
<head>
<title>Handlebars Arango CMS Template Editor</title>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="style.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.5/marked.js" type="text/javascript"></script>
<script src="handlebars.min.js" type="text/javascript"></script>
<script src="templates.js" type="text/javascript"></script>
<script src="formatting.js" type="text/javascript"></script>
</head>
<body>
<div></div>
<div class="w3-bar w3-teal w3-display-container" id="navbar_lrg">
<form>
<button class="w3-button" onclick="plusDivs(-1)">❮</button>
<select id="allcollections" name="collection" size="1" onchange="updateDocuments()">
</select>
<select id="alldocuments" name="document" size="1" onchange="updateContext(this)">
</select>
<button class="w3-button" onclick="plusDivs(+1)">❯</button>
<button class="w3-btn w3-circle">+</button>
<button class="w3-btn w3-circle"><i class='fa fa-clone'></i></button>
<button class="w3-btn w3-circle">✕</button>
<input type="file" id="json_data" multiple size="50" onchange="openFiles()">
<div class="w3-row-padding"></div>
<div class="w3-third">
<input class="w3-input w3-hover-grey" type="text" id="un" name="username" placeholder="Username" />
</div>
<div class="w3-third">
<input class="w3-input w3-hover-grey" type="password" id="pw" name="password" placeholder="Password" />
</div>
<div class="w3-third">
<input class="w3-btn" type="button" onclick="login()" value="Login" />
</div>
</div>
</form>
</div>
<p id="selected_files_list"></p>
<script>
function login() {
var url = "/_db/school/_open/auth";
var data = {
"username": document.getElementById("un").value,
"password": document.getElementById("pw").value
};
fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, cors, *same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'include', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json',
// 'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow', // manual, *follow, error
referrer: 'no-referrer', // no-referrer, *client
body: JSON.stringify(data), // body data type must match "Content-Type" header
})
.then(function(response) {
return response.json();
}).then(jwt => {
console.log(jwt);
sessionStorage.setItem("jwt", jwt.jwt);
}).catch(function(rejected) {
document.getElementById("resp").innerText = rejected;
});
}
function getSampleDoc() {
console.log("get sample doc");
var jwt = sessionStorage.getItem("jwt");
var docHandle = document.getElementById("alldocuments");
var selectedDoc = docHandle.options[docHandle.selectedIndex].value;
var url = "/_db/school/_api/document/" + selectedDoc; //test_handlebarsTemplates/editmenu";
fetch(url, {
method: 'GET', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, cors, *same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'include', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json',
'Authorization': 'bearer ' + jwt
},
redirect: 'follow', // manual, *follow, error
referrer: 'no-referrer' // no-referrer, *client
}).then(function(r) {
return r.json();
}).then(function(responseJSON) {
console.log(responseJSON);
sessionStorage.setItem("json", JSON.stringify(responseJSON));
console.log(sessionStorage.getItem("json"));
setElement(responseJSON, document.getElementById("handlebarsTemplate"), "templateeditor");
});
}
function updateContext(selectDocument) {
console.log(selectDocument);
getSampleDoc();
}
function setElement(jsonData, tgt_element, template) {
if (template == "markdown") {
tgt_element.innerHTML = marked(jsonData);
} else {
tgt_element.innerHTML = Handlebars.templates[template](jsonData);
}
}
function populateCollectionsList(selectElementId, listOfCollections) {
var selectList = document.getElementById(selectElementId);
for (var i = 0; i < listOfCollections.allcollections.length; i++) {
var option = document.createElement("option");
option.value = listOfCollections.allcollections[i];
option.text = listOfCollections.allcollections[i];
selectList.appendChild(option);
}
}
function populateDocumentsList(selectElementId, listOfDocuments) {
var selectList = document.getElementById(selectElementId);
for (var c of selectList.children) {
c.remove();
}
for (var i = 0; i < listOfDocuments.length; i++) {
var option = document.createElement("option");
option.value = listOfDocuments[i]._id;
option.text = listOfDocuments[i]._key;
selectList.appendChild(option);
}
}
function fetchData(url, elementId, func) {
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(respJSON) {
console.log(respJSON);
func(elementId, respJSON);
})
.catch(function(err) {
console.log("Something went wrong!", err);
});
}
// function getXMLHttpData(url, elementId, func) {
// var xmlhttp = new XMLHttpRequest();
// xmlhttp.onreadystatechange = function() {
// if (this.readyState == 4 && this.status == 200) {
// var newData = JSON.parse(this.responseText);
// func(elementId, newData);
// }
// };
// xmlhttp.open("GET", url, true);
// xmlhttp.send();
// }
fetchData("collections/", "allcollections", populateCollectionsList);
updateDocuments();
function updateDocuments() {
var allcollections = document.getElementById("allcollections");
var selectedCollection = allcollections.options[allcollections.selectedIndex].value;
fetchData("collections/" + selectedCollection, "alldocuments", populateDocumentsList);
}
function openFiles() {
var x = document.getElementById("json_data");
var txt = "";
if ('files' in x) {
if (x.files.length == 0) {
txt = "Select one or more files.";
} else {
for (var i = 0; i < x.files.length; i++) {
txt += "<br><strong>" + (i + 1) + ". file</strong><br>";
var file = x.files[i];
if ('name' in file) {
txt += "name: " + file.name + "<br>";
}
if ('size' in file) {
txt += "size: " + file.size + " bytes <br>";
}
}
}
} else {
if (x.value == "") {
txt += "Select one or more files.";
} else {
txt += "The files property is not supported by your browser!";
txt += "<br>The path of the selected file: " + x.value; // If the browser does not support the files property, it will return the path of the selected file instead.
}
}
document.getElementById("selected_files_list").innerHTML = txt;
}
</script>
<div id="handlebarsTemplate">
<div>
<label for="source" class="w3-block w3-teal">Handlebars Template</label> Resize: <input type="range" min="5" max="20" value="8" class="slider" onchange="resizeTextArea(this)" />
<textarea id="source" class="w3-input" type="textarea" spellcheck="false" rows="8">
<div class="w3-bar w3-teal w3-display-container" id="navbar_lrg">
<form>
<button class="w3-button" onclick="plusDivs(-1)">&#10094;</button>
<select id="allcollections" name="collection" size="1" onchange="updateDocuments()">
{{#each collection}}
<option value="{{this}}">{{this}}</option>
{{/each}}
</select>
<select id="alldocuments" name="document" size="1" onchange="updateContext(this)">
{{#each document}}
<option value="{{{this._id}}}">{{{this._key}}}</option>
{{/each}}
</select>
<button class="w3-button" onclick="plusDivs(+1)">&#10095;</button>
<button class="w3-btn w3-circle">+</button>
<button class="w3-btn w3-circle"><i class='fa fa-clone'></i></button>
<button class="w3-btn w3-circle">&#10005;</button>
<input type="file" id="json_data" multiple size="50" onchange="openFiles()">
</form>
<form>
Username:
<input class="w3-input w3-hover-grey" type="text" id="un" name="username" value="root">Password:
<input class="w3-input w3-hover-grey" type="password" id="pw" name="password" value="123">
<input class="w3-button" type="button" onclick="ffetch()" value="Login" />
</form>
</div>
<p id="selected_files_list"></p>
<script>
function updateContext(selectDocument){
var event = new CustomEvent('newDocumentSelected', { detail: selectDocument.value });
selectDocument.dispatchEvent(event);
}
function populateCollectionsList(selectElementId, listOfCollections){
var selectList = document.getElementById(selectElementId);
for (var i = 0; i < listOfCollections.allcollections.length; i++) {
var option = document.createElement("option");
option.value = listOfCollections.allcollections[i];
option.text = listOfCollections.allcollections[i];
selectList.appendChild(option);
}
}
function populateDocumentsList(selectElementId, listOfDocuments){
var selectList = document.getElementById(selectElementId);
for (var c of selectList.children){
c.remove();
}
for (var i = 0; i < listOfDocuments.length; i++) {
var option = document.createElement("option");
option.value = listOfDocuments[i]._id;
option.text = listOfDocuments[i]._key;
selectList.appendChild(option);
}
}
function fetchData(url, elementId, func){
fetch(url)
.then(function(response){
func(elementId, JSON.parse(response.json()));
})
.catch(function (err) {
console.log("Something went wrong!", err);
});
}
function getXMLHttpData(url, elementId, func){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var newData = JSON.parse(this.responseText);
func(elementId, newData);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
getXMLHttpData("collections/", "allcollections", populateCollectionsList);
updateDocuments();
function updateDocuments(){
var allcollections = document.getElementById("allcollections");
var selectedCollection = allcollections.options[allcollections.selectedIndex].value;
getXMLHttpData("collections/"+selectedCollection, "alldocuments", populateDocumentsList);
}
function openFiles(){
var x = document.getElementById("json_data");
var txt = "";
if ('files' in x) {
if (x.files.length == 0) {
txt = "Select one or more files.";
} else {
for (var i = 0; i < x.files.length; i++) {
txt += "<br><strong>" + (i+1) + ". file</strong><br>";
var file = x.files[i];
if ('name' in file) {
txt += "name: " + file.name + "<br>";
}
if ('size' in file) {
txt += "size: " + file.size + " bytes <br>";
}
}
}
}
else {
if (x.value == "") {
txt += "Select one or more files.";
} else {
txt += "The files property is not supported by your browser!";
txt += "<br>The path of the selected file: " + x.value; // If the browser does not support the files property, it will return the path of the selected file instead.
}
}
document.getElementById("selected_files_list").innerHTML = txt;
}
</script></textarea>
</div>
<div>
<label for="context" class="w3-block w3-teal">Context (JavaScript literal or JSON) </label> Resize: <input type="range" min="5" max="20" value="8" class="slider" onchange="resizeTextArea(this)" />
<textarea id="context" class="w3-input" type="textarea" spellcheck="false" rows="8">
</textarea>
</div>
<div id="helpersWrap">
<label for="helpers" class="w3-block w3-teal">Register Helper functions (if any)</label> Resize: <input type="range" min="5" max="20" value="8" class="slider" onchange="resizeTextArea(this)" />
<textarea id="helpers" class="w3-input" type="textarea" spellcheck="false" rows="8">
</textarea>
</div>
</div>
<div>
<input class="w3-btn w3-ripple w3-block w3-grey " type="button" value="Compile Handlebars Template" onclick="setOutput()">
<p class="errors padding--left padding--right"><span></span></p>
<label>Client</label>
<input id="client" class="w3-check w3-hover-grey" type="checkbox" checked="checked"></input>
</div>
<div>
<div>
<p class="w3-block w3-teal">HTML Source Output</p>
Resize: <input type="range" min="5" max="20" value="8" class="slider" onchange="resizeTextArea(this)" />
<textarea id="output-html-code" class="w3-input">
</textarea>
</div>
<div>
<p class="w3-block w3-teal">HTML Preview</p>
<div id="output-html-view" class="w3-card-4 w3-border-red">
</div>
<p class="w3-block w3-teal">Compiled Template Preview</p>
Resize: <input type="range" min="5" max="20" value="8" class="slider" onchange="resizeTextArea(this)" />
<textarea id="compiled" class="w3-input" spellcheck="false" rows="8">
{"1":function(container,depth0,helpers,partials,data) {
return " <option value=\""
+ container.escapeExpression(container.lambda(depth0, depth0))
+ "\">"
+ container.escapeExpression(container.lambda(depth0, depth0))
+ "</option>\n";
},"3":function(container,depth0,helpers,partials,data) {
var stack1;
return " <option value=\""
+ ((stack1 = container.lambda((depth0 != null ? depth0._id : depth0), depth0)) != null ? stack1 : "")
+ "\">"
+ ((stack1 = container.lambda((depth0 != null ? depth0._key : depth0), depth0)) != null ? stack1 : "")
+ "</option>\n";
},"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) {
var stack1;
return " <div class=\"w3-bar w3-teal w3-display-container\" id=\"navbar_lrg\">\n <form>\n <button class=\"w3-button\" onclick=\"plusDivs(-1)\">&#10094;</button>\n <select id=\"allcollections\" name=\"collection\" size=\"1\" onchange=\"updateDocuments()\">\n"
+ ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.collection : depth0),{"name":"each","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ " </select>\n <select id=\"alldocuments\" name=\"document\" size=\"1\" onchange=\"updateContext(this)\">\n"
+ ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.document : depth0),{"name":"each","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ " </select>\n\n <button class=\"w3-button\" onclick=\"plusDivs(+1)\">&#10095;</button> \n <button class=\"w3-btn w3-circle\">+</button>\n <button class=\"w3-btn w3-circle\"><i class='fa fa-clone'></i></button>\n <button class=\"w3-btn w3-circle\">&#10005;</button>\n <input type=\"file\" id=\"json_data\" multiple size=\"50\" onchange=\"openFiles()\">\n\n </form>\n <form>\n Username:\n <input class=\"w3-input w3-hover-grey\" type=\"text\" id=\"un\" name=\"username\" value=\"root\">Password:\n <input class=\"w3-input w3-hover-grey\" type=\"password\" id=\"pw\" name=\"password\" value=\"123\">\n <input class=\"w3-button\" type=\"button\" onclick=\"ffetch()\" value=\"Login\" />\n </form>\n</div>\n\n<p id=\"selected_files_list\"></p>\n \n<script>\nfunction updateContext(selectDocument){\n var event = new CustomEvent('newDocumentSelected', { detail: selectDocument.value });\n selectDocument.dispatchEvent(event);\n}\n\n\n\nfunction populateCollectionsList(selectElementId, listOfCollections){\n var selectList = document.getElementById(selectElementId);\n for (var i = 0; i < listOfCollections.allcollections.length; i++) {\n var option = document.createElement(\"option\");\n option.value = listOfCollections.allcollections[i];\n option.text = listOfCollections.allcollections[i];\n selectList.appendChild(option);\n }\n}\n\nfunction populateDocumentsList(selectElementId, listOfDocuments){\n var selectList = document.getElementById(selectElementId);\n for (var c of selectList.children){\n c.remove();\n }\n for (var i = 0; i < listOfDocuments.length; i++) {\n var option = document.createElement(\"option\");\n option.value = listOfDocuments[i]._id;\n option.text = listOfDocuments[i]._key;\n selectList.appendChild(option);\n }\n}\n\nfunction fetchData(url, elementId, func){\n fetch(url)\n .then(function(response){\n func(elementId, JSON.parse(response.json()));\n })\n .catch(function (err) {\n console.log(\"Something went wrong!\", err);\n });\n}\n\nfunction getXMLHttpData(url, elementId, func){\n var xmlhttp = new XMLHttpRequest();\n xmlhttp.onreadystatechange = function() {\n if (this.readyState == 4 && this.status == 200) {\n var newData = JSON.parse(this.responseText);\n func(elementId, newData); \n }\n };\n xmlhttp.open(\"GET\", url, true);\n xmlhttp.send();\n}\ngetXMLHttpData(\"collections/\", \"allcollections\", populateCollectionsList);\nupdateDocuments();\n\nfunction updateDocuments(){\n var allcollections = document.getElementById(\"allcollections\");\n var selectedCollection = allcollections.options[allcollections.selectedIndex].value;\n getXMLHttpData(\"collections/\"+selectedCollection, \"alldocuments\", populateDocumentsList);\n}\n\nfunction openFiles(){\n var x = document.getElementById(\"json_data\");\n var txt = \"\";\n if ('files' in x) {\n if (x.files.length == 0) {\n txt = \"Select one or more files.\";\n } else {\n for (var i = 0; i < x.files.length; i++) {\n txt += \"<br><strong>\" + (i+1) + \". file</strong><br>\";\n var file = x.files[i];\n if ('name' in file) {\n txt += \"name: \" + file.name + \"<br>\";\n }\n if ('size' in file) {\n txt += \"size: \" + file.size + \" bytes <br>\";\n }\n }\n }\n } \n else {\n if (x.value == \"\") {\n txt += \"Select one or more files.\";\n } else {\n txt += \"The files property is not supported by your browser!\";\n txt += \"<br>The path of the selected file: \" + x.value; // If the browser does not support the files property, it will return the path of the selected file instead. \n }\n }\n document.getElementById(\"selected_files_list\").innerHTML = txt;\n}\n</script>";
},"useData":true}
</textarea>
</div>
</div>
<script>
function runTemplate() {
var rawTemplate = document.getElementById("source").value;
var context = JSON.parse(document.getElementById("context").value);
var compiled = Handlebars.compile(rawTemplate);
var output = compiled(context);
return output;
}
function setOutput() {
var output = runTemplate();
document.getElementById("output-html-code").value = output;
document.getElementById("output-html-view").innerHTML = output;
}
function resizeTextArea(caller) {
var textAreaRef = caller.parentElement.getElementsByTagName("textarea")[0];
textAreaRef.rows = caller.value;
}
</script>
</body>
</html>