ulrich
2020-05-19 a52c39a698c68fa04789629a79c45ad1f882e627
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') {
bc1d28 89       xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
5daeb2 90       xhr.send(data);
U 91     }
92   };
93   
94 /* ----- Hilfsfunktionen ----- */
95
96   this.serialisieren = function(obj) {
97     return '{"' + obj.constructor.name + '":' + JSON.stringify(obj) + '}';
98   };
99   
100   this.addEvtListener = function(selector, eventName, func) {
101     var elems = document.querySelectorAll(selector);
102     var index;
103     for (index = 0; index < elems.length; index++) {
104       elems[index].addEventListener(eventName, func);
105     }
106   };
107   
108   this.removeAllListeners = function(elementId) {
109     var el = document.getElementById(elementId);
110     elClone = el.cloneNode(true);
111     el.parentNode.replaceChild(elClone, el);
112   }; // https://stackoverflow.com/questions/19469881/remove-all-event-listeners-of-specific-type
113
114   /* ---- Vorlagen ---- */
115
116   this.html_erzeugen = function(vurl, inhalt, cb) {
117     var vorlage = self.cache[vurl];
118     if(vorlage === undefined) {
119       self.vorlage_laden_und_fuellen(vurl, inhalt, cb);
120     } else {
121       self.vorlage_fuellen(vurl, inhalt, cb);
122     }
123   };
124
125   this.vorlage_fuellen = function(vurl, inhalt, cb) {
126     cb(Mustache.render(self.cache[vurl], inhalt));
127   };
128
129   this.vorlage_laden_und_fuellen = function(vurl, inhalt, cb) {
130     app.http_get(vurl, function(antwort) {
131       self.cache[vurl] = antwort;
132       self.vorlage_fuellen(vurl, inhalt, cb);
133     });
134   };
135 }