ulrich
2020-12-27 5ef7f0b3dccf1ce5c4f4271a2eaec5158615253d
commit | author | age
5daeb2 1 function NutzerProfilApp() {
U 2   var self = this;
3   this.cache = {}; // mustache template cache
4
5   this.init = function () {
58aa3b 6     document.querySelector('#top-logout-btn').addEventListener('click', self.logout);
5daeb2 7     var dlg = document.querySelector(".dialog");
U 8     dlg.style.flexBasis = '0em';
9     document.querySelector('.west').style.flexBasis = '0em';
10     document.querySelector('.ost').style.flexBasis = '0em';
11     self.get_user_data();
12     
13     var suedDiv = document.querySelector('.sued');
2b837d 14     suedDiv.classList.remove('sued-open');
U 15     suedDiv.style.height = '0';
16     document.querySelector('.zurueck-btn').style.color = '#eee';
5daeb2 17   };
U 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   };
2b837d 28
5daeb2 29   this.kennwort_speichern = function() {
2b837d 30     var userId = document.querySelector('#anmeldename').textContent;
U 31     var aktKw = document.querySelector('#kennwort').value;
32     var neuKw = document.querySelector('#kennwortNeu').value;
33     var wKw = document.querySelector('#kennwortw').value;
34     if(neuKw === wKw) {
35       var m = '?c=de.uhilger.um.api.Profil&m=setUserPw&p=' + userId + '&p=' + aktKw + '&p=' + neuKw;
36       var u = '../prf' + m;
37       self.http_get(u, function(antwort) {
38         self.meldung(antwort, 1500);
39       });
40     } else {
41       // Kennworte unterschiedlich
42       self.meldung('Kennworte stimmen nicht überein', 1500);
43     }
44   };
45   
46   this.meldung = function(text, dauer) {
5daeb2 47     var suedDiv = document.querySelector('.sued');
2b837d 48     suedDiv.textContent = text;
U 49     suedDiv.classList.add('sued-open');
50     suedDiv.style.height = '1.5em';
51     setTimeout(function () {
52       var suedDiv = document.querySelector('.sued');
53       if (suedDiv.classList.contains('sued-open')) {
54         suedDiv.classList.remove('sued-open');
55         suedDiv.style.height = '0';
56       }
57     }, 1500);    
5daeb2 58   };
U 59   
60   this.get_user_data = function() {
61     var m = '?c=de.uhilger.um.pub.SessionManager&m=getSessionUser';
62     var u = '../pub' + m;
63     self.http_get(u, function (antwort) {
64       self.profil_zeigen(JSON.parse(antwort));
65     });
66   };
67
58aa3b 68   this.logout = function() {
U 69     var m = '?c=de.uhilger.um.pub.SessionManager&m=expireSession';
70     var u = '../pub' + m;
71     self.http_get(u, function (resp) {
72       //$('#userMenu').text('nicht angemeldet');
73       window.location.href = '../logout.jsp';
74     });
75   };
76
5daeb2 77   /* -------- ajax helper functions ----------- */
U 78   
79   this.http_get = function(u, cb)  {
80     self.http_call('GET', u, null, cb);
81   };
82   
83   this.http_post = function(u, data, cb) {
84     self.http_call('POST', u, data, cb);
85   };
86
87   this.http_call = function (method, u, data, scallback) {    
88     var xhr = new XMLHttpRequest();
89     var url = u;
90     xhr.onreadystatechange = function() {
91       if (this.readyState === 4 && this.status === 200) {
92         scallback(this.responseText);
93       }
94     };
95     xhr.open(method, url);
96     if(method === 'GET')  {
97       xhr.send();
98     } else if(method === 'POST' || method === 'PUT') {
bc1d28 99       xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
5daeb2 100       xhr.send(data);
U 101     }
102   };
103   
104 /* ----- Hilfsfunktionen ----- */
105
106   this.serialisieren = function(obj) {
107     return '{"' + obj.constructor.name + '":' + JSON.stringify(obj) + '}';
108   };
109   
110   this.addEvtListener = function(selector, eventName, func) {
111     var elems = document.querySelectorAll(selector);
112     var index;
113     for (index = 0; index < elems.length; index++) {
114       elems[index].addEventListener(eventName, func);
115     }
116   };
117   
118   this.removeAllListeners = function(elementId) {
119     var el = document.getElementById(elementId);
120     elClone = el.cloneNode(true);
121     el.parentNode.replaceChild(elClone, el);
122   }; // https://stackoverflow.com/questions/19469881/remove-all-event-listeners-of-specific-type
123
124   /* ---- Vorlagen ---- */
125
126   this.html_erzeugen = function(vurl, inhalt, cb) {
127     var vorlage = self.cache[vurl];
128     if(vorlage === undefined) {
129       self.vorlage_laden_und_fuellen(vurl, inhalt, cb);
130     } else {
131       self.vorlage_fuellen(vurl, inhalt, cb);
132     }
133   };
134
135   this.vorlage_fuellen = function(vurl, inhalt, cb) {
136     cb(Mustache.render(self.cache[vurl], inhalt));
137   };
138
139   this.vorlage_laden_und_fuellen = function(vurl, inhalt, cb) {
140     app.http_get(vurl, function(antwort) {
141       self.cache[vurl] = antwort;
142       self.vorlage_fuellen(vurl, inhalt, cb);
143     });
144   };
145 }