From 785ece971e823983b0cce84465680ea432272d60 Mon Sep 17 00:00:00 2001 From: andreas Date: Wed, 2 Mar 2022 10:53:47 +0100 Subject: [PATCH] #18: export and import of config --- web/index.html | 2 ++ web/index.js | 96 +++++++++++++++++++++++++++++++------------------- 2 files changed, 61 insertions(+), 37 deletions(-) diff --git a/web/index.html b/web/index.html index d6cc011..c5bde6e 100644 --- a/web/index.html +++ b/web/index.html @@ -62,6 +62,8 @@ + +
diff --git a/web/index.js b/web/index.js index 6cf3fcb..ae9a10c 100644 --- a/web/index.js +++ b/web/index.js @@ -204,39 +204,46 @@ function checkXDR(v,allValues){ } } } +function getAllConfigs(omitPass) { + let values = document.querySelectorAll('.configForm select , .configForm input'); + let allValues = {}; + for (let i = 0; i < values.length; i++) { + let v = values[i]; + let name = v.getAttribute('name'); + if (!name) continue; + if (name.indexOf("_") >= 0) continue; + let def = getConfigDefition(name); + if (def.type === 'password' && ( v.value == '' || omitPass)) { + continue; + } + let check = v.getAttribute('data-check'); + if (check) { + if (typeof (self[check]) === 'function') { + let res = self[check](v.value, allValues, getConfigDefition(name)); + if (res) { + let value = v.value; + if (v.type === 'password') value = "******"; + alert("invalid config for " + v.getAttribute('name') + "(" + value + "):\n" + res); + return; + } + } + } + allValues[name] = v.value; + } + return allValues; +} function changeConfig() { ensurePass() .then(function (pass) { let newAdminPass; let url = "/api/setConfig?_hash="+encodeURIComponent(pass)+"&"; - let values = document.querySelectorAll('.configForm select , .configForm input'); - let allValues = {}; - for (let i = 0; i < values.length; i++) { - let v = values[i]; - let name = v.getAttribute('name'); - if (!name) continue; - if (name.indexOf("_") >= 0) continue; - let def = getConfigDefition(name); - if (def.type === 'password' && v.value == '') { - continue; - } - let check = v.getAttribute('data-check'); - if (check) { - if (typeof (self[check]) === 'function') { - let res = self[check](v.value, allValues, getConfigDefition(name)); - if (res) { - let value = v.value; - if (v.type === 'password') value = "******"; - alert("invalid config for " + v.getAttribute('name') + "(" + value + "):\n" + res); - return; - } - } - } + let allValues=getAllConfigs(); + if (!allValues) return; + for (let name in allValues){ if (name == 'adminPassword'){ - newAdminPass=v.value; + newAdminPass=allValues[name]; } - allValues[name] = v.value; - url += name + "=" + encodeURIComponent(v.value) + "&"; + url += name + "=" + encodeURIComponent(allValues[name]) + "&"; } getJson(url) .then(function (status) { @@ -254,7 +261,7 @@ function changeConfig() { } }) }) - .catch(function (e) { alert("Invalid password"); }) + .catch(function (e) { alert(e); }) } function factoryReset() { ensurePass() @@ -861,6 +868,19 @@ function formatDate(d){ else rt+=v; return rt; } +function downloadData(data,name){ + let url="data:application/octet-stream,"+encodeURIComponent(JSON.stringify(data,undefined,2)); + let target=document.getElementById('downloadXdr'); + if (! target) return; + target.setAttribute('href',url); + target.setAttribute('download',name); + target.click(); +} +function exportConfig(){ + let data=getAllConfigs(true); + if (! data) return; + downloadData(data,"config"+formatDate()+".json"); +} function exportXdr(){ let data={}; forEl('.xdrvalue',function(el) { @@ -873,18 +893,14 @@ function exportXdr(){ } data[name]=value; }) - let url="data:application/octet-stream,"+encodeURIComponent(JSON.stringify(data,undefined,2)); - let target=document.getElementById('downloadXdr'); - if (! target) return; - target.setAttribute('href',url); - target.setAttribute('download',"xdr"+formatDate()+".json"); - target.click(); + downloadData(data,"xdr"+formatDate()+".json"); } -function importXdr(){ - forEl('.uploadXdr',function(ul){ +function importJson(opt_keyPattern){ + let clazz='importJson'; + forEl('.'+clazz,function(ul){ ul.remove(); }); - let ip=addEl('input','uploadXdr',document.body); + let ip=addEl('input',clazz,document.body); ip.setAttribute('type','file'); ip.addEventListener('change',function(ev){ if (ip.files.length > 0){ @@ -895,7 +911,7 @@ function importXdr(){ let idata=JSON.parse(reader.result); let hasOverwrites=false; for (let k in idata){ - if (! k.match(/^XDR[0-9][0-9]*/)){ + if (opt_keyPattern && ! k.match(opt_keyPattern)){ alert("file contains invalid key "+k); return; } @@ -926,6 +942,12 @@ function importXdr(){ }); ip.click(); } +function importXdr(){ + importJson(new RegExp(/^XDR[0-9][0-9]*/)); +} +function importConfig(){ + importJson(); +} function toggleClass(el,id,classList){ let nc=classList[id]; let rt=false;