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