From 748b6fdf9af24a605525fbf7b5d5a759460108bb Mon Sep 17 00:00:00 2001
From: ulrich
Date: Fri, 09 Apr 2021 13:46:23 +0000
Subject: [PATCH] JavaScript aufgeraeumt

---
 www/ui/js/app.js |  391 ++++++++++++++++++++++++++++++++++---------------------
 1 files changed, 242 insertions(+), 149 deletions(-)

diff --git a/www/ui/js/app.js b/www/ui/js/app.js
index ded0e0e..7a903d3 100644
--- a/www/ui/js/app.js
+++ b/www/ui/js/app.js
@@ -4,6 +4,8 @@
   var cache; // mustache templates
   var ortPfad;
   var mediaPfad;
+  var katUrl;
+  var selTitel;
 
   this.init = function () {
     self.mediaPfad = '/';
@@ -25,41 +27,24 @@
     self.addEvtListener('#mi-orte', 'click', self.ablageort_liste);
     self.addEvtListener('#mi-prefs', 'click', self.prefs_liste);
     self.addEvtListener('#mi-player', 'click', self.abspieler_liste);
+    self.addEvtListener('#mi-listen', 'click', self.abspielliste_liste);
+    self.addEvtListener('#mi-list', 'click', self.titel_liste);
     
     self.fusszeile_umschalten();
     self.seitenleiste_umschalten();
     self.dialog_unten_zeigen();
   };
   
-  this.abspieler_auswahl_fuellen = function() {
-    self.http_get('../api/store/Abspieler/', function (responseText) {
-      /*
-       {"ArrayList": [{"name":"Wohnz","url":"http://rpi4-wz:9090/"},{"name":"Arbz","url":"http://rpi4-az:9090/"}]}
-       */
-      self.vorlage_laden_und_fuellen("data/tpl/abs_sel.tpl", JSON.parse(responseText), function (html) {
-        document.querySelector(".abs-sel").innerHTML = html;
-      });    
-    });
-  };
+  /* ---------------- Entitaets-Listen ----------------- */
 
-  /* Unterer Einblendbereich */
-  
-  this.dialog_unten_zeigen = function() {
-    self.vorlage_laden_und_fuellen("data/tpl/ctrl.tpl", "", function (html) {
-      var dlg = document.querySelector(".dialog-unten");
-      dlg.style.height = '4.5em';
-      dlg.innerHTML = html;
-      self.abspieler_auswahl_fuellen();
-    });
-  };
-
-  
   // auf der obersten Ebene werden die Kataloge angezeigt,
   // darunter der Inhalt des aktuellen Pfades
   this.media_liste = function() {
+    self.reset_top_buttons();
     if(self.ortPfad === '/') {
       // Kataloge listen
       self.http_get('../api/store/Ablageort/liste/', function (responseText) {
+        //document.querySelector('#top-up-btn').removeEventListener('click', self.media_liste_herauf);
         self.vorlage_laden_und_fuellen("data/tpl/katalog_root_liste.tpl", JSON.parse(responseText), function (html) {
           document.querySelector(".zentraler-inhalt").innerHTML = html;
           self.addEvtListener('.entity-eintrag', 'click', function (event) {
@@ -73,41 +58,34 @@
         });
       });
     } else {
-      // Pfad listen
-      // console.log("vorher ortPfad: " + self.ortPfad);
-      //console.log("vorher mediaPfad: " + self.mediaPfad);
-      //self.http_get('..' + self.ortPfad + '/' + self.mediaPfad + '/', function(responseText) {
       var url = '..' + self.ortPfad + self.mediaPfad;
       if(!url.endsWith('/')) {
         url = url + '/';
       }
-      //console.log("url: " + url);      
       self.http_get(url, function(responseText) {
-        //console.log(responseText);
         self.vorlage_laden_und_fuellen("data/tpl/katalog_inhalt_liste.tpl", JSON.parse(responseText), function (html) {
           document.querySelector(".zentraler-inhalt").innerHTML = html;
-          //console.log("mediaPfad bei Anzeige: " + self.mediaPfad);
           self.addEvtListener('.entity-eintrag', 'click', function (event) {
             var t = event.target;
             var tx = t.textContent;
-            //console.log("tx: " + tx);
-            //console.log("mediaPfad nach Auswahl: " + self.mediaPfad);
             if(t.classList.contains("entity-typ-folder")) {
               if(self.mediaPfad.endsWith('/')) {
                 self.mediaPfad = self.mediaPfad + tx;                
               } else {
                 self.mediaPfad = self.mediaPfad + '/' + tx;
               }
-              //self.mediaPfad = self.mediaPfad + t.textContent;
-              //console.log("mediaPfad neu: " + self.mediaPfad);
               self.media_liste();
             } else {
-              //console.log("Media-Inhalt auswaehlen oder abspielen");
-              self.removeClassMulti('selected');
-              t.classList.add('selected');
+              if(t.classList.contains('selected')) {
+                self.addSelectedTitel();
+              } else {
+                self.removeClassMulti('selected');
+                t.classList.add('selected');
+              }
+              //self.selTitel = new Titel(t.textContent, self.ortPfad);       
             }
           });
-          self.addEvtListener('#zurueck-btn', 'click', function (event) {
+          self.addEvtListener('#top-up-btn', 'click', function(event) {
             if(self.mediaPfad === '/') {
               self.ortPfad = '/';              
             } else {
@@ -118,17 +96,35 @@
               } else {
                 parent = '/';
               }
-              //console.log("Parent: " + parent);
               self.mediaPfad = parent;
             }
             self.media_liste();
-          });        
+          });
         });
       });
     }
   };
+  
+  this.addSelectedTitel = function() {
+    var elem = document.querySelector(".selected");
+    var titelName = elem.textContent;
+    var album = elem.attributes.album.nodeValue;
+    var interpret = elem.attributes.interpret.nodeValue;
+    var anzName = elem.attributes.titelAnzName.nodeValue;
+    var titel;
+    if(self.mediaPfad.endsWith('/')) {
+      titel = new Titel(titelName, self.mediaPfad, self.ortPfad, interpret, anzName, album);
+    } else {
+      titel = new Titel(titelName, self.mediaPfad + '/', self.ortPfad, interpret, anzName, album);
+    }
+    var plname = document.querySelector('#playlist').value;
+    self.http_put('../api/alist/' + plname, JSON.stringify(titel), function(responseText) {
+      self.meldung_mit_timeout(responseText, 1500);
+    });
+  };
 
   this.ablageort_liste = function() {
+    self.reset_top_buttons();
     self.http_get('../api/store/Ablageort/liste/', function (responseText) {
       self.vorlage_laden_und_fuellen("data/tpl/ablageort_liste.tpl", JSON.parse(responseText), function (html) {
         document.querySelector(".zentraler-inhalt").innerHTML = html;
@@ -139,7 +135,8 @@
             self.ablageort_form(ablageort);
           });
         });
-        self.addEvtListener('#neu-btn', 'click', function (event) {
+        //self.addEvtListener('#neu-btn', 'click', function (event) {
+        self.addEvtListener('#top-neu-btn', 'click', function(event) {
           eval("self.ablageort_form" + "(this)");
         });        
       });
@@ -147,6 +144,7 @@
   };
 
   this.prefs_liste = function() {
+    self.reset_top_buttons();
     self.http_get('../api/store/Einstellung/liste/', function (responseText) {
       self.vorlage_laden_und_fuellen("data/tpl/einstellung_liste.tpl", JSON.parse(responseText), function (html) {
         document.querySelector(".zentraler-inhalt").innerHTML = html;
@@ -157,7 +155,7 @@
             self.prefs_form(einstellung);
           });
         });
-        self.addEvtListener('#neu-btn', 'click', function (event) {
+        self.addEvtListener('#top-neu-btn', 'click', function(event) {
           eval("self.prefs_form" + "(this)");
         });        
       });
@@ -175,55 +173,75 @@
             self.abspieler_form(abspieler);
           });
         });
-        self.addEvtListener('#neu-btn', 'click', function (event) {
+        //self.addEvtListener('#neu-btn', 'click', function(event) {
+        self.addEvtListener('#top-neu-btn', 'click', function(event) {
           eval("self.abspieler_form" + "(this)");
-        });        
+        });           
       });
     });
   };
-
+  
+  this.abspielliste_liste = function() {
+    self.http_get('../api/store/Abspielliste/liste/', function (responseText) {
+      self.vorlage_laden_und_fuellen("data/tpl/abspielliste_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/Abspielliste/' + t.textContent, function(responseText){
+            var abspielliste = JSON.parse(responseText);
+            self.abspielliste_form(abspielliste);
+          });
+        });
+        self.addEvtListener('#top-neu-btn', 'click', function(event) {
+          eval("self.abspielliste_form" + "(this)");
+        });            
+      });
+    });
+  };
+  
+  /* -------------------- Entitaets-Formulare ------------------ */
+  
+  this.abspielliste_form = function(al) {
+    self.vorlage_laden_und_fuellen("data/tpl/form_abspielliste.tpl", al, function (html) {
+      document.querySelector(".zentraler-inhalt").innerHTML = html;
+      const form = document.querySelector('form');      
+      form.addEventListener('submit', function(event) {
+        self.handle_submit(event, al.name, '../api/store/Abspielliste/', '#abspielliste-name', function() {
+          self.abspielliste_liste();
+        });
+      });
+      self.addEvtListener('#cancel-btn', 'click', function () {
+        //document.querySelector(".zentraler-inhalt").innerHTML = '';
+        self.abspielliste_liste();
+      });
+      self.addEvtListener('#loeschen-btn', 'click', function(event) {
+        event.preventDefault();
+        self.handle_del_btn('#abspielliste-name', '../api/store/Abspielliste/', function() {
+          self.abspielliste_auswahl_fuellen();
+          self.abspielliste_liste();
+        });
+      });
+    });
+  };
+  
   this.abspieler_form = function(pl) {
     self.vorlage_laden_und_fuellen("data/tpl/form_abspieler.tpl", pl, function (html) {
       document.querySelector(".zentraler-inhalt").innerHTML = html;
-      self.addEvtListener('#ok-btn', 'click', function () {
-        var plname = document.querySelector('#abspieler-name').value;
-        plname = plname.replace(' ', '').replace(/[\W]+/g, '');
-        var abspieler = new Abspieler(
-          plname,
-          document.querySelector('#abspieler-url').value
-        );
-        var daten = JSON.stringify(abspieler);        
-        if(typeof pl === "undefined" || pl.key !== plname) {
-          // neu
-          self.http_post('../api/store/Abspieler', daten, function (responseText) {
-            // hier die Antwort verarbeiten
-          });
-        } else {
-          // aendern
-          self.http_put('../api/store/Abspieler', daten, function (responseText) {
-            // hier die Antwort verarbeiten
-          });
-        }
-        //document.querySelector(".zentraler-inhalt").innerHTML = '';
-        self.abspieler_liste();
+      const form = document.querySelector('form');      
+      form.addEventListener('submit', function(event) {
+        self.handle_submit(event, pl.key, '../api/store/Abspieler/', '#abspieler-name', function() {
+          self.abspieler_liste();
+        });
       });
       self.addEvtListener('#cancel-btn', 'click', function () {
         //document.querySelector(".zentraler-inhalt").innerHTML = '';
         self.abspieler_liste();
       });
-      self.addEvtListener('#loeschen-btn', 'click', function() {
-        var plname = document.querySelector('#abspieler-name').value;
-        var dlgdata = {"del-elem": plname};
-        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/Abspieler/' + plname, '', function (responseText) {
-              // hier die Antwort verarbeiten
-              self.dialog_schliessen();
-              //document.querySelector(".zentraler-inhalt").innerHTML = '';
-              self.abspieler_liste();
-            });
-          });
+      self.addEvtListener('#loeschen-btn', 'click', function(event) {
+        event.preventDefault();
+        self.handle_del_btn('#abspieler-name', '../api/store/Abspieler/', function() {
+          self.abspieler_auswahl_fuellen();
+          self.abspieler_liste();
         });
       });
     });
@@ -232,45 +250,21 @@
   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
-          });
-        }
+      const form = document.querySelector('form');      
+      form.addEventListener('submit', function(event) {
+        self.handle_submit(event, k.key, '../api/store/Einstellung/', '#einstellung-key', function() {
+          self.prefs_liste();
+        });
+      });
+      self.addEvtListener('#cancel-btn', 'click', function (event) {
         //document.querySelector(".zentraler-inhalt").innerHTML = '';
+        event.preventDefault();
         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();
-            });
-          });
+      self.addEvtListener('#loeschen-btn', 'click', function(event) {
+        event.preventDefault();
+        self.handle_del_btn('#einstellung-key', '../api/store/Einstellung/', function() {
+          self.prefs_liste();
         });
       });
     });
@@ -287,51 +281,137 @@
   this.ablageort_form = function(ort) {
     self.vorlage_laden_und_fuellen("data/tpl/form_ablageort.tpl", ort, function (html) {
       document.querySelector(".zentraler-inhalt").innerHTML = html;
-      self.addEvtListener('#ok-btn', 'click', function () {
-        var aName = document.querySelector('#ablageort-name').value;
-        aName = aName.replace(' ', '').replace(/[\W]+/g, '');
-        var a = new Ablageort(
-          aName,
-          document.querySelector('#ablageort-ort').value,
-          document.querySelector('#ablageort-url').value
-        );
-        var daten = JSON.stringify(a);        
-        if(typeof ort === "undefined" || ort.name !== aName) {
-          // neu
-          self.http_post('../api/store/Ablageort', daten, function (responseText) {
-            // hier die Antwort verarbeiten
-          });
-        } else {
-          // aendern
-          self.http_put('../api/store/Ablageort', daten, function (responseText) {
-            // hier die Antwort verarbeiten
-          });
-        }
-        //document.querySelector(".zentraler-inhalt").innerHTML = '';
-        self.ablageort_liste();
+      const form = document.querySelector('form');      
+      form.addEventListener('submit', function(event) {
+        self.handle_submit(event, ort.name, '../api/store/Ablageort/', '#ablageort-name', function() {
+          self.ablageort_liste();
+        });
       });
       self.addEvtListener('#cancel-btn', 'click', function () {
         //document.querySelector(".zentraler-inhalt").innerHTML = '';
         self.ablageort_liste();
       });
-      self.addEvtListener('#loeschen-btn', 'click', function() {
-        var aoname = document.querySelector('#ablageort-name').value;
-        var dlgdata = {"del-elem": aoname};
-        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/Ablageort/' + aoname, '', function (responseText) {
-              // hier die Antwort verarbeiten
-              self.dialog_schliessen();
-              //document.querySelector(".zentraler-inhalt").innerHTML = '';
-              self.ablageort_liste();
-            });
-          });
+      self.addEvtListener('#loeschen-btn', 'click', function(event) {
+        event.preventDefault();
+        self.handle_del_btn('#ablageort-name', '../api/store/Ablageort/', function() {
+          self.ablageort_liste();
         });
       });
     });
   };
+  
+  /* ------------------------------- UI-Dynamik ----------------------- */
+  
+  self.reset_top_buttons = function() {
+    self.vorlage_laden_und_fuellen("data/tpl/top_btns.tpl", '', function (html) {
+      document.querySelector(".top-btns").innerHTML = html;
+    });
+  };
+  
+  this.abspieler_auswahl_fuellen = function() {
+    self.http_get('../api/store/Abspieler/', function (responseText) {
+      self.vorlage_laden_und_fuellen("data/tpl/abs_sel.tpl", JSON.parse(responseText), function (html) {
+        document.querySelector(".abs-sel").innerHTML = html;
+      });    
+    });
+  };
 
+  this.abspielliste_auswahl_fuellen = function() {
+    self.http_get('../api/store/Abspielliste/', function (responseText) {
+      self.vorlage_laden_und_fuellen("data/tpl/pl_sel.tpl", JSON.parse(responseText), function (html) {
+        document.querySelector(".pl-sel").innerHTML = html;
+      });    
+    });
+  };
+  
+  /* Unterer Einblendbereich */
+  
+  this.dialog_unten_zeigen = function() {
+    self.vorlage_laden_und_fuellen("data/tpl/ctrl.tpl", "", function (html) {
+      var dlg = document.querySelector(".dialog-unten");
+      dlg.style.height = '4.5em';
+      dlg.innerHTML = html;
+      self.abspieler_auswahl_fuellen();
+      self.abspielliste_auswahl_fuellen();
+      self.addEvtListener('#dazu-btn', 'click', self.addSelectedTitel);
+      self.media_liste();
+    });
+  };
+  
+  /* Titel einer Abspielliste */
+  
+  this.titel_liste = function() {
+    self.reset_top_buttons();
+    var plname = document.querySelector('#playlist').value;
+    self.http_get('../api/alist/' + plname, function (responseText) {
+      self.vorlage_laden_und_fuellen("data/tpl/titel_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.removeClassMulti('selected');
+          t.classList.add('selected');
+        });
+      });
+    });
+  };  
+  
+  /* ------------- Helfer fuer Entitaets-Formulare ----------------------- */
+      
+  /*
+   * existingKey: wenn die Entitaet existiert und geandert werden soll
+   *                 leer, wenn neue Entitaet 
+   */
+  this.handle_submit = function(event, existingKey, putUrl, keySelector, cb) {
+    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) {
+        if(typeof(cb) !== 'function') {
+          // ..
+        } else {
+          cb();
+        }
+      });
+    } else {
+      // aendern
+      self.http_put(putUrl + existingKey, daten, function (responseText) {
+        if(typeof(cb) !== 'function') {
+          // ..
+        } else {
+          cb();
+        }
+      });
+    }
+  };
+  
+  this.handle_del_btn = function(selectorKey, delUrl, cb) {
+    var pkey = document.querySelector(selectorKey).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(event) {
+        console.log("loeschen geklickt.");
+        self.http_delete(delUrl + pkey, '', function (responseText) {
+          self.dialog_schliessen();
+          if(typeof(cb) !== 'function') {
+            // ..
+          } else {
+            cb();
+          }
+        });
+      });
+    });    
+  };  
+  
+  /* ------------------ sonstige Helfer ----------------------- */
+      
   this.addEvtListener = function(selector, eventName, func) {
     document.querySelectorAll(selector).forEach(elem => { elem.addEventListener(eventName, func); });
   };
@@ -564,4 +644,17 @@
 function Abspieler(n, u) {
   this.name = n;
   this.url = u;
+}
+
+function Abspielliste(n) {
+  this.name = n;
+}
+
+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;
 }
\ No newline at end of file

--
Gitblit v1.9.3