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