From e9a62c48bc8856f222c3e133b52074c5a45a0fb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Tue, 21 May 2024 13:55:23 +0300 Subject: [PATCH] =?UTF-8?q?Opettajien=20lis=C3=A4=C3=A4minen=20ja=20poisto?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 5 ++++ käyttöliittymä.js | 61 +++++++++++++++++++++++++++++++++++++++++++++-- tietokanta.js | 5 +++- 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index 9c56ee4..ca28741 100644 --- a/index.html +++ b/index.html @@ -18,6 +18,11 @@
Opettajat + +
+ + +
diff --git a/käyttöliittymä.js b/käyttöliittymä.js index b460bce..e2e532a 100644 --- a/käyttöliittymä.js +++ b/käyttöliittymä.js @@ -15,6 +15,28 @@ document.getElementById('luokat-uusi').addEventListener('submit', (e) => { })); }); +document.getElementById('opettajat-uusi').addEventListener('submit', (e) => { + e.preventDefault(); + suorita(_tietokanta.transaktio((t) => { + const nimi = document.getElementById('opettajat-uusi-nimi').value; + const lyhenne = document.getElementById('opettajat-uusi-lyhenne').value; + if (nimi !== '' || lyhenne !== '') { + t.lisää(taulut.opettajat, {nimi, lyhenne}); + document.getElementById('opettajat-uusi-nimi').value = ''; + document.getElementById('opettajat-uusi-lyhenne').value = ''; + } + })); +}); + +document.getElementById('opettajat-uusi-nimi').addEventListener('change', () => { + const nimi = document.getElementById('opettajat-uusi-nimi').value; + // TODO: Kunnollinen tuki grafeemiklustereille + // TODO: Älä ehdota lyhennettä, joka on jo käytössä + const lyhenne = nimi.split(' ') + .map((x) => String.fromCodePoint(x.codePointAt(0))).join(''); + document.getElementById('opettajat-uusi-lyhenne').value = lyhenne; +}); + function suorita([tietokanta, muutokset]) { for (const muutos of muutokset) { suoritaMuutos(tietokanta, muutos); @@ -26,8 +48,7 @@ function suoritaMuutos(tietokanta, muutos) { const {taulu, id, vanha, uusi} = muutos; if (taulu === taulut.luokat && vanha === undefined) { // Uusi luokka - const järjestys = tietokanta.järjestyksessä(taulu, vertaa); - const seuraavaId = järjestys[järjestys.indexOf(id) + 1]; + const seuraavaId = idJälkeen(tietokanta, taulu, id, vertaa); const luokatLista = document.getElementById('luokat-lista'); // getElementById palauttaa null:n, jos id:tä ei löydy. Jos tämä luokka // on viimeinen, seuraavaId on undefined, eikä DOM:ssa ole luokkaa @@ -39,11 +60,31 @@ function suoritaMuutos(tietokanta, muutos) { // Luokka poistettu const luokka = document.getElementById(`luokka-${id}`); luokka.parentElement.removeChild(luokka); + // TODO: luokka muutos + } else if (taulu === taulut.opettajat && vanha === undefined) { + // Uusi opettaja + const seuraavaId = idJälkeen(tietokanta, taulu, id, + (a, b) => vertaa(a.nimi, b.nimi) + ); + const opettajatLista = document.getElementById('opettajat-lista'); + // ks. kommentti uuden luokan tapauksessa + const seuraava = document.getElementById(`opettaja-${seuraavaId}`); + opettajatLista.insertBefore(luoOpettaja(id, uusi), seuraava); + } else if (taulu === taulut.opettajat && uusi === undefined) { + // Opettaja poistettu + const opettaja = document.getElementById(`opettaja-${id}`); + opettaja.parentElement.removeChild(opettaja); + // TODO: opettaja muutos } else { throw new Error(`Ei toteutettu ${taulu} ${id} ${vanha} ${uusi}`); } } +function idJälkeen(tietokanta, taulu, id, vertaa) { + const järjestys = tietokanta.järjestyksessä(taulu, vertaa); + return järjestys[järjestys.indexOf(id) + 1]; +} + function vertaa(a, b) { // TODO: Parempi vertailufunktio? return a.localeCompare(b); @@ -64,3 +105,19 @@ function luoLuokka(id, nimi) { li.appendChild(document.createTextNode(nimi)); return li; } + +function luoOpettaja(id, {nimi, lyhenne}) { + const li = document.createElement('li'); + li.id = `opettaja-${id}`; + const poistoPainike = document.createElement('input'); + poistoPainike.type = 'button'; + poistoPainike.value = '-'; + poistoPainike.addEventListener('click', () => { + suorita(_tietokanta.transaktio((t) => { + t.poista(taulut.opettajat, id); + })); + }); + li.appendChild(poistoPainike); + li.appendChild(document.createTextNode(`${nimi} (${lyhenne})`)); + return li; +} diff --git a/tietokanta.js b/tietokanta.js index 23ea59d..760e250 100644 --- a/tietokanta.js +++ b/tietokanta.js @@ -2,6 +2,7 @@ const taulut = { luokat: 'luokat', + opettajat: 'opettajat', }; class Transaktio { @@ -91,7 +92,9 @@ class Tietokanta { } constructor() { - this.taulut.set(taulut.luokat, new Map); + for (let taulu in taulut) { + this.taulut.set(taulu, new Map); + } } transaktio(funktio) {