diff options
| author | Marin Ivanov <[email protected]> | 2024-08-20 12:02:40 +0300 |
|---|---|---|
| committer | Marin Ivanov <[email protected]> | 2024-08-20 12:02:40 +0300 |
| commit | ec34163617e3588a63dc611834a372746f9d4107 (patch) | |
| tree | 3d8220a89b9a0c5bb13bd911084b117d344e8a9f /app.js | |
| parent | 08a1acccd6c6e6a11211b065a40629c08312686a (diff) | |
wip: dynamic data
Diffstat (limited to 'app.js')
| -rw-r--r-- | app.js | 89 |
1 files changed, 89 insertions, 0 deletions
@@ -0,0 +1,89 @@ +let isLoading = 0; +const loading = (val) => { + isLoading = val; + loader.$cls(isLoading ? "" : "hidden"); +} +loader.$click(() => loading(0)); + +let error = null; +let data = {}; +function onerror(err) { + error = err; + console.error(err); +} +function parsedata(r) { + return Object.fromEntries(r.split("\n").map(x=>x.trim()).filter(x=>x).map(x=>x.split('='))); +} +function cmd(url, body) { + return fetch(url, {method:body?'POST':'GET', body}) + .then(r => { + if (!r.ok){ + throw new Error(r.statusText); + } + return r.text(); + }) + .then(r => parsedata(r)); +}; +function loaddata() { + loading(1); + cmd("/cgi-bin/getconfig") + .then(x => { + data = x; + app.reload(); + }) + .catch(onerror) + .then(() => loading(0)) +} + +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("Мрежови настройки"), + table( + tr( + td("IP"), + td(input("text").$value(data["cfg.ip"])), + ), + tr( + td("MAC"), + td( + input("text").$value(data["cfg.mac"]) + .$change2state(data, "cfg.mac") + ), + ) + ).$cls("network"), + input("button").$value("Съхрани") + ), + "/config": () => { + loaddata(); + return div(); + }, + "/waiting": () => { + loading(1); + setTimeout(() => { + loading(0); + }, 3000); + return div("Пуйчи"); + }, + "": () => div( + h1("Несъществуваща страница"), + p("Страницата не е намерена.") + ), +}; +const onchange = () => { + doc.querySelectorAll('nav a').forEach(x => x.$cls((location.href===x.href)?"active":"")); + loading(isLoading); +}; + +router(app, routes, onchange); +loaddata(); |
