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