MDL-28666: filelib - remove unusused globals
[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();
469a53a0
MG
119 // set event handler for lazy loading of thumbnails
120 this.filemanager.one('.fp-content').on(['scroll','resize'], this.content_scrolled, this);
e709ddd2
MG
121 // display files
122 this.viewmode = 1; // TODO take from cookies?
123 this.filemanager.all('.fp-vb-icons,.fp-vb-tree,.fp-vb-details').removeClass('checked')
124 this.filemanager.all('.fp-vb-icons').addClass('checked')
910e1ecd 125 this.refresh(this.currentpath); // MDL-31113 get latest list from server
840912d5 126 },
4c508047 127
694beb54 128 wait: function() {
906e7d89 129 this.filemanager.addClass('fm-updating');
403eabd4
DC
130 },
131 request: function(args, redraw) {
840912d5
DC
132 var api = this.api + '?action='+args.action;
133 var params = {};
134 var scope = this;
135 if (args['scope']) {
136 scope = args['scope'];
137 }
4c508047 138 params['sesskey'] = M.cfg.sesskey;
840912d5
DC
139 params['client_id'] = this.client_id;
140 params['filepath'] = this.currentpath;
141 params['itemid'] = this.options.itemid?this.options.itemid:0;
142 if (args['params']) {
143 for (i in args['params']) {
144 params[i] = args['params'][i];
56a7bf68 145 }
56a7bf68 146 }
840912d5
DC
147 var cfg = {
148 method: 'POST',
149 on: {
150 complete: function(id,o,p) {
151 if (!o) {
152 alert('IO FATAL');
153 return;
154 }
e709ddd2
MG
155 var data = null;
156 try {
157 data = Y.JSON.parse(o.responseText);
158 } catch(e) {
159 // TODO display error
694beb54 160 scope.print_msg(M.str.repository.invalidjson, 'error');
e709ddd2
MG
161 //scope.display_error(M.str.repository.invalidjson+'<pre>'+stripHTML(o.responseText)+'</pre>', 'invalidjson')
162 return;
163 }
164 if (data && data.tree && scope.set_current_tree) {
165 scope.set_current_tree(data.tree);
166 }
840912d5
DC
167 args.callback(id,data,p);
168 }
169 },
170 arguments: {
171 scope: scope
172 },
173 headers: {
174 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
175 'User-Agent': 'MoodleFileManager/3.0'
176 },
177 data: build_querystring(params)
178 };
179 if (args.form) {
180 cfg.form = args.form;
56a7bf68 181 }
840912d5 182 Y.io(api, cfg);
403eabd4 183 if (redraw) {
694beb54 184 this.wait();
403eabd4 185 }
840912d5
DC
186 },
187 filepicker_callback: function(obj) {
554cd8fc 188 this.filecount++;
910e1ecd
DS
189 this.check_buttons();
190 this.refresh(this.currentpath);
a5dd1f4f
SH
191 if (typeof M.core_formchangechecker != 'undefined') {
192 M.core_formchangechecker.set_form_changed();
193 }
910e1ecd
DS
194 },
195 check_buttons: function() {
906e7d89
MG
196 if (this.filecount>0) {this.filemanager.removeClass('fm-nofiles');}
197 else {this.filemanager.addClass('fm-nofiles');}
198 if (this.filecount >= this.maxfiles && this.maxfiles!=-1)
199 {this.filemanager.addClass('fm-maxfiles');}
200 else {this.filemanager.removeClass('fm-maxfiles');}
840912d5
DC
201 },
202 refresh: function(filepath) {
203 var scope = this;
59eeb81b 204 this.currentpath = filepath;
840912d5
DC
205 if (!filepath) {
206 filepath = this.currentpath;
207 } else {
208 this.currentpath = filepath;
56a7bf68 209 }
840912d5
DC
210 this.request({
211 action: 'list',
212 scope: scope,
213 params: {'filepath':filepath},
214 callback: function(id, obj, args) {
910e1ecd
DS
215 scope.filecount = obj.filecount;
216 scope.check_buttons();
840912d5 217 scope.options = obj;
469a53a0 218 scope.lazyloading = {};
840912d5 219 scope.render(obj);
56a7bf68 220 }
403eabd4 221 }, true);
840912d5 222 },
694beb54
MG
223 /** displays message in a popup */
224 print_msg: function(msg, type) {
225 var header = M.str.moodle.error;
226 if (type != 'error') {
227 type = 'info'; // one of only two types excepted
228 header = M.str.moodle.info;
229 }
230 if (!this.msg_dlg) {
231 var node = Y.Node.create(M.form_filemanager.templates.message);
232 this.filemanager.appendChild(node);
233
234 this.msg_dlg = new Y.Panel({
235 srcNode : node,
236 zIndex : 800000,
237 centered : true,
238 modal : true,
239 visible : false,
240 render : true
241 });
4325db53 242 this.msg_dlg.plug(Y.Plugin.Drag,{handles:['.yui3-widget-hd']});
694beb54
MG
243 node.one('.fp-msg-butok').on('click', function(e) {
244 e.preventDefault();
245 this.msg_dlg.hide();
246 }, this);
247 }
248
249 this.msg_dlg.set('headerContent', header);
250 this.filemanager.one('.fp-msg').removeClass('fp-msg-info').removeClass('fp-msg-error').addClass('fp-msg-'+type)
251 this.filemanager.one('.fp-msg .fp-msg-text').setContent(msg);
252 this.msg_dlg.show();
253 },
840912d5 254 setup_buttons: function() {
e709ddd2
MG
255 var button_download = this.filemanager.one('.fp-btn-download');
256 var button_create = this.filemanager.one('.fp-btn-mkdir');
257 var button_addfile = this.filemanager.one('.fp-btn-add');
840912d5
DC
258
259 // setup 'add file' button
260 // if maxfiles == -1, the no limit
906e7d89
MG
261 button_addfile.on('click', function(e) {
262 e.preventDefault();
263 var options = this.filepicker_options;
264 options.formcallback = this.filepicker_callback;
265 // XXX: magic here, to let filepicker use filemanager scope
266 options.magicscope = this;
267 options.savepath = this.currentpath;
268 M.core_filepicker.show(Y, options);
269 }, this);
840912d5
DC
270
271 // setup 'make a folder' button
272 if (this.options.subdirs) {
273 button_create.on('click',function(e) {
906e7d89 274 e.preventDefault();
840912d5
DC
275 var scope = this;
276 // a function used to perform an ajax request
694beb54
MG
277 var perform_action = function(e) {
278 e.preventDefault();
279 var foldername = Y.one('#fm-newname-'+scope.client_id).get('value');
840912d5 280 if (!foldername) {
694beb54 281 scope.mkdir_dialog.hide();
840912d5
DC
282 return;
283 }
284 scope.request({
285 action:'mkdir',
286 params: {filepath:scope.currentpath, newdirname:foldername},
287 callback: function(id, obj, args) {
288 var filepath = obj.filepath;
289 scope.mkdir_dialog.hide();
290 scope.refresh(filepath);
694beb54 291 Y.one('#fm-newname-'+scope.client_id).set('value', '');
a5dd1f4f
SH
292 if (typeof M.core_formchangechecker != 'undefined') {
293 M.core_formchangechecker.set_form_changed();
294 }
840912d5
DC
295 }
296 });
297 }
694beb54
MG
298 if (!this.mkdir_dialog) {
299 var node = Y.Node.create(M.form_filemanager.templates.mkdir);
300 this.filemanager.appendChild(node);
301 this.mkdir_dialog = new Y.Panel({
302 srcNode : node,
303 zIndex : 800000,
304 centered : true,
305 modal : true,
306 visible : false,
307 render : true
308 });
4325db53 309 this.mkdir_dialog.plug(Y.Plugin.Drag,{handles:['.yui3-widget-hd']});
694beb54
MG
310 node.one('.fp-dlg-butcreate').on('click', perform_action, this);
311 node.one('input').set('id', 'fm-newname-'+this.client_id).
312 on('keydown', function(e){
313 if (e.keyCode == 13) {Y.bind(perform_action, this)(e);}
314 }, this);
315 node.all('.fp-dlg-butcancel').on('click', function(e){e.preventDefault();this.mkdir_dialog.hide();}, this);
316 node.all('.fp-dlg-curpath').set('id', 'fm-curpath-'+this.client_id);
840912d5 317 }
840912d5 318 this.mkdir_dialog.show();
694beb54
MG
319 Y.one('#fm-newname-'+scope.client_id).focus();
320 Y.all('#fm-curpath-'+scope.client_id).setContent(this.currentpath)
840912d5 321 }, this);
56a7bf68 322 } else {
906e7d89 323 this.filemanager.addClass('fm-nomkdir');
56a7bf68 324 }
840912d5
DC
325
326 // setup 'download this folder' button
327 // NOTE: popup window must be enabled to perform download process
906e7d89
MG
328 button_download.on('click',function(e) {
329 e.preventDefault();
840912d5
DC
330 var scope = this;
331 // perform downloaddir ajax request
332 this.request({
333 action: 'downloaddir',
334 scope: scope,
335 callback: function(id, obj, args) {
7210e887
DC
336 if (obj) {
337 scope.refresh(obj.filepath);
338 var win = window.open(obj.fileurl, 'fm-download-folder');
339 if (!win) {
694beb54 340 scope.print_msg(M.str.repository.popupblockeddownload, 'error');
7210e887
DC
341 }
342 } else {
694beb54 343 scope.print_msg(M.str.repository.draftareanofiles, 'error');
840912d5
DC
344 }
345 }
346 });
347 }, this);
e709ddd2
MG
348
349 this.filemanager.all('.fp-vb-icons,.fp-vb-tree,.fp-vb-details').
350 on('click', function(e) {
351 e.preventDefault();
352 var viewbar = this.filemanager.one('.fp-viewbar')
353 if (!viewbar || !viewbar.hasClass('disabled')) {
354 this.filemanager.all('.fp-vb-icons,.fp-vb-tree,.fp-vb-details').removeClass('checked')
355 if (e.currentTarget.hasClass('fp-vb-tree')) {
356 this.viewmode = 2;
357 } else if (e.currentTarget.hasClass('fp-vb-details')) {
358 this.viewmode = 3;
359 } else {
360 this.viewmode = 1;
361 }
362 e.currentTarget.addClass('checked')
363 this.render();
364 //Y.Cookie.set('recentviewmode', this.viewmode);
365 }
366 }, this);
840912d5 367 },
e709ddd2
MG
368 print_path: function() {
369 var p = this.options.path;
370 this.pathbar.setContent('').addClass('empty');
371 if (p && p.length!=0 && this.viewmode != 2) {
372 for(var i = 0; i < p.length; i++) {
373 var el = this.pathnode.cloneNode(true);
374 this.pathbar.appendChild(el);
375 if (i == 0) {el.addClass('first');}
376 if (i == p.length-1) {el.addClass('last');}
377 if (i%2) {el.addClass('even');} else {el.addClass('odd');}
378 el.one('.fp-path-folder-name').setContent(p[i].name).
379 on('click', function(e, path) {
380 e.preventDefault();
469a53a0 381 this.refresh(path);
e709ddd2
MG
382 }, this, p[i].path);
383 }
384 this.pathbar.removeClass('empty');
385 }
386 },
387 get_filepath: function(obj) {
388 if (obj.path && obj.path.length) {
389 return obj.path[obj.path.length-1].path;
390 }
391 return '';
392 },
393 treeview_dynload: function(node, cb) {
394 var retrieved_children = {};
395 if (node.children) {
396 for (var i in node.children) {
397 retrieved_children[node.children[i].path] = node.children[i];
398 }
399 }
e0ff0867
MG
400 if (!node.path || node.path == '/') {
401 // this is a root pseudo folder
402 node.fileinfo.filepath = '/';
403 node.fileinfo.type = 'folder';
404 node.fileinfo.fullname = node.fileinfo.title;
405 node.fileinfo.filename = '.';
406 }
e709ddd2
MG
407 this.request({
408 action:'list',
409 params: {filepath:node.path?node.path:''},
410 scope:this,
411 callback: function(id, obj, args) {
412 var list = obj.list;
413 var scope = args.scope;
414 // check that user did not leave the view mode before recieving this response
415 if (!(scope.viewmode == 2 && node && node.getChildrenEl())) {
416 return;
417 }
418 if (cb != null) { // (in manual mode do not update current path)
419 scope.options = obj;
694beb54 420 scope.currentpath = node.path?node.path:'/';
e709ddd2
MG
421 }
422 node.highlight(false);
423 node.origlist = obj.list?obj.list:null;
424 node.origpath = obj.path?obj.path:null;
425 node.children = [];
426 for(k in list) {
427 if (list[k].type == 'folder' && retrieved_children[list[k].filepath]) {
428 // if this child is a folder and has already been retrieved
c092681b 429 retrieved_children[list[k].filepath].fileinfo = list[k];
e709ddd2
MG
430 node.children[node.children.length] = retrieved_children[list[k].filepath];
431 } else {
432 // append new file to the list
433 scope.view_files([list[k]]);
434 }
435 }
436 if (cb == null) {
437 node.refresh();
840912d5 438 } else {
e709ddd2
MG
439 // invoke callback requested by TreeView component
440 cb();
494bf5c8 441 }
469a53a0 442 scope.content_scrolled();
56a7bf68 443 }
e709ddd2
MG
444 }, false);
445 },
469a53a0
MG
446 content_scrolled: function(e) {
447 setTimeout(Y.bind(function() {
448 if (this.processingimages) {return;}
449 this.processingimages = true;
450 var scope = this,
451 fpcontent = this.filemanager.one('.fp-content'),
452 fpcontenty = fpcontent.getY(),
453 fpcontentheight = fpcontent.getStylePx('height'),
454 is_node_visible = function(node) {
455 var offset = node.getY()-fpcontenty;
456 if (offset <= fpcontentheight && (offset >=0 || offset+node.getStylePx('height')>=0)) {
457 return true;
458 }
459 return false;
460 };
461 // replace src for visible images that need to be lazy-loaded
462 if (scope.lazyloading) {
463 fpcontent.all('img').each( function(node) {
464 if (node.get('id') && scope.lazyloading[node.get('id')] && is_node_visible(node)) {
465 node.set('src', scope.lazyloading[node.get('id')]);
466 delete scope.lazyloading[node.get('id')];
467 }
468 });
469 }
470 this.processingimages = false;
471 }, this), 200)
472 },
e709ddd2 473 view_files: function(appendfiles) {
906e7d89 474 this.filemanager.removeClass('fm-updating').removeClass('fm-noitems');
1908ae06 475 if ((appendfiles == null) && (!this.options.list || this.options.list.length == 0) && this.viewmode != 2) {
906e7d89 476 this.filemanager.addClass('fm-noitems');
56a7bf68 477 return;
56a7bf68 478 }
e709ddd2
MG
479 var list = (appendfiles != null) ? appendfiles : this.options.list;
480 var element_template;
481 if (this.viewmode == 2 || this.viewmode == 3) {
482 element_template = Y.Node.create(M.form_filemanager.templates.listfilename);
483 } else {
484 this.viewmode = 1;
485 element_template = Y.Node.create(M.form_filemanager.templates.iconfilename);
486 }
487 var options = {
488 viewmode : this.viewmode,
489 appendonly : appendfiles != null,
490 filenode : element_template,
491 callbackcontext : this,
492 callback : function(e, node) {
23b83009 493 if (e.preventDefault) { e.preventDefault(); }
e709ddd2
MG
494 if (node.type == 'folder') {
495 this.refresh(node.filepath);
496 } else {
497 this.select_file(node);
498 }
499 },
500 rightclickcallback : function(e, node) {
23b83009 501 if (e.preventDefault) { e.preventDefault(); }
e709ddd2 502 this.select_file(node);
6c2367cc
MG
503 },
504 classnamecallback : function(node) {
505 var classname = '';
e0ff0867
MG
506 if (node.type == 'folder' || (!node.type && !node.filename)) {
507 classname = classname + ' fp-folder';
508 }
509 if (node.filename || node.filepath || (node.path && node.path != '/')) {
510 classname = classname + ' fp-hascontextmenu';
511 }
9a62f779
MG
512 if (node.isref) {
513 classname = classname + ' fp-isreference';
514 }
515 if (node.refcount) {
516 classname = classname + ' fp-hasreferences';
517 }
6c2367cc
MG
518 if (node.sortorder == 1) { classname = classname + ' fp-mainfile';}
519 return Y.Lang.trim(classname);
ac9c14dd 520 }
e709ddd2
MG
521 };
522 if (this.viewmode == 2) {
523 options.dynload = true;
524 options.filepath = this.options.path;
525 options.treeview_dynload = this.treeview_dynload;
23b83009 526 options.norootrightclick = true;
e709ddd2 527 options.callback = function(e, node) {
23b83009
MG
528 // TODO MDL-32736 e is not an event here but an object with properties 'event' and 'node'
529 if (!node.fullname) {return;}
e709ddd2
MG
530 if (node.type != 'folder') {
531 if (e.node.parent && e.node.parent.origpath) {
532 // set the current path
533 this.options.path = e.node.parent.origpath;
534 this.options.list = e.node.parent.origlist;
535 this.print_path();
536 }
694beb54 537 this.currentpath = node.filepath;
e709ddd2
MG
538 this.select_file(node);
539 } else {
540 // save current path and filelist (in case we want to jump to other viewmode)
541 this.options.path = e.node.origpath;
542 this.options.list = e.node.origlist;
e0ff0867 543 this.currentpath = node.filepath;
e709ddd2
MG
544 this.print_path();
545 //this.content_scrolled();
546 }
547 };
840912d5 548 }
e709ddd2
MG
549 if (!this.lazyloading) {this.lazyloading={};}
550 this.filemanager.one('.fp-content').fp_display_filelist(options, list, this.lazyloading);
469a53a0 551 this.content_scrolled();
840912d5 552 },
e709ddd2
MG
553 populate_licenses_select: function(node) {
554 if (!node) {return;}
555 node.setContent('');
556 var licenses = this.options.licenses;
557 for (var i in licenses) {
558 var option = Y.Node.create('<option/>').
559 set('value', licenses[i].shortname).
560 setContent(licenses[i].fullname);
561 node.appendChild(option)
f45dfeeb 562 }
e709ddd2
MG
563 },
564 set_current_tree: function(tree) {
565 var appendfilepaths = function(list, node) {
566 if (!node || !node.children || !node.children.length) {return;}
567 for (var i in node.children) {
568 list[list.length] = node.children[i].filepath;
569 appendfilepaths(list, node.children[i]);
570 }
f0a163ed 571 }
e709ddd2
MG
572 var list = ['/'];
573 appendfilepaths(list, tree);
55089a9d 574 var selectnode = this.selectnode;
e709ddd2
MG
575 node = selectnode.one('.fp-path select');
576 node.setContent('');
577 for (var i in list) {
578 node.appendChild(Y.Node.create('<option/>').
579 set('value', list[i]).setContent(list[i]))
f45dfeeb 580 }
840912d5 581 },
9a62f779 582 update_file: function(confirmed) {
55089a9d 583 var selectnode = this.selectnode;
e709ddd2
MG
584 var fileinfo = this.selectui.fileinfo;
585
c092681b 586 var newfilename = Y.Lang.trim(selectnode.one('.fp-saveas input').get('value'));
e709ddd2
MG
587 var filenamechanged = (newfilename && newfilename != fileinfo.fullname);
588 var pathselect = selectnode.one('.fp-path select'),
589 pathindex = pathselect.get('selectedIndex'),
590 targetpath = pathselect.get("options").item(pathindex).get('value');
591 var filepathchanged = (targetpath != this.get_parent_folder_name(fileinfo));
c092681b
MG
592 var newauthor = Y.Lang.trim(selectnode.one('.fp-author input').get('value'));
593 var authorchanged = (newauthor != Y.Lang.trim(fileinfo.author));
594 var licenseselect = selectnode.one('.fp-license select'),
595 licenseindex = licenseselect.get('selectedIndex'),
596 newlicense = licenseselect.get("options").item(licenseindex).get('value');
597 var licensechanged = (newlicense != fileinfo.license);
e709ddd2 598
c092681b 599 var params, action;
9a62f779 600 var dialog_options = {callback:this.update_file, callbackargs:[true], scope:this};
c092681b
MG
601 if (fileinfo.type == 'folder') {
602 if (!newfilename) {
603 this.print_msg(M.str.repository.entername, 'error');
604 return;
605 }
606 if (filenamechanged || filepathchanged) {
9a62f779
MG
607 if (!confirmed) {
608 dialog_options.message = M.str.repository.confirmrenamefolder;
609 this.show_confirm_dialog(dialog_options);
610 return;
611 }
c092681b
MG
612 params = {filepath:fileinfo.filepath, newdirname:newfilename, newfilepath:targetpath};
613 action = 'updatedir';
614 }
615 } else {
616 if (!newfilename) {
617 this.print_msg(M.str.repository.enternewname, 'error');
618 return;
619 }
9a62f779
MG
620 if ((filenamechanged || filepathchanged) && !confirmed && fileinfo.refcount) {
621 dialog_options.message = M.util.get_string('confirmrenamefile', 'repository', fileinfo.refcount);
622 this.show_confirm_dialog(dialog_options);
623 return;
624 }
c092681b
MG
625 if (filenamechanged || filepathchanged || licensechanged || authorchanged) {
626 params = {filepath:fileinfo.filepath, filename:fileinfo.fullname,
627 newfilename:newfilename, newfilepath:targetpath,
628 newlicense:newlicense, newauthor:newauthor};
629 action = 'updatefile';
630 }
e709ddd2 631 }
c092681b 632 if (!action) {
e709ddd2
MG
633 // no changes
634 this.selectui.hide();
c092681b 635 return;
e709ddd2 636 }
e709ddd2 637 selectnode.addClass('loading');
c092681b
MG
638 this.request({
639 action: action,
640 scope: this,
641 params: params,
642 callback: function(id, obj, args) {
643 if (obj.error) {
644 selectnode.removeClass('loading');
645 args.scope.print_msg(obj.error, 'error');
646 } else {
e709ddd2
MG
647 args.scope.selectui.hide();
648 args.scope.refresh((obj && obj.filepath) ? obj.filepath : '/');
649 if (typeof M.core_formchangechecker != 'undefined') {
650 M.core_formchangechecker.set_form_changed();
651 }
1a79133b 652 }
c092681b
MG
653 }
654 });
840912d5 655 },
9a62f779
MG
656 /**
657 * Displays a confirmation dialog
658 * Expected attributes in dialog_options: message, callback, callbackargs(optional), scope(optional)
659 */
660 show_confirm_dialog: function(dialog_options) {
661 // instead of M.util.show_confirm_dialog(e, dialog_options);
662 if (!this.confirm_dlg) {
663 this.confirm_dlg_node = Y.Node.create(M.form_filemanager.templates.confirmdialog);
664 var node = this.confirm_dlg_node;
665 node.generateID();
666 Y.one(document.body).appendChild(node);
667 this.confirm_dlg = new Y.Panel({
668 srcNode : node,
669 zIndex : 800000,
670 centered : true,
671 modal : true,
672 visible : false,
673 render : true,
674 buttons : {}
675 });
676 this.confirm_dlg.plug(Y.Plugin.Drag,{handles:['#'+node.get('id')+' .yui3-widget-hd']});
677 var handleConfirm = function(ev) {
678 var dlgopt = this.confirm_dlg.dlgopt;
679 ev.preventDefault();
680 this.confirm_dlg.hide();
681 if (dlgopt.callback) {
682 if (dlgopt.callbackargs) {
683 dlgopt.callback.apply(dlgopt.scope || this, dlgopt.callbackargs);
684 } else {
685 dlgopt.callback.apply(dlgopt.scope || this);
686 }
687 }
688 }
689 var handleCancel = function(ev) {
690 ev.preventDefault();
691 this.confirm_dlg.hide();
692 }
693 node.one('.fp-dlg-butconfirm').on('click', handleConfirm, this);
694 node.one('.fp-dlg-butcancel').on('click', handleCancel, this);
695 }
696 this.confirm_dlg.dlgopt = dialog_options;
697 this.confirm_dlg_node.one('.fp-dlg-text').setContent(dialog_options.message);
698 this.confirm_dlg.show();
699 },
e709ddd2 700 setup_select_file: function() {
55089a9d 701 var selectnode = this.selectnode;
e709ddd2
MG
702 // bind labels with corresponding inputs
703 selectnode.all('.fp-saveas,.fp-path,.fp-author,.fp-license').each(function (node) {
704 node.all('label').set('for', node.one('input,select').generateID());
705 });
706 this.populate_licenses_select(selectnode.one('.fp-license select'));
707 // register event on clicking buttons
708 selectnode.one('.fp-file-update').on('click', function(e) {
709 e.preventDefault();
710 this.update_file();
711 }, this);
712 selectnode.one('.fp-file-download').on('click', function(e) {
713 e.preventDefault();
6c2367cc
MG
714 if (this.selectui.fileinfo.type != 'folder') {
715 window.open(this.selectui.fileinfo.url, 'fm-download-file');
716 }
e709ddd2
MG
717 }, this);
718 selectnode.one('.fp-file-delete').on('click', function(e) {
719 e.preventDefault();
840912d5
DC
720 var dialog_options = {};
721 var params = {};
9a62f779 722 var fileinfo = this.selectui.fileinfo;
840912d5 723 dialog_options.scope = this;
9a62f779
MG
724 params.filepath = fileinfo.filepath;
725 if (fileinfo.type == 'folder') {
840912d5 726 params.filename = '.';
9a62f779 727 dialog_options.message = M.str.repository.confirmdeletefolder;
840912d5 728 } else {
9a62f779
MG
729 params.filename = fileinfo.fullname;
730 if (fileinfo.refcount) {
731 dialog_options.message = M.util.get_string('confirmdeletefilewithhref', 'repository', fileinfo.refcount);
732 } else {
733 dialog_options.message = M.str.repository.confirmdeletefile;
734 }
840912d5 735 }
20fb563e
PS
736 dialog_options.callbackargs = [params];
737 dialog_options.callback = function(params) {
e709ddd2 738 //selectnode.addClass('loading');
840912d5
DC
739 this.request({
740 action: 'delete',
20fb563e 741 scope: this,
840912d5
DC
742 params: params,
743 callback: function(id, obj, args) {
e709ddd2
MG
744 //args.scope.selectui.hide();
745 args.scope.filecount--;
746 args.scope.refresh(obj.filepath);
a5dd1f4f
SH
747 if (typeof M.core_formchangechecker != 'undefined') {
748 M.core_formchangechecker.set_form_changed();
749 }
56a7bf68 750 }
840912d5 751 });
d3067516 752 };
e709ddd2 753 this.selectui.hide(); // TODO remove this after confirm dialog is replaced with YUI3
9a62f779 754 this.show_confirm_dialog(dialog_options);
e709ddd2
MG
755 }, this);
756 selectnode.one('.fp-file-zip').on('click', function(e) {
757 e.preventDefault();
758 var params = {};
759 var fileinfo = this.selectui.fileinfo;
6c2367cc
MG
760 if (fileinfo.type != 'folder') {
761 // this button should not even be shown
762 return;
763 }
e709ddd2
MG
764 params['filepath'] = fileinfo.filepath;
765 params['filename'] = '.';
766 selectnode.addClass('loading');
767 this.request({
768 action: 'zip',
769 scope: this,
770 params: params,
771 callback: function(id, obj, args) {
772 args.scope.selectui.hide();
773 args.scope.refresh(obj.filepath);
840912d5 774 }
e709ddd2
MG
775 });
776 }, this);
777 selectnode.one('.fp-file-unzip').on('click', function(e) {
778 e.preventDefault();
779 var params = {};
780 var fileinfo = this.selectui.fileinfo;
6c2367cc
MG
781 if (fileinfo.type != 'zip') {
782 // this button should not even be shown
783 return;
784 }
e709ddd2
MG
785 params['filepath'] = fileinfo.filepath;
786 params['filename'] = fileinfo.fullname;
787 selectnode.addClass('loading');
788 this.request({
789 action: 'unzip',
790 scope: this,
791 params: params,
792 callback: function(id, obj, args) {
793 args.scope.selectui.hide();
794 args.scope.refresh(obj.filepath);
840912d5 795 }
e709ddd2
MG
796 });
797 }, this);
798 selectnode.one('.fp-file-setmain').on('click', function(e) {
799 e.preventDefault();
800 var params = {};
801 var fileinfo = this.selectui.fileinfo;
6c2367cc
MG
802 if (fileinfo.type == 'folder') {
803 // this button should not even be shown for folders
804 return;
805 }
e709ddd2
MG
806 params['filepath'] = fileinfo.filepath;
807 params['filename'] = fileinfo.fullname;
808 selectnode.addClass('loading');
809 this.request({
810 action: 'setmainfile',
811 scope: this,
812 params: params,
813 callback: function(id, obj, args) {
814 args.scope.selectui.hide();
6c2367cc 815 args.scope.refresh(fileinfo.filepath);
840912d5 816 }
840912d5 817 });
e709ddd2
MG
818 }, this);
819 selectnode.all('.fp-file-cancel').on('click', function(e) {
820 e.preventDefault();
821 // TODO if changed asked to confirm, the same with close button
822 this.selectui.hide();
823 }, this);
824 },
825 get_parent_folder_name: function(node) {
826 if (node.type != 'folder' || node.filepath.length < node.fullname.length+1) { return node.filepath; }
827 var basedir = node.filepath.substr(0, node.filepath.length - node.fullname.length - 1);
828 var lastdir = node.filepath.substr(node.filepath.length - node.fullname.length - 2);
829 if (lastdir == '/' + node.fullname + '/') { return basedir; }
830 return node.filepath;
831 },
832 select_file: function(node) {
55089a9d 833 var selectnode = this.selectnode;
e709ddd2
MG
834 selectnode.removeClass('loading').removeClass('fp-folder').
835 removeClass('fp-file').removeClass('fp-zip').removeClass('fp-cansetmain');
836 if (node.type == 'folder' || node.type == 'zip') {selectnode.addClass('fp-'+node.type);}
837 else {selectnode.addClass('fp-file');}
6c2367cc 838 if (this.enablemainfile && (node.sortorder != 1) && node.type == 'file') {
e709ddd2 839 selectnode.addClass('fp-cansetmain');
840912d5 840 }
e709ddd2
MG
841 this.selectui.fileinfo = node;
842 selectnode.one('.fp-saveas input').set('value', node.fullname);
843 var foldername = this.get_parent_folder_name(node);
e709ddd2
MG
844 selectnode.all('.fp-author input').set('value', node.author);
845 selectnode.all('.fp-license select option[selected]').set('selected', false);
846 selectnode.all('.fp-license select option[value='+node.license+']').set('selected', true);
847 selectnode.all('.fp-path select option[selected]').set('selected', false);
848 selectnode.all('.fp-path select option').each(function(el){
849 if (el.get('value') == foldername) {el.set('selected', true);}
840912d5 850 });
c092681b 851 selectnode.all('.fp-author input, .fp-license select').set('disabled',(node.type == 'folder')?'disabled':'');
e709ddd2 852 // display static information about a file (when known)
9dbdf31f 853 var attrs = ['datemodified','datecreated','size','dimensions','original','reflist'];
e709ddd2
MG
854 for (var i in attrs) {
855 if (selectnode.one('.fp-'+attrs[i])) {
856 var value = (node[attrs[i]+'_f']) ? node[attrs[i]+'_f'] : (node[attrs[i]] ? node[attrs[i]] : '');
857 selectnode.one('.fp-'+attrs[i]).addClassIf('fp-unknown', ''+value == '')
858 .one('.fp-value').setContent(value);
859 }
860 }
861 // display thumbnail
862 var imgnode = Y.Node.create('<img/>').
863 set('src', node.realthumbnail ? node.realthumbnail : node.thumbnail).
864 setStyle('maxHeight', ''+(node.thumbnail_height ? node.thumbnail_height : 90)+'px').
865 setStyle('maxWidth', ''+(node.thumbnail_width ? node.thumbnail_width : 90)+'px');
866 selectnode.one('.fp-thumbnail').setContent('').appendChild(imgnode);
9a62f779
MG
867 // load original location if applicable
868 if (node.isref && !node.original) {
869 selectnode.one('.fp-original').removeClass('fp-unknown').addClass('fp-loading');
870 this.request({
871 action: 'getoriginal',
872 scope: this,
873 params: {'filepath':node.filepath,'filename':node.fullname},
874 callback: function(id, obj, args) {
9dbdf31f 875 // check if we did not select another file meanwhile
9a62f779
MG
876 var scope = args.scope;
877 if (scope.selectui.fileinfo && node &&
878 scope.selectui.fileinfo.filepath == node.filepath &&
879 scope.selectui.fileinfo.fullname == node.fullname) {
880 selectnode.one('.fp-original').removeClass('fp-loading');
881 if (obj.original) {
882 node.original = obj.original;
883 selectnode.one('.fp-original .fp-value').setContent(node.original);
884 } else {
885 selectnode.one('.fp-original .fp-value').setContent(M.str.repository.unknownsource);
886 }
887 }
888 }
889 }, false);
890 }
9dbdf31f
MG
891 // load references list if applicable
892 selectnode.one('.fp-refcount').setContent(node.refcount ? M.util.get_string('referencesexist', 'repository', node.refcount) : '');
893 if (node.refcount && !node.reflist) {
894 selectnode.one('.fp-reflist').removeClass('fp-unknown').addClass('fp-loading');
895 this.request({
896 action: 'getreferences',
897 scope: this,
898 params: {'filepath':node.filepath,'filename':node.fullname},
899 callback: function(id, obj, args) {
900 // check if we did not select another file meanwhile
901 var scope = args.scope;
902 if (scope.selectui.fileinfo && node &&
903 scope.selectui.fileinfo.filepath == node.filepath &&
904 scope.selectui.fileinfo.fullname == node.fullname) {
905 selectnode.one('.fp-reflist').removeClass('fp-loading');
906 if (obj.references) {
907 node.reflist = '';
908 for (var i in obj.references) {
909 node.reflist += '<li>'+obj.references[i]+'</li>';
910 }
911 selectnode.one('.fp-reflist .fp-value').setContent(node.reflist);
912 } else {
913 selectnode.one('.fp-reflist .fp-value').setContent('');
914 }
915 }
916 }
917 }, false);
918 }
e709ddd2
MG
919 // show panel
920 this.selectui.show();
921 },
922 render: function() {
923 this.print_path();
924 this.view_files();
840912d5
DC
925 }
926 });
4c508047
PS
927
928 // finally init everything needed
906e7d89
MG
929 // hide loading picture, display filemanager interface
930 var filemanager = Y.one('#filemanager-'+options.client_id);
931 filemanager.removeClass('fm-loading').addClass('fm-loaded');
4c508047 932
f08fac7c
DS
933 var manager = new FileManagerHelper(options);
934 var dndoptions = {
935 filemanager: manager,
936 acceptedtypes: options.accepted_types,
937 clientid: options.client_id,
938 maxfiles: options.maxfiles,
939 maxbytes: options.maxbytes,
940 itemid: options.itemid,
941 repositories: manager.filepicker_options.repositories,
906e7d89 942 containerid: manager.dndcontainer.get('id')
f08fac7c
DS
943 };
944 M.form_dndupload.init(Y, dndoptions);
4c508047 945};