From 0e332aa0269ff18aa18fe5771f1733f1541c51a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Fri, 17 May 2024 15:32:19 +0300 Subject: [PATCH] =?UTF-8?q?N=C3=A4yt=C3=A4=20luokat=20aakkosj=C3=A4rjestyk?= =?UTF-8?q?sess=C3=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 3 ++- käyttöliittymä.js | 21 ++++++++++++++++----- tietokanta.js | 20 ++++++++++++++++++-- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/index.html b/index.html index 5ccf76c..904c585 100644 --- a/index.html +++ b/index.html @@ -27,8 +27,9 @@ diff --git a/käyttöliittymä.js b/käyttöliittymä.js index a1c5c94..449da71 100644 --- a/käyttöliittymä.js +++ b/käyttöliittymä.js @@ -4,19 +4,25 @@ document.getElementById('kumoa').addEventListener('click', () => { suorita(tietokanta.kumoa()); }); -function suorita(muutokset) { +function suorita([tietokanta, muutokset]) { for (const muutos of muutokset) { - suoritaMuutos(muutos); + suoritaMuutos(tietokanta, muutos); } } -function suoritaMuutos(muutos) { +function suoritaMuutos(tietokanta, muutos) { const {taulu, id, vanha, uusi} = muutos; if (taulu === taulut.luokat && vanha === undefined) { // Uusi luokka - // TODO: Järjestele + const järjestys = tietokanta.järjestyksessä(taulu, vertaa); + const seuraavaId = järjestys[järjestys.indexOf(id) + 1]; const luokatLista = document.getElementById('luokat-lista'); - luokatLista.appendChild(luoLuokka(id, uusi)); + // getElementById palauttaa null:n, jos id:tä ei löydy. Jos tämä luokka + // on viimeinen, seuraavaId on undefined, eikä DOM:ssa ole luokkaa + // "luokka-undefined". seuraava on siis null silloin kuin tämä luokka + // tulee lisätä listan loppuun, joka vastaa insertBefore:n toimintaa + const seuraava = document.getElementById(`luokka-${seuraavaId}`); + luokatLista.insertBefore(luoLuokka(id, uusi), seuraava); } else if (taulu === taulut.luokat && uusi === undefined) { // Luokka poistettu const luokka = document.getElementById(`luokka-${id}`); @@ -26,6 +32,11 @@ function suoritaMuutos(muutos) { } } +function vertaa(a, b) { + // TODO: Parempi vertailufunktio? + return a.localeCompare(b); +} + function luoLuokka(id, nimi) { const li = document.createElement('li'); li.id = `luokka-${id}`; diff --git a/tietokanta.js b/tietokanta.js index c33f574..b734ebf 100644 --- a/tietokanta.js +++ b/tietokanta.js @@ -80,7 +80,7 @@ class Tietokanta { transaktio(funktio) { const transaktio = new Transaktio(this); funktio(transaktio); - return this.suorita(transaktio); + return [tietokanta, this.suorita(transaktio)]; } suorita(transaktio) { @@ -129,7 +129,7 @@ class Tietokanta { uusi: vanha, }); } - return kumotut; + return [tietokanta, kumotut]; } hae(taulu, id) { @@ -138,6 +138,22 @@ class Tietokanta { } return this.taulut.get(taulu).get(id); } + + järjestyksessä(taulu, järjestys) { + if (!this.taulut.has(taulu)) { + throw new Error(`ei taulua ${taulu}`); + } + const taulukko = Array.from(this.taulut.get(taulu).entries()); + taulukko.sort(([xId, x], [yId, y]) => { + const vertaus = järjestys(x, y); + if (vertaus < 0 || vertaus > 0) { + return vertaus; + } else { + return xId - yId; + } + }); + return taulukko.map(([id, _]) => id); + } } const tietokanta = new Tietokanta;