ulrich@undisclosed
2020-05-11 f9d0c4a3da9d183c90bc7351f233fa67ea3c66a7
commit | author | age
3d5fe0 1 function NutzerApp() {
002c44 2   var self = this;
U 3   var appMenu;
4   var vorlagen;
5   var api;
6   var userid;
7   var pfad = '';
8   var loc;
9   var modus = 'kacheln';
10
11   this.datei_neuer_text = function () {
12     self.meldung_mit_timeout("Neuer Text", 1500);
13   };
f9d0c4 14   
U 15   /* Nutzerverwaltung */
16   
17   this.nutzerliste_klick = function(event) {
18     var target = event.target;
19     var gewaehlterNutzer = document.querySelector(".nutzer-gewaehlt");
20     if(gewaehlterNutzer !== null) {
21       gewaehlterNutzer.classList.remove("nutzer-gewaehlt");
22     }
23     target.classList.add("nutzer-gewaehlt");
24   };
002c44 25
U 26   /* Funktionen aus App-Vorlage */
27
28   this.init = function () {
29     self.vorlagen = new Vorlagen();
30     self.appMenu = new AppMenu();
31     self.appMenu.init(
32             "data/menu/",
33             "hauptmenue.json",
34             "data/tpl/app-menu.tpl",
35             ".west",
36             "8em");
37     document.querySelector('.hamburger').addEventListener('click', function (e) {
38       self.menue_umschalten();
39     });
e86f3c 40     var dlg = document.querySelector(".dialog");
U 41     dlg.style.flexBasis = '0em';
42     self.seitenleiste_umschalten();
002c44 43     self.um_get_login();
U 44     self.um_get_user_list();
45     self.loc = window.location.protocol + '//' + window.location.host;
46   };
47
48   this.login_zeigen = function() {
49     self.meldung_mit_timeout("Benutzer: " + self.userid, 1500);
50   };
51   
52   this.menue_umschalten = function () {
53     var ham = document.querySelector(".hamburger");
54     ham.classList.toggle("is-active"); // hamburger-icon umschalten
55     self.appMenu.toggle(); // menue oeffnen/schliessen
56   };
57
58   this.info_dialog_zeigen = function () {
59     self.dialog_laden_und_zeigen('data/tpl/dlg-info.tpl', '');
3d5fe0 60     self.menue_umschalten();
U 61   };
62
63   this.nutzer_neu_dialog_zeigen = function () {
64     self.dialog_laden_und_zeigen('data/tpl/dlg-nutzer-neu.tpl', '');
002c44 65     self.menue_umschalten();
U 66   };
67
68   this.seitenleiste_umschalten = function () {
69     var ostDiv = document.querySelector('.ost');
70     if (ostDiv.classList.contains('ost-open')) {
71       ostDiv.classList.remove('ost-open');
72       ostDiv.style.flexBasis = '0em';
73     } else {
74       ostDiv.classList.add('ost-open');
75       ostDiv.style.flexBasis = '6em';
76     }
77     self.menue_umschalten();
78   };
79
80   this.fusszeile_umschalten = function () {
81     var suedDiv = document.querySelector('.sued');
82     if (suedDiv.classList.contains('sued-open')) {
83       suedDiv.classList.remove('sued-open');
84       suedDiv.style.height = '0';
85     } else {
86       suedDiv.classList.add('sued-open');
87       suedDiv.style.height = '1.5em';
88     }
89     self.menue_umschalten();
90   };
91
92   this.meldung_mit_timeout = function (meldung, timeout) {
93     var s = document.querySelector('.sued');
94     s.textContent = meldung;
95     setTimeout(function () {
96       s.textContent = 'Bereit.';
97       setTimeout(function () {
98         var suedDiv = document.querySelector('.sued');
99         if (suedDiv.classList.contains('sued-open')) {
100           suedDiv.classList.remove('sued-open');
101           suedDiv.style.height = '0';
102         }
103       }, 500);
104     }, timeout);
105   };
106
107   /* Dialog-Funktionen */
108
109   /*
110    Einen Dialog aus Vorlagen erzeugen
111    
112    vurl - URL zur Dialogvorlage
113    msgTpl - URL mit einer Vorlage eines Mitteilungstextes (optional)
114    */
115   this.dialog_laden_und_zeigen = function (vurl, msgTpl) {
116     if (msgTpl !== '') {
117       fetch(msgTpl)
118               .then(data => {
119                 // Handle data
120                 self.dialog_zeigen(vurl, data);
121               }).catch(error => {
122         // Handle error
123       });
124     } else {
125       self.dialog_zeigen(vurl, '');
126     }
127   };
128
129   this.dialog_zeigen = function (vurl, inhalt) {
130     var dlg = document.querySelector(".dialog");
131     self.vorlagen.html_erzeugen(
132             vurl,
133             inhalt,
134             function (html) {
3d5fe0 135               dlg.style.flexBasis = '14em';
e86f3c 136               setTimeout(function () {
U 137                 dlg.innerHTML = html;
138                 document.querySelector('.close-btn').addEventListener('click', self.dialog_schliessen);
139               }, 300);
002c44 140             });
U 141   };
142
143   this.dialog_schliessen = function () {
144     document.querySelector('.close-btn').removeEventListener('click', self.dialog_schliessen);
145     var dlg = document.querySelector('.dialog');
146     dlg.innerHTML = '';
e86f3c 147     dlg.style.flexBasis = '0em';
002c44 148   };
U 149
150   /* API functions */
151   
152   this.um_get_user_list = function() {
b73bb6 153     //var m = 'getUserNameList';
002c44 154     var m = 'getUserNameList';
U 155     var u = '../svc/' + m;
e86f3c 156     self.um_get(u, "json", function (antwort) {
b73bb6 157     self.vorlagen.html_erzeugen(
U 158       'data/tpl/inhalt.tpl',
159       JSON.parse(antwort),
160       function (h) {
161         var elem = document.getElementById('nutzer');
162         elem.innerHTML = h;
f9d0c4 163         
U 164         var absaetze = document.querySelectorAll('p.nutzer-liste-eintrag');
165         var index;
166         for (index = 0; index < absaetze.length; index++) {
167           absaetze[index].addEventListener("click", self.nutzerliste_klick);
168         }
169         
b73bb6 170       });
002c44 171     });
b73bb6 172   };
002c44 173
U 174   /* -------- An- und Abmelden ------------- */
175
176   this.um_get_login = function() {
177     var m = '?c=de.uhilger.um.pub.SessionManager&m=getSessionUser';
178     var u = '../pub' + m;
e86f3c 179     self.um_get(u, "text", function (resp) {
002c44 180       self.userid = resp;
U 181       self.login_zeigen();
182       //document.querySelector("#userMenu").textContent = resp;
183     });
184   };
185
186   this.um_logout = function() {
187     var m = '?c=de.uhilger.um.pub.SessionManager&m=expireSession';
188     var u = '../pub' + m;
e86f3c 189     self.um_get(u, "text", function (resp) {
002c44 190       //$('#userMenu').text('nicht angemeldet');
U 191       window.location.href = '../logout.html';
192     });
193   };
194
195   /* -------- ajax helper functions ----------- */
196
e86f3c 197   this.um_get = function (u, dtype, scallback) {    
002c44 198     var xmlhttp = new XMLHttpRequest();
U 199     var url = u;
200     xmlhttp.onreadystatechange = function() {
201       if (this.readyState == 4 && this.status == 200) {
202         scallback(this.responseText);
203       }
204     };
205     xmlhttp.open("GET", url, true);
206     xmlhttp.send();
207   };
3d5fe0 208   
U 209 /* ----- Hilfsfunktionen ----- */
210
211   this.serialisieren = function(obj) {
212     return '{"' + obj.constructor.name + '":' + JSON.stringify(obj) + '}';
213   };
002c44 214
U 215 }
3d5fe0 216
U 217
218 /* ----- Objekte ----- */
219
220 function User(i, p, fn, ln, em) {
221   this.id = i;
222   this.pw = p;
223   this.firstName = fn;
224   this.lastName = ln;
225   this.email = em;
226 }
227