MDL-31901: FileManager, added image for accessing right-click action
[moodle.git] / lib / form / filemanager.js
CommitLineData
494bf5c8
DC
1// This file is part of Moodle - http://moodle.org/
2//
3// Moodle is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// Moodle is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
494bf5c8 15/**
494bf5c8 16 *
840912d5
DC
17 * File Manager UI
18 * =====
19 * this.api, stores the URL to make ajax request
20 * this.currentpath
21 * this.filepicker_options
22 * this.movefile_dialog
23 * this.mkdir_dialog
24 * this.rename_dialog
25 * this.client_id
554cd8fc
DC
26 * this.filecount, how many files in this filemanager
27 * this.maxfiles
28 * this.maxbytes
840912d5
DC
29 *
30 * FileManager options:
31 * =====
32 * this.options.currentpath
33 * this.options.itemid
494bf5c8 34 */
56a7bf68 35
4c508047 36
906e7d89
MG
37M.form_filemanager = {templates:{}};
38
39M.form_filemanager.set_templates = function(Y, templates) {
40 M.form_filemanager.templates = templates;
41}
4c508047
PS
42
43/**
44 * This fucntion is called for each file picker on page.
45 */
46M.form_filemanager.init = function(Y, options) {
47 var FileManagerHelper = function(options) {
48 FileManagerHelper.superclass.constructor.apply(this, arguments);
d3067516 49 };
4c508047
PS
50 FileManagerHelper.NAME = "FileManager";
51 FileManagerHelper.ATTRS = {
840912d5
DC
52 options: {},
53 lang: {}
54 };
4c508047
PS
55
56 Y.extend(FileManagerHelper, Y.Base, {
64f93798 57 api: M.cfg.wwwroot+'/repository/draftfiles_ajax.php',
840912d5 58 menus: {},
4c508047
PS
59 initializer: function(options) {
60 this.options = options;
f45dfeeb 61 if (options.mainfile) {
ac9c14dd 62 this.enablemainfile = options.mainfile;
f45dfeeb 63 }
4c508047 64 this.client_id = options.client_id;
840912d5 65 this.currentpath = '/';
4c508047
PS
66 this.maxfiles = options.maxfiles;
67 this.maxbytes = options.maxbytes;
adce0230 68 this.emptycallback = null; // Used by drag and drop upload
133fd70b
DC
69
70 this.filepicker_options = options.filepicker?options.filepicker:{};
71 this.filepicker_options.client_id = this.client_id;
be85f7ab 72 this.filepicker_options.context = options.context;
133fd70b
DC
73 this.filepicker_options.maxfiles = this.maxfiles;
74 this.filepicker_options.maxbytes = this.maxbytes;
75 this.filepicker_options.env = 'filemanager';
133fd70b
DC
76 this.filepicker_options.itemid = options.itemid;
77
4c508047
PS
78 if (options.filecount) {
79 this.filecount = options.filecount;
554cd8fc
DC
80 } else {
81 this.filecount = 0;
82 }
e709ddd2 83 // prepare filemanager for drag-and-drop upload
906e7d89
MG
84 this.filemanager = Y.one('#filemanager-'+options.client_id);
85 if (this.filemanager.hasClass('filemanager-container') || !this.filemanager.one('.filemanager-container')) {
86 this.dndcontainer = this.filemanager;
87 } else {
88 this.dndcontainer = this.filemanager.one('.filemanager-container');
89 if (!this.dndcontainer.get('id')) {
90 this.dndcontainer.generateID();
91 }
92 }
e709ddd2
MG
93 // save template for one path element and location of path bar
94 if (this.filemanager.one('.fp-path-folder')) {
95 this.pathnode = this.filemanager.one('.fp-path-folder');
96 this.pathbar = this.pathnode.get('parentNode');
97 this.pathbar.removeChild(this.pathnode);
98 }
99 // initialize 'select file' panel
100 var fpselectnode = Y.Node.create(M.form_filemanager.templates.fileselectlayout);
101 this.filemanager.appendChild(fpselectnode);
102 this.selectui = new Y.Panel({
103 srcNode : fpselectnode,
104 zIndex : 600000,
105 centered : true,
106 modal : true,
107 close : true,
108 render : true
109 });
110 this.selectui.hide();
111 this.setup_select_file();
112 // setup buttons onclick events
840912d5 113 this.setup_buttons();
e709ddd2
MG
114 // display files
115 this.viewmode = 1; // TODO take from cookies?
116 this.filemanager.all('.fp-vb-icons,.fp-vb-tree,.fp-vb-details').removeClass('checked')
117 this.filemanager.all('.fp-vb-icons').addClass('checked')
910e1ecd 118 this.refresh(this.currentpath); // MDL-31113 get latest list from server
840912d5 119 },
4c508047 120
403eabd4 121 wait: function(client_id) {
906e7d89 122 this.filemanager.addClass('fm-updating');
403eabd4
DC
123 },
124 request: function(args, redraw) {
840912d5
DC
125 var api = this.api + '?action='+args.action;
126 var params = {};
127 var scope = this;
128 if (args['scope']) {
129 scope = args['scope'];
130 }
4c508047 131 params['sesskey'] = M.cfg.sesskey;
840912d5
DC
132 params['client_id'] = this.client_id;
133 params['filepath'] = this.currentpath;
134 params['itemid'] = this.options.itemid?this.options.itemid:0;
135 if (args['params']) {
136 for (i in args['params']) {
137 params[i] = args['params'][i];
56a7bf68 138 }
56a7bf68 139 }
840912d5
DC
140 var cfg = {
141 method: 'POST',
142 on: {
143 complete: function(id,o,p) {
144 if (!o) {
145 alert('IO FATAL');
146 return;
147 }
e709ddd2
MG
148 var data = null;
149 try {
150 data = Y.JSON.parse(o.responseText);
151 } catch(e) {
152 // TODO display error
153 //scope.print_msg(M.str.repository.invalidjson, 'error');
154 //scope.display_error(M.str.repository.invalidjson+'<pre>'+stripHTML(o.responseText)+'</pre>', 'invalidjson')
155 return;
156 }
157 if (data && data.tree && scope.set_current_tree) {
158 scope.set_current_tree(data.tree);
159 }
840912d5
DC
160 args.callback(id,data,p);
161 }
162 },
163 arguments: {
164 scope: scope
165 },
166 headers: {
167 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
168 'User-Agent': 'MoodleFileManager/3.0'
169 },
170 data: build_querystring(params)
171 };
172 if (args.form) {
173 cfg.form = args.form;
56a7bf68 174 }
840912d5 175 Y.io(api, cfg);
403eabd4
DC
176 if (redraw) {
177 this.wait(this.client_id);
178 }
840912d5
DC
179 },
180 filepicker_callback: function(obj) {
554cd8fc 181 this.filecount++;
910e1ecd
DS
182 this.check_buttons();
183 this.refresh(this.currentpath);
a5dd1f4f
SH
184 if (typeof M.core_formchangechecker != 'undefined') {
185 M.core_formchangechecker.set_form_changed();
186 }
910e1ecd
DS
187 },
188 check_buttons: function() {
906e7d89
MG
189 if (this.filecount>0) {this.filemanager.removeClass('fm-nofiles');}
190 else {this.filemanager.addClass('fm-nofiles');}
191 if (this.filecount >= this.maxfiles && this.maxfiles!=-1)
192 {this.filemanager.addClass('fm-maxfiles');}
193 else {this.filemanager.removeClass('fm-maxfiles');}
840912d5
DC
194 },
195 refresh: function(filepath) {
196 var scope = this;
59eeb81b 197 this.currentpath = filepath;
840912d5
DC
198 if (!filepath) {
199 filepath = this.currentpath;
200 } else {
201 this.currentpath = filepath;
56a7bf68 202 }
840912d5
DC
203 this.request({
204 action: 'list',
205 scope: scope,
206 params: {'filepath':filepath},
207 callback: function(id, obj, args) {
910e1ecd
DS
208 scope.filecount = obj.filecount;
209 scope.check_buttons();
840912d5
DC
210 scope.options = obj;
211 scope.render(obj);
56a7bf68 212 }
403eabd4 213 }, true);
840912d5
DC
214 },
215 setup_buttons: function() {
e709ddd2
MG
216 var button_download = this.filemanager.one('.fp-btn-download');
217 var button_create = this.filemanager.one('.fp-btn-mkdir');
218 var button_addfile = this.filemanager.one('.fp-btn-add');
840912d5
DC
219
220 // setup 'add file' button
221 // if maxfiles == -1, the no limit
906e7d89
MG
222 button_addfile.on('click', function(e) {
223 e.preventDefault();
224 var options = this.filepicker_options;
225 options.formcallback = this.filepicker_callback;
226 // XXX: magic here, to let filepicker use filemanager scope
227 options.magicscope = this;
228 options.savepath = this.currentpath;
229 M.core_filepicker.show(Y, options);
230 }, this);
840912d5
DC
231
232 // setup 'make a folder' button
233 if (this.options.subdirs) {
234 button_create.on('click',function(e) {
906e7d89 235 e.preventDefault();
840912d5
DC
236 var scope = this;
237 // a function used to perform an ajax request
238 function perform_action(e) {
239 var foldername = Y.one('#fm-newname').get('value');
240 if (!foldername) {
241 return;
242 }
243 scope.request({
244 action:'mkdir',
245 params: {filepath:scope.currentpath, newdirname:foldername},
246 callback: function(id, obj, args) {
247 var filepath = obj.filepath;
248 scope.mkdir_dialog.hide();
249 scope.refresh(filepath);
250 Y.one('#fm-newname').set('value', '');
a5dd1f4f
SH
251 if (typeof M.core_formchangechecker != 'undefined') {
252 M.core_formchangechecker.set_form_changed();
253 }
840912d5
DC
254 }
255 });
256 }
257 if (!Y.one('#fm-mkdir-dlg')) {
2b728cb5 258 var dialog = Y.Node.create('<div id="fm-mkdir-dlg"><div class="hd">'+M.str.repository.entername+'</div><div class="bd"><input type="text" id="fm-newname" /></div></div>');
840912d5
DC
259 Y.one(document.body).appendChild(dialog);
260 this.mkdir_dialog = new YAHOO.widget.Dialog("fm-mkdir-dlg", {
261 width: "300px",
262 visible: true,
263 x:e.pageX,
264 y:e.pageY,
265 constraintoviewport : true
266 });
56a7bf68 267
840912d5 268 }
2b728cb5
PS
269 var buttons = [ { text:M.str.moodle.ok, handler:perform_action, isDefault:true },
270 { text:M.str.moodle.cancel, handler:function(){this.cancel();}}];
56a7bf68 271
840912d5
DC
272 this.mkdir_dialog.cfg.queueProperty("buttons", buttons);
273 this.mkdir_dialog.render();
274 this.mkdir_dialog.show();
275 }, this);
56a7bf68 276 } else {
906e7d89 277 this.filemanager.addClass('fm-nomkdir');
56a7bf68 278 }
840912d5
DC
279
280 // setup 'download this folder' button
281 // NOTE: popup window must be enabled to perform download process
906e7d89
MG
282 button_download.on('click',function(e) {
283 e.preventDefault();
840912d5
DC
284 var scope = this;
285 // perform downloaddir ajax request
286 this.request({
287 action: 'downloaddir',
288 scope: scope,
289 callback: function(id, obj, args) {
7210e887
DC
290 if (obj) {
291 scope.refresh(obj.filepath);
292 var win = window.open(obj.fileurl, 'fm-download-folder');
293 if (!win) {
294 alert(M.str.repository.popupblockeddownload);
295 }
296 } else {
297 alert(M.str.repository.draftareanofiles);
840912d5
DC
298 }
299 }
300 });
301 }, this);
e709ddd2
MG
302
303 this.filemanager.all('.fp-vb-icons,.fp-vb-tree,.fp-vb-details').
304 on('click', function(e) {
305 e.preventDefault();
306 var viewbar = this.filemanager.one('.fp-viewbar')
307 if (!viewbar || !viewbar.hasClass('disabled')) {
308 this.filemanager.all('.fp-vb-icons,.fp-vb-tree,.fp-vb-details').removeClass('checked')
309 if (e.currentTarget.hasClass('fp-vb-tree')) {
310 this.viewmode = 2;
311 } else if (e.currentTarget.hasClass('fp-vb-details')) {
312 this.viewmode = 3;
313 } else {
314 this.viewmode = 1;
315 }
316 e.currentTarget.addClass('checked')
317 this.render();
318 //Y.Cookie.set('recentviewmode', this.viewmode);
319 }
320 }, this);
840912d5 321 },
e709ddd2
MG
322 print_path: function() {
323 var p = this.options.path;
324 this.pathbar.setContent('').addClass('empty');
325 if (p && p.length!=0 && this.viewmode != 2) {
326 for(var i = 0; i < p.length; i++) {
327 var el = this.pathnode.cloneNode(true);
328 this.pathbar.appendChild(el);
329 if (i == 0) {el.addClass('first');}
330 if (i == p.length-1) {el.addClass('last');}
331 if (i%2) {el.addClass('even');} else {el.addClass('odd');}
332 el.one('.fp-path-folder-name').setContent(p[i].name).
333 on('click', function(e, path) {
334 e.preventDefault();
335 var scope = this;
336 this.currentpath = path;
337 this.request({
338 action: 'list',
339 scope: scope,
340 params: {filepath:path},
341 callback: function(id, obj, args) {
342 scope.filecount = obj.filecount;
343 scope.check_buttons();
344 scope.options = obj;
345 scope.render(obj);
346 }
347 }, true);
348 }, this, p[i].path);
349 }
350 this.pathbar.removeClass('empty');
351 }
352 },
353 get_filepath: function(obj) {
354 if (obj.path && obj.path.length) {
355 return obj.path[obj.path.length-1].path;
356 }
357 return '';
358 },
359 treeview_dynload: function(node, cb) {
360 var retrieved_children = {};
361 if (node.children) {
362 for (var i in node.children) {
363 retrieved_children[node.children[i].path] = node.children[i];
364 }
365 }
366 this.request({
367 action:'list',
368 params: {filepath:node.path?node.path:''},
369 scope:this,
370 callback: function(id, obj, args) {
371 var list = obj.list;
372 var scope = args.scope;
373 // check that user did not leave the view mode before recieving this response
374 if (!(scope.viewmode == 2 && node && node.getChildrenEl())) {
375 return;
376 }
377 if (cb != null) { // (in manual mode do not update current path)
378 scope.options = obj;
379 }
380 node.highlight(false);
381 node.origlist = obj.list?obj.list:null;
382 node.origpath = obj.path?obj.path:null;
383 node.children = [];
384 for(k in list) {
385 if (list[k].type == 'folder' && retrieved_children[list[k].filepath]) {
386 // if this child is a folder and has already been retrieved
387 node.children[node.children.length] = retrieved_children[list[k].filepath];
388 } else {
389 // append new file to the list
390 scope.view_files([list[k]]);
391 }
392 }
393 if (cb == null) {
394 node.refresh();
840912d5 395 } else {
e709ddd2
MG
396 // invoke callback requested by TreeView component
397 cb();
494bf5c8 398 }
e709ddd2 399 //scope.content_scrolled();
56a7bf68 400 }
e709ddd2
MG
401 }, false);
402 },
403 view_files: function(appendfiles) {
906e7d89 404 this.filemanager.removeClass('fm-updating').removeClass('fm-noitems');
e709ddd2 405 if ((appendfiles == null) && (!this.options.list || this.options.list.length == 0)) {
906e7d89 406 this.filemanager.addClass('fm-noitems');
56a7bf68 407 return;
56a7bf68 408 }
e709ddd2
MG
409 var list = (appendfiles != null) ? appendfiles : this.options.list;
410 var element_template;
411 if (this.viewmode == 2 || this.viewmode == 3) {
412 element_template = Y.Node.create(M.form_filemanager.templates.listfilename);
413 } else {
414 this.viewmode = 1;
415 element_template = Y.Node.create(M.form_filemanager.templates.iconfilename);
416 }
417 var options = {
418 viewmode : this.viewmode,
419 appendonly : appendfiles != null,
420 filenode : element_template,
421 callbackcontext : this,
422 callback : function(e, node) {
23b83009 423 if (e.preventDefault) { e.preventDefault(); }
e709ddd2
MG
424 if (node.type == 'folder') {
425 this.refresh(node.filepath);
426 } else {
427 this.select_file(node);
428 }
429 },
430 rightclickcallback : function(e, node) {
23b83009 431 if (e.preventDefault) { e.preventDefault(); }
e709ddd2 432 this.select_file(node);
ac9c14dd 433 }
e709ddd2
MG
434 };
435 if (this.viewmode == 2) {
436 options.dynload = true;
437 options.filepath = this.options.path;
438 options.treeview_dynload = this.treeview_dynload;
23b83009 439 options.norootrightclick = true;
e709ddd2 440 options.callback = function(e, node) {
23b83009
MG
441 // TODO MDL-32736 e is not an event here but an object with properties 'event' and 'node'
442 if (!node.fullname) {return;}
e709ddd2
MG
443 if (node.type != 'folder') {
444 if (e.node.parent && e.node.parent.origpath) {
445 // set the current path
446 this.options.path = e.node.parent.origpath;
447 this.options.list = e.node.parent.origlist;
448 this.print_path();
449 }
450 this.select_file(node);
451 } else {
452 // save current path and filelist (in case we want to jump to other viewmode)
453 this.options.path = e.node.origpath;
454 this.options.list = e.node.origlist;
455 this.print_path();
456 //this.content_scrolled();
457 }
458 };
840912d5 459 }
e709ddd2
MG
460 if (!this.lazyloading) {this.lazyloading={};}
461 this.filemanager.one('.fp-content').fp_display_filelist(options, list, this.lazyloading);
840912d5 462 },
e709ddd2
MG
463 populate_licenses_select: function(node) {
464 if (!node) {return;}
465 node.setContent('');
466 var licenses = this.options.licenses;
467 for (var i in licenses) {
468 var option = Y.Node.create('<option/>').
469 set('value', licenses[i].shortname).
470 setContent(licenses[i].fullname);
471 node.appendChild(option)
f45dfeeb 472 }
e709ddd2
MG
473 },
474 set_current_tree: function(tree) {
475 var appendfilepaths = function(list, node) {
476 if (!node || !node.children || !node.children.length) {return;}
477 for (var i in node.children) {
478 list[list.length] = node.children[i].filepath;
479 appendfilepaths(list, node.children[i]);
480 }
f0a163ed 481 }
e709ddd2
MG
482 var list = ['/'];
483 appendfilepaths(list, tree);
484 var selectnode = this.filemanager.one('.fp-select');
485 node = selectnode.one('.fp-path select');
486 node.setContent('');
487 for (var i in list) {
488 node.appendChild(Y.Node.create('<option/>').
489 set('value', list[i]).setContent(list[i]))
f45dfeeb 490 }
840912d5 491 },
e709ddd2
MG
492 update_file: function() {
493 var selectnode = this.filemanager.one('.fp-select');
494 var fileinfo = this.selectui.fileinfo;
495
496 var newfilename = selectnode.one('.fp-saveas input').get('value');
497 var filenamechanged = (newfilename && newfilename != fileinfo.fullname);
498 var pathselect = selectnode.one('.fp-path select'),
499 pathindex = pathselect.get('selectedIndex'),
500 targetpath = pathselect.get("options").item(pathindex).get('value');
501 var filepathchanged = (targetpath != this.get_parent_folder_name(fileinfo));
502
503 if (filenamechanged && filepathchanged) {
504 alert('Sorry, simultaneous changing of name and path is not supported yet'); // TODO
505 return;
506 }
507 if (!filenamechanged && !filepathchanged) {
508 // no changes
509 this.selectui.hide();
510 }
511
512 selectnode.addClass('loading');
513
514 // RENAME
515 if (filenamechanged) {
516 var action = '';
840912d5 517 var params = {};
e709ddd2
MG
518 if (fileinfo.type == 'folder') {
519 params['filepath'] = fileinfo.filepath;
520 params['filename'] = '.';
521 params['newdirname'] = newfilename;
522 action = 'renamedir';
523 } else {
524 params['filepath'] = fileinfo.filepath;
525 params['filename'] = fileinfo.fullname;
526 params['newfilename'] = newfilename;
527 action = 'rename';
528 }
840912d5 529 this.request({
e709ddd2
MG
530 action: action,
531 scope: this,
840912d5
DC
532 params: params,
533 callback: function(id, obj, args) {
e709ddd2
MG
534 args.scope.selectui.hide();
535 if (obj == false) {
536 alert(M.str.repository.fileexists); // TODO!
537 } else {
538 args.scope.refresh(obj.filepath);
539 if (typeof M.core_formchangechecker != 'undefined') {
540 M.core_formchangechecker.set_form_changed();
541 }
542 }
56a7bf68 543 }
840912d5 544 });
56a7bf68 545 }
840912d5 546
e709ddd2
MG
547 // MOVE
548 if (filepathchanged) {
840912d5 549 var params = {};
e709ddd2
MG
550 if (fileinfo.type == 'folder') {
551 action = 'movedir';
552 } else {
553 action = 'movefile';
554 }
840912d5
DC
555 params['filepath'] = fileinfo.filepath;
556 params['filename'] = fileinfo.fullname;
e709ddd2 557 params['newfilepath'] = targetpath;
840912d5 558 this.request({
e709ddd2
MG
559 action: action,
560 scope: this,
840912d5
DC
561 params: params,
562 callback: function(id, obj, args) {
e709ddd2
MG
563 args.scope.selectui.hide();
564 args.scope.refresh((obj && obj.filepath) ? obj.filepath : '/');
565 if (typeof M.core_formchangechecker != 'undefined') {
566 M.core_formchangechecker.set_form_changed();
567 }
1a79133b
DC
568 }
569 });
570 }
840912d5 571 },
e709ddd2
MG
572 setup_select_file: function() {
573 var selectnode = this.filemanager.one('.fp-select');
574 // bind labels with corresponding inputs
575 selectnode.all('.fp-saveas,.fp-path,.fp-author,.fp-license').each(function (node) {
576 node.all('label').set('for', node.one('input,select').generateID());
577 });
578 this.populate_licenses_select(selectnode.one('.fp-license select'));
579 // register event on clicking buttons
580 selectnode.one('.fp-file-update').on('click', function(e) {
581 e.preventDefault();
582 this.update_file();
583 }, this);
584 selectnode.one('.fp-file-download').on('click', function(e) {
585 e.preventDefault();
586 window.open(this.selectui.fileinfo.url, 'fm-download-file');
587 }, this);
588 selectnode.one('.fp-file-delete').on('click', function(e) {
589 e.preventDefault();
840912d5
DC
590 var dialog_options = {};
591 var params = {};
2b728cb5 592 dialog_options.message = M.str.repository.confirmdeletefile;
840912d5 593 dialog_options.scope = this;
e709ddd2 594 if (this.selectui.fileinfo.type == 'folder') {
840912d5 595 params.filename = '.';
e709ddd2 596 params.filepath = this.selectui.fileinfo.filepath;
840912d5 597 } else {
e709ddd2 598 params.filename = this.selectui.fileinfo.fullname;
840912d5 599 }
20fb563e
PS
600 dialog_options.callbackargs = [params];
601 dialog_options.callback = function(params) {
e709ddd2 602 //selectnode.addClass('loading');
840912d5
DC
603 this.request({
604 action: 'delete',
20fb563e 605 scope: this,
840912d5
DC
606 params: params,
607 callback: function(id, obj, args) {
e709ddd2
MG
608 //args.scope.selectui.hide();
609 args.scope.filecount--;
610 args.scope.refresh(obj.filepath);
a5dd1f4f
SH
611 if (typeof M.core_formchangechecker != 'undefined') {
612 M.core_formchangechecker.set_form_changed();
613 }
56a7bf68 614 }
840912d5 615 });
d3067516 616 };
e709ddd2
MG
617 this.selectui.hide(); // TODO remove this after confirm dialog is replaced with YUI3
618 M.util.show_confirm_dialog(e, dialog_options);
619 }, this);
620 selectnode.one('.fp-file-zip').on('click', function(e) {
621 e.preventDefault();
622 var params = {};
623 var fileinfo = this.selectui.fileinfo;
624 params['filepath'] = fileinfo.filepath;
625 params['filename'] = '.';
626 selectnode.addClass('loading');
627 this.request({
628 action: 'zip',
629 scope: this,
630 params: params,
631 callback: function(id, obj, args) {
632 args.scope.selectui.hide();
633 args.scope.refresh(obj.filepath);
840912d5 634 }
e709ddd2
MG
635 });
636 }, this);
637 selectnode.one('.fp-file-unzip').on('click', function(e) {
638 e.preventDefault();
639 var params = {};
640 var fileinfo = this.selectui.fileinfo;
641 params['filepath'] = fileinfo.filepath;
642 params['filename'] = fileinfo.fullname;
643 selectnode.addClass('loading');
644 this.request({
645 action: 'unzip',
646 scope: this,
647 params: params,
648 callback: function(id, obj, args) {
649 args.scope.selectui.hide();
650 args.scope.refresh(obj.filepath);
840912d5 651 }
e709ddd2
MG
652 });
653 }, this);
654 selectnode.one('.fp-file-setmain').on('click', function(e) {
655 e.preventDefault();
656 var params = {};
657 var fileinfo = this.selectui.fileinfo;
658 params['filepath'] = fileinfo.filepath;
659 params['filename'] = fileinfo.fullname;
660 selectnode.addClass('loading');
661 this.request({
662 action: 'setmainfile',
663 scope: this,
664 params: params,
665 callback: function(id, obj, args) {
666 args.scope.selectui.hide();
667 args.scope.refresh(obj.filepath);
840912d5 668 }
840912d5 669 });
e709ddd2
MG
670 }, this);
671 selectnode.all('.fp-file-cancel').on('click', function(e) {
672 e.preventDefault();
673 // TODO if changed asked to confirm, the same with close button
674 this.selectui.hide();
675 }, this);
676 },
677 get_parent_folder_name: function(node) {
678 if (node.type != 'folder' || node.filepath.length < node.fullname.length+1) { return node.filepath; }
679 var basedir = node.filepath.substr(0, node.filepath.length - node.fullname.length - 1);
680 var lastdir = node.filepath.substr(node.filepath.length - node.fullname.length - 2);
681 if (lastdir == '/' + node.fullname + '/') { return basedir; }
682 return node.filepath;
683 },
684 select_file: function(node) {
685 var selectnode = this.filemanager.one('.fp-select');
686 selectnode.removeClass('loading').removeClass('fp-folder').
687 removeClass('fp-file').removeClass('fp-zip').removeClass('fp-cansetmain');
688 if (node.type == 'folder' || node.type == 'zip') {selectnode.addClass('fp-'+node.type);}
689 else {selectnode.addClass('fp-file');}
690 if (this.enablemainfile && (node.sortorder != 1)) {
691 selectnode.addClass('fp-cansetmain');
840912d5 692 }
e709ddd2
MG
693 this.selectui.fileinfo = node;
694 selectnode.one('.fp-saveas input').set('value', node.fullname);
695 var foldername = this.get_parent_folder_name(node);
696 selectnode.all('.fp-origpath .fp-value').setContent(foldername);
697 selectnode.all('.fp-author input').set('value', node.author);
698 selectnode.all('.fp-license select option[selected]').set('selected', false);
699 selectnode.all('.fp-license select option[value='+node.license+']').set('selected', true);
700 selectnode.all('.fp-path select option[selected]').set('selected', false);
701 selectnode.all('.fp-path select option').each(function(el){
702 if (el.get('value') == foldername) {el.set('selected', true);}
840912d5 703 });
e709ddd2
MG
704 selectnode.all('.fp-author input').set('disabled','disabled'); //TODO
705 selectnode.all('.fp-license select').set('disabled','disabled'); //TODO
706 // display static information about a file (when known)
707 var attrs = ['datemodified','datecreated','size','dimensions'];
708 for (var i in attrs) {
709 if (selectnode.one('.fp-'+attrs[i])) {
710 var value = (node[attrs[i]+'_f']) ? node[attrs[i]+'_f'] : (node[attrs[i]] ? node[attrs[i]] : '');
711 selectnode.one('.fp-'+attrs[i]).addClassIf('fp-unknown', ''+value == '')
712 .one('.fp-value').setContent(value);
713 }
714 }
715 // display thumbnail
716 var imgnode = Y.Node.create('<img/>').
717 set('src', node.realthumbnail ? node.realthumbnail : node.thumbnail).
718 setStyle('maxHeight', ''+(node.thumbnail_height ? node.thumbnail_height : 90)+'px').
719 setStyle('maxWidth', ''+(node.thumbnail_width ? node.thumbnail_width : 90)+'px');
720 selectnode.one('.fp-thumbnail').setContent('').appendChild(imgnode);
721 // show panel
722 this.selectui.show();
723 },
724 render: function() {
725 this.print_path();
726 this.view_files();
840912d5
DC
727 }
728 });
4c508047
PS
729
730 // finally init everything needed
906e7d89
MG
731 // hide loading picture, display filemanager interface
732 var filemanager = Y.one('#filemanager-'+options.client_id);
733 filemanager.removeClass('fm-loading').addClass('fm-loaded');
4c508047 734
f08fac7c
DS
735 var manager = new FileManagerHelper(options);
736 var dndoptions = {
737 filemanager: manager,
738 acceptedtypes: options.accepted_types,
739 clientid: options.client_id,
740 maxfiles: options.maxfiles,
741 maxbytes: options.maxbytes,
742 itemid: options.itemid,
743 repositories: manager.filepicker_options.repositories,
906e7d89 744 containerid: manager.dndcontainer.get('id')
f08fac7c
DS
745 };
746 M.form_dndupload.init(Y, dndoptions);
4c508047 747};