Dateiverwaltung für die WebBox
ulrich
2017-03-22 fad71945f44fd188b2ef78b88e638eb9d1cd0674
commit | author | age
2f9e6f 1 /*
U 2     Dateiverwaltung - File management in your browser
3     Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
4
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU Affero General Public License as
7     published by the Free Software Foundation, either version 3 of the
8     License, or (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU Affero General Public License for more details.
14
15     You should have received a copy of the GNU Affero General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20
21 /*
22  * In api.js finden sich die HTTP-Aufrufe vom Browser-Client 
23  * der Dateiverwaltung an deren Server-Kern
24  */
25
26
27
28
29 /* ---------- Dateien kopieren, verschieben --------------- */
30
31 /*
32  * Hier wird fuer eine zuvor markierte Liste von Dateien, fuer die 
33  * Cut oder Copy gewaehlt wurde, die Operations ausgefuehrt (move oder copy)
34  * @returns {undefined}
35  */
36 function fm_paste_files() {
37   var m;
38   if(cutCopyOperation === 'cut') {
39     //m = '?c=de.uhilger.filecms.api.FileMgr&m=moveFiles'; //&p=' + cutCopySrcDir + '&p=' + pfad + '&p=' + encodeURIComponent(liste);
40     m = '?c=de.uhilger.filecms.api.FileMgr&m=moveFiles&p=' + cutCopySrcDir + '&p=' + pfad + '&p=' + encodeURIComponent(cutCopyFiles);
41   } else {
42     //m = '?c=de.uhilger.filecms.api.FileMgr&m=copyFiles'; //&p=' + cutCopySrcDir + '&p=' + pfad + '&p=' + encodeURIComponent(liste);
43     m = '?c=de.uhilger.filecms.api.FileMgr&m=copyFiles&p=' + cutCopySrcDir + '&p=' + pfad + '&p=' + encodeURIComponent(cutCopyFiles);
44   }
45   var u = '../svc' + m;  
46   fm_get(u, "text", function(resp) {
47     // console.log('deleteFiles gab folgendes zurueck: ' + resp);
48     fm_get_list(pfad);
49   });
50   /*
51   fm_post(u, {p1: encodeURIComponent(cutCopySrcDir), p2: encodeURIComponent(pfad), p3: encodeURIComponent(cutCopyFiles)},'text', function(resp) {
52     // resp evtl. zeigen..
53     fm_get_list(pfad);
54   });
55   */
56 }
57
58 /* ---------- Dateien listen, loeschen, umbenennen --------------- */
59
60 // http://localhost:8079/file-cms/svc?c=de.uhilger.filecms.api.FileMgr&f=JSONNICE&m=list&p=
61 function fm_get_list(relPfad) {
62   $('#ansicht').attr('onclick','').unbind('click');
63   var m = '?c=de.uhilger.filecms.api.FileMgr&m=list&p=' + relPfad;
64   var u = '../svc' + m;
65   fm_get(u, "json", function(resp) {
66     
67     if(resp.List[0].FileRef !== undefined) {
68       var files = new Array();
69       if(resp.List[0].FileRef instanceof Array) {
70         for(var i = 0; i < resp.List[0].FileRef.length; i++) {
71           files.push(new FileRef(resp.List[0].FileRef[i]));
72         }
73       } else {
74         files.push(new FileRef(resp.List[0].FileRef));
75       }
76       var fl = new FileList(files);
77       fm_render_list(fl);
78     } else {
79       $('#dateien').empty();
80     }
81     
82     // Breadcrumb-Ansicht
83     var template;
84     $('.breadcrumb-item').attr('onclick','').unbind('click');
85     $('#bcnav').empty();
86     var dirList = new Array();
87     var rp = '';
88     //console.log("'" + relPfad + "'");
89     var dirs = relPfad.split('/');
90     //console.log(dirs.length);
91     dirList.push(new BcrFile(rp, 'Home'));
92     if(relPfad.length > 0) {
93       for(var i = 0; i < dirs.length - 1; i++) {
94         if(rp.length > 0 ) {
95           dirList.push(new BcrFile(rp + '/' + dirs[i], dirs[i]));
96           rp = rp + '/' + dirs[i];
97         } else {
98           dirList.push(new BcrFile(dirs[i], dirs[i]));
99           rp = dirs[i];
100         }
101       }
102       var bl = new BcrFiles(dirList);
103  
104       if(dirList.length > 0) {
105         template = $('#tpl-bcr').html();
106         Mustache.parse(template);   // optional, speeds up future uses
107         $('#bcnav').html(Mustache.render(template, bl));
108       }
109       
110       if(dirs.length > 0) {
111         dirList.push(new BcrFile(rp + '/' + dirs[dirs.length-1], dirs[dirs.length-1]));
112         template = $('#tpl-bcr2').html();
113         Mustache.parse(template);   // optional, speeds up future uses
114         $('#bcnav').append(Mustache.render(template, dirList[dirList.length-1]));        
115       } else {
116         template = $('#tpl-bcr2').html();
117         Mustache.parse(template);   // optional, speeds up future uses
118         $('#bcnav').append(Mustache.render(template, dirList[0]));        
119       }
120       
121       $('#bcnav').append($('#tpl-bcr3').html());
122     } else {
123       pfad = '';
124       template = $('#tpl-bcr2').html();
125       Mustache.parse(template);   // optional, speeds up future uses
126       $('#bcnav').append(Mustache.render(template, dirList[0]));   
127       $('#bcnav').append($('#tpl-bcr3').html());
128     }
129     $('.breadcrumb-item').click(fm_bc_click);
130     $('#ansicht').click(fm_ansicht_umschalten);
131     fm_set_modus();
132   });
133 }
134
135 function fm_del_files() {
136   var liste = fm_gewaehlte_dateien();
137   var m = '?c=de.uhilger.filecms.api.FileMgr&m=deleteFiles&p=' + pfad + '&p=' + encodeURIComponent(liste);
138   var u = '../svc' + m;
139   fm_get(u, "text", function(resp) {
140     // console.log('deleteFiles gab folgendes zurueck: ' + resp);
141     fm_get_list(pfad);
142   });
143 }
144
145 function fm_rename_file(fn, p, neuerName) {
146   var m = '?c=de.uhilger.filecms.api.FileMgr&m=renameFile';
147   m = m + '&p=' + p; 
148   m = m + '&p=' + fn; 
149   m = m + '&p=' + neuerName; 
150   var u = '../svc' + m;
151   fm_get(u, "text", function(resp) {
152     fm_get_list(pfad);
153   });  
154 }
155
156 /* -------------------- Ordner ---------------- */
157
158 function fm_get_new_folder() {
159   $('#modal_ok').click(function() {
160     // hier speichern
161     var m = '?c=de.uhilger.filecms.api.FileMgr&m=newFolder&p=' + pfad + '&p=' + $('#dateiname').val();
162     var u = '../svc' + m;
163     fm_get(u, "json", function(resp) {
164       fm_get_list(pfad);
165     });
166   });
167   $('#saveModalTitle').text('Neuer Ordner');
168   $('#dialogfrage').text("Name?");
169   $('#dateiname').val('');
170   $('#dateiname').attr('placeholder', 'Ordnername');
171   $('#saveModal').modal({
172     keyboard: false,
173     show: true
174   });
175 }
176
177 function fm_unzip_file(fn) {
178   var m = '?c=de.uhilger.filecms.api.FileMgr&m=extractZipfile';
179   m = m + '&p=' + pfad; 
180   m = m + '&p=' + fn; 
181   var u = '../svc' + m;
182   fm_get(u, "text", function(resp) {
183     $('.system-out').empty();
184     $('.system-out').append('Rueckmeldung vom Entpacken: ' + resp);
185     fm_fusszeile_zeigen();
186     fm_get_list(pfad);
187   });  
188 }
189
fad719 190 function fm_export_html() {
U 191   var m = '?c=de.uhilger.filecms.api.HtmlExportService&m=exportHtml';
192   m = m + '&p=' + pfad; 
193   var u = '../svc' + m;
194   fm_get(u, "text", function(resp) {
195     $('.system-out').empty();
196     $('.system-out').append('Rueckmeldung vom HTML-Export: ' + resp);
197     fm_fusszeile_zeigen();
198   });  
199 }
200
2f9e6f 201 /* ------------------- Dateiinhalte --------------------- */
U 202
203 function fm_save_file(saveFileName, method, callback) {
204   var content;
205   if(openEditor === 'text') {
206     content = cm.getValue();
207     cm.getDoc().markClean();
208   } else {
209     content = ed.getContent();
210     tinymce.activeEditor.undoManager.clear();
211   }
212   var m = '?c=de.uhilger.filecms.api.FileMgr&m=' + method;
213   var u = '../svc' + m;
214   fm_post(u, {p1: pfad, p2: saveFileName, p3: content}, function(resp) {
215     // hier scheint nichts zurueckzukommen..
216   });
217   openFileName = saveFileName;
218   if(typeof (callback) !== 'function') {
219     
220   } else {
221     callback();
222   }
223 }
224
225 function fm_get_file_content(typ) {
226   var gewaehlte = $('.datei-gewaehlt');
227   //var fname = $(gewaehlte).find('.dateiname').text();
228   
229   var fname = $(gewaehlte[0]).text();
230   openFileName = fname;
231   var m = '?c=de.uhilger.filecms.api.FileMgr&m=getCode&p=' + pfad + '&p=' + fname;
232   var u = '../svc' + m;
233   fm_get(u, "text", function(resp) {
234     if(typ == 'text') {
235       var mode = "text/x-java";
236       if(fname.endsWith('js')) {
237         mode = 'javascript';
238       } else if(fname.endsWith('xml')) {
239         mode = 'xml';
240       } else if(fname.endsWith('properties')) {
241         mode = 'xml';
242       }
243       fm_text_edit(resp, mode);
244     } else {
245       fm_dok_edit(resp);
246     }
247   });
248   
249 }
250
251 /* -------- An- und Abmelden ------------- */
252
253 function fm_get_login() {
254   var m = '?c=de.uhilger.filecms.pub.SessionManager&m=getSessionUser';
255   var u = '../pub' + m;
256   fm_get(u, "text", function(resp) {
257     userid = resp;
258     $('#userMenu').text(resp);
259   });  
260 }
261
262 function fm_logout() {
263   var m = '?c=de.uhilger.filecms.pub.SessionManager&m=expireSession';
264   var u = '../pub' + m;
265   
266   fm_get(u, "text", function(resp) {
267     $('#userMenu').text('nicht angemeldet');
268     window.location.href = '../logout.html';
269   });
270 }
271
272 /* -------- Compile / Build ------------- */
273
274 function fm_build_app() {
275   var m = '?c=de.uhilger.filecms.api.CompileService&m=buildApp&p=' + pfad;
276   var u = '../svc' + m;
277   fm_get(u, "text", function(resp) {
278     $('.system-out').empty();
279     $('.system-out').append('Ergebnis von Build app: ' + resp);
280     fm_fusszeile_zeigen();
281   });
282 }
283
284 function fm_compile_all() {
285   var m = '?c=de.uhilger.filecms.api.CompileService&m=compileAll&p=' + pfad;
286   var u = '../svc' + m;
287   fm_get(u, "json", function(resp) {
288     if(resp.List[0].CompilerIssue !== undefined) {
289       var lno;
290       var eMsg;
291       var issueList = new Array();
292       if(resp.List[0].CompilerIssue instanceof Array) {
293         var issueNo = 0;
294         while(issueNo < resp.List[0].CompilerIssue.length) {
295           /*
296           $('.system-out').append('   +++ ---- +++   ');
297           $('.system-out').append(resp.List[0].CompilerIssue[issueNo].kind);
298           $('.system-out').append(resp.List[0].CompilerIssue[issueNo].lineNumber);
299           $('.system-out').append(resp.List[0].CompilerIssue[issueNo].sourceName);
300           $('.system-out').append(resp.List[0].CompilerIssue[issueNo].message);
301           */
302           var issue = new CompilerIssue(
303             resp.List[0].CompilerIssue[issueNo].sourceName,
304             resp.List[0].CompilerIssue[issueNo].message,
305             resp.List[0].CompilerIssue[issueNo].kind,
306             resp.List[0].CompilerIssue[issueNo].lineNumber
307           );
308           issueList.push(issue);
309
310           /*
311           console.log('   +++ ---- +++   ');
312           console.log(resp.List[0].CompilerIssue[issueNo].kind);
313           console.log(resp.List[0].CompilerIssue[issueNo].lineNumber);
314           console.log(resp.List[0].CompilerIssue[issueNo].sourceName);
315           console.log(resp.List[0].CompilerIssue[issueNo].message);
316           */
317           issueNo++;
318         }
319       } else {
320         //lno = resp.List[0].CompilerIssue.lineNumber;
321         //eMsg = resp.List[0].CompilerIssue.kind + ' ' + resp.List[0].CompilerIssue.message;
322         //$('.system-out').append(lno + ' ' + eMsg);
323         //console.log(lno + ' ' + eMsg);
324         var issue = new CompilerIssue(
325           resp.List[0].CompilerIssue.sourceName,
326           resp.List[0].CompilerIssue.message,
327           resp.List[0].CompilerIssue.kind,
328           resp.List[0].CompilerIssue.lineNumber
329         );
330         issueList.push(issue);        
331       }
332       var theList = new IssueList(issueList);
333       var template = $('#tpl-ci').html();
334       Mustache.parse(template);   // optional, speeds up future uses
335       $('.system-out').empty();
336       $('.system-out').html(Mustache.render(template, theList));
337       $('.sued').show();
338     }
339   });
340 }
341
342 function fm_compile(modeStr, callback) {
343   var liste = fm_gewaehlte_dateien();
344   var m = '?c=de.uhilger.filecms.api.CompileService&m=compile&p=' + pfad + '&p=' + encodeURIComponent(liste) + 
345           '&p=' + modeStr;
346   var u = '../svc' + m;
347   fm_get(u, "json", function(resp) {
348     callback(resp);
349   });
350 }
351
352 /* --------------- Bilder ------------------------- */
353
354 function fm_menu_shrink() {
355   var gewaehlte = $('.datei-gewaehlt');
356   var fname = $(gewaehlte[0]).text();
357   var m = '?c=de.uhilger.filecms.api.FileMgr&m=bildVerkleinern';
358   m = m + '&p=' + pfad; 
359   m = m + '&p=' + fname; 
360   var u = '../svc' + m;
361   fm_get(u, "text", function(resp) {
362     fm_get_list(pfad);
363   });  
364 }
365
366 /* -------- upload ----------- */
367
368 function sendFile(datei) {
369   var uri = "../api/upload";
370   var xhr = new XMLHttpRequest();  
371   var fd = new FormData();  
372   xhr.open("POST", uri, true);  
373   xhr.onreadystatechange = function() {  
374     if (xhr.readyState == 4 && xhr.status == 200) {  
375       fm_get_list(pfad);
376       if(dateien.length > 0) {
377         sendFile(dateien.pop());
378       }
379     }  
380   };  
381   fd.append('dateiauswahlfeld', datei);  
382   fd.append('pfad', pfad);
383   xhr.send(fd);          
384 }
385
386 /* -------- helper functions ----------- */
387
388 function fm_get(u, dtype, scallback) {
389   $.ajax({
390     url: u,
391     type: "GET",
392     dataType: dtype,
393     success: scallback,
394     error: function (xhr, status, errorThrown) {
395       alert("Error: " + errorThrown + " Status: " + status + " URL: " + u);
396     },
397     complete: function (xhr, status) {
398       //console.log( "The request is complete!" );
399     }
400
401   });
402 }
403
404 function fm_post(u, d, dtype, scallback) {
405   $.ajax({
406     url: u,
407     data: d,
408     type: "POST",
409     dataType: dtype,
410     success: scallback,
411     error: function (xhr, status, errorThrown) {
412       $('#fehler').html("Error: " + errorThrown + " Status: " + status);
413     },
414     complete: function (xhr, status) {
415       //alert( "The request is complete!" );
416     }
417   });
418 }