Dateiverwaltung für die WebBox
ulrich
2018-08-10 c02f76900882d57ebbaa3d94d87a0ce68a33df3b
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();
630c3f 132     
76660f 133     if(fm_slideshow) {
U 134       $("[data-fancybox]").fancybox({
135         loop: true,
136         idleTime: 2
137       });
138     }
2f9e6f 139   });
U 140 }
141
142 function fm_del_files() {
143   var liste = fm_gewaehlte_dateien();
144   var m = '?c=de.uhilger.filecms.api.FileMgr&m=deleteFiles&p=' + pfad + '&p=' + encodeURIComponent(liste);
145   var u = '../svc' + m;
146   fm_get(u, "text", function(resp) {
147     // console.log('deleteFiles gab folgendes zurueck: ' + resp);
148     fm_get_list(pfad);
149   });
150 }
151
af9930 152 function fm_pack_folder() {
U 153   //var liste = fm_gewaehlte_dateien();
154   var m = '?c=de.uhilger.filecms.api.FileMgr&m=packFolder&p=' + pfad;
155   var u = '../svc' + m;
156   fm_get(u, "text", function(resp) {
157     // console.log('deleteFiles gab folgendes zurueck: ' + resp);
158     fm_get_list(pfad);
159   });
160 }
161
2f9e6f 162 function fm_rename_file(fn, p, neuerName) {
U 163   var m = '?c=de.uhilger.filecms.api.FileMgr&m=renameFile';
164   m = m + '&p=' + p; 
165   m = m + '&p=' + fn; 
166   m = m + '&p=' + neuerName; 
167   var u = '../svc' + m;
168   fm_get(u, "text", function(resp) {
169     fm_get_list(pfad);
170   });  
171 }
172
173 /* -------------------- Ordner ---------------- */
174
175 function fm_get_new_folder() {
176   $('#modal_ok').click(function() {
177     // hier speichern
178     var m = '?c=de.uhilger.filecms.api.FileMgr&m=newFolder&p=' + pfad + '&p=' + $('#dateiname').val();
179     var u = '../svc' + m;
180     fm_get(u, "json", function(resp) {
181       fm_get_list(pfad);
182     });
183   });
184   $('#saveModalTitle').text('Neuer Ordner');
185   $('#dialogfrage').text("Name?");
186   $('#dateiname').val('');
187   $('#dateiname').attr('placeholder', 'Ordnername');
188   $('#saveModal').modal({
189     keyboard: false,
190     show: true
191   });
192 }
193
194 function fm_unzip_file(fn) {
195   var m = '?c=de.uhilger.filecms.api.FileMgr&m=extractZipfile';
196   m = m + '&p=' + pfad; 
197   m = m + '&p=' + fn; 
198   var u = '../svc' + m;
199   fm_get(u, "text", function(resp) {
200     $('.system-out').empty();
201     $('.system-out').append('Rueckmeldung vom Entpacken: ' + resp);
202     fm_fusszeile_zeigen();
203     fm_get_list(pfad);
204   });  
205 }
206
fad719 207 function fm_export_html() {
U 208   var m = '?c=de.uhilger.filecms.api.HtmlExportService&m=exportHtml';
209   m = m + '&p=' + pfad; 
210   var u = '../svc' + m;
211   fm_get(u, "text", function(resp) {
212     $('.system-out').empty();
213     $('.system-out').append('Rueckmeldung vom HTML-Export: ' + resp);
214     fm_fusszeile_zeigen();
215   });  
216 }
217
2f9e6f 218 /* ------------------- Dateiinhalte --------------------- */
U 219
220 function fm_save_file(saveFileName, method, callback) {
221   var content;
222   if(openEditor === 'text') {
223     content = cm.getValue();
224     cm.getDoc().markClean();
225   } else {
226     content = ed.getContent();
227     tinymce.activeEditor.undoManager.clear();
228   }
229   var m = '?c=de.uhilger.filecms.api.FileMgr&m=' + method;
230   var u = '../svc' + m;
231   fm_post(u, {p1: pfad, p2: saveFileName, p3: content}, function(resp) {
232     // hier scheint nichts zurueckzukommen..
233   });
234   openFileName = saveFileName;
235   if(typeof (callback) !== 'function') {
236     
237   } else {
238     callback();
239   }
240 }
241
242 function fm_get_file_content(typ) {
243   var gewaehlte = $('.datei-gewaehlt');
244   //var fname = $(gewaehlte).find('.dateiname').text();
245   
246   var fname = $(gewaehlte[0]).text();
247   openFileName = fname;
248   var m = '?c=de.uhilger.filecms.api.FileMgr&m=getCode&p=' + pfad + '&p=' + fname;
249   var u = '../svc' + m;
250   fm_get(u, "text", function(resp) {
251     if(typ == 'text') {
252       var mode = "text/x-java";
253       if(fname.endsWith('js')) {
254         mode = 'javascript';
255       } else if(fname.endsWith('xml')) {
256         mode = 'xml';
257       } else if(fname.endsWith('properties')) {
258         mode = 'xml';
259       }
260       fm_text_edit(resp, mode);
261     } else {
262       fm_dok_edit(resp);
263     }
264   });
265   
266 }
267
268 /* -------- An- und Abmelden ------------- */
269
270 function fm_get_login() {
271   var m = '?c=de.uhilger.filecms.pub.SessionManager&m=getSessionUser';
272   var u = '../pub' + m;
273   fm_get(u, "text", function(resp) {
274     userid = resp;
275     $('#userMenu').text(resp);
276   });  
277 }
278
279 function fm_logout() {
280   var m = '?c=de.uhilger.filecms.pub.SessionManager&m=expireSession';
281   var u = '../pub' + m;
282   
283   fm_get(u, "text", function(resp) {
284     $('#userMenu').text('nicht angemeldet');
285     window.location.href = '../logout.html';
286   });
287 }
288
289 /* -------- Compile / Build ------------- */
290
291 function fm_build_app() {
292   var m = '?c=de.uhilger.filecms.api.CompileService&m=buildApp&p=' + pfad;
293   var u = '../svc' + m;
294   fm_get(u, "text", function(resp) {
295     $('.system-out').empty();
296     $('.system-out').append('Ergebnis von Build app: ' + resp);
297     fm_fusszeile_zeigen();
298   });
299 }
300
301 function fm_compile_all() {
302   var m = '?c=de.uhilger.filecms.api.CompileService&m=compileAll&p=' + pfad;
303   var u = '../svc' + m;
304   fm_get(u, "json", function(resp) {
305     if(resp.List[0].CompilerIssue !== undefined) {
306       var lno;
307       var eMsg;
308       var issueList = new Array();
309       if(resp.List[0].CompilerIssue instanceof Array) {
310         var issueNo = 0;
311         while(issueNo < resp.List[0].CompilerIssue.length) {
312           /*
313           $('.system-out').append('   +++ ---- +++   ');
314           $('.system-out').append(resp.List[0].CompilerIssue[issueNo].kind);
315           $('.system-out').append(resp.List[0].CompilerIssue[issueNo].lineNumber);
316           $('.system-out').append(resp.List[0].CompilerIssue[issueNo].sourceName);
317           $('.system-out').append(resp.List[0].CompilerIssue[issueNo].message);
318           */
319           var issue = new CompilerIssue(
320             resp.List[0].CompilerIssue[issueNo].sourceName,
321             resp.List[0].CompilerIssue[issueNo].message,
322             resp.List[0].CompilerIssue[issueNo].kind,
323             resp.List[0].CompilerIssue[issueNo].lineNumber
324           );
325           issueList.push(issue);
326
327           /*
328           console.log('   +++ ---- +++   ');
329           console.log(resp.List[0].CompilerIssue[issueNo].kind);
330           console.log(resp.List[0].CompilerIssue[issueNo].lineNumber);
331           console.log(resp.List[0].CompilerIssue[issueNo].sourceName);
332           console.log(resp.List[0].CompilerIssue[issueNo].message);
333           */
334           issueNo++;
335         }
336       } else {
337         //lno = resp.List[0].CompilerIssue.lineNumber;
338         //eMsg = resp.List[0].CompilerIssue.kind + ' ' + resp.List[0].CompilerIssue.message;
339         //$('.system-out').append(lno + ' ' + eMsg);
340         //console.log(lno + ' ' + eMsg);
341         var issue = new CompilerIssue(
342           resp.List[0].CompilerIssue.sourceName,
343           resp.List[0].CompilerIssue.message,
344           resp.List[0].CompilerIssue.kind,
345           resp.List[0].CompilerIssue.lineNumber
346         );
347         issueList.push(issue);        
348       }
349       var theList = new IssueList(issueList);
350       var template = $('#tpl-ci').html();
351       Mustache.parse(template);   // optional, speeds up future uses
352       $('.system-out').empty();
353       $('.system-out').html(Mustache.render(template, theList));
354       $('.sued').show();
355     }
356   });
357 }
358
359 function fm_compile(modeStr, callback) {
360   var liste = fm_gewaehlte_dateien();
361   var m = '?c=de.uhilger.filecms.api.CompileService&m=compile&p=' + pfad + '&p=' + encodeURIComponent(liste) + 
362           '&p=' + modeStr;
363   var u = '../svc' + m;
364   fm_get(u, "json", function(resp) {
365     callback(resp);
366   });
367 }
368
369 /* --------------- Bilder ------------------------- */
370
371 function fm_menu_shrink() {
372   var gewaehlte = $('.datei-gewaehlt');
373   var fname = $(gewaehlte[0]).text();
374   var m = '?c=de.uhilger.filecms.api.FileMgr&m=bildVerkleinern';
375   m = m + '&p=' + pfad; 
376   m = m + '&p=' + fname; 
377   var u = '../svc' + m;
378   fm_get(u, "text", function(resp) {
379     fm_get_list(pfad);
380   });  
381 }
382
bb9f8c 383 function fm_menu_rotate() {
U 384   var gewaehlte = $('.datei-gewaehlt');
385   var fname = $(gewaehlte[0]).text();
386   var m = '?c=de.uhilger.filecms.api.FileMgr&m=bildRotieren';
387   m = m + '&p=' + pfad; 
388   m = m + '&p=' + fname; 
389   var u = '../svc' + m;
390   fm_get(u, "text", function(resp) {
391     fm_get_list(pfad);
392   });  
393 }
394
2f9e6f 395 /* -------- upload ----------- */
U 396
397 function sendFile(datei) {
398   var uri = "../api/upload";
399   var xhr = new XMLHttpRequest();  
400   var fd = new FormData();  
401   xhr.open("POST", uri, true);  
402   xhr.onreadystatechange = function() {  
403     if (xhr.readyState == 4 && xhr.status == 200) {  
404       fm_get_list(pfad);
405       if(dateien.length > 0) {
406         sendFile(dateien.pop());
407       }
408     }  
409   };  
410   fd.append('dateiauswahlfeld', datei);  
411   fd.append('pfad', pfad);
412   xhr.send(fd);          
413 }
414
415 /* -------- helper functions ----------- */
416
417 function fm_get(u, dtype, scallback) {
418   $.ajax({
419     url: u,
420     type: "GET",
421     dataType: dtype,
422     success: scallback,
423     error: function (xhr, status, errorThrown) {
424       alert("Error: " + errorThrown + " Status: " + status + " URL: " + u);
425     },
426     complete: function (xhr, status) {
427       //console.log( "The request is complete!" );
428     }
429
430   });
431 }
432
433 function fm_post(u, d, dtype, scallback) {
434   $.ajax({
435     url: u,
436     data: d,
437     type: "POST",
438     dataType: dtype,
439     success: scallback,
440     error: function (xhr, status, errorThrown) {
441       $('#fehler').html("Error: " + errorThrown + " Status: " + status);
442     },
443     complete: function (xhr, status) {
444       //alert( "The request is complete!" );
445     }
446   });
447 }