forked from tuna/mirror-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzdocs.es6
101 lines (90 loc) · 3.34 KB
/
zdocs.es6
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
---
---
window.addEventListener('DOMContentLoaded', function () {
function generateFormConfig(form) {
const formData = Object.fromEntries(new FormData(form).entries());
Array.from( // FormData ignores unchecked checkboxes, workaround
form.querySelectorAll('input[type=checkbox]:not(:checked)')
).forEach((elm) => { formData[elm.name] = '' });
let conf = {};
for (const x in formData) {
conf[x] = formData[x];
const varConf = GLOBAL_CONFIG.input[x];
if (!varConf) continue;
let optConf = null;
if ('option' in varConf) optConf = varConf.option[formData[x]];
else if ('true' in varConf || 'false' in varConf) {
optConf = (formData[x] === 'on') ? varConf.true : varConf.false;
}
if (typeof optConf === 'object') Object.assign(conf, optConf);
if (typeof optConf === 'string') conf[x] = optConf;
}
return conf
}
function renderCode(tmpl) {
// generate mustache config
let conf = {
path: (window.mirrorId.endsWith(".git") ? '/git/' : '/') + window.mirrorId
}
Array.from(document.querySelectorAll('form.z-global')).forEach((elm) => {
Object.assign(conf, generateFormConfig(elm));
})
conf.scheme = conf._scheme ? 'https' : 'http';
conf.host = conf.host.replace(/^https?:\/\//, '');
conf.sudo = conf._sudo ? 'sudo ' : '';
if (conf.filter && GLOBAL_CONFIG.filter.scheme) {
conf.scheme = GLOBAL_CONFIG.filter.scheme;
}
// find div.z-wrap
const div = tmpl.previousElementSibling;
// find form.z-form
const form = div.querySelector('form.z-form');
// find form.z-code
var code = div.querySelector('pre.z-code');
if (code === null) {
code = document.createElement('pre');
code.classList.add('z-code');
div.appendChild(code);
}
if (form) Object.assign(conf, generateFormConfig(form));
conf.endpoint = conf.scheme + '://' + conf.host + conf.path;
// render with mustache
let rendered = globalThis.Mustache.render(
tmpl.textContent.trim(), conf, {}, { escape: x => x }
)
try {
const lang = tmpl.attributes.getNamedItem('z-lang');
if (lang && hljs.getLanguage(lang.value)) {
rendered = hljs.highlight(rendered, { language: lang.value }).value;
}
} catch (err) {
console.error(err);
}
code.innerHTML = rendered;
}
function renderForm(event) {
if (!event || event.currentTarget.classList.contains('z-global')) {
Array.from(document.querySelectorAll('.z-help pre.z-tmpl')).forEach(renderCode);
}
else {
renderCode(event.currentTarget.parentElement.nextElementSibling);
}
}
// Load project config
const GLOBAL_CONFIG = JSON.parse(atob(document.getElementById('z-config').textContent));
// Hide HTTPS selector if filtered
if (GLOBAL_CONFIG.filter && GLOBAL_CONFIG.filter.scheme) {
document.querySelector('input[name="_scheme"]').parentElement.style.display = 'none';
}
// Render code
renderForm(null);
const ignoreEventHandler = (event) => event.preventDefault();
for(const form of document.querySelectorAll('form.z-form')) {
form.addEventListener('submit', ignoreEventHandler);
if(form.classList.contains('z-global')) {
form.addEventListener('change', ()=>renderForm(null));
}else{
form.addEventListener('change', renderForm);
}
}
});