Dateiverwaltung für die WebBox
ulrich
2021-01-03 719f73ea3ea9204585de5487fb83f6d5be97d1ac
commit | author | age
05e9c4 1 function AppVorlage() {
U 2   var self = this;
3   var appMenu;
4   var vorlagen;
5   var api;
94b871 6   var userid;
58bcbd 7   var pfad = '';
U 8   var loc;
9   var modus = 'kacheln';
10   var PERS_DIR = "Persoenlich";
11   var PUB_DIR = "Oeffentlich";
12   var DAV_DIR = "Austausch";
13   var BASE_DIR = "$basis";
14   var DATA_DIR = "$daten";
15   var WWW_DIR = "www";
05e9c4 16
94b871 17   this.datei_neuer_text = function () {
58bcbd 18     self.meldung_mit_timeout("Neuer Text", 1500);
05e9c4 19   };
U 20
21   /* Funktionen aus App-Vorlage */
94b871 22
U 23   this.init = function () {
05e9c4 24     self.vorlagen = new Vorlagen();
U 25     self.appMenu = new AppMenu();
26     self.appMenu.init(
94b871 27             "data/menu/",
U 28             "hauptmenue.json",
29             "data/tpl/app-menu.tpl",
30             ".west",
31             "8em");
32     document.querySelector('.hamburger').addEventListener('click', function (e) {
05e9c4 33       self.menue_umschalten();
U 34     });
58bcbd 35     self.fm_get_login();
U 36     self.fm_get_list('');
37     self.loc = window.location.protocol + '//' + window.location.host;
05e9c4 38   };
U 39
58bcbd 40   this.login_zeigen = function() {
U 41     self.meldung_mit_timeout("Benutzer: " + self.userid, 1500);
42   };
43   
94b871 44   this.menue_umschalten = function () {
05e9c4 45     var ham = document.querySelector(".hamburger");
U 46     ham.classList.toggle("is-active"); // hamburger-icon umschalten
47     self.appMenu.toggle(); // menue oeffnen/schliessen
48   };
49
94b871 50   this.info_dialog_zeigen = function () {
05e9c4 51     self.dialog_laden_und_zeigen('data/tpl/dlg-info.tpl', '');
U 52     self.menue_umschalten();
53   };
54
94b871 55   this.seitenleiste_umschalten = function () {
05e9c4 56     var ostDiv = document.querySelector('.ost');
94b871 57     if (ostDiv.classList.contains('ost-open')) {
05e9c4 58       ostDiv.classList.remove('ost-open');
94b871 59       ostDiv.style.flexBasis = '0em';
05e9c4 60     } else {
94b871 61       ostDiv.classList.add('ost-open');
U 62       ostDiv.style.flexBasis = '6em';
05e9c4 63     }
U 64     self.menue_umschalten();
65   };
66
94b871 67   this.fusszeile_umschalten = function () {
05e9c4 68     var suedDiv = document.querySelector('.sued');
94b871 69     if (suedDiv.classList.contains('sued-open')) {
05e9c4 70       suedDiv.classList.remove('sued-open');
94b871 71       suedDiv.style.height = '0';
05e9c4 72     } else {
U 73       suedDiv.classList.add('sued-open');
94b871 74       suedDiv.style.height = '1.5em';
05e9c4 75     }
U 76     self.menue_umschalten();
77   };
78
94b871 79   this.meldung_mit_timeout = function (meldung, timeout) {
05e9c4 80     var s = document.querySelector('.sued');
U 81     s.textContent = meldung;
94b871 82     setTimeout(function () {
05e9c4 83       s.textContent = 'Bereit.';
94b871 84       setTimeout(function () {
05e9c4 85         var suedDiv = document.querySelector('.sued');
94b871 86         if (suedDiv.classList.contains('sued-open')) {
U 87           suedDiv.classList.remove('sued-open');
88           suedDiv.style.height = '0';
05e9c4 89         }
U 90       }, 500);
91     }, timeout);
92   };
93
94   /* Dialog-Funktionen */
95
96   /*
94b871 97    Einen Dialog aus Vorlagen erzeugen
U 98    
99    vurl - URL zur Dialogvorlage
100    msgTpl - URL mit einer Vorlage eines Mitteilungstextes (optional)
101    */
102   this.dialog_laden_und_zeigen = function (vurl, msgTpl) {
103     if (msgTpl !== '') {
05e9c4 104       fetch(msgTpl)
94b871 105               .then(data => {
U 106                 // Handle data
107                 self.dialog_zeigen(vurl, data);
108               }).catch(error => {
109         // Handle error
110       });
05e9c4 111     } else {
U 112       self.dialog_zeigen(vurl, '');
113     }
114   };
115
94b871 116   this.dialog_zeigen = function (vurl, inhalt) {
05e9c4 117     var dlg = document.querySelector(".dialog");
U 118     self.vorlagen.html_erzeugen(
94b871 119             vurl,
U 120             inhalt,
121             function (html) {
122               //dlg.html(html);
123               dlg.style.height = '5em';
124               dlg.innerHTML = html;
125               document.querySelector('.close-btn').addEventListener('click', self.dialog_schliessen);
126               //dlg.slideDown(300);
127             });
05e9c4 128   };
U 129
58bcbd 130   this.dialog_schliessen = function () {
05e9c4 131     document.querySelector('.close-btn').removeEventListener('click', self.dialog_schliessen);
U 132     //$('.dialog').slideUp(300);
133     var dlg = document.querySelector('.dialog');
134     //dlg.style.display = "none";
135     dlg.style.height = '0';
136     dlg.innerHTML = '';
137   };
58bcbd 138   
U 139   this.fm_render_list = function (fl) {
140     if (self.modus == 'kacheln') {
141       // Kachelansicht
142       
143       // neu bauen
144       
145       /*
146       var template = $('#tpl-kacheln').html();
147       Mustache.parse(template);   // optional, speeds up future uses
148       $('.datei-zeile').attr('onclick', '').unbind('click');
149       $('#dateien').empty();
150       $('#dateien').html(Mustache.render(template, fl));
151       $('.figure').click(fm_dateiwahl);
152       */
153     } else {
154       // Listenansicht
155       
156       // neu bauen
157       
158       /*
159       var template = $('#tpl-liste').html();
160       Mustache.parse(template);   // optional, speeds up future uses
161       $('.figure').attr('onclick', '').unbind('click');
162       $('#dateien').empty();
163       $('#dateien').html(Mustache.render(template, fl));
164       $('.datei-zeile').click(fm_dateiwahl);
165       */
166     }
167   };
168   
169   this.fm_get_path = function (uid) {
170     //console.log('pfad: ' + pfad);
171     var restdir;
172     if (self.pfad.indexOf(self.PUB_DIR) > -1) {
173       restdir = self.pfad.substr(self.PUB_DIR.length);
174     } else if (self.pfad.indexOf(self.PERS_DIR) > -1) {
175       restdir = self.pfad.substr(self.PERS_DIR.length);
176     } else if (self.pfad.indexOf(self.BASE_DIR) > -1) {
177       restdir = self.pfad.substr(self.BASE_DIR.length);
178     } else if (self.pfad.indexOf(self.DATA_DIR) > -1) {
179       restdir = self.pfad.substr(self.DATA_DIR.length);
180     } else if (self.pfad.indexOf(self.DAV_DIR) > -1) {
181       restdir = self.pfad.substr(self.DAV_DIR.length);
182     }
183     if (restdir !== undefined && restdir.startsWith('/')) {
184       restdir = restdir.substr(1);
185       if (restdir.indexOf(self.WWW_DIR) > -1) {
186         restdir = restdir.replace(self.WWW_DIR, 'data');
187       }
188     }
189     var pdir = fm_get_base(uid);
190     // console.log('fm_get_path path: ' + pdir + "/" + restdir);
191     if (restdir.length > 1) {
192       return pdir + "/" + restdir;
193     } else {
194       return pdir;
195     }
196   };
197
198   this.fm_get_base = function (uid) {
199     //console.log('pfad: ' + pfad);
200     var pdir;
201     if (self.pfad.indexOf(self.PUB_DIR) > -1) {
202       pdir = '/data/' + uid;
203     } else if (self.pfad.indexOf(self.PERS_DIR) > -1) {
204       pdir = '/home/' + uid;
205     } else if (self.pfad.indexOf(self.BASE_DIR) > -1) {
206       pdir = '';
207     } else if (self.pfad.indexOf(self.DATA_DIR) > -1) {
208       pdir = '';
209     }
210     //console.log('fm_get_base base: ' + pdir + uid);
211     return pdir;
212   };
213
05e9c4 214
94b871 215   /* API functions */
05e9c4 216
94b871 217   // http://localhost:8079/file-cms/svc?c=de.uhilger.filecms.api.FileMgr&f=JSONNICE&m=list&p=
U 218   this.fm_get_list = function (relPfad) {
58bcbd 219     //$('#ansicht').attr('onclick', '').unbind('click');
94b871 220     var m = '?c=de.uhilger.filecms.api.FileMgr&m=list&p=' + relPfad;
U 221     var u = '../svc' + m;
58bcbd 222     self.fm_get(u, "json", function (respText) {
U 223       var resp = JSON.parse(respText);
94b871 224       if (resp.List[0].FileRef !== undefined) {
U 225         var files = new Array();
226         if (resp.List[0].FileRef instanceof Array) {
227           for (var i = 0; i < resp.List[0].FileRef.length; i++) {
228             files.push(new FileRef(resp.List[0].FileRef[i]));
05e9c4 229           }
U 230         } else {
94b871 231           files.push(new FileRef(resp.List[0].FileRef));
05e9c4 232         }
94b871 233         var fl = new FileList(files);
58bcbd 234         self.fm_render_list(fl);
94b871 235       } else {
58bcbd 236         // #dateien leeren
U 237         var elems = document.querySelector("#dateien");
238         var count = elems[0].childElementCount;
239         for(var i = count-1; i > -1; i--) {
240           elems[0].removeChild(i);
241         }
94b871 242       }
U 243     });
244   };
05e9c4 245
94b871 246   /* -------- An- und Abmelden ------------- */
05e9c4 247
94b871 248   this.fm_get_login = function() {
U 249     var m = '?c=de.uhilger.filecms.pub.SessionManager&m=getSessionUser';
250     var u = '../pub' + m;
251     self.fm_get(u, "text", function (resp) {
252       self.userid = resp;
58bcbd 253       self.login_zeigen();
U 254       //document.querySelector("#userMenu").textContent = resp;
94b871 255     });
U 256   };
05e9c4 257
94b871 258   this.fm_logout = function() {
U 259     var m = '?c=de.uhilger.filecms.pub.SessionManager&m=expireSession';
260     var u = '../pub' + m;
261     self.fm_get(u, "text", function (resp) {
262       //$('#userMenu').text('nicht angemeldet');
263       window.location.href = '../logout.html';
264     });
265   };
266
267   /* -------- ajax helper functions ----------- */
268
58bcbd 269   this.fm_get = function (u, dtype, scallback) {    
U 270     var xmlhttp = new XMLHttpRequest();
271     var url = u;
272     xmlhttp.onreadystatechange = function() {
273       if (this.readyState == 4 && this.status == 200) {
274         scallback(this.responseText);
94b871 275       }
58bcbd 276     };
U 277     xmlhttp.open("GET", url, true);
278     xmlhttp.send();
94b871 279   };
U 280
05e9c4 281 }