MDL-31901: FileManager uses more YUI3 windows and more renderers
[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
694beb54 121 wait: function() {
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
694beb54 153 scope.print_msg(M.str.repository.invalidjson, 'error');
e709ddd2
MG
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 176 if (redraw) {
694beb54 177 this.wait();
403eabd4 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 214 },
694beb54
MG
215 /** displays message in a popup */
216 print_msg: function(msg, type) {
217 var header = M.str.moodle.error;
218 if (type != 'error') {
219 type = 'info'; // one of only two types excepted
220 header = M.str.moodle.info;
221 }
222 if (!this.msg_dlg) {
223 var node = Y.Node.create(M.form_filemanager.templates.message);
224 this.filemanager.appendChild(node);
225
226 this.msg_dlg = new Y.Panel({
227 srcNode : node,
228 zIndex : 800000,
229 centered : true,
230 modal : true,
231 visible : false,
232 render : true
233 });
234 node.one('.fp-msg-butok').on('click', function(e) {
235 e.preventDefault();
236 this.msg_dlg.hide();
237 }, this);
238 }
239
240 this.msg_dlg.set('headerContent', header);
241 this.filemanager.one('.fp-msg').removeClass('fp-msg-info').removeClass('fp-msg-error').addClass('fp-msg-'+type)
242 this.filemanager.one('.fp-msg .fp-msg-text').setContent(msg);
243 this.msg_dlg.show();
244 },
840912d5 245 setup_buttons: function() {
e709ddd2
MG
246 var button_download = this.filemanager.one('.fp-btn-download');
247 var button_create = this.filemanager.one('.fp-btn-mkdir');
248 var button_addfile = this.filemanager.one('.fp-btn-add');
840912d5
DC
249
250 // setup 'add file' button
251 // if maxfiles == -1, the no limit
906e7d89
MG
252 button_addfile.on('click', function(e) {
253 e.preventDefault();
254 var options = this.filepicker_options;
255 options.formcallback = this.filepicker_callback;
256 // XXX: magic here, to let filepicker use filemanager scope
257 options.magicscope = this;
258 options.savepath = this.currentpath;
259 M.core_filepicker.show(Y, options);
260 }, this);
840912d5
DC
261
262 // setup 'make a folder' button
263 if (this.options.subdirs) {
264 button_create.on('click',function(e) {
906e7d89 265 e.preventDefault();
840912d5
DC
266 var scope = this;
267 // a function used to perform an ajax request
694beb54
MG
268 var perform_action = function(e) {
269 e.preventDefault();
270 var foldername = Y.one('#fm-newname-'+scope.client_id).get('value');
840912d5 271 if (!foldername) {
694beb54 272 scope.mkdir_dialog.hide();
840912d5
DC
273 return;
274 }
275 scope.request({
276 action:'mkdir',
277 params: {filepath:scope.currentpath, newdirname:foldername},
278 callback: function(id, obj, args) {
279 var filepath = obj.filepath;
280 scope.mkdir_dialog.hide();
281 scope.refresh(filepath);
694beb54 282 Y.one('#fm-newname-'+scope.client_id).set('value', '');
a5dd1f4f
SH
283 if (typeof M.core_formchangechecker != 'undefined') {
284 M.core_formchangechecker.set_form_changed();
285 }
840912d5
DC
286 }
287 });
288 }
694beb54
MG
289 if (!this.mkdir_dialog) {
290 var node = Y.Node.create(M.form_filemanager.templates.mkdir);
291 this.filemanager.appendChild(node);
292 this.mkdir_dialog = new Y.Panel({
293 srcNode : node,
294 zIndex : 800000,
295 centered : true,
296 modal : true,
297 visible : false,
298 render : true
299 });
300 node.one('.fp-dlg-butcreate').on('click', perform_action, this);
301 node.one('input').set('id', 'fm-newname-'+this.client_id).
302 on('keydown', function(e){
303 if (e.keyCode == 13) {Y.bind(perform_action, this)(e);}
304 }, this);
305 node.all('.fp-dlg-butcancel').on('click', function(e){e.preventDefault();this.mkdir_dialog.hide();}, this);
306 node.all('.fp-dlg-curpath').set('id', 'fm-curpath-'+this.client_id);
840912d5 307 }
840912d5 308 this.mkdir_dialog.show();
694beb54
MG
309 Y.one('#fm-newname-'+scope.client_id).focus();
310 Y.all('#fm-curpath-'+scope.client_id).setContent(this.currentpath)
840912d5 311 }, this);
56a7bf68 312 } else {
906e7d89 313 this.filemanager.addClass('fm-nomkdir');
56a7bf68 314 }
840912d5
DC
315
316 // setup 'download this folder' button
317 // NOTE: popup window must be enabled to perform download process
906e7d89
MG
318 button_download.on('click',function(e) {
319 e.preventDefault();
840912d5
DC
320 var scope = this;
321 // perform downloaddir ajax request
322 this.request({
323 action: 'downloaddir',
324 scope: scope,
325 callback: function(id, obj, args) {
7210e887
DC
326 if (obj) {
327 scope.refresh(obj.filepath);
328 var win = window.open(obj.fileurl, 'fm-download-folder');
329 if (!win) {
694beb54 330 scope.print_msg(M.str.repository.popupblockeddownload, 'error');
7210e887
DC
331 }
332 } else {
694beb54 333 scope.print_msg(M.str.repository.draftareanofiles, 'error');
840912d5
DC
334 }
335 }
336 });
337 }, this);
e709ddd2
MG
338
339 this.filemanager.all('.fp-vb-icons,.fp-vb-tree,.fp-vb-details').
340 on('click', function(e) {
341 e.preventDefault();
342 var viewbar = this.filemanager.one('.fp-viewbar')
343 if (!viewbar || !viewbar.hasClass('disabled')) {
344 this.filemanager.all('.fp-vb-icons,.fp-vb-tree,.fp-vb-details').removeClass('checked')
345 if (e.currentTarget.hasClass('fp-vb-tree')) {
346 this.viewmode = 2;
347 } else if (e.currentTarget.hasClass('fp-vb-details')) {
348 this.viewmode = 3;
349 } else {
350 this.viewmode = 1;
351 }
352 e.currentTarget.addClass('checked')
353 this.render();
354 //Y.Cookie.set('recentviewmode', this.viewmode);
355 }
356 }, this);
840912d5 357 },
e709ddd2
MG
358 print_path: function() {
359 var p = this.options.path;
360 this.pathbar.setContent('').addClass('empty');
361 if (p && p.length!=0 && this.viewmode != 2) {
362 for(var i = 0; i < p.length; i++) {
363 var el = this.pathnode.cloneNode(true);
364 this.pathbar.appendChild(el);
365 if (i == 0) {el.addClass('first');}
366 if (i == p.length-1) {el.addClass('last');}
367 if (i%2) {el.addClass('even');} else {el.addClass('odd');}
368 el.one('.fp-path-folder-name').setContent(p[i].name).
369 on('click', function(e, path) {
370 e.preventDefault();
371 var scope = this;
372 this.currentpath = path;
373 this.request({
374 action: 'list',
375 scope: scope,
376 params: {filepath:path},
377 callback: function(id, obj, args) {
378 scope.filecount = obj.filecount;
379 scope.check_buttons();
380 scope.options = obj;
381 scope.render(obj);
382 }
383 }, true);
384 }, this, p[i].path);
385 }
386 this.pathbar.removeClass('empty');
387 }
388 },
389 get_filepath: function(obj) {
390 if (obj.path && obj.path.length) {
391 return obj.path[obj.path.length-1].path;
392 }
393 return '';
394 },
395 treeview_dynload: function(node, cb) {
396 var retrieved_children = {};
397 if (node.children) {
398 for (var i in node.children) {
399 retrieved_children[node.children[i].path] = node.children[i];
400 }
401 }
402 this.request({
403 action:'list',
404 params: {filepath:node.path?node.path:''},
405 scope:this,
406 callback: function(id, obj, args) {
407 var list = obj.list;
408 var scope = args.scope;
409 // check that user did not leave the view mode before recieving this response
410 if (!(scope.viewmode == 2 && node && node.getChildrenEl())) {
411 return;
412 }
413 if (cb != null) { // (in manual mode do not update current path)
414 scope.options = obj;
694beb54 415 scope.currentpath = node.path?node.path:'/';
e709ddd2
MG
416 }
417 node.highlight(false);
418 node.origlist = obj.list?obj.list:null;
419 node.origpath = obj.path?obj.path:null;
694beb54 420 node.origcurrentpath = node.path?node.path:'/';
e709ddd2
MG
421 node.children = [];
422 for(k in list) {
423 if (list[k].type == 'folder' && retrieved_children[list[k].filepath]) {
424 // if this child is a folder and has already been retrieved
425 node.children[node.children.length] = retrieved_children[list[k].filepath];
426 } else {
427 // append new file to the list
428 scope.view_files([list[k]]);
429 }
430 }
431 if (cb == null) {
432 node.refresh();
840912d5 433 } else {
e709ddd2
MG
434 // invoke callback requested by TreeView component
435 cb();
494bf5c8 436 }
e709ddd2 437 //scope.content_scrolled();
56a7bf68 438 }
e709ddd2
MG
439 }, false);
440 },
441 view_files: function(appendfiles) {
906e7d89 442 this.filemanager.removeClass('fm-updating').removeClass('fm-noitems');
e709ddd2 443 if ((appendfiles == null) && (!this.options.list || this.options.list.length == 0)) {
906e7d89 444 this.filemanager.addClass('fm-noitems');
56a7bf68 445 return;
56a7bf68 446 }
e709ddd2
MG
447 var list = (appendfiles != null) ? appendfiles : this.options.list;
448 var element_template;
449 if (this.viewmode == 2 || this.viewmode == 3) {
450 element_template = Y.Node.create(M.form_filemanager.templates.listfilename);
451 } else {
452 this.viewmode = 1;
453 element_template = Y.Node.create(M.form_filemanager.templates.iconfilename);
454 }
455 var options = {
456 viewmode : this.viewmode,
457 appendonly : appendfiles != null,
458 filenode : element_template,
459 callbackcontext : this,
460 callback : function(e, node) {
23b83009 461 if (e.preventDefault) { e.preventDefault(); }
e709ddd2
MG
462 if (node.type == 'folder') {
463 this.refresh(node.filepath);
464 } else {
465 this.select_file(node);
466 }
467 },
468 rightclickcallback : function(e, node) {
23b83009 469 if (e.preventDefault) { e.preventDefault(); }
e709ddd2 470 this.select_file(node);
ac9c14dd 471 }
e709ddd2
MG
472 };
473 if (this.viewmode == 2) {
474 options.dynload = true;
475 options.filepath = this.options.path;
476 options.treeview_dynload = this.treeview_dynload;
23b83009 477 options.norootrightclick = true;
e709ddd2 478 options.callback = function(e, node) {
23b83009
MG
479 // TODO MDL-32736 e is not an event here but an object with properties 'event' and 'node'
480 if (!node.fullname) {return;}
e709ddd2
MG
481 if (node.type != 'folder') {
482 if (e.node.parent && e.node.parent.origpath) {
483 // set the current path
484 this.options.path = e.node.parent.origpath;
485 this.options.list = e.node.parent.origlist;
486 this.print_path();
487 }
694beb54 488 this.currentpath = node.filepath;
e709ddd2
MG
489 this.select_file(node);
490 } else {
491 // save current path and filelist (in case we want to jump to other viewmode)
492 this.options.path = e.node.origpath;
493 this.options.list = e.node.origlist;
694beb54 494 this.currentpath = e.node.origcurrentpath;
e709ddd2
MG
495 this.print_path();
496 //this.content_scrolled();
497 }
498 };
840912d5 499 }
e709ddd2
MG
500 if (!this.lazyloading) {this.lazyloading={};}
501 this.filemanager.one('.fp-content').fp_display_filelist(options, list, this.lazyloading);
840912d5 502 },
e709ddd2
MG
503 populate_licenses_select: function(node) {
504 if (!node) {return;}
505 node.setContent('');
506 var licenses = this.options.licenses;
507 for (var i in licenses) {
508 var option = Y.Node.create('<option/>').
509 set('value', licenses[i].shortname).
510 setContent(licenses[i].fullname);
511 node.appendChild(option)
f45dfeeb 512 }
e709ddd2
MG
513 },
514 set_current_tree: function(tree) {
515 var appendfilepaths = function(list, node) {
516 if (!node || !node.children || !node.children.length) {return;}
517 for (var i in node.children) {
518 list[list.length] = node.children[i].filepath;
519 appendfilepaths(list, node.children[i]);
520 }
f0a163ed 521 }
e709ddd2
MG
522 var list = ['/'];
523 appendfilepaths(list, tree);
524 var selectnode = this.filemanager.one('.fp-select');
525 node = selectnode.one('.fp-path select');
526 node.setContent('');
527 for (var i in list) {
528 node.appendChild(Y.Node.create('<option/>').
529 set('value', list[i]).setContent(list[i]))
f45dfeeb 530 }
840912d5 531 },
e709ddd2
MG
532 update_file: function() {
533 var selectnode = this.filemanager.one('.fp-select');
534 var fileinfo = this.selectui.fileinfo;
535
536 var newfilename = selectnode.one('.fp-saveas input').get('value');
537 var filenamechanged = (newfilename && newfilename != fileinfo.fullname);
538 var pathselect = selectnode.one('.fp-path select'),
539 pathindex = pathselect.get('selectedIndex'),
540 targetpath = pathselect.get("options").item(pathindex).get('value');
541 var filepathchanged = (targetpath != this.get_parent_folder_name(fileinfo));
542
543 if (filenamechanged && filepathchanged) {
544 alert('Sorry, simultaneous changing of name and path is not supported yet'); // TODO
545 return;
546 }
547 if (!filenamechanged && !filepathchanged) {
548 // no changes
549 this.selectui.hide();
550 }
551
552 selectnode.addClass('loading');
553
554 // RENAME
555 if (filenamechanged) {
556 var action = '';
840912d5 557 var params = {};
e709ddd2
MG
558 if (fileinfo.type == 'folder') {
559 params['filepath'] = fileinfo.filepath;
560 params['filename'] = '.';
561 params['newdirname'] = newfilename;
562 action = 'renamedir';
563 } else {
564 params['filepath'] = fileinfo.filepath;
565 params['filename'] = fileinfo.fullname;
566 params['newfilename'] = newfilename;
567 action = 'rename';
568 }
840912d5 569 this.request({
e709ddd2
MG
570 action: action,
571 scope: this,
840912d5
DC
572 params: params,
573 callback: function(id, obj, args) {
e709ddd2 574 if (obj == false) {
694beb54
MG
575 selectnode.removeClass('loading');
576 args.scope.print_msg(M.str.repository.fileexists, 'error');
e709ddd2 577 } else {
694beb54 578 args.scope.selectui.hide();
e709ddd2
MG
579 args.scope.refresh(obj.filepath);
580 if (typeof M.core_formchangechecker != 'undefined') {
581 M.core_formchangechecker.set_form_changed();
582 }
583 }
56a7bf68 584 }
840912d5 585 });
56a7bf68 586 }
840912d5 587
e709ddd2
MG
588 // MOVE
589 if (filepathchanged) {
840912d5 590 var params = {};
e709ddd2
MG
591 if (fileinfo.type == 'folder') {
592 action = 'movedir';
593 } else {
594 action = 'movefile';
595 }
840912d5
DC
596 params['filepath'] = fileinfo.filepath;
597 params['filename'] = fileinfo.fullname;
e709ddd2 598 params['newfilepath'] = targetpath;
840912d5 599 this.request({
e709ddd2
MG
600 action: action,
601 scope: this,
840912d5
DC
602 params: params,
603 callback: function(id, obj, args) {
e709ddd2
MG
604 args.scope.selectui.hide();
605 args.scope.refresh((obj && obj.filepath) ? obj.filepath : '/');
606 if (typeof M.core_formchangechecker != 'undefined') {
607 M.core_formchangechecker.set_form_changed();
608 }
1a79133b
DC
609 }
610 });
611 }
840912d5 612 },
e709ddd2
MG
613 setup_select_file: function() {
614 var selectnode = this.filemanager.one('.fp-select');
615 // bind labels with corresponding inputs
616 selectnode.all('.fp-saveas,.fp-path,.fp-author,.fp-license').each(function (node) {
617 node.all('label').set('for', node.one('input,select').generateID());
618 });
619 this.populate_licenses_select(selectnode.one('.fp-license select'));
620 // register event on clicking buttons
621 selectnode.one('.fp-file-update').on('click', function(e) {
622 e.preventDefault();
623 this.update_file();
624 }, this);
625 selectnode.one('.fp-file-download').on('click', function(e) {
626 e.preventDefault();
627 window.open(this.selectui.fileinfo.url, 'fm-download-file');
628 }, this);
629 selectnode.one('.fp-file-delete').on('click', function(e) {
630 e.preventDefault();
840912d5
DC
631 var dialog_options = {};
632 var params = {};
2b728cb5 633 dialog_options.message = M.str.repository.confirmdeletefile;
840912d5 634 dialog_options.scope = this;
e709ddd2 635 if (this.selectui.fileinfo.type == 'folder') {
840912d5 636 params.filename = '.';
e709ddd2 637 params.filepath = this.selectui.fileinfo.filepath;
840912d5 638 } else {
e709ddd2 639 params.filename = this.selectui.fileinfo.fullname;
840912d5 640 }
20fb563e
PS
641 dialog_options.callbackargs = [params];
642 dialog_options.callback = function(params) {
e709ddd2 643 //selectnode.addClass('loading');
840912d5
DC
644 this.request({
645 action: 'delete',
20fb563e 646 scope: this,
840912d5
DC
647 params: params,
648 callback: function(id, obj, args) {
e709ddd2
MG
649 //args.scope.selectui.hide();
650 args.scope.filecount--;
651 args.scope.refresh(obj.filepath);
a5dd1f4f
SH
652 if (typeof M.core_formchangechecker != 'undefined') {
653 M.core_formchangechecker.set_form_changed();
654 }
56a7bf68 655 }
840912d5 656 });
d3067516 657 };
e709ddd2
MG
658 this.selectui.hide(); // TODO remove this after confirm dialog is replaced with YUI3
659 M.util.show_confirm_dialog(e, dialog_options);
660 }, this);
661 selectnode.one('.fp-file-zip').on('click', function(e) {
662 e.preventDefault();
663 var params = {};
664 var fileinfo = this.selectui.fileinfo;
665 params['filepath'] = fileinfo.filepath;
666 params['filename'] = '.';
667 selectnode.addClass('loading');
668 this.request({
669 action: 'zip',
670 scope: this,
671 params: params,
672 callback: function(id, obj, args) {
673 args.scope.selectui.hide();
674 args.scope.refresh(obj.filepath);
840912d5 675 }
e709ddd2
MG
676 });
677 }, this);
678 selectnode.one('.fp-file-unzip').on('click', function(e) {
679 e.preventDefault();
680 var params = {};
681 var fileinfo = this.selectui.fileinfo;
682 params['filepath'] = fileinfo.filepath;
683 params['filename'] = fileinfo.fullname;
684 selectnode.addClass('loading');
685 this.request({
686 action: 'unzip',
687 scope: this,
688 params: params,
689 callback: function(id, obj, args) {
690 args.scope.selectui.hide();
691 args.scope.refresh(obj.filepath);
840912d5 692 }
e709ddd2
MG
693 });
694 }, this);
695 selectnode.one('.fp-file-setmain').on('click', function(e) {
696 e.preventDefault();
697 var params = {};
698 var fileinfo = this.selectui.fileinfo;
699 params['filepath'] = fileinfo.filepath;
700 params['filename'] = fileinfo.fullname;
701 selectnode.addClass('loading');
702 this.request({
703 action: 'setmainfile',
704 scope: this,
705 params: params,
706 callback: function(id, obj, args) {
707 args.scope.selectui.hide();
708 args.scope.refresh(obj.filepath);
840912d5 709 }
840912d5 710 });
e709ddd2
MG
711 }, this);
712 selectnode.all('.fp-file-cancel').on('click', function(e) {
713 e.preventDefault();
714 // TODO if changed asked to confirm, the same with close button
715 this.selectui.hide();
716 }, this);
717 },
718 get_parent_folder_name: function(node) {
719 if (node.type != 'folder' || node.filepath.length < node.fullname.length+1) { return node.filepath; }
720 var basedir = node.filepath.substr(0, node.filepath.length - node.fullname.length - 1);
721 var lastdir = node.filepath.substr(node.filepath.length - node.fullname.length - 2);
722 if (lastdir == '/' + node.fullname + '/') { return basedir; }
723 return node.filepath;
724 },
725 select_file: function(node) {
726 var selectnode = this.filemanager.one('.fp-select');
727 selectnode.removeClass('loading').removeClass('fp-folder').
728 removeClass('fp-file').removeClass('fp-zip').removeClass('fp-cansetmain');
729 if (node.type == 'folder' || node.type == 'zip') {selectnode.addClass('fp-'+node.type);}
730 else {selectnode.addClass('fp-file');}
731 if (this.enablemainfile && (node.sortorder != 1)) {
732 selectnode.addClass('fp-cansetmain');
840912d5 733 }
e709ddd2
MG
734 this.selectui.fileinfo = node;
735 selectnode.one('.fp-saveas input').set('value', node.fullname);
736 var foldername = this.get_parent_folder_name(node);
737 selectnode.all('.fp-origpath .fp-value').setContent(foldername);
738 selectnode.all('.fp-author input').set('value', node.author);
739 selectnode.all('.fp-license select option[selected]').set('selected', false);
740 selectnode.all('.fp-license select option[value='+node.license+']').set('selected', true);
741 selectnode.all('.fp-path select option[selected]').set('selected', false);
742 selectnode.all('.fp-path select option').each(function(el){
743 if (el.get('value') == foldername) {el.set('selected', true);}
840912d5 744 });
e709ddd2
MG
745 selectnode.all('.fp-author input').set('disabled','disabled'); //TODO
746 selectnode.all('.fp-license select').set('disabled','disabled'); //TODO
747 // display static information about a file (when known)
748 var attrs = ['datemodified','datecreated','size','dimensions'];
749 for (var i in attrs) {
750 if (selectnode.one('.fp-'+attrs[i])) {
751 var value = (node[attrs[i]+'_f']) ? node[attrs[i]+'_f'] : (node[attrs[i]] ? node[attrs[i]] : '');
752 selectnode.one('.fp-'+attrs[i]).addClassIf('fp-unknown', ''+value == '')
753 .one('.fp-value').setContent(value);
754 }
755 }
756 // display thumbnail
757 var imgnode = Y.Node.create('<img/>').
758 set('src', node.realthumbnail ? node.realthumbnail : node.thumbnail).
759 setStyle('maxHeight', ''+(node.thumbnail_height ? node.thumbnail_height : 90)+'px').
760 setStyle('maxWidth', ''+(node.thumbnail_width ? node.thumbnail_width : 90)+'px');
761 selectnode.one('.fp-thumbnail').setContent('').appendChild(imgnode);
762 // show panel
763 this.selectui.show();
764 },
765 render: function() {
766 this.print_path();
767 this.view_files();
840912d5
DC
768 }
769 });
4c508047
PS
770
771 // finally init everything needed
906e7d89
MG
772 // hide loading picture, display filemanager interface
773 var filemanager = Y.one('#filemanager-'+options.client_id);
774 filemanager.removeClass('fm-loading').addClass('fm-loaded');
4c508047 775
f08fac7c
DS
776 var manager = new FileManagerHelper(options);
777 var dndoptions = {
778 filemanager: manager,
779 acceptedtypes: options.accepted_types,
780 clientid: options.client_id,
781 maxfiles: options.maxfiles,
782 maxbytes: options.maxbytes,
783 itemid: options.itemid,
784 repositories: manager.filepicker_options.repositories,
906e7d89 785 containerid: manager.dndcontainer.get('id')
f08fac7c
DS
786 };
787 M.form_dndupload.init(Y, dndoptions);
4c508047 788};