Dateiverwaltung für die WebBox
ulrich
2017-02-23 af343d346e4e22ebffb3f515b90ed117b6016923
commit | author | age
a4d3b5 1 var cm;
5dfab6 2 var pfad = '';
10d3d3 3 var modus = 'kacheln';
5ebb8e 4 var openFileName = '';
c7c502 5
U 6 function fm_init() {
a4d3b5 7   $('.codeeditor-space').hide();
U 8   $('.code-editor-container').hide();
bef5c6 9   //$('.ost').attr('flex-basis', 0);
a4d3b5 10   $('#newTextFile').on('click', fm_menu_neue_textdatei);
adf812 11   $('#newFolder').on('click', fm_get_new_folder);
3ad4db 12   $('#editTextFile').on('click', fm_get_file_content);
e5ff42 13   $('#saveFile').on('click', fm_menu_datei_speichern);
5ebb8e 14   $('#saveFileAs').on('click', fm_menu_datei_speichern_unter);
a4d3b5 15   $('#closeFile').on('click', fm_menu_datei_schliessen);
bef5c6 16   $('#m-test').on('click', fm_menu_test);
c509a0 17   $('#saveModal').on('hidden.bs.modal', function (e) {
915927 18     $('#modal_ok').attr('onclick','').unbind('click');
b7475d 19   });
U 20   $('#logout').click(fm_logout);  
e5ff42 21   fm_get_login();
7342b1 22   fm_get_list('');
7aa566 23   fm_init_uploader();
10d3d3 24 }
7aa566 25
U 26 /* ----- Uploader Anfang ----------*/
27
28 var idNr; 
29 var dateien = new Array();
30 var sendet = false;
31
32 function fm_init_uploader() {
33   var dropzone = document.getElementById("dateien");  
34   dropzone.ondragover = dropzone.ondragenter = function(event) {  
35     event.stopPropagation();  
36     event.preventDefault();  
37   };
38
39   dropzone.ondrop = function(event) {  
af343d 40     //console.log('drop ' + event.dataTransfer.files[0]);
7aa566 41     event.stopPropagation();  
U 42     event.preventDefault();  
43
44     var filesArray = event.dataTransfer.files;  
45     for (var i=0; i<filesArray.length; i++) {  
46       //sendFile(filesArray[i]);  
47       var datei = new Datei(++idNr, filesArray[i]);
48       dateien.push(datei);
af343d 49       //dateiListen(datei);
7aa566 50     }  
U 51     naechsteDatei();
52   };
53 }
54
af343d 55 /*
7aa566 56 function dateiListen(d) {
U 57   $('.ost').append(d.file.name);
58 }
af343d 59 */
7aa566 60
U 61 function naechsteDatei() {
62   if(!sendet && dateien.length > 0) {
63     sendFile(dateien.pop());
64   }
65 }
66
67 function sendFile(datei) {
68   // Datei senden
69   var uri = "../api/upload"; // "../api/UploadFile"; //"/index.php";  
70   var xhr = new XMLHttpRequest();  
71   var fd = new FormData();  
72     
73   sendet = true;
74   xhr.open("POST", uri, true);  
75   xhr.onreadystatechange = function() {  
76     if (xhr.readyState == 4 && xhr.status == 200) {  
77       sendet = false;
78       fm_get_list(pfad);
79       naechsteDatei();
80     }  
81   };  
82   fd.append('dateiauswahlfeld', datei.file);  
83   // Initiate a multipart/form-data upload  
84   xhr.send(fd);          
85 }
86
87 function Datei (n, d) {
88   this.nr = n;
89   this.file = d;
90 }
91
92 /* ----- Uploader Ende --------------- */
10d3d3 93
bef5c6 94 function fm_menu_test() {
U 95   var w = $('.ost').width();
96   console.log('.ost.width: ' + w);
97   if(w == 0) {
98     $('.ost').width('20%');
99     $('.ost').text('Hallo Welt!');
100   } else {
101     $('.ost').empty();
102     $('.ost').width(0);
103   }
104 }
105
10d3d3 106 function fm_ansicht_umschalten() {
U 107   if($('#ansicht').children(0).hasClass('fa-th-list')) {
108     $('#ansicht').children(0).addClass('fa-th-large');
109     $('#ansicht').children(0).removeClass('fa-th-list');
110     modus = 'liste';
111   } else {
112     $('#ansicht').children(0).addClass('fa-th-list');    
113     $('#ansicht').children(0).removeClass('fa-th-large');
114     modus = 'kacheln';
115   }
116   fm_get_list(pfad);
117 }
118
119 function fm_set_modus() {
120   if(modus == 'kacheln') {
121     $('#ansicht').children(0).addClass('fa-th-list');    
122     $('#ansicht').children(0).removeClass('fa-th-large');  
123   } else {
124     $('#ansicht').children(0).addClass('fa-th-large');
125     $('#ansicht').children(0).removeClass('fa-th-list');    
126   }
a4d3b5 127 }
U 128
129 function fm_menu_neue_textdatei() {
adf812 130   fm_text_edit('Neue Datei');
a4d3b5 131 }
U 132
133 function fm_menu_datei_schliessen() {
134   $('.codeeditor-space').hide();
135   $('.code-editor-container').hide();
136   cm.toTextArea();
f5505f 137   $('#bcnav').show();
U 138   $('#dateien').show();
5ebb8e 139   openFileName = '';
adf812 140   fm_get_list(pfad);
e5ff42 141 }
U 142
5dfab6 143 function fm_dateiwahl() {
U 144   var elem = this;
10d3d3 145   if(modus == 'kacheln') {
U 146     if($(elem).children(0).hasClass('fa-folder')) {
147       var ordner = $(elem).text().trim();
148       if(pfad.length > 0) {
149         pfad = pfad + '/' + ordner;
150       } else {
151         pfad = ordner;
152       }
153       fm_get_list(pfad);
154     } else if($(elem).children(0).hasClass('fa-file')) {
155       $('.datei-gewaehlt').removeClass('datei-gewaehlt');
156       $(elem).children(0).addClass('datei-gewaehlt');
5dfab6 157     } else {
10d3d3 158       //console.log('kein folder oder file...');
5dfab6 159     }
U 160   } else {
10d3d3 161     if($(elem).find('.datei-elem').children(0).hasClass('fa-file-o')) {
U 162       $('.table-info').removeClass('table-info');
163       $(elem).addClass('table-info');
164       $('.datei-gewaehlt').removeClass('datei-gewaehlt');
165       $(elem).find('.dateiname').addClass('datei-gewaehlt');
166     } else {
167       var ordner = $(elem).find('.dateiname').text();
168       if(pfad.length > 0) {
169         pfad = pfad + '/' + ordner;
170       } else {
171         pfad = ordner;
172       }
173       fm_get_list(pfad);
174     }   
5dfab6 175   }
c509a0 176 }
U 177
a94216 178 function fm_bc_click() {
U 179   var elem = this;
180   var bcPfad = $(elem).attr('rpath');
181   if(bcPfad !== undefined) {
c509a0 182     pfad = bcPfad;
a94216 183     fm_get_list(bcPfad);
U 184   } else {
185     pfad = '';
186     fm_get_list('');
187   }
188 }
189
3ad4db 190 function fm_text_edit(content) {
f5505f 191   $('#bcnav').hide();
U 192   $('#dateien').hide();
3ad4db 193   $('.codeeditor-space').show();
U 194   $('.code-editor-container').show();
195   fm_code_edit(content);  
196 }
197
b7475d 198 /* ----- API Calls ------------- */
U 199
e5ff42 200 function fm_get_login() {
3ad4db 201   var m = '?c=de.uhilger.filecms.pub.SessionManager&m=getSessionUser';
U 202   var u = '../pub' + m;
203   fm_get(u, "text", function(resp) {
204     $('#userMenu').text(resp);
7342b1 205   });  
c509a0 206 }
U 207
208 function fm_get_new_folder() {
209   $('#modal_ok').click(function() {
210     // hier speichern
211     var m = '?c=de.uhilger.filecms.api.FileMgr&m=newFolder&p=' + pfad + '&p=' + $('#dateiname').val();
212     var u = '../svc' + m;
213     fm_get(u, "json", function(resp) {
adf812 214       fm_get_list(pfad);
c509a0 215     });
U 216   });
217   $('.modal-title').text('Neuer Ordner');
218   $('#dialogfrage').text("Name?");
219   $('#dateiname').val('');
220   $('#dateiname').attr('placeholder', 'Ordnername');
221   $('#saveModal').modal({
222     keyboard: false,
223     show: true
224   });
7342b1 225 }
U 226
2121cc 227 function fm_get_file_content() {
U 228   var fname = $('.datei-gewaehlt').text();
5ebb8e 229   openFileName = fname;
3ad4db 230   var m = '?c=de.uhilger.filecms.api.FileMgr&m=getCode&p=' + pfad + '&p=' + fname;
2121cc 231   var u = '../svc' + m;
3ad4db 232   fm_get(u, "text", function(resp) {
U 233     fm_text_edit(resp);
2121cc 234   });
3ad4db 235   
2121cc 236 }
U 237
10d3d3 238 function fm_render_list(fl) {
U 239   if(modus == 'kacheln') {
240     // Kachelansicht
241     var template = $('#tpl-kacheln').html();
242     Mustache.parse(template);   // optional, speeds up future uses
243     $('.datei-zeile').attr('onclick','').unbind('click');
244     $('#dateien').empty();
245     $('#dateien').html(Mustache.render(template, fl));
246     $('.figure').click(fm_dateiwahl);
247   } else {
248     // Listenansicht
249     var template = $('#tpl-liste').html();
250     Mustache.parse(template);   // optional, speeds up future uses
251     $('.figure').attr('onclick','').unbind('click');
252     $('#dateien').empty();
253     $('#dateien').html(Mustache.render(template, fl));
254     $('.datei-zeile').click(fm_dateiwahl);
255   }
256 }
257
7342b1 258 // http://localhost:8079/file-cms/svc?c=de.uhilger.filecms.api.FileMgr&f=JSONNICE&m=list&p=
a94216 259 function fm_get_list(relPfad) {
10d3d3 260   $('#ansicht').attr('onclick','').unbind('click');
a94216 261   var m = '?c=de.uhilger.filecms.api.FileMgr&m=list&p=' + relPfad;
7342b1 262   var u = '../svc' + m;
U 263   fm_get(u, "json", function(resp) {
10d3d3 264     
2121cc 265     if(resp.List[0].FileRef !== undefined) {
U 266       var files = new Array();
c509a0 267       if(resp.List[0].FileRef instanceof Array) {
U 268         for(var i = 0; i < resp.List[0].FileRef.length; i++) {
269           files.push(new FileRef(resp.List[0].FileRef[i]));
270         }
271       } else {
272         files.push(new FileRef(resp.List[0].FileRef));
2121cc 273       }
U 274       var fl = new FileList(files);
10d3d3 275       fm_render_list(fl);
2121cc 276     } else {
U 277       $('#dateien').empty();
7342b1 278     }
5dfab6 279     
10d3d3 280     // Breadcrumb-Ansicht
2315a0 281     var template;
a94216 282     $('.breadcrumb-item').attr('onclick','').unbind('click');
2315a0 283     $('#bcnav').empty();
5dfab6 284     var dirList = new Array();
U 285     var rp = '';
a94216 286     //console.log("'" + relPfad + "'");
U 287     var dirs = relPfad.split('/');
288     //console.log(dirs.length);
2121cc 289     dirList.push(new BcrFile(rp, 'Home'));
a94216 290     if(relPfad.length > 0) {
2315a0 291       for(var i = 0; i < dirs.length - 1; i++) {
5dfab6 292         if(rp.length > 0 ) {
U 293           dirList.push(new BcrFile(rp + '/' + dirs[i], dirs[i]));
2315a0 294           rp = rp + '/' + dirs[i];
5dfab6 295         } else {
U 296           dirList.push(new BcrFile(dirs[i], dirs[i]));
2315a0 297           rp = dirs[i];
5dfab6 298         }
U 299       }
300       var bl = new BcrFiles(dirList);
2315a0 301
U 302       if(dirList.length > 0) {
303         template = $('#tpl-bcr').html();
304         Mustache.parse(template);   // optional, speeds up future uses
305         $('#bcnav').html(Mustache.render(template, bl));
306       }
307       
308       if(dirs.length > 0) {
309         dirList.push(new BcrFile(rp + '/' + dirs[dirs.length-1], dirs[dirs.length-1]));
310         template = $('#tpl-bcr2').html();
311         Mustache.parse(template);   // optional, speeds up future uses
312         $('#bcnav').append(Mustache.render(template, dirList[dirList.length-1]));        
313       } else {
314         template = $('#tpl-bcr2').html();
315         Mustache.parse(template);   // optional, speeds up future uses
316         $('#bcnav').append(Mustache.render(template, dirList[0]));        
317       }
318       
5dfab6 319       $('#bcnav').append($('#tpl-bcr3').html());
2315a0 320     } else {
a94216 321       pfad = '';
2315a0 322       template = $('#tpl-bcr2').html();
U 323       Mustache.parse(template);   // optional, speeds up future uses
10d3d3 324       $('#bcnav').append(Mustache.render(template, dirList[0]));   
U 325       $('#bcnav').append($('#tpl-bcr3').html());
5dfab6 326     }
a94216 327     $('.breadcrumb-item').click(fm_bc_click);
10d3d3 328     $('#ansicht').click(fm_ansicht_umschalten);
U 329     fm_set_modus();
e5ff42 330   });  
U 331 }
332
333 function fm_menu_datei_speichern() {
5ebb8e 334   //var fname = $('.datei-gewaehlt').text();
U 335   var fname = openFileName;
336   if(fname !== undefined && fname !== '') {
337     fm_save_file(fname);
338   } else {
339     fm_menu_datei_speichern_unter();
340   }
341 }
342
343 function fm_save_file(saveFileName) {
344   var m = '?c=de.uhilger.filecms.api.FileMgr&m=saveTextFile';
345   var u = '../svc' + m;
346   fm_post(u, {p1: pfad, p2: saveFileName, p3: cm.getValue()}, function(resp) {
347     openFileName = saveFileName;
348   });
349 }
350
351 function fm_menu_datei_speichern_unter() {
e5ff42 352   
915927 353   $('#modal_ok').click(function() {
U 354     // hier speichern
5ebb8e 355     fm_save_file($('#dateiname').val());
U 356     /*
915927 357     var m = '?c=de.uhilger.filecms.api.FileMgr&m=saveTextFile';
U 358     var u = '../svc' + m;
2121cc 359     fm_post(u, {p1: pfad, p2: $('#dateiname').val(), p3: cm.getValue()}, function(resp) {
915927 360
U 361     });
5ebb8e 362     */
915927 363   });
c509a0 364   $('.modal-title').text('Datei speichern');
U 365   $('#dialogfrage').text("Dateiname?");
3ad4db 366   
U 367   var fname = $('.datei-gewaehlt').text();
368   if(fname !== undefined) {
369     $('#dateiname').val(fname);
370   } else {
371     $('#dateiname').val('');
372   }
c509a0 373   $('#dateiname').attr('placeholder', 'Dateiname');
915927 374   $('#saveModal').modal({
U 375     keyboard: false,
376     show: true
377   });
a4d3b5 378 }
U 379
b7475d 380 function fm_logout() {
U 381   var m = '?c=de.uhilger.filecms.pub.SessionManager&m=expireSession';
382   var u = '../pub' + m;
383   
384   fm_get(u, "text", function(resp) {
385     $('#userMenu').text('nicht angemeldet');
386     window.location.href = '../logout.html';
387   });
388 }
389
390
a4d3b5 391 /* ---- codemirror editor handling -------- */
U 392
393 function fm_code_edit(content) {
394   cm = CodeMirror.fromTextArea(document.getElementById("editspace"), {
395     lineNumbers: true,
396     mode: "xml",
397     viewportMargin : Infinity,
398     extraKeys: {
915927 399         "F9": function(cm) {
U 400         cm.setOption("fullScreen", !cm.getOption("fullScreen"));
401       },
402         "Esc": function(cm) {
403         if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
404       }
a4d3b5 405     }
U 406   });
407   cm.setValue(content);
915927 408 }
a4d3b5 409
U 410
411
412 /* -------- helper functions ----------- */
413
b7475d 414 function fm_get(u, dtype, scallback) {
a4d3b5 415   $.ajax({
U 416     url: u,
417     type: "GET",
b7475d 418     dataType: dtype,
a4d3b5 419     success: scallback,
U 420     error: function (xhr, status, errorThrown) {
421       alert("Error: " + errorThrown + " Status: " + status + " URL: " + u);
422     },
423     complete: function (xhr, status) {
424       //console.log( "The request is complete!" );
425     }
426
427   });
915927 428 }
a4d3b5 429
U 430 function fm_post(u, d, scallback) {
431   $.ajax({
432     url: u,
433     data: d,
434     type: "POST",
435     dataType: "json",
436     success: scallback,
437     error: function (xhr, status, errorThrown) {
438       $('#fehler').html("Error: " + errorThrown + " Status: " + status);
439     },
440     complete: function (xhr, status) {
441       //alert( "The request is complete!" );
442     }
443   });
915927 444 }
a4d3b5 445
e5ff42 446 function fm_serialise(obj) {
U 447   return '{"' + obj.constructor.name + '":' + JSON.stringify(obj) + '}';
915927 448 }
e5ff42 449
7342b1 450 /* ----- Objekte ----- */
U 451
452 function FileList(fl) {
453   this.files = fl;
454 }
455
456 function FileRef(obj) {
457   var self = this;
458   this.fr = obj;
459   
460   this.typeClass = function() {
10d3d3 461     if(modus == 'kacheln') {
U 462       if(self.fr.isDirectory) {
463         return 'fa-folder';
464       } else {
465         return 'fa-file';
466       }
7342b1 467     } else {
10d3d3 468       if(self.fr.isDirectory) {
U 469         return 'fa-folder';
470       } else {
471         return 'fa-file-o';
472       }
7342b1 473     }
2121cc 474   };
U 475   
476   this.fileName = function() {
477     var namen = self.fr.absolutePath.split('/');
478     if(namen.length > 0) {
479       return namen[namen.length - 1];
480     } else {
481       return self.fr.absolutePath;
482     }
483   };
7342b1 484 }
5dfab6 485
U 486 function BcrFiles(fl) {
487   this.files = fl;
488 }
489
490 function BcrFile(rp, n) {
491   this.relPath = rp;
492   this.fName = n;
7aa566 493 }
U 494