Dateiverwaltung für die WebBox
ulrich
2017-02-20 2121ccd4015d7c0dc81485fcaf8655bde28ea396
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     
U 109     var dirList = new Array();
110     var rp = '';
111     var dirs = relPath.split('/');
2121cc 112     dirList.push(new BcrFile(rp, 'Home'));
5dfab6 113     if(dirs.length > 0) {
U 114       for(var i = 0; i < dirs.length; i++) {
115         if(rp.length > 0 ) {
116           dirList.push(new BcrFile(rp + '/' + dirs[i], dirs[i]));
117         } else {
118           dirList.push(new BcrFile(dirs[i], dirs[i]));
119         }
120       }
121       var bl = new BcrFiles(dirList);
122       var template = $('#tpl-bcr').html();
123       Mustache.parse(template);   // optional, speeds up future uses
124       $('#bcnav').empty();
125       $('#bcnav').html(Mustache.render(template, bl));
126       $('#bcnav').append($('#tpl-bcr2').html());
127       $('#bcnav').append($('#tpl-bcr3').html());
128     }
e5ff42 129   });  
U 130 }
131
132 function fm_menu_datei_speichern() {
133   
915927 134   $('#modal_ok').click(function() {
U 135     // hier speichern
136     var m = '?c=de.uhilger.filecms.api.FileMgr&m=saveTextFile';
137     var u = '../svc' + m;
2121cc 138     fm_post(u, {p1: pfad, p2: $('#dateiname').val(), p3: cm.getValue()}, function(resp) {
915927 139
U 140     });
141   });
142   $('#saveModal').modal({
143     keyboard: false,
144     show: true
145   });
146   
147   
148   // FileRef saveTextFile(String relPath, String fileName, String contents)
e5ff42 149   
U 150   
151   /*
152   var t = new Template(-2, $('#filename').val(), self.cm.getValue(), 3);
153   var u = '../api/tr/?c=de.uhilger.webbox.api.ContentApi&m=newTemplate';
154   self.post(u, {p: self.serialise(t)}, function (resp) {
155     self.isnew = false;
156     self.editid = resp.Template.id;
157     $('#contlist').append(self.buildContListItem(3, resp.Template.name, resp.Template.id));
158     $('#templateeditor').addClass('hidden');
159   });
160   */
a4d3b5 161 }
U 162
b7475d 163 function fm_logout() {
U 164   var m = '?c=de.uhilger.filecms.pub.SessionManager&m=expireSession';
165   var u = '../pub' + m;
166   
167   fm_get(u, "text", function(resp) {
168     $('#userMenu').text('nicht angemeldet');
169     window.location.href = '../logout.html';
170   });
171 }
172
173
a4d3b5 174 /* ---- codemirror editor handling -------- */
U 175
176 function fm_code_edit(content) {
915927 177   //var windowHeight = $(window).height();
a4d3b5 178   //$("editspace").empty();
U 179   //self.cm.toTextArea();
180
181   cm = CodeMirror.fromTextArea(document.getElementById("editspace"), {
182     lineNumbers: true,
183     mode: "xml",
184     viewportMargin : Infinity,
185     extraKeys: {
915927 186         "F9": function(cm) {
U 187         cm.setOption("fullScreen", !cm.getOption("fullScreen"));
188       },
189         "Esc": function(cm) {
190         if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
191       }
a4d3b5 192     }
U 193   });
194   cm.setValue(content);
915927 195 }
a4d3b5 196
U 197
198
199 /* -------- helper functions ----------- */
200
b7475d 201 function fm_get(u, dtype, scallback) {
a4d3b5 202   $.ajax({
U 203     url: u,
204     type: "GET",
b7475d 205     dataType: dtype,
a4d3b5 206     success: scallback,
U 207     error: function (xhr, status, errorThrown) {
208       alert("Error: " + errorThrown + " Status: " + status + " URL: " + u);
209     },
210     complete: function (xhr, status) {
211       //console.log( "The request is complete!" );
212     }
213
214   });
915927 215 }
a4d3b5 216
U 217 function fm_post(u, d, scallback) {
218   $.ajax({
219     url: u,
220     data: d,
221     type: "POST",
222     dataType: "json",
223     success: scallback,
224     error: function (xhr, status, errorThrown) {
225       $('#fehler').html("Error: " + errorThrown + " Status: " + status);
226     },
227     complete: function (xhr, status) {
228       //alert( "The request is complete!" );
229     }
230   });
915927 231 }
a4d3b5 232
e5ff42 233 function fm_serialise(obj) {
U 234   return '{"' + obj.constructor.name + '":' + JSON.stringify(obj) + '}';
915927 235 }
e5ff42 236
7342b1 237 /* ----- Objekte ----- */
U 238
239 function FileList(fl) {
240   this.files = fl;
241 }
242
243 function FileRef(obj) {
244   var self = this;
245   this.fr = obj;
246   
247   this.typeClass = function() {
248     if(self.fr.isDirectory) {
249       return 'fa-folder';
250     } else {
251       return 'fa-file';
252     }
2121cc 253   };
U 254   
255   this.fileName = function() {
256     var namen = self.fr.absolutePath.split('/');
257     if(namen.length > 0) {
258       return namen[namen.length - 1];
259     } else {
260       return self.fr.absolutePath;
261     }
262     
263   };
7342b1 264 }
5dfab6 265
U 266 function BcrFiles(fl) {
267   this.files = fl;
268 }
269
270 function BcrFile(rp, n) {
271   this.relPath = rp;
272   this.fName = n;
273 }