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