ulrich
2020-05-18 a0da3240882184b461d70c519cc8ec0469be9367
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');
2b837d 13     suedDiv.classList.remove('sued-open');
U 14     suedDiv.style.height = '0';
15     document.querySelector('.zurueck-btn').style.color = '#eee';
5daeb2 16   };
U 17   
18   /*
19       {"UserData":{"id":"test","firstName":"Testvorname","lastName":"Testnachname","email":"-"}}
20    */
21   this.profil_zeigen = function(profil) {
22     self.html_erzeugen('profil-form.html', profil, function(html) {
23       document.querySelector('.zentraler-inhalt').innerHTML = html;
24       document.querySelector('.user-save-btn').addEventListener('click', self.kennwort_speichern);
25     });
26   };
2b837d 27
5daeb2 28   this.kennwort_speichern = function() {
2b837d 29     var userId = document.querySelector('#anmeldename').textContent;
U 30     var aktKw = document.querySelector('#kennwort').value;
31     var neuKw = document.querySelector('#kennwortNeu').value;
32     var wKw = document.querySelector('#kennwortw').value;
33     if(neuKw === wKw) {
34       var m = '?c=de.uhilger.um.api.Profil&m=setUserPw&p=' + userId + '&p=' + aktKw + '&p=' + neuKw;
35       var u = '../prf' + m;
36       self.http_get(u, function(antwort) {
37         self.meldung(antwort, 1500);
38       });
39     } else {
40       // Kennworte unterschiedlich
41       self.meldung('Kennworte stimmen nicht überein', 1500);
42     }
43   };
44   
45   this.meldung = function(text, dauer) {
5daeb2 46     var suedDiv = document.querySelector('.sued');
2b837d 47     suedDiv.textContent = text;
U 48     suedDiv.classList.add('sued-open');
49     suedDiv.style.height = '1.5em';
50     setTimeout(function () {
51       var suedDiv = document.querySelector('.sued');
52       if (suedDiv.classList.contains('sued-open')) {
53         suedDiv.classList.remove('sued-open');
54         suedDiv.style.height = '0';
55       }
56     }, 1500);    
5daeb2 57   };
U 58   
59   this.get_user_data = function() {
60     var m = '?c=de.uhilger.um.pub.SessionManager&m=getSessionUser';
61     var u = '../pub' + m;
62     self.http_get(u, function (antwort) {
63       self.profil_zeigen(JSON.parse(antwort));
64     });
65   };
66
67   /* -------- ajax helper functions ----------- */
68   
69   this.http_get = function(u, cb)  {
70     self.http_call('GET', u, null, cb);
71   };
72   
73   this.http_post = function(u, data, cb) {
74     self.http_call('POST', u, data, cb);
75   };
76
77   this.http_call = function (method, u, data, scallback) {    
78     var xhr = new XMLHttpRequest();
79     var url = u;
80     xhr.onreadystatechange = function() {
81       if (this.readyState === 4 && this.status === 200) {
82         scallback(this.responseText);
83       }
84     };
85     xhr.open(method, url);
86     if(method === 'GET')  {
87       xhr.send();
88     } else if(method === 'POST' || method === 'PUT') {
89       xhr.send(data);
90     }
91   };
92   
93 /* ----- Hilfsfunktionen ----- */
94
95   this.serialisieren = function(obj) {
96     return '{"' + obj.constructor.name + '":' + JSON.stringify(obj) + '}';
97   };
98   
99   this.addEvtListener = function(selector, eventName, func) {
100     var elems = document.querySelectorAll(selector);
101     var index;
102     for (index = 0; index < elems.length; index++) {
103       elems[index].addEventListener(eventName, func);
104     }
105   };
106   
107   this.removeAllListeners = function(elementId) {
108     var el = document.getElementById(elementId);
109     elClone = el.cloneNode(true);
110     el.parentNode.replaceChild(elClone, el);
111   }; // https://stackoverflow.com/questions/19469881/remove-all-event-listeners-of-specific-type
112
113   /* ---- Vorlagen ---- */
114
115   this.html_erzeugen = function(vurl, inhalt, cb) {
116     var vorlage = self.cache[vurl];
117     if(vorlage === undefined) {
118       self.vorlage_laden_und_fuellen(vurl, inhalt, cb);
119     } else {
120       self.vorlage_fuellen(vurl, inhalt, cb);
121     }
122   };
123
124   this.vorlage_fuellen = function(vurl, inhalt, cb) {
125     cb(Mustache.render(self.cache[vurl], inhalt));
126   };
127
128   this.vorlage_laden_und_fuellen = function(vurl, inhalt, cb) {
129     app.http_get(vurl, function(antwort) {
130       self.cache[vurl] = antwort;
131       self.vorlage_fuellen(vurl, inhalt, cb);
132     });
133   };
134 }