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