импорт

This commit is contained in:
Dmitry Borisenko
2022-09-30 23:48:56 +02:00
parent c3d503d8b2
commit 6ef976187c
3 changed files with 66 additions and 23 deletions

View File

@@ -60,11 +60,11 @@
function windowHeight() {
console.log("test", scenarioTxt);
let scenStr = scenarioTxt;
height = scenStr.split("\n").length;
height = scenStr.split("\n").length + 1;
}
// Function to download data to a file
function download(data, filename, type) {
function saveFile2(data, filename, type) {
var file = new Blob([data], { type: type });
if (window.navigator.msSaveOrOpenBlob)
// IE10+
@@ -77,6 +77,7 @@
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function () {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
@@ -84,6 +85,24 @@
}
}
function saveFile(data, filename, type) {
var file = new Blob([data], { type: type });
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(file, filename);
} else {
const a = document.createElement("a");
document.body.appendChild(a);
const url = window.URL.createObjectURL(file);
a.href = url;
a.download = filename;
a.click();
setTimeout(() => {
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
}, 0);
}
}
const syntaxHighlight = (json) => {
try {
json = JSON.stringify(JSON.parse(json), null, 4);
@@ -100,7 +119,11 @@
function createExportFile() {
exportJson.mark = "iotm";
exportJson.config = configJson;
exportJson.scenario = scenarioTxt;
let exportAsText = syntaxHighlight(JSON.stringify(exportJson));
exportAsText = exportAsText + "\n\nscenario=>" + scenarioTxt;
saveFile(exportAsText, "export.json", "application/json");
}
let template = null;
@@ -113,23 +136,33 @@
const fileText = files[0].text();
fileText.then((text) => {
template = text;
if (IsJsonParse(template)) {
let json = JSON.parse(template);
if (json.mark === "iotm") {
if (window.confirm(alertOk)) {
configJson = [];
scenarioTxt = "";
configJson = json.config;
scenarioTxt = json.scenario;
console.log("config updated");
}
} else {
window.alert(alertErr);
}
} else {
if (!template.includes("scenario=>")) {
window.alert(alertErr);
return;
}
let jsonPart = selectToMarker(template, "scenario=>");
let txtPart = deleteBeforeDelimiter(template, "scenario=>");
if (!IsJsonParse(jsonPart)) {
window.alert(alertErr);
return;
}
let json = JSON.parse(jsonPart);
if (json.mark !== "iotm") {
window.alert(alertErr);
return;
}
if (window.confirm(alertOk)) {
configJson = [];
scenarioTxt = "";
configJson = json.config;
scenarioTxt = txtPart;
console.log("config updated");
}
});
files = null;
@@ -149,6 +182,16 @@
}
return true;
}
function selectToMarker(str, found) {
let p = str.indexOf(found);
return str.substring(0, p);
}
function deleteBeforeDelimiter(str, found) {
let p = str.indexOf(found) + found.length;
return str.substring(p);
}
</script>
{#if show}
@@ -231,7 +274,7 @@
<div class="grd-2col1">
<button class="btn-lg" on:click={() => saveConfig()}>{"Сохранить на устройстве"}</button>
<button class="btn-lg" on:click={() => rebootEsp()}>{"Перезагрузить устройство"}</button>
<button class="btn-lg" on:click={() => (createExportFile(), download(syntaxHighlight(JSON.stringify(exportJson)), "export.json", "application/json"))}>{"Экспорт конфигурации"}</button>
<button class="btn-lg" on:click={() => (createExportFile(), createExportFile())}>{"Экспорт конфигурации"}</button>
<label on:click={() => reset()} class="btn-lg cursor-pointer select-none">
<input bind:files accept="application/JSON" type="file" id="formFile" />
{"Импорт конфигурации"}