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