kconfig/main.js

79 lines
2.3 KiB
JavaScript
Raw Normal View History

2021-11-24 09:08:11 +00:00
import {JSONEditor} from '@json-editor/json-editor'
import 'spectre.css/dist/spectre.min.css'
import 'spectre.css/dist/spectre-exp.min.css'
import 'spectre.css/dist/spectre-icons.min.css'
2021-11-19 03:58:55 +00:00
(function() {
console.log("start")
2021-11-24 09:08:11 +00:00
var editor = new JSONEditor(document.getElementById('editor_holder'),{
theme: 'spectre',
iconlib: 'spectre',
ajax: true,
// The schema for the editor
schema: {
$ref: "api/schema",
format: "grid"
},
});
// Hook up the submit button to log to the console
document.getElementById('submit').addEventListener('click',async function() {
// Get the value from the editor
console.log(editor.getValue());
const res = await fetch('api/apply', {
method: 'POST',
body: JSON.stringify(editor.getValue()),
headers: new Headers({
'Content-Type': 'application/json'
})
}).catch(err=>{console.log(err);return;});
if (!res.ok) {
console.log(res)
2021-11-24 09:39:04 +00:00
const errMsg = await res.text()
alert(errMsg)
2021-11-24 09:08:11 +00:00
}
});
// Hook up the load from server button
document.getElementById('load').addEventListener('click',async function() {
const res = await fetch('api/load').catch(err=>{console.log(err);return;});
if (!res.ok) {
console.log(res)
2021-11-24 09:39:04 +00:00
const errMsg = await res.text()
alert(errMsg)
return
2021-11-24 09:08:11 +00:00
}
const body = await res.json();
editor.setValue(body);
});
// Hook up the validation indicator to update its
// status whenever the editor changes
editor.on('change',function() {
// Get an array of errors from the validator
var errors = editor.validate();
var indicator = document.getElementById('valid_indicator');
// Not valid
if(errors.length) {
indicator.className = 'label alert';
indicator.textContent = 'not valid';
}
// Valid
else {
indicator.className = 'label success';
indicator.textContent = 'valid';
}
});
document.getElementById('download').addEventListener('click',async function() {
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(editor.getValue(),null,2));
var dlAnchorElem = document.getElementById('downloadAnchorElem');
dlAnchorElem.setAttribute('href', dataStr );
dlAnchorElem.setAttribute('download', AppName+'.json');
dlAnchorElem.click();
});
2021-11-19 03:58:55 +00:00
})();