53a07c31b7e5e67c15ca063a1c146e7c82c42e3a
[moodle.git] / lib / form / filemanager.js
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/>.
15 /**
16  *
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
26  * this.filecount, how many files in this filemanager
27  * this.maxfiles
28  * this.maxbytes
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
32  *
33  * FileManager options:
34  * =====
35  * this.options.currentpath
36  * this.options.itemid
37  */
40 M.form_filemanager = {templates:{}};
42 M.form_filemanager.set_templates = function(Y, templates) {
43     M.form_filemanager.templates = templates;
44 }
46 /**
47  * This fucntion is called for each file picker on page.
48  */
49 M.form_filemanager.init = function(Y, options) {
50     var FileManagerHelper = function(options) {
51         FileManagerHelper.superclass.constructor.apply(this, arguments);
52     };
53     FileManagerHelper.NAME = "FileManager";
54     FileManagerHelper.ATTRS = {
55         options: {},
56         lang: {}
57     };
59     Y.extend(FileManagerHelper, Y.Base, {
60         api: M.cfg.wwwroot+'/repository/draftfiles_ajax.php',
61         menus: {},
62         initializer: function(options) {
63             this.options = options;
64             if (options.mainfile) {
65                 this.enablemainfile = options.mainfile;
66             }
67             this.client_id = options.client_id;
68             this.currentpath = '/';
69             this.maxfiles = options.maxfiles;
70             this.maxbytes = options.maxbytes;
71             this.emptycallback = null; // Used by drag and drop upload
73             this.filepicker_options = options.filepicker?options.filepicker:{};
74             this.filepicker_options.client_id = this.client_id;
75             this.filepicker_options.context = options.context;
76             this.filepicker_options.maxfiles = this.maxfiles;
77             this.filepicker_options.maxbytes = this.maxbytes;
78             this.filepicker_options.env = 'filemanager';
79             this.filepicker_options.itemid = options.itemid;
81             if (options.filecount) {
82                 this.filecount = options.filecount;
83             } else {
84                 this.filecount = 0;
85             }
86             // prepare filemanager for drag-and-drop upload
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             }
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
103             this.selectnode = Y.Node.create(M.form_filemanager.templates.fileselectlayout);
104             this.selectnode.generateID();
105             Y.one(document.body).appendChild(this.selectnode);
106             this.selectui = new Y.Panel({
107                 srcNode      : this.selectnode,
108                 zIndex       : 600000,
109                 centered     : true,
110                 modal        : true,
111                 close        : true,
112                 render       : true
113             });
114             this.selectui.plug(Y.Plugin.Drag,{handles:['#'+this.selectnode.get('id')+' .yui3-widget-hd']});
115             this.selectui.hide();
116             this.setup_select_file();
117             // setup buttons onclick events
118             this.setup_buttons();
119             // set event handler for lazy loading of thumbnails
120             this.filemanager.one('.fp-content').on(['scroll','resize'], this.content_scrolled, this);
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')
125             this.refresh(this.currentpath); // MDL-31113 get latest list from server
126         },
128         wait: function() {
129            this.filemanager.addClass('fm-updating');
130         },
131         request: function(args, redraw) {
132             var api = this.api + '?action='+args.action;
133             var params = {};
134             var scope = this;
135             if (args['scope']) {
136                 scope = args['scope'];
137             }
138             params['sesskey'] = M.cfg.sesskey;
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];
145                 }
146             }
147             var cfg = {
148                 method: 'POST',
149                 on: {
150                     complete: function(id,o,p) {
151                         if (!o) {
152                             alert('IO FATAL');
153                             return;
154                         }
155                         var data = null;
156                         try {
157                             data = Y.JSON.parse(o.responseText);
158                         } catch(e) {
159                             // TODO display error
160                             scope.print_msg(M.str.repository.invalidjson, 'error');
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                         }
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;
181             }
182             Y.io(api, cfg);
183             if (redraw) {
184                 this.wait();
185             }
186         },
187         filepicker_callback: function(obj) {
188             this.filecount++;
189             this.check_buttons();
190             this.refresh(this.currentpath);
191             if (typeof M.core_formchangechecker != 'undefined') {
192                 M.core_formchangechecker.set_form_changed();
193             }
194         },
195         check_buttons: function() {
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');}
201         },
202         refresh: function(filepath) {
203             var scope = this;
204             this.currentpath = filepath;
205             if (!filepath) {
206                 filepath = this.currentpath;
207             } else {
208                 this.currentpath = filepath;
209             }
210             this.request({
211                 action: 'list',
212                 scope: scope,
213                 params: {'filepath':filepath},
214                 callback: function(id, obj, args) {
215                     scope.filecount = obj.filecount;
216                     scope.check_buttons();
217                     scope.options = obj;
218                     scope.lazyloading = {};
219                     scope.render(obj);
220                 }
221             }, true);
222         },
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);
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                 });
242                 this.msg_dlg.plug(Y.Plugin.Drag,{handles:['.yui3-widget-hd']});
243                 node.one('.fp-msg-butok').on('click', function(e) {
244                     e.preventDefault();
245                     this.msg_dlg.hide();
246                 }, this);
247             }
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         },
254         setup_buttons: function() {
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');
259             // setup 'add file' button
260             // if maxfiles == -1, the no limit
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);
271             // setup 'make a folder' button
272             if (this.options.subdirs) {
273                 button_create.on('click',function(e) {
274                     e.preventDefault();
275                     var scope = this;
276                     // a function used to perform an ajax request
277                     var perform_action = function(e) {
278                         e.preventDefault();
279                         var foldername = Y.one('#fm-newname-'+scope.client_id).get('value');
280                         if (!foldername) {
281                             scope.mkdir_dialog.hide();
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);
291                                 Y.one('#fm-newname-'+scope.client_id).set('value', '');
292                                 if (typeof M.core_formchangechecker != 'undefined') {
293                                     M.core_formchangechecker.set_form_changed();
294                                 }
295                             }
296                         });
297                     }
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                         });
309                         this.mkdir_dialog.plug(Y.Plugin.Drag,{handles:['.yui3-widget-hd']});
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);
317                     }
318                     this.mkdir_dialog.show();
319                     Y.one('#fm-newname-'+scope.client_id).focus();
320                     Y.all('#fm-curpath-'+scope.client_id).setContent(this.currentpath)
321                 }, this);
322             } else {
323                 this.filemanager.addClass('fm-nomkdir');
324             }
326             // setup 'download this folder' button
327             // NOTE: popup window must be enabled to perform download process
328             button_download.on('click',function(e) {
329                 e.preventDefault();
330                 var scope = this;
331                 // perform downloaddir ajax request
332                 this.request({
333                     action: 'downloaddir',
334                     scope: scope,
335                     callback: function(id, obj, args) {
336                         if (obj) {
337                             scope.refresh(obj.filepath);
338                             var win = window.open(obj.fileurl, 'fm-download-folder');
339                             if (!win) {
340                                 scope.print_msg(M.str.repository.popupblockeddownload, 'error');
341                             }
342                         } else {
343                             scope.print_msg(M.str.repository.draftareanofiles, 'error');
344                         }
345                     }
346                 });
347             }, this);
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);
367         },
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();
381                             this.refresh(path);
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             }
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             }
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;
420                         scope.currentpath = node.path?node.path:'/';
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
429                             retrieved_children[list[k].filepath].fileinfo = list[k];
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();
438                     } else {
439                         // invoke callback requested by TreeView component
440                         cb();
441                     }
442                     scope.content_scrolled();
443                 }
444             }, false);
445         },
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         },
473         view_files: function(appendfiles) {
474             this.filemanager.removeClass('fm-updating').removeClass('fm-noitems');
475             if ((appendfiles == null) && (!this.options.list || this.options.list.length == 0) && this.viewmode != 2) {
476                 this.filemanager.addClass('fm-noitems');
477                 return;
478             }
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) {
493                     if (e.preventDefault) { e.preventDefault(); }
494                     if (node.type == 'folder') {
495                         this.refresh(node.filepath);
496                     } else {
497                         this.select_file(node);
498                     }
499                 },
500                 rightclickcallback : function(e, node) {
501                     if (e.preventDefault) { e.preventDefault(); }
502                     this.select_file(node);
503                 },
504                 classnamecallback : function(node) {
505                     var classname = '';
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                     }
512                     if (node.isref) {
513                         classname = classname + ' fp-isreference';
514                     }
515                     if (node.refcount) {
516                         classname = classname + ' fp-hasreferences';
517                     }
518                     if (node.sortorder == 1) { classname = classname + ' fp-mainfile';}
519                     return Y.Lang.trim(classname);
520                 }
521             };
522             if (this.viewmode == 2) {
523                 options.dynload = true;
524                 options.filepath = this.options.path;
525                 options.treeview_dynload = this.treeview_dynload;
526                 options.norootrightclick = true;
527                 options.callback = function(e, node) {
528                     // TODO MDL-32736 e is not an event here but an object with properties 'event' and 'node'
529                     if (!node.fullname) {return;}
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                         }
537                         this.currentpath = node.filepath;
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;
543                         this.currentpath = node.filepath;
544                         this.print_path();
545                         //this.content_scrolled();
546                     }
547                 };
548             }
549             if (!this.lazyloading) {this.lazyloading={};}
550             this.filemanager.one('.fp-content').fp_display_filelist(options, list, this.lazyloading);
551             this.content_scrolled();
552         },
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)
562             }
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                 }
571             }
572             var list = ['/'];
573             appendfilepaths(list, tree);
574             var selectnode = this.selectnode;
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]))
580             }
581         },
582         update_file: function(confirmed) {
583             var selectnode = this.selectnode;
584             var fileinfo = this.selectui.fileinfo;
586             var newfilename = Y.Lang.trim(selectnode.one('.fp-saveas input').get('value'));
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));
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);
599             var params, action;
600             var dialog_options = {callback:this.update_file, callbackargs:[true], scope:this};
601             if (fileinfo.type == 'folder') {
602                 if (!newfilename) {
603                     this.print_msg(M.str.repository.entername, 'error');
604                     return;
605                 }
606                 if (filenamechanged || filepathchanged) {
607                     if (!confirmed) {
608                         dialog_options.message = M.str.repository.confirmrenamefolder;
609                         this.show_confirm_dialog(dialog_options);
610                         return;
611                     }
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                 }
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                 }
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                 }
631             }
632             if (!action) {
633                 // no changes
634                 this.selectui.hide();
635                 return;
636             }
637             selectnode.addClass('loading');
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 {
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                         }
652                     }
653                 }
654             });
655         },
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         },
700         setup_select_file: function() {
701             var selectnode = this.selectnode;
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();
714                 if (this.selectui.fileinfo.type != 'folder') {
715                     window.open(this.selectui.fileinfo.url, 'fm-download-file');
716                 }
717             }, this);
718             selectnode.one('.fp-file-delete').on('click', function(e) {
719                 e.preventDefault();
720                 var dialog_options = {};
721                 var params = {};
722                 var fileinfo = this.selectui.fileinfo;
723                 dialog_options.scope = this;
724                 params.filepath = fileinfo.filepath;
725                 if (fileinfo.type == 'folder') {
726                     params.filename = '.';
727                     dialog_options.message = M.str.repository.confirmdeletefolder;
728                 } else {
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                     }
735                 }
736                 dialog_options.callbackargs = [params];
737                 dialog_options.callback = function(params) {
738                     //selectnode.addClass('loading');
739                     this.request({
740                         action: 'delete',
741                         scope: this,
742                         params: params,
743                         callback: function(id, obj, args) {
744                             //args.scope.selectui.hide();
745                             args.scope.filecount--;
746                             args.scope.refresh(obj.filepath);
747                             if (typeof M.core_formchangechecker != 'undefined') {
748                                 M.core_formchangechecker.set_form_changed();
749                             }
750                         }
751                     });
752                 };
753                 this.selectui.hide(); // TODO remove this after confirm dialog is replaced with YUI3
754                 this.show_confirm_dialog(dialog_options);
755             }, this);
756             selectnode.one('.fp-file-zip').on('click', function(e) {
757                 e.preventDefault();
758                 var params = {};
759                 var fileinfo = this.selectui.fileinfo;
760                 if (fileinfo.type != 'folder') {
761                     // this button should not even be shown
762                     return;
763                 }
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);
774                     }
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;
781                 if (fileinfo.type != 'zip') {
782                     // this button should not even be shown
783                     return;
784                 }
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);
795                     }
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;
802                 if (fileinfo.type == 'folder') {
803                     // this button should not even be shown for folders
804                     return;
805                 }
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();
815                         args.scope.refresh(fileinfo.filepath);
816                     }
817                 });
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) {
833             var selectnode = this.selectnode;
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');}
838             if (this.enablemainfile && (node.sortorder != 1) && node.type == 'file') {
839                 selectnode.addClass('fp-cansetmain');
840             }
841             this.selectui.fileinfo = node;
842             selectnode.one('.fp-saveas input').set('value', node.fullname);
843             var foldername = this.get_parent_folder_name(node);
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);}
850             });
851             selectnode.all('.fp-author input, .fp-license select').set('disabled',(node.type == 'folder')?'disabled':'');
852             // display static information about a file (when known)
853             var attrs = ['datemodified','datecreated','size','dimensions','original','reflist'];
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);
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) {
875                         // check if we did not select another file meanwhile
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             }
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             }
919             // show panel
920             this.selectui.show();
921         },
922         render: function() {
923             this.print_path();
924             this.view_files();
925         }
926     });
928     // finally init everything needed
929     // hide loading picture, display filemanager interface
930     var filemanager = Y.one('#filemanager-'+options.client_id);
931     filemanager.removeClass('fm-loading').addClass('fm-loaded');
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,
942         containerid: manager.dndcontainer.get('id')
943     };
944     M.form_dndupload.init(Y, dndoptions);
945 };