From 71def106dcac230c9f39f5c117ad5e275ac00d5d Mon Sep 17 00:00:00 2001
From: ulrich
Date: Fri, 09 Apr 2021 09:23:46 +0000
Subject: [PATCH] Generischer Submit-Handler (in Arbeit)

---
 www/ui/data/menu/hauptmenue.json     |    5 +
 www/ui/data/tpl/form_einstellung.tpl |   24 ++++----
 www/ui/js/app.js                     |   93 +++++++++++++++++++++++++-----
 www/ui/data/tpl/test_form.tpl        |   10 +++
 4 files changed, 103 insertions(+), 29 deletions(-)

diff --git a/www/ui/data/menu/hauptmenue.json b/www/ui/data/menu/hauptmenue.json
index 45a2884..478d0a4 100644
--- a/www/ui/data/menu/hauptmenue.json
+++ b/www/ui/data/menu/hauptmenue.json
@@ -8,6 +8,11 @@
     },
     "inhalt":  [
       {
+        "titel": "Form Test",
+        "umenue": false,
+        "funktion": "app.test_form"
+      },
+      {
         "titel": "Media",
         "umenue": false,
         "funktion": "app.media_liste"
diff --git a/www/ui/data/tpl/form_einstellung.tpl b/www/ui/data/tpl/form_einstellung.tpl
index b5493e1..9500ecf 100644
--- a/www/ui/data/tpl/form_einstellung.tpl
+++ b/www/ui/data/tpl/form_einstellung.tpl
@@ -1,14 +1,14 @@
-<div class="entity-formular">
-  Einstellung
+<form>
+  <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="Wert" 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&ouml;schen</button>
+    <input name="key" class="entity-element" type="text" id="einstellung-key" placeholder="Key" value="{{key}}" />
+    <input name="value" class="entity-element" type="text" id="einstellung-value" placeholder="Wert" value="{{value}}" />
+    <div class="entity-buttons">
+      <button type="submit" class="button-primary" id="ok-btn">Speichern</button>
+      <button class="button" id="cancel-btn">Abbrechen</button>
+      <button class="button" id="loeschen-btn">L&ouml;schen</button>
+    </div>
+
   </div>
-
-</div>
-
-
+</form>
\ No newline at end of file
diff --git a/www/ui/data/tpl/test_form.tpl b/www/ui/data/tpl/test_form.tpl
new file mode 100644
index 0000000..eee396e
--- /dev/null
+++ b/www/ui/data/tpl/test_form.tpl
@@ -0,0 +1,10 @@
+
+<form name="myForm">
+  <input name="key" class="entity-element" type="text" id="einstellung-key" placeholder="Key" value="{{key}}" />
+  <input name="val" class="entity-element" type="text" id="einstellung-value" placeholder="Wert" value="{{value}}" />
+  <div class="entity-buttons">
+    <button class="button-primary" id="ok-btn" type="submit">Speichern</button>
+    <button class="button" id="cancel-btn">Abbrechen</button>
+    <button class="button" id="loeschen-btn">L&ouml;schen</button>
+  </div>
+</form>
diff --git a/www/ui/js/app.js b/www/ui/js/app.js
index 6b62511..22ad482 100644
--- a/www/ui/js/app.js
+++ b/www/ui/js/app.js
@@ -35,6 +35,42 @@
     self.dialog_unten_zeigen();
   };
   
+  /*
+   * Der StorageHandler verarbeitet Entitaeten mit z.B. 
+   * HTTP PUT Ablageort/[Name]
+   * und erwartet dabei eine JSON-Struktur, die der Entitaet entspricht. 
+   * Beim Ablageort z.B.
+   *   private String name;
+   *   private String ort;
+   *   private String url;
+   *   
+   * Im HTML-Formular muessen die input-Elemente mit name="..." 
+   * so genannt werden wie die Felder der Entitaet. Fuer Ablageort also
+   * 
+   *   <input name="name" 
+   *   <input name="ort" 
+   *   <input name="url" 
+   * 
+   * Auf diese Weise kann eine generische Funktion die Formulardaten 
+   * an den REST-Endpunkt des Servers uebermitteln.
+   */  
+  this.test_form = function() {
+    self.vorlage_laden_und_fuellen("data/tpl/test_form.tpl", "", function (html) {
+      document.querySelector(".zentraler-inhalt").innerHTML = html;
+      const form = document.querySelector('form');
+      form.addEventListener('submit', self.handleSubmit);
+    });    
+  };
+
+  this.handleSubmit = function(event) {
+    event.preventDefault();
+    const data = new FormData(event.target);
+    const value = Object.fromEntries(data.entries());
+    console.log({ value });
+    console.log(JSON.stringify(value));
+  };
+  
+  
   this.abspieler_auswahl_fuellen = function() {
     self.http_get('../api/store/Abspieler/', function (responseText) {
       /*
@@ -175,22 +211,6 @@
       });
     }
   };
-  
-  /*
-
-        {"Medialiste": [{"name":"sound_city.m4v","typ":"video","interpret":"","titelAnzName":"","album":""},{"name":"sub","typ":"folder","interpret":"","titelAnzName":"","album":""},{"name":"Aufnahmen.ods","typ":"file","interpret":"","titelAnzName":"","album":""},{"name":"index.html","typ":"file","interpret":"","titelAnzName":"","album":""},{"name":"A","typ":"folder","interpret":"","titelAnzName":"","album":""},{"name":"sleepers.m4v","typ":"video","interpret":"","titelAnzName":"","album":""}]}
-
-
-        function Titel(n, p, u, i, t, a) {
-          this.katalogUrl = u;
-          this.pfad = p;
-          this.name = n;
-          this.interpret = i;
-          this.titelAnzName = t;
-          this.album  = a;
-        }
-
-   */
   
   this.addSelectedTitel = function() {
     var elem = document.querySelector(".selected");
@@ -336,7 +356,7 @@
       });
     });
   };
-
+  
   this.abspieler_form = function(pl) {
     self.vorlage_laden_und_fuellen("data/tpl/form_abspieler.tpl", pl, function (html) {
       document.querySelector(".zentraler-inhalt").innerHTML = html;
@@ -388,9 +408,47 @@
     });
   };
 
+  /*
+   * existingKey: wenn die Entitaet existiert und geandert werden soll
+   *                 leer, wenn neue Entitaet 
+   */
+  this.handle_my_submit = function(event, existingKey, putUrl, keySelector) {
+    event.preventDefault();
+    const data = new FormData(event.target);
+    const value = Object.fromEntries(data.entries());
+    console.log({ value });
+    console.log(JSON.stringify(value));
+    var daten = JSON.stringify(value);
+    var formkey = document.querySelector(keySelector).value;
+    formkey = formkey.replace(' ', '').replace(/[\W]+/g, '');
+
+    if(typeof existingKey === "undefined" ||  existingKey.length < 1) {
+      // neu
+      self.http_put(putUrl + formkey, daten, function (responseText) {
+        // hier die Antwort verarbeiten
+        self.prefs_liste();
+      });
+    } else {
+      // aendern
+      self.http_put(putUrl + existingKey, daten, function (responseText) {
+        // hier die Antwort verarbeiten
+        self.prefs_liste();
+      });
+    }
+    
+    
+  };
+
   this.prefs_form = function(k) {
     self.vorlage_laden_und_fuellen("data/tpl/form_einstellung.tpl", k, function (html) {
       document.querySelector(".zentraler-inhalt").innerHTML = html;
+      const form = document.querySelector('form');
+      form.addEventListener('submit', function(event) {
+        self.handle_my_submit(event, k.key, '../api/store/Einstellung/', '#einstellung-key');
+      });
+      
+      
+      /*
       self.addEvtListener('#ok-btn', 'click', function () {
         var pkey = document.querySelector('#einstellung-key').value;
         pkey = pkey.replace(' ', '').replace(/[\W]+/g, '');
@@ -414,6 +472,7 @@
           });
         }
       });
+      */
       self.addEvtListener('#cancel-btn', 'click', function () {
         //document.querySelector(".zentraler-inhalt").innerHTML = '';
         self.prefs_liste();

--
Gitblit v1.9.3