ulrich
2020-05-18 5daeb20e992d5d0c6ab5c68051a2448c062ffe53
commit | author | age
5daeb2 1 function NutzerProfilApp() {
U 2   var self = this;
3   this.cache = {}; // mustache template cache
4
5   this.init = function () {
6     var dlg = document.querySelector(".dialog");
7     dlg.style.flexBasis = '0em';
8     document.querySelector('.west').style.flexBasis = '0em';
9     document.querySelector('.ost').style.flexBasis = '0em';
10     self.get_user_data();
11     
12     var suedDiv = document.querySelector('.sued');
13     //if (suedDiv.classList.contains('sued-open')) {
14       suedDiv.classList.remove('sued-open');
15       suedDiv.style.height = '0';
16     //}
17   };
18   
19   /*
20       {"UserData":{"id":"test","firstName":"Testvorname","lastName":"Testnachname","email":"-"}}
21    */
22   this.profil_zeigen = function(profil) {
23     self.html_erzeugen('profil-form.html', profil, function(html) {
24       document.querySelector('.zentraler-inhalt').innerHTML = html;
25       document.querySelector('.user-save-btn').addEventListener('click', self.kennwort_speichern);
26     });
27   };
28   
29   this.kennwort_speichern = function() {
30     var suedDiv = document.querySelector('.sued');
31     suedDiv.textContent = 'Kennwort speichern';
32     //if (!suedDiv.classList.contains('sued-open')) {
33       suedDiv.classList.add('sued-open');
34       suedDiv.style.height = '1.5em';
35     //}
36   };
37   
38   this.get_user_data = function() {
39     var m = '?c=de.uhilger.um.pub.SessionManager&m=getSessionUser';
40     var u = '../pub' + m;
41     self.http_get(u, function (antwort) {
42       self.profil_zeigen(JSON.parse(antwort));
43     });
44   };
45
46   /* -------- ajax helper functions ----------- */
47   
48   this.http_get = function(u, cb)  {
49     self.http_call('GET', u, null, cb);
50   };
51   
52   this.http_post = function(u, data, cb) {
53     self.http_call('POST', u, data, cb);
54   };
55
56   this.http_call = function (method, u, data, scallback) {    
57     var xhr = new XMLHttpRequest();
58     var url = u;
59     xhr.onreadystatechange = function() {
60       if (this.readyState === 4 && this.status === 200) {
61         scallback(this.responseText);
62       }
63     };
64     xhr.open(method, url);
65     if(method === 'GET')  {
66       xhr.send();
67     } else if(method === 'POST' || method === 'PUT') {
68       xhr.send(data);
69     }
70   };
71   
72 /* ----- Hilfsfunktionen ----- */
73
74   this.serialisieren = function(obj) {
75     return '{"' + obj.constructor.name + '":' + JSON.stringify(obj) + '}';
76   };
77   
78   this.addEvtListener = function(selector, eventName, func) {
79     var elems = document.querySelectorAll(selector);
80     var index;
81     for (index = 0; index < elems.length; index++) {
82       elems[index].addEventListener(eventName, func);
83     }
84   };
85   
86   this.removeAllListeners = function(elementId) {
87     var el = document.getElementById(elementId);
88     elClone = el.cloneNode(true);
89     el.parentNode.replaceChild(elClone, el);
90   }; // https://stackoverflow.com/questions/19469881/remove-all-event-listeners-of-specific-type
91
92   /* ---- Vorlagen ---- */
93
94   this.html_erzeugen = function(vurl, inhalt, cb) {
95     var vorlage = self.cache[vurl];
96     if(vorlage === undefined) {
97       self.vorlage_laden_und_fuellen(vurl, inhalt, cb);
98     } else {
99       self.vorlage_fuellen(vurl, inhalt, cb);
100     }
101   };
102
103   this.vorlage_fuellen = function(vurl, inhalt, cb) {
104     cb(Mustache.render(self.cache[vurl], inhalt));
105   };
106
107   this.vorlage_laden_und_fuellen = function(vurl, inhalt, cb) {
108     app.http_get(vurl, function(antwort) {
109       self.cache[vurl] = antwort;
110       self.vorlage_fuellen(vurl, inhalt, cb);
111     });
112   };
113 }