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