From 0c24aba5dd6b7804ba63d596e3ae692fccb68e88 Mon Sep 17 00:00:00 2001 From: Marin Ivanov Date: Tue, 20 Aug 2024 15:24:28 +0300 Subject: add errors --- app.js | 45 +++++++++++++++++++++++++++++++-------------- index.html | 9 ++++----- ka.js | 4 ++-- style.css | 20 ++++++++++++++++++++ 4 files changed, 57 insertions(+), 21 deletions(-) diff --git a/app.js b/app.js index 889afc0..8dec438 100644 --- a/app.js +++ b/app.js @@ -6,10 +6,15 @@ const loading = (val) => { loader.$click(() => loading(0)); let error = null; +function errClose() { + error = null; + app.reload(); +} let data = {}; function onerror(err) { error = err; console.error(err); + app.reload(); } function parsedata(r) { return Object.fromEntries(r.split("\n").map(x=>x.trim()).filter(x=>x).map(x=>x.split('='))); @@ -24,7 +29,7 @@ function cmd(url, body) { }) .then(r => parsedata(r)); }; -function loaddata() { +function loadconfig() { loading(1); cmd("/cgi-bin/getconfig") .then(x => { @@ -34,21 +39,34 @@ function loaddata() { .catch(onerror) .then(() => loading(0)) } +function saveconfig(groups) { + cmd("/cgi-bin/setconfig", data) + .catch(onerror) + .then(loadconfig) +} + +function CErr(text) { + var el = div( + div("🗙").$cls("close").$click(errClose), + span("⚠ "), + text, + ).$cls("err"); + function errClose() { + error = null; + el.remove(); + }; + return el; +} const routes = { "/": () => div( h2("Lorem Ipsum"), p("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "), p("Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."), - - h2("Ipsum Lorem"), - p("Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur."), - p("Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."), - p("Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."), - p("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "), ), "/network": () => div( - h1("Мрежови настройки"), + error && CErr(error.message), + h1("Network settings"), table( tr( td("IP"), @@ -62,10 +80,9 @@ const routes = { ), ) ).$cls("network"), - input("button").$value("Съхрани") + input("button").$value("Save").$click(saveconfig) ), "/config": () => { - loaddata(); return div(); }, "/waiting": () => { @@ -73,11 +90,11 @@ const routes = { setTimeout(() => { loading(0); }, 3000); - return div("Пуйчи"); + return div(""); }, "": () => div( - h1("Несъществуваща страница"), - p("Страницата не е намерена.") + h1("Not Found"), + p("The requested page is not available.") ), }; const onchange = () => { @@ -86,4 +103,4 @@ const onchange = () => { }; router(app, routes, onchange); -loaddata(); +loadconfig(); diff --git a/index.html b/index.html index 5332fce..7a19004 100644 --- a/index.html +++ b/index.html @@ -13,11 +13,10 @@ - Данни - Мрежа - Настройки - Инструменти - Пуйчи + Home + Network + Configuration + Tools
diff --git a/ka.js b/ka.js index a73bc9f..b7f9d6a 100644 --- a/ka.js +++ b/ka.js @@ -34,13 +34,13 @@ elproto.$change2state = function(state, field, valgetter=getvalue) { function tag(name, attrs, ...children) { const el = doc.createElement(name); el.$attrs(attrs||{}); - for (const child of children) { + for (const child of children.filter(x=>x)) { el.appendChild((typeof(child) === 'string') ? doc.createTextNode(child) : child); } return el; } -const TRIVIAL = "h1,h2,h3,p,a,div,span,select,table,tr,td"; +const TRIVIAL = "h1,h2,h3,p,,div,span,select,table,tr,td"; for (let name of TRIVIAL.split(",")) { window[name] = (...children) => tag(name, null, ...children); } diff --git a/style.css b/style.css index b89d135..334a765 100644 --- a/style.css +++ b/style.css @@ -86,6 +86,26 @@ input[type=button]:active { background-color: #079290; } +.err { + color: #fff; + background-color: #f00; + position: relative; + padding: 16px; + margin-bottom: 10px; + line-height: 1.3em; +} +.err span { + font-size: 1.5em; + font-weight: bold; +} +.err .close { + font-size: 1.3em; + position: absolute; + top:16px; + right:16px; + cursor: pointer; +} + #loader { position: absolute; left: 0; -- cgit v1.2.3