1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * this.api, stores the URL to make ajax request
21 * this.filepicker_options
22 * this.movefile_dialog
26 * this.filecount, how many files in this filemanager
30 * FileManager options:
32 * this.options.currentpath
37 M.form_filemanager = {};
40 * This fucntion is called for each file picker on page.
42 M.form_filemanager.init = function(Y, options) {
43 var FileManagerHelper = function(options) {
44 FileManagerHelper.superclass.constructor.apply(this, arguments);
46 FileManagerHelper.NAME = "FileManager";
47 FileManagerHelper.ATTRS = {
52 Y.extend(FileManagerHelper, Y.Base, {
53 api: M.cfg.wwwroot+'/repository/draftfiles_ajax.php',
55 initializer: function(options) {
56 this.options = options;
57 if (options.mainfile) {
58 this.enablemainfile = options.mainfile;
60 this.client_id = options.client_id;
61 this.currentpath = '/';
62 this.maxfiles = options.maxfiles;
63 this.maxbytes = options.maxbytes;
65 this.filepicker_options = options.filepicker?options.filepicker:{};
66 this.filepicker_options.client_id = this.client_id;
67 this.filepicker_options.context = options.context;
68 this.filepicker_options.maxfiles = this.maxfiles;
69 this.filepicker_options.maxbytes = this.maxbytes;
70 this.filepicker_options.env = 'filemanager';
71 this.filepicker_options.itemid = options.itemid;
73 if (options.filecount) {
74 this.filecount = options.filecount;
79 this.refresh(this.currentpath); // MDL-31113 get latest list from server
82 wait: function(client_id) {
83 var container = Y.one('#filemanager-'+client_id);
84 container.set('innerHTML', '');
85 var html = Y.Node.create('<ul id="draftfiles-'+client_id+'"></ul>');
86 container.appendChild(html);
87 var panel = Y.one('#draftfiles-'+client_id);
89 var str = '<div style="text-align:center">';
90 str += '<img src="'+M.util.image_url('i/loading_small')+'" />';
93 panel.set('innerHTML', str);
98 request: function(args, redraw) {
99 var api = this.api + '?action='+args.action;
103 scope = args['scope'];
105 params['sesskey'] = M.cfg.sesskey;
106 params['client_id'] = this.client_id;
107 params['filepath'] = this.currentpath;
108 params['itemid'] = this.options.itemid?this.options.itemid:0;
109 if (args['params']) {
110 for (i in args['params']) {
111 params[i] = args['params'][i];
117 complete: function(id,o,p) {
122 var data = Y.JSON.parse(o.responseText);
123 args.callback(id,data,p);
130 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
131 'User-Agent': 'MoodleFileManager/3.0'
133 data: build_querystring(params)
136 cfg.form = args.form;
140 this.wait(this.client_id);
143 filepicker_callback: function(obj) {
145 this.check_buttons();
146 this.refresh(this.currentpath);
147 if (typeof M.core_formchangechecker != 'undefined') {
148 M.core_formchangechecker.set_form_changed();
151 check_buttons: function() {
152 var button_addfile = Y.one("#btnadd-"+this.client_id);
153 if (this.filecount > 0) {
154 Y.one("#btndwn-"+this.client_id).setStyle('display', 'inline');
156 if (this.filecount >= this.maxfiles && this.maxfiles!=-1) {
157 button_addfile.setStyle('display', 'none');
160 refresh: function(filepath) {
162 this.currentpath = filepath;
164 filepath = this.currentpath;
166 this.currentpath = filepath;
171 params: {'filepath':filepath},
172 callback: function(id, obj, args) {
173 scope.filecount = obj.filecount;
174 scope.check_buttons();
180 setup_buttons: function() {
181 var button_download = Y.one("#btndwn-"+this.client_id);
182 var button_create = Y.one("#btncrt-"+this.client_id);
183 var button_addfile = Y.one("#btnadd-"+this.client_id);
185 // setup 'add file' button
186 // if maxfiles == -1, the no limit
187 if (this.filecount >= this.maxfiles
188 && this.maxfiles!=-1) {
189 button_addfile.setStyle('display', 'none');
191 button_addfile.on('click', function(e) {
192 var options = this.filepicker_options;
193 options.formcallback = this.filepicker_callback;
194 // XXX: magic here, to let filepicker use filemanager scope
195 options.magicscope = this;
196 options.savepath = this.currentpath;
197 M.core_filepicker.show(Y, options);
201 // setup 'make a folder' button
202 if (this.options.subdirs) {
203 button_create.on('click',function(e) {
205 // a function used to perform an ajax request
206 function perform_action(e) {
207 var foldername = Y.one('#fm-newname').get('value');
213 params: {filepath:scope.currentpath, newdirname:foldername},
214 callback: function(id, obj, args) {
215 var filepath = obj.filepath;
216 scope.mkdir_dialog.hide();
217 scope.refresh(filepath);
218 Y.one('#fm-newname').set('value', '');
219 if (typeof M.core_formchangechecker != 'undefined') {
220 M.core_formchangechecker.set_form_changed();
225 if (!Y.one('#fm-mkdir-dlg')) {
226 var dialog = Y.Node.create('<div id="fm-mkdir-dlg"><div class="hd">'+M.str.repository.entername+'</div><div class="bd"><input type="text" id="fm-newname" /></div></div>');
227 Y.one(document.body).appendChild(dialog);
228 this.mkdir_dialog = new YAHOO.widget.Dialog("fm-mkdir-dlg", {
233 constraintoviewport : true
237 var buttons = [ { text:M.str.moodle.ok, handler:perform_action, isDefault:true },
238 { text:M.str.moodle.cancel, handler:function(){this.cancel();}}];
240 this.mkdir_dialog.cfg.queueProperty("buttons", buttons);
241 this.mkdir_dialog.render();
242 this.mkdir_dialog.show();
245 button_create.setStyle('display', 'none');
248 // setup 'download this folder' button
249 // NOTE: popup window must be enabled to perform download process
250 button_download.on('click',function() {
252 // perform downloaddir ajax request
254 action: 'downloaddir',
256 callback: function(id, obj, args) {
258 scope.refresh(obj.filepath);
259 var win = window.open(obj.fileurl, 'fm-download-folder');
261 alert(M.str.repository.popupblockeddownload);
264 alert(M.str.repository.draftareanofiles);
270 empty_filelist: function(container) {
271 container.set('innerHTML', '<div class="mdl-align">'+M.str.repository.nofilesattached+'</div>'+this.upload_message());
273 upload_message: function() {
274 var div = '<div id="filemanager-uploadmessage'+this.client_id+'" style="display:none" class="dndupload-target">';
275 div += M.util.get_string('droptoupload', 'moodle');
280 var options = this.options;
281 var path = this.options.path;
282 var list = this.options.list;
283 var breadcrumb = Y.one('#fm-path-'+this.client_id);
287 breadcrumb.set('innerHTML', '');
292 arrow = Y.Node.create('<span>'+M.str.moodle.path + ': </span>');
294 arrow = Y.Node.create('<span> â–¶ </span>');
298 var pathid = 'fm-path-node-'+this.client_id;
299 pathid += ('-'+count);
301 var crumb = Y.Node.create('<a href="###" id="'+pathid+'">'+path[p].name+'</a>');
302 breadcrumb.appendChild(arrow);
303 breadcrumb.appendChild(crumb);
306 args.requestpath = path[p].path;
307 args.client_id = this.client_id;
308 Y.one('#'+pathid).on('click', function(e, args) {
311 params['filepath'] = args.requestpath;
312 this.currentpath = args.requestpath;
317 callback: function(id, obj, args) {
318 scope.filecount = obj.filecount;
319 scope.check_buttons();
327 var template = Y.one('#fm-template');
328 var container = Y.one('#filemanager-' + this.client_id);
333 var folder_data = {};
335 // normal file list items
339 // archives list items
346 file_data.itemid = folder_data.itemid = zip_data.itemid = options.itemid;
347 file_data.client_id = folder_data.client_id = zip_data.client_id = this.client_id;
349 var foldername_ids = [];
350 if (!list || list.length == 0) {
351 // hide file browser and breadcrumb
352 //container.setStyle('display', 'none');
353 this.empty_filelist(container);
354 if (!path || path.length <= 1) {
355 breadcrumb.setStyle('display', 'none');
359 container.setStyle('display', 'block');
360 breadcrumb.setStyle('display', 'block');
366 // the li html element
367 var htmlid = 'fileitem-'+this.client_id+'-'+count;
369 var fileid = 'filename-'+this.client_id+'-'+count;
371 var action = 'action-' +this.client_id+'-'+count;
373 var html = template.get('innerHTML');
375 html_ids.push('#'+htmlid);
376 html_data[htmlid] = action;
378 list[i].htmlid = htmlid;
379 list[i].fileid = fileid;
380 list[i].action = action;
384 switch (list[i].type) {
387 foldername_ids.push('#'+fileid);
389 folder_ids.push('#'+action);
390 folder_data[action] = list[i];
391 folder_data[fileid] = list[i];
394 file_ids.push('#'+action);
396 file_ids.push('#'+fileid);
397 file_data[action] = list[i];
398 file_data[fileid] = list[i];
404 zip_ids.push('#'+action);
405 zip_ids.push('#'+fileid);
406 zip_data[action] = list[i];
407 zip_data[fileid] = list[i];
413 var fullname = list[i].fullname;
415 if (list[i].sortorder == 1) {
416 html = html.replace('___fullname___', '<strong><a title="'+fullname+'" href="'+url+'" id="'+fileid+'"><img src="'+list[i].icon+'" /> ' + fullname + '</a></strong>');
418 html = html.replace('___fullname___', '<a title="'+fullname+'" href="'+url+'" id="'+fileid+'"><img src="'+list[i].icon+'" /> ' + fullname + '</a>');
420 html = html.replace('___action___', '<span class="fm-menuicon" id="'+action+'"><img alt="â–¶" src="'+M.util.image_url('i/menu')+'" /></span>');
421 html = '<li id="'+htmlid+'">'+html+'</li>';
424 if (!Y.one('#draftfiles-'+this.client_id)) {
425 var filelist = Y.Node.create('<ul id="draftfiles-'+this.client_id+'"></ul>');
426 container.appendChild(filelist);
428 listhtml += this.upload_message();
429 Y.one('#draftfiles-'+this.client_id).set('innerHTML', listhtml);
431 // click normal file menu
432 Y.on('click', this.create_filemenu, file_ids, this, file_data);
433 Y.on('contextmenu', this.create_filemenu, file_ids, this, file_data);
435 Y.on('click', this.create_foldermenu, folder_ids, this, folder_data);
436 Y.on('contextmenu', this.create_foldermenu, folder_ids, this, folder_data);
437 Y.on('contextmenu', this.create_foldermenu, foldername_ids, this, folder_data);
438 // click archievs menu
439 Y.on('click', this.create_zipmenu, zip_ids, this, zip_data);
440 Y.on('contextmenu', this.create_zipmenu, zip_ids, this, zip_data);
442 Y.on('click', this.enter_folder, foldername_ids, this, folder_data);
444 enter_folder: function(e, data) {
445 var node = e.currentTarget;
446 var file = data[node.get('id')];
447 this.refresh(file.filepath);
449 create_filemenu: function(e, data) {
451 var options = this.options;
452 var node = e.currentTarget;
453 var file = data[node.get('id')];
457 {text: M.str.moodle.download, url:file.url}
459 function setmainfile(type, ev, obj) {
460 var file = obj[node.get('id')];
461 //Y.one(mainid).set('value', file.filepath+file.filename);
463 params['filepath'] = file.filepath;
464 params['filename'] = file.filename;
466 action: 'setmainfile',
469 callback: function(id, obj, args) {
470 scope.refresh(scope.currentpath);
474 if (this.enablemainfile && (file.sortorder != 1)) {
475 var mainid = '#id_'+this.enablemainfile;
476 var menu = {text: M.str.repository.setmainfile, onclick:{fn: setmainfile, obj:data, scope:this}};
477 menuitems.push(menu);
479 this.create_menu(e, 'filemenu', menuitems, file, data);
481 create_foldermenu: function(e, data) {
484 var node = e.currentTarget;
485 var fileinfo = data[node.get('id')];
486 // an extra menu item for folder to zip it
487 function archive_folder(type,ev,obj) {
489 params['filepath'] = fileinfo.filepath;
490 params['filename'] = '.';
495 callback: function(id, obj, args) {
496 scope.refresh(obj.filepath);
501 {text: M.str.editor.zip, onclick: {fn: archive_folder, obj: data, scope: this}},
503 this.create_menu(e, 'foldermenu', menuitems, fileinfo, data);
505 create_zipmenu: function(e, data) {
508 var node = e.currentTarget;
509 var fileinfo = data[node.get('id')];
511 function unzip(type, ev, obj) {
513 params['filepath'] = fileinfo.filepath;
514 params['filename'] = fileinfo.fullname;
519 callback: function(id, obj, args) {
520 scope.refresh(obj.filepath);
525 {text: M.str.moodle.download, url:fileinfo.url},
526 {text: M.str.moodle.unzip, onclick: {fn: unzip, obj: data, scope: this}}
528 function setmainfile(type, ev, obj) {
529 var file = obj[node.get('id')];
530 //Y.one(mainid).set('value', file.filepath+file.filename);
532 params['filepath'] = file.filepath;
533 params['filename'] = file.filename;
535 action: 'setmainfile',
538 callback: function(id, obj, args) {
539 scope.refresh(scope.currentpath);
543 if (this.enablemainfile && (fileinfo.sortorder != 1)) {
544 var mainid = '#id_'+this.enablemainfile;
545 var menu = {text: M.str.repository.setmainfile, onclick:{fn: setmainfile, obj:data, scope:this}};
546 menuitems.push(menu);
548 this.create_menu(e, 'zipmenu', menuitems, fileinfo, data);
550 create_menu: function(ev, menuid, menuitems, fileinfo, options) {
551 var position = [ev.pageX, ev.pageY];
553 function remove(type, ev, obj) {
554 var dialog_options = {};
556 dialog_options.message = M.str.repository.confirmdeletefile;
557 dialog_options.scope = this;
560 if (fileinfo.type == 'folder') {
561 params.filename = '.';
562 params.filepath = fileinfo.filepath;
564 params.filename = fileinfo.fullname;
566 dialog_options.callbackargs = [params];
567 dialog_options.callback = function(params) {
572 callback: function(id, obj, args) {
574 scope.refresh(obj.filepath);
575 if (typeof M.core_formchangechecker != 'undefined') {
576 M.core_formchangechecker.set_form_changed();
578 if (scope.filecount < scope.maxfiles && scope.maxfiles!=-1) {
579 var button_addfile = Y.one("#btnadd-"+scope.client_id);
580 button_addfile.setStyle('display', 'inline');
581 button_addfile.on('click', function(e) {
582 var options = scope.filepicker_options;
583 options.formcallback = scope.filepicker_callback;
584 // XXX: magic here, to let filepicker use filemanager scope
585 options.magicscope = scope;
586 options.savepath = scope.currentpath;
587 M.core_filepicker.show(Y, options);
593 M.util.show_confirm_dialog(ev, dialog_options);
595 function rename (type, ev, obj) {
597 var perform = function(e) {
598 var newfilename = Y.one('#fm-rename-input').get('value');
605 if (fileinfo.type == 'folder') {
606 params['filepath'] = fileinfo.filepath;
607 params['filename'] = '.';
608 params['newdirname'] = newfilename;
609 action = 'renamedir';
611 params['filepath'] = fileinfo.filepath;
612 params['filename'] = fileinfo.fullname;
613 params['newfilename'] = newfilename;
620 callback: function(id, obj, args) {
622 alert(M.str.repository.fileexists);
624 scope.refresh(obj.filepath);
625 if (typeof M.core_formchangechecker != 'undefined') {
626 M.core_formchangechecker.set_form_changed();
629 Y.one('#fm-rename-input').set('value', '');
630 scope.rename_dialog.hide();
635 var dialog = Y.one('#fm-rename-dlg');
637 dialog = Y.Node.create('<div id="fm-rename-dlg"><div class="hd">'+M.str.repository.enternewname+'</div><div class="bd"><input type="text" id="fm-rename-input" /></div></div>');
638 Y.one(document.body).appendChild(dialog);
639 this.rename_dialog = new YAHOO.widget.Dialog("fm-rename-dlg", {
643 constraintoviewport : true
647 var buttons = [ { text:M.str.moodle.rename, handler:perform, isDefault:true},
648 { text:M.str.moodle.cancel, handler:function(){this.cancel();}}];
650 this.rename_dialog.cfg.queueProperty('buttons', buttons);
651 this.rename_dialog.render();
652 this.rename_dialog.show();
653 //var k1 = new YAHOO.util.KeyListener(scope, {keys:13}, {fn:function(){perform();}, correctScope: true});
655 Y.one('#fm-rename-input').set('value', fileinfo.fullname);
657 function move(type, ev, obj) {
659 var itemid = this.options.itemid;
660 // setup move file dialog
662 if (!Y.one('#fm-move-dlg')) {
663 dialog = Y.Node.create('<div id="fm-move-dlg"></div>');
664 Y.one(document.body).appendChild(dialog);
666 dialog = Y.one('#fm-move-dlg');
669 dialog.set('innerHTML', '<div class="hd">'+M.str.repository.moving+'</div><div class="bd"><div id="fm-move-div">'+M.str.repository.nopathselected+'</div><div id="fm-tree"></div></div>');
671 this.movefile_dialog = new YAHOO.widget.Dialog("fm-move-dlg", {
675 constraintoviewport : true
678 var treeview = new YAHOO.widget.TreeView("fm-tree");
680 var dialog = this.movefile_dialog;
682 if (!treeview.targetpath) {
686 if (fileinfo.type == 'folder') {
691 params['filepath'] = fileinfo.filepath;
692 params['filename'] = fileinfo.fullname;
693 params['newfilepath'] = treeview.targetpath;
698 callback: function(id, obj, args) {
705 if (typeof M.core_formchangechecker != 'undefined') {
706 M.core_formchangechecker.set_form_changed();
712 var buttons = [ { text:M.str.moodle.move, handler:_move, isDefault:true },
713 { text:M.str.moodle.cancel, handler:function(){this.cancel();}}];
715 this.movefile_dialog.cfg.queueProperty("buttons", buttons);
716 this.movefile_dialog.render();
718 treeview.subscribe("dblClickEvent", function(e) {
719 // update destidatoin folder
720 this.targetpath = e.node.data.path;
721 var title = Y.one('#fm-move-div');
722 title.set('innerHTML', '<strong>"' + this.targetpath + '"</strong> has been selected.');
725 function loadDataForNode(node, onCompleteCallback) {
727 params['filepath'] = node.data.path;
732 callback: function(id, obj, args) {
734 if (data.length == 0) {
737 for (var i in data) {
738 var textnode = {label: data[i].fullname, path: data[i].filepath, itemid: this.itemid};
739 var tmpNode = new YAHOO.widget.TextNode(textnode, node, false);
745 obj.oncomplete = onCompleteCallback;
749 this.movefile_dialog.subscribe('show', function(){
750 var rootNode = treeview.getRoot();
751 treeview.setDynamicLoad(loadDataForNode);
752 treeview.removeChildren(rootNode);
753 var textnode = {label: M.str.moodle.files, path: '/'};
754 var tmpNode = new YAHOO.widget.TextNode(textnode, rootNode, true);
758 this.movefile_dialog.show();
761 {text: M.str.moodle.rename+'...', onclick: {fn: rename, obj: options, scope: this}},
762 {text: M.str.moodle.move+'...', onclick: {fn: move, obj: options, scope: this}}
764 // delete is reserve word in Javascript
765 shared_items.push({text: M.str.moodle['delete']+'...', onclick: {fn: remove, obj: options, scope: this}});
766 var menu = new YAHOO.widget.Menu(menuid, {xy:position, clicktohide:true});
768 menu.addItems(menuitems);
769 menu.addItems(shared_items);
770 menu.render(document.body);
771 menu.subscribe('hide', function(){
772 this.fireEvent('destroy');
778 // finally init everything needed
779 // kill nonjs filemanager
780 var item = document.getElementById('nonjs-filemanager-'+options.client_id);
781 if (item && !options.usenonjs) {
782 item.parentNode.removeChild(item);
784 // hide loading picture
785 item = document.getElementById('filemanager-loading-'+options.client_id);
787 item.parentNode.removeChild(item);
789 // display filemanager interface
790 item = document.getElementById('filemanager-wrapper-'+options.client_id);
792 item.style.display = '';
795 var manager = new FileManagerHelper(options);
797 filemanager: manager,
798 acceptedtypes: options.accepted_types,
799 clientid: options.client_id,
800 maxfiles: options.maxfiles,
801 maxbytes: options.maxbytes,
802 itemid: options.itemid,
803 repositories: manager.filepicker_options.repositories,
804 containerprefix: '#filemanager-',
806 M.form_dndupload.init(Y, dndoptions);