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