configui/src/main.js

104 lines
2.3 KiB
JavaScript

window.ToolIsFollow = false;
FileGet= async function(follower=false){
let f = ''
if (Active == 'ConfigUI') {
if (follower) f = '?f=true'
const res = await fetch('/api/conf'+f)
.catch(err=>{console.log(err);return;});
const body = await res.text();
if(!res.ok){
Catch(res)
return
}
editor.session.setValue(body);
}
else {
if (follower) f = '&f=true'
const res = await fetch('/api/file?name=' + Active + f)
.catch(err=>{console.log(err);return;});
const body = await res.json();
if(!res.ok){
Catch(res)
return
}
editor.session.setValue(body.data);
}
}
FileSave = async function(){
if (Active == 'ConfigUI') {
const res = await fetch('/api/conf', {
method: 'POST',
body: editor.getValue(),
headers: new Headers({
'Content-Type': 'application/json'
})
}).catch(err=>{console.log(err);return;});
if (res.ok) window.location.reload();
else Catch(res)
}
else {
const res = await fetch('/api/file', {
method: 'POST',
body: JSON.stringify({"name":Active,"data":editor.getValue()}),
headers: new Headers({
'Content-Type': 'application/json'
})
}).catch(err=>{console.log(err);return;});
if(!res.ok) Catch(res)
}
}
FileApply = async function(){
if (Active == 'ConfigUI') {
return;
}
else {
const res = await fetch('/api/apply?name='+ Active, {
method: 'POST',
}).catch(err=>{console.log(err);return;});
if(!res.ok){
const result = await Catch(res)
result_editor.session.setValue(result)
return
}
const result = await res.text()
result_editor.session.setValue(result)
}
}
Catch=async function(res){
console.trace()
console.log(res)
const msg = await res.text()
ShowError(msg)
return msg
}
// 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)
// block ctl-s
window.addEventListener("keypress", function(event) {
if (!(event.which == 115 && event.ctrlKey) && !(event.which == 19)) return true
alert("Ctrl-S pressed")
event.preventDefault()
return false
})
}())