configui/public/js/main.js

70 lines
1.5 KiB
JavaScript

window.ToolIsFollow = false;
async function FileGet(follower=false){
let f = ''
if (Active == 'ConfigUI') {
if (follower) f = '?f=true'
const res = await fetch('/api/conf'+f);
const body = await res.text();
editor.session.setValue(body);
}
else {
if (follower) f = '&f=true'
const res = await fetch('/api/file?name=' + Active + f);
const body = await res.json();
editor.session.setValue(body.data);
}
}
async function FileSave(){
if (Active == 'ConfigUI') {
const res = await fetch('/api/conf', {
method: 'POST',
body: editor.getValue(),
headers: new Headers({
'Content-Type': 'application/json'
})
})
if (res.ok) window.location.reload();
}
else {
const res = await fetch('/api/file', {
method: 'POST',
body: JSON.stringify({"name":Active,"data":editor.getValue()}),
headers: new Headers({
'Content-Type': 'application/json'
})
})
}
}
async function FileApply(){
if (Active == 'ConfigUI') {
return;
}
else {
const res = await fetch('/api/apply?name='+ Active, {
method: 'POST',
})
const result = await res.text()
result_editor.session.setValue(result)
}
}
// starting point
(function(){
// setup ace editor
setEditor()
// setup result code block
setResult()
// for follow mode
setInterval((async ()=>{
if (ToolIsFollow){
await FileGet(true)
editor.gotoLine(editor.session.getLength());
}
}), 1000)
}())