Dateiverwaltung für die WebBox
ulrich
2017-02-21 3ad4db4a15b4ba59c65e2af797967941f2cb2ba2
commit | author | age
a4d3b5 1 var cm;
5dfab6 2 var pfad = '';
c7c502 3
U 4 function fm_init() {
a4d3b5 5   $('.codeeditor-space').hide();
U 6   $('.code-editor-container').hide();
7   $('#newTextFile').on('click', fm_menu_neue_textdatei);
c509a0 8   $('#newFolder').on('click', fm_menu_neuer_ordner);
3ad4db 9   $('#editTextFile').on('click', fm_get_file_content);
e5ff42 10   $('#saveFile').on('click', fm_menu_datei_speichern);
a4d3b5 11   $('#closeFile').on('click', fm_menu_datei_schliessen);
c509a0 12   $('#saveModal').on('hidden.bs.modal', function (e) {
915927 13     $('#modal_ok').attr('onclick','').unbind('click');
b7475d 14   });
U 15   $('#logout').click(fm_logout);  
e5ff42 16   fm_get_login();
7342b1 17   fm_get_list('');
a4d3b5 18 }
U 19
20 function fm_menu_neue_textdatei() {
2121cc 21   /*
U 22   $('#dateiansicht').hide();
23   $('.codeeditor-space').show();
24   $('.code-editor-container').show();
25   fm_code_edit('Test');
26   */
3ad4db 27   fm_text_edit('Test');
a4d3b5 28 }
U 29
30 function fm_menu_datei_schliessen() {
31   $('.codeeditor-space').hide();
32   $('.code-editor-container').hide();
33   cm.toTextArea();
34   $('#dateiansicht').show();  
e5ff42 35 }
U 36
5dfab6 37 function fm_dateiwahl() {
U 38   var elem = this;
39   //console.log('datei gewaehlt ' + $(elem).text().trim());
40   if($(elem).children(0).hasClass('fa-folder')) {
41     var ordner = $(elem).text().trim();
42     if(pfad.length > 0) {
43       pfad = pfad + '/' + ordner;
44     } else {
45       pfad = ordner;
46     }
47     fm_get_list(pfad);
48   } else if($(elem).children(0).hasClass('fa-file')) {
49     $('.datei-gewaehlt').removeClass('datei-gewaehlt');
50     $(elem).children(0).addClass('datei-gewaehlt');
51   } else {
52     //console.log('kein folder oder file...');
53   }
54 }
55
c509a0 56 function fm_menu_neuer_ordner() {
U 57   fm_get_new_folder();
58 }
59
a94216 60 function fm_bc_click() {
U 61   var elem = this;
62   var bcPfad = $(elem).attr('rpath');
63   //console.log(relPath);
64   if(bcPfad !== undefined) {
c509a0 65     pfad = bcPfad;
a94216 66     fm_get_list(bcPfad);
U 67   } else {
68     pfad = '';
69     fm_get_list('');
70   }
71   
72   
73   //console.log($(elem).text() + ' ' + $(elem).attr('rpath') + ' ' + $(elem).hasClass('active'));
74 }
75
3ad4db 76 function fm_text_edit(content) {
U 77   $('#dateiansicht').hide();
78   $('.codeeditor-space').show();
79   $('.code-editor-container').show();
80   fm_code_edit(content);  
81 }
82
b7475d 83 /* ----- API Calls ------------- */
U 84
e5ff42 85 function fm_get_login() {
3ad4db 86   var m = '?c=de.uhilger.filecms.pub.SessionManager&m=getSessionUser';
U 87   var u = '../pub' + m;
88   fm_get(u, "text", function(resp) {
89     $('#userMenu').text(resp);
7342b1 90   });  
c509a0 91 }
U 92
93 function fm_get_new_folder() {
94   $('#modal_ok').click(function() {
95     // hier speichern
96     var m = '?c=de.uhilger.filecms.api.FileMgr&m=newFolder&p=' + pfad + '&p=' + $('#dateiname').val();
97     var u = '../svc' + m;
98     fm_get(u, "json", function(resp) {
99       
100     });
101   
102     /*
103     var m = '?c=de.uhilger.filecms.api.FileMgr&m=saveTextFile';
104     var u = '../svc' + m;
105     fm_post(u, {p1: pfad, p2: $('#dateiname').val(), p3: cm.getValue()}, function(resp) {
106
107     });
108     */
109   });
110   $('.modal-title').text('Neuer Ordner');
111   $('#dialogfrage').text("Name?");
112   $('#dateiname').val('');
113   $('#dateiname').attr('placeholder', 'Ordnername');
114   $('#saveModal').modal({
115     keyboard: false,
116     show: true
117   });
7342b1 118 }
U 119
2121cc 120 function fm_get_file_content() {
U 121   
122   var fname = $('.datei-gewaehlt').text();
123   
a94216 124   //console.log(pfad + '/' + fname);
2121cc 125   
3ad4db 126   var m = '?c=de.uhilger.filecms.api.FileMgr&m=getCode&p=' + pfad + '&p=' + fname;
2121cc 127   var u = '../svc' + m;
3ad4db 128   fm_get(u, "text", function(resp) {
U 129     /*
130     $('#dateiansicht').hide();
131     $('.codeeditor-space').show();
132     $('.code-editor-container').show();
133     fm_code_edit(resp);
134     */
135     fm_text_edit(resp);
2121cc 136   });
3ad4db 137   
2121cc 138 }
U 139
7342b1 140 // http://localhost:8079/file-cms/svc?c=de.uhilger.filecms.api.FileMgr&f=JSONNICE&m=list&p=
a94216 141 function fm_get_list(relPfad) {
U 142   var m = '?c=de.uhilger.filecms.api.FileMgr&m=list&p=' + relPfad;
7342b1 143   var u = '../svc' + m;
U 144   fm_get(u, "json", function(resp) {
2121cc 145     if(resp.List[0].FileRef !== undefined) {
U 146       var files = new Array();
c509a0 147       if(resp.List[0].FileRef instanceof Array) {
U 148         for(var i = 0; i < resp.List[0].FileRef.length; i++) {
149           files.push(new FileRef(resp.List[0].FileRef[i]));
150         }
151       } else {
152         files.push(new FileRef(resp.List[0].FileRef));
2121cc 153       }
U 154       var fl = new FileList(files);
155       var template = $('#tpl-kacheln').html();
156       Mustache.parse(template);   // optional, speeds up future uses
157       $('.figure').attr('onclick','').unbind('click');
158       $('#dateien').empty();
159       $('#dateien').html(Mustache.render(template, fl));
160       $('.figure').click(fm_dateiwahl);
161     } else {
162       $('#dateien').empty();
7342b1 163     }
5dfab6 164     
2315a0 165     var template;
a94216 166     $('.breadcrumb-item').attr('onclick','').unbind('click');
2315a0 167     $('#bcnav').empty();
5dfab6 168     var dirList = new Array();
U 169     var rp = '';
a94216 170     //console.log("'" + relPfad + "'");
U 171     var dirs = relPfad.split('/');
172     //console.log(dirs.length);
2121cc 173     dirList.push(new BcrFile(rp, 'Home'));
a94216 174     if(relPfad.length > 0) {
2315a0 175       for(var i = 0; i < dirs.length - 1; i++) {
5dfab6 176         if(rp.length > 0 ) {
U 177           dirList.push(new BcrFile(rp + '/' + dirs[i], dirs[i]));
2315a0 178           rp = rp + '/' + dirs[i];
5dfab6 179         } else {
U 180           dirList.push(new BcrFile(dirs[i], dirs[i]));
2315a0 181           rp = dirs[i];
5dfab6 182         }
U 183       }
184       var bl = new BcrFiles(dirList);
2315a0 185
U 186       if(dirList.length > 0) {
187         template = $('#tpl-bcr').html();
188         Mustache.parse(template);   // optional, speeds up future uses
189         $('#bcnav').html(Mustache.render(template, bl));
190       }
191       
192       if(dirs.length > 0) {
193         dirList.push(new BcrFile(rp + '/' + dirs[dirs.length-1], dirs[dirs.length-1]));
194         template = $('#tpl-bcr2').html();
195         Mustache.parse(template);   // optional, speeds up future uses
196         $('#bcnav').append(Mustache.render(template, dirList[dirList.length-1]));        
197       } else {
198         template = $('#tpl-bcr2').html();
199         Mustache.parse(template);   // optional, speeds up future uses
200         $('#bcnav').append(Mustache.render(template, dirList[0]));        
201       }
202       
5dfab6 203       $('#bcnav').append($('#tpl-bcr3').html());
2315a0 204     } else {
a94216 205       pfad = '';
2315a0 206       template = $('#tpl-bcr2').html();
U 207       Mustache.parse(template);   // optional, speeds up future uses
208       $('#bcnav').append(Mustache.render(template, dirList[0]));        
5dfab6 209     }
a94216 210     
U 211     $('.breadcrumb-item').click(fm_bc_click);
e5ff42 212   });  
U 213 }
214
215 function fm_menu_datei_speichern() {
216   
915927 217   $('#modal_ok').click(function() {
U 218     // hier speichern
219     var m = '?c=de.uhilger.filecms.api.FileMgr&m=saveTextFile';
220     var u = '../svc' + m;
2121cc 221     fm_post(u, {p1: pfad, p2: $('#dateiname').val(), p3: cm.getValue()}, function(resp) {
915927 222
U 223     });
224   });
c509a0 225   $('.modal-title').text('Datei speichern');
U 226   $('#dialogfrage').text("Dateiname?");
3ad4db 227   
U 228   var fname = $('.datei-gewaehlt').text();
229   if(fname !== undefined) {
230     $('#dateiname').val(fname);
231   } else {
232     $('#dateiname').val('');
233   }
c509a0 234   $('#dateiname').attr('placeholder', 'Dateiname');
915927 235   $('#saveModal').modal({
U 236     keyboard: false,
237     show: true
238   });
239   
240   
241   // FileRef saveTextFile(String relPath, String fileName, String contents)
e5ff42 242   
U 243   
244   /*
245   var t = new Template(-2, $('#filename').val(), self.cm.getValue(), 3);
246   var u = '../api/tr/?c=de.uhilger.webbox.api.ContentApi&m=newTemplate';
247   self.post(u, {p: self.serialise(t)}, function (resp) {
248     self.isnew = false;
249     self.editid = resp.Template.id;
250     $('#contlist').append(self.buildContListItem(3, resp.Template.name, resp.Template.id));
251     $('#templateeditor').addClass('hidden');
252   });
253   */
a4d3b5 254 }
U 255
b7475d 256 function fm_logout() {
U 257   var m = '?c=de.uhilger.filecms.pub.SessionManager&m=expireSession';
258   var u = '../pub' + m;
259   
260   fm_get(u, "text", function(resp) {
261     $('#userMenu').text('nicht angemeldet');
262     window.location.href = '../logout.html';
263   });
264 }
265
266
a4d3b5 267 /* ---- codemirror editor handling -------- */
U 268
269 function fm_code_edit(content) {
915927 270   //var windowHeight = $(window).height();
a4d3b5 271   //$("editspace").empty();
U 272   //self.cm.toTextArea();
273
274   cm = CodeMirror.fromTextArea(document.getElementById("editspace"), {
275     lineNumbers: true,
276     mode: "xml",
277     viewportMargin : Infinity,
278     extraKeys: {
915927 279         "F9": function(cm) {
U 280         cm.setOption("fullScreen", !cm.getOption("fullScreen"));
281       },
282         "Esc": function(cm) {
283         if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
284       }
a4d3b5 285     }
U 286   });
287   cm.setValue(content);
915927 288 }
a4d3b5 289
U 290
291
292 /* -------- helper functions ----------- */
293
b7475d 294 function fm_get(u, dtype, scallback) {
a4d3b5 295   $.ajax({
U 296     url: u,
297     type: "GET",
b7475d 298     dataType: dtype,
a4d3b5 299     success: scallback,
U 300     error: function (xhr, status, errorThrown) {
301       alert("Error: " + errorThrown + " Status: " + status + " URL: " + u);
302     },
303     complete: function (xhr, status) {
304       //console.log( "The request is complete!" );
305     }
306
307   });
915927 308 }
a4d3b5 309
U 310 function fm_post(u, d, scallback) {
311   $.ajax({
312     url: u,
313     data: d,
314     type: "POST",
315     dataType: "json",
316     success: scallback,
317     error: function (xhr, status, errorThrown) {
318       $('#fehler').html("Error: " + errorThrown + " Status: " + status);
319     },
320     complete: function (xhr, status) {
321       //alert( "The request is complete!" );
322     }
323   });
915927 324 }
a4d3b5 325
e5ff42 326 function fm_serialise(obj) {
U 327   return '{"' + obj.constructor.name + '":' + JSON.stringify(obj) + '}';
915927 328 }
e5ff42 329
7342b1 330 /* ----- Objekte ----- */
U 331
332 function FileList(fl) {
333   this.files = fl;
334 }
335
336 function FileRef(obj) {
337   var self = this;
338   this.fr = obj;
339   
340   this.typeClass = function() {
341     if(self.fr.isDirectory) {
342       return 'fa-folder';
343     } else {
344       return 'fa-file';
345     }
2121cc 346   };
U 347   
348   this.fileName = function() {
349     var namen = self.fr.absolutePath.split('/');
350     if(namen.length > 0) {
351       return namen[namen.length - 1];
352     } else {
353       return self.fr.absolutePath;
354     }
355   };
7342b1 356 }
5dfab6 357
U 358 function BcrFiles(fl) {
359   this.files = fl;
360 }
361
362 function BcrFile(rp, n) {
363   this.relPath = rp;
364   this.fName = n;
365 }