configui/templates/base/script.tmpl

134 lines
3.0 KiB
Cheetah
Raw Normal View History

{{define "base/script"}}
<script>
window.ToolIsFollow = false;
2021-11-03 08:21:49 +00:00
window.LastDelta = 0;
window.ContentChanged = false;
2021-11-03 08:21:49 +00:00
async function FileGet(){
if (Active == '{{.AppName}}') {
const res = await fetch('{{.BaseUrl}}api/conf')
.catch(err=>{console.log(err);return;});
const body = await res.text();
if(!res.ok){
Catch(res)
return
}
2021-11-03 08:21:49 +00:00
LastDelta = 0;
editor.session.setValue(body);
}
else {
2021-11-03 08:21:49 +00:00
const res = await fetch('{{.BaseUrl}}api/file?name=' + Active)
.catch(err=>{console.log(err);return;});
const body = await res.json();
if(!res.ok){
Catch(res)
return
}
2021-11-03 08:21:49 +00:00
LastDelta = body.delta;
editor.session.setValue(body.data);
}
}
async function FileSave(){
if (Active == '{{.AppName}}') {
2021-10-24 07:06:02 +00:00
const res = await fetch('{{.BaseUrl}}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();
window.ContentChanged = false
}
else Catch(res)
}
else {
2021-10-24 07:06:02 +00:00
const res = await fetch('{{.BaseUrl}}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)
else window.ContentChanged = false
}
}
async function FileApply(){
if (Active == '{{.AppName}}') {
return;
}
else {
2021-10-24 07:06:02 +00:00
const res = await fetch('{{.BaseUrl}}api/apply?name='+ Active, {
method: 'POST',
});
if(!res.ok){
const result = await Catch(res)
result_editor.session.setValue(result)
return
}
const result = await res.text()
result_editor.session.setValue(result)
}
}
2021-11-03 08:21:49 +00:00
async function FileDelta(){
if (Active == '{{.AppName}}') {
await FileGet()
return
}
else {
const res = await fetch('{{.BaseUrl}}api/delta?f=true&name=' + Active+'&delta='+LastDelta)
.catch(err=>{console.log(err);return;});
const body = await res.json();
if(!res.ok){
Catch(res)
return
}
LastDelta = body.delta;
editor.session.setValue(editor.getValue()+body.data);
}
}
async function Catch(res){
{{/* console.trace()
console.log(res) */}}
const msg = await res.text()
ShowError(msg)
return msg
}
function setResult(){
var result_editor = ace.edit("result_editor");
window.result_editor = result_editor
result_editor.setTheme("ace/theme/monokai");
result_editor.session.setMode("ace/mode/sh");
result_editor.setReadOnly(true);
result_editor.setOptions({
maxLines: 1000
});
}
// starting point
(function(){
// setup ace editor
setEditor()
// setup result code block
setResult()
// for follow mode
setInterval((async ()=>{
if (ToolIsFollow){
2021-11-03 08:21:49 +00:00
await FileDelta()
editor.gotoLine(editor.session.getLength());
}
}), 1000)
}())
</script>
{{end}}