From cf65097eb1b5ecd25fe05416be24c196868f15e8 Mon Sep 17 00:00:00 2001 From: ulrich Date: Wed, 07 Apr 2021 11:25:19 +0000 Subject: [PATCH] Einstellungen --- src/de/uhilger/mediaz/api/StorageHandler.java | 2 src/de/uhilger/mediaz/entity/Einstellung.java | 25 +++++++- src/de/uhilger/mediaz/store/FileStorage.java | 24 ++++--- www/ui/data/tpl/einstellung_liste.tpl | 11 +++ www/ui/data/tpl/form_einstellung.tpl | 14 ++++ www/ui/js/app.js | 74 ++++++++++++++++++++++++ src/de/uhilger/mediaz/api/ListFileHandler.java | 20 +++++- www/ui/index.html | 1 8 files changed, 151 insertions(+), 20 deletions(-) diff --git a/src/de/uhilger/mediaz/api/ListFileHandler.java b/src/de/uhilger/mediaz/api/ListFileHandler.java index ac53618..cda66b7 100644 --- a/src/de/uhilger/mediaz/api/ListFileHandler.java +++ b/src/de/uhilger/mediaz/api/ListFileHandler.java @@ -1,7 +1,19 @@ /* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. + Mediazentrale - Personal Media Center + Copyright (C) 2021 Ulrich Hilger + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. */ package de.uhilger.mediaz.api; @@ -58,8 +70,6 @@ list.add(sf); } } - //Gson gson = new Gson(); - //String json = gson.toJson(fileNames); String json = jsonWithCustomType(list, "Medialiste"); logger.fine(json); e.sendResponseHeaders(200, json.length()); diff --git a/src/de/uhilger/mediaz/api/StorageHandler.java b/src/de/uhilger/mediaz/api/StorageHandler.java index f022ada..5899bef 100644 --- a/src/de/uhilger/mediaz/api/StorageHandler.java +++ b/src/de/uhilger/mediaz/api/StorageHandler.java @@ -143,7 +143,7 @@ String filename = ""; FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); Gson gson = new Gson(); - logger.log(Level.FINE, "type: {0}", type); + logger.log(Level.FINE, "type: {0}, token: {1}", new Object[]{type, fs.typeFromName(type).getType().getTypeName()}); Object o = gson.fromJson(body, fs.typeFromName(type).getType()); if(o instanceof Entity) { Object antwortObjekt = fs.write((Entity) o, overwrite); diff --git a/src/de/uhilger/mediaz/entity/Einstellung.java b/src/de/uhilger/mediaz/entity/Einstellung.java index 1b2e552..555fb76 100644 --- a/src/de/uhilger/mediaz/entity/Einstellung.java +++ b/src/de/uhilger/mediaz/entity/Einstellung.java @@ -1,7 +1,19 @@ /* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. + Mediazentrale - Personal Media Center + Copyright (C) 2021 Ulrich Hilger + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. */ package de.uhilger.mediaz.entity; @@ -9,7 +21,7 @@ * * @author ulrich */ -public class Einstellung { +public class Einstellung implements Entity { private String key; private String value; @@ -28,6 +40,11 @@ public void setValue(String value) { this.value = value; } + + @Override + public String getName() { + return key; + } } diff --git a/src/de/uhilger/mediaz/store/FileStorage.java b/src/de/uhilger/mediaz/store/FileStorage.java index 524e4d2..2d4bb1e 100644 --- a/src/de/uhilger/mediaz/store/FileStorage.java +++ b/src/de/uhilger/mediaz/store/FileStorage.java @@ -22,6 +22,7 @@ import de.uhilger.mediaz.App; import de.uhilger.mediaz.Server; import de.uhilger.mediaz.entity.Ablageort; +import de.uhilger.mediaz.entity.Einstellung; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; @@ -54,17 +55,19 @@ /** StorageType Ablageort */ public static final String ST_ABLAGEORT = "Ablageort"; - private String fileBase; + private final String fileBase; - private Map<String, TypeToken> types; + private final Map<String, TypeToken> types; public FileStorage(String base) { this.fileBase = base; // Beispiel: TypeToken<List<String>> list = new TypeToken<List<String>>() {}; TypeToken<Ablageort> ttAblageort = new TypeToken<Ablageort>() {}; + TypeToken<Einstellung> ttEinstellung = new TypeToken<Einstellung>() {}; types = new HashMap(); types.put(Ablageort.class.getSimpleName(), ttAblageort); + types.put(Einstellung.class.getSimpleName(), ttEinstellung); } /** @@ -72,13 +75,17 @@ * * Es wird in den Ordner geschrieben, der von conf angegeben ist * - * Wenn es z.B. ein Ablage-Objekt ist, wird das Objekt in die Datei - * [conf]/Ablage/[name der Ablage].json geschrieben - * - * Der Name der Ablage muss eindeutig sein + * Wenn es z.B.ein Ablage-Objekt ist, wird das Objekt in die Datei + [conf]/Ablage/[name der Ablage].json geschrieben + + Der Name der Ablage muss eindeutig sein * * - * @param entity + * @param entity das Objekt, das geschrieben werden soll + * @param overwrite true, wenn Aenderung, false fuer neue Elemente + * @return die Datei oder null, wenn die Datei existiert und ein + * neues Element (overwrite=false) uebergeben wurde + * @throws java.io.IOException */ public File writeToFile(Entity entity, boolean overwrite) throws IOException { String className = entity.getClass().getSimpleName(); @@ -86,9 +93,6 @@ File dir = new File(fileBase, className); dir.mkdirs(); File file = new File(dir, entity.getName()); - //if(file.exists()) { - // file.delete(); - //} if(file.exists() && !overwrite) { return null; } else { diff --git a/www/ui/data/tpl/einstellung_liste.tpl b/www/ui/data/tpl/einstellung_liste.tpl new file mode 100644 index 0000000..d56c893 --- /dev/null +++ b/www/ui/data/tpl/einstellung_liste.tpl @@ -0,0 +1,11 @@ +<div class='entity-formular'> + <div class='entity-liste-kopf'> + Einstellungen + <button class='ctrl-btn' id='neu-btn' title='Neu'><i class='icon-doc'></i></button> + </div> + <ul class='entity-liste'> + {{#ArrayList}} + <li class='entity-eintrag'>{{.}}</li> + {{/ArrayList}} + </ul> +</div> \ No newline at end of file diff --git a/www/ui/data/tpl/form_einstellung.tpl b/www/ui/data/tpl/form_einstellung.tpl new file mode 100644 index 0000000..382dad1 --- /dev/null +++ b/www/ui/data/tpl/form_einstellung.tpl @@ -0,0 +1,14 @@ +<div class="entity-formular"> + Einstellung + + <input class="entity-element" type="text" id="einstellung-key" placeholder="Key" value="{{key}}" /> + <input class="entity-element" type="text" id="einstellung-value" placeholder="Pfad" value="{{value}}" /> + <div class="entity-buttons"> + <button class="button-primary" id="ok-btn">Speichern</button> + <button class="button" id="cancel-btn">Abbrechen</button> + <button class="button" id="loeschen-btn">Löschen</button> + </div> + +</div> + + diff --git a/www/ui/index.html b/www/ui/index.html index a5b5e4d..e4240b9 100644 --- a/www/ui/index.html +++ b/www/ui/index.html @@ -49,6 +49,7 @@ <a id="mi-orte"><i class="icon-database"></i> Kataloge</a> <a id="mi-player"><i class="icon-play"></i> Abspieler</a> <a id="mi-radio"><i class="icon-ellipsis-vert"></i> Radio</a> + <a id="mi-prefs"><i class="icon-sliders"></i> Einstellungen</a> </div> </div> </div> diff --git a/www/ui/js/app.js b/www/ui/js/app.js index a1fbec4..0063a18 100644 --- a/www/ui/js/app.js +++ b/www/ui/js/app.js @@ -4,6 +4,10 @@ this.url = u; } +function Einstellung(k, v) { + this.key = k; + this.value = v; +} function Mediazentrale() { @@ -64,6 +68,8 @@ }); } }; + + /* Die folgenden '_liste' Funktionen koennen noch vereinheitlicht werden */ this.ablageort_liste = function() { self.http_get('../api/store/Ablageort/', function (responseText) { @@ -79,6 +85,73 @@ self.addEvtListener('#neu-btn', 'click', function (event) { eval("self.ablageort_form" + "(this)"); }); + }); + }); + }; + + this.prefs_liste = function() { + self.http_get('../api/store/Einstellung/', function (responseText) { + self.vorlage_laden_und_fuellen("data/tpl/einstellung_liste.tpl", JSON.parse(responseText), function (html) { + document.querySelector(".zentraler-inhalt").innerHTML = html; + self.addEvtListener('.entity-eintrag', 'click', function (event) { + var t = event.target; + self.http_get('../api/store/Einstellung/' + t.textContent, function(responseText){ + var einstellung = JSON.parse(responseText); + self.prefs_form(einstellung); + }); + }); + self.addEvtListener('#neu-btn', 'click', function (event) { + eval("self.prefs_form" + "(this)"); + }); + }); + }); + }; + + /* Die folgenden '_form' Funktionen koennen noch vereinheitlicht werden */ + + this.prefs_form = function(k) { + self.vorlage_laden_und_fuellen("data/tpl/form_einstellung.tpl", k, function (html) { + document.querySelector(".zentraler-inhalt").innerHTML = html; + self.addEvtListener('#ok-btn', 'click', function () { + var pkey = document.querySelector('#einstellung-key').value; + pkey = pkey.replace(' ', '').replace(/[\W]+/g, ''); + var pref = new Einstellung( + pkey, + document.querySelector('#einstellung-value').value + ); + var daten = JSON.stringify(pref); + if(typeof k === "undefined" || k.key !== pkey) { + // neu + self.http_post('../api/store/Einstellung', daten, function (responseText) { + // hier die Antwort verarbeiten + }); + } else { + // aendern + self.http_put('../api/store/Einstellung', daten, function (responseText) { + // hier die Antwort verarbeiten + }); + } + //document.querySelector(".zentraler-inhalt").innerHTML = ''; + self.prefs_liste(); + }); + self.addEvtListener('#cancel-btn', 'click', function () { + //document.querySelector(".zentraler-inhalt").innerHTML = ''; + self.prefs_liste(); + }); + self.addEvtListener('#loeschen-btn', 'click', function() { + var pkey = document.querySelector('#einstellung-key').value; + var dlgdata = {"del-elem": pkey}; + self.dialog_laden_und_zeigen('data/tpl/dlg-loeschen.tpl', dlgdata, function() { + self.addEvtListener('#nein-btn', 'click', self.dialog_schliessen); + self.addEvtListener('#ja-btn', 'click', function() { + self.http_delete('../api/store/Einstellung/' + pkey, '', function (responseText) { + // hier die Antwort verarbeiten + self.dialog_schliessen(); + //document.querySelector(".zentraler-inhalt").innerHTML = ''; + self.prefs_liste(); + }); + }); + }); }); }); }; @@ -226,6 +299,7 @@ self.addEvtListener('#mi-katalog', 'click', self.media_liste); self.addEvtListener('#mi-orte', 'click', self.ablageort_liste); + self.addEvtListener('#mi-prefs', 'click', self.prefs_liste); //self.addEvtListener('#mi-list', 'click', self.media_liste); self.fusszeile_umschalten(); -- Gitblit v1.9.3