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