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 M.util.set_form_changed();
149 check_buttons: function() {
150 var button_addfile = Y.one("#btnadd-"+this.client_id);
151 if (this.filecount > 0) {
152 Y.one("#btndwn-"+this.client_id).setStyle('display', 'inline');
154 if (this.filecount >= this.maxfiles && this.maxfiles!=-1) {
155 button_addfile.setStyle('display', 'none');
158 refresh: function(filepath) {
160 this.currentpath = filepath;
162 filepath = this.currentpath;
164 this.currentpath = filepath;
169 params: {'filepath':filepath},
170 callback: function(id, obj, args) {
171 scope.filecount = obj.filecount;
172 scope.check_buttons();
178 setup_buttons: function() {
179 var button_download = Y.one("#btndwn-"+this.client_id);
180 var button_create = Y.one("#btncrt-"+this.client_id);
181 var button_addfile = Y.one("#btnadd-"+this.client_id);
183 // setup 'add file' button
184 // if maxfiles == -1, the no limit
185 if (this.filecount >= this.maxfiles
186 && this.maxfiles!=-1) {
187 button_addfile.setStyle('display', 'none');
189 button_addfile.on('click', function(e) {
190 var options = this.filepicker_options;
191 options.formcallback = this.filepicker_callback;
192 // XXX: magic here, to let filepicker use filemanager scope
193 options.magicscope = this;
194 options.savepath = this.currentpath;
195 M.core_filepicker.show(Y, options);
199 // setup 'make a folder' button
200 if (this.options.subdirs) {
201 button_create.on('click',function(e) {
203 // a function used to perform an ajax request
204 function perform_action(e) {
205 var foldername = Y.one('#fm-newname').get('value');
211 params: {filepath:scope.currentpath, newdirname:foldername},
212 callback: function(id, obj, args) {
213 var filepath = obj.filepath;
214 scope.mkdir_dialog.hide();
215 scope.refresh(filepath);
216 Y.one('#fm-newname').set('value', '');
217 M.util.set_form_changed();
221 if (!Y.one('#fm-mkdir-dlg')) {
222 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>');
223 Y.one(document.body).appendChild(dialog);
224 this.mkdir_dialog = new YAHOO.widget.Dialog("fm-mkdir-dlg", {
229 constraintoviewport : true
233 var buttons = [ { text:M.str.moodle.ok, handler:perform_action, isDefault:true },
234 { text:M.str.moodle.cancel, handler:function(){this.cancel();}}];
236 this.mkdir_dialog.cfg.queueProperty("buttons", buttons);
237 this.mkdir_dialog.render();
238 this.mkdir_dialog.show();
241 button_create.setStyle('display', 'none');
244 // setup 'download this folder' button
245 // NOTE: popup window must be enabled to perform download process
246 button_download.on('click',function() {
248 // perform downloaddir ajax request
250 action: 'downloaddir',
252 callback: function(id, obj, args) {
254 scope.refresh(obj.filepath);
255 var win = window.open(obj.fileurl, 'fm-download-folder');
257 alert(M.str.repository.popupblockeddownload);
260 alert(M.str.repository.draftareanofiles);
266 empty_filelist: function(container) {
267 container.set('innerHTML', '<div class="mdl-align">'+M.str.repository.nofilesattached+'</div>'+this.upload_message());
269 upload_message: function() {
270 var div = '<div id="filemanager-uploadmessage'+this.client_id+'" style="display:none" class="dndupload-target">';
271 div += M.util.get_string('droptoupload', 'moodle');
276 var options = this.options;
277 var path = this.options.path;
278 var list = this.options.list;
279 var breadcrumb = Y.one('#fm-path-'+this.client_id);
283 breadcrumb.set('innerHTML', '');
288 arrow = Y.Node.create('<span>'+M.str.moodle.path + ': </span>');
290 arrow = Y.Node.create('<span> â–¶ </span>');
294 var pathid = 'fm-path-node-'+this.client_id;
295 pathid += ('-'+count);
297 var crumb = Y.Node.create('<a href="###" id="'+pathid+'">'+path[p].name+'</a>');
298 breadcrumb.appendChild(arrow);
299 breadcrumb.appendChild(crumb);
302 args.requestpath = path[p].path;
303 args.client_id = this.client_id;
304 Y.one('#'+pathid).on('click', function(e, args) {
307 params['filepath'] = args.requestpath;
308 this.currentpath = args.requestpath;
313 callback: function(id, obj, args) {
314 scope.filecount = obj.filecount;
315 scope.check_buttons();
323 var template = Y.one('#fm-template');
324 var container = Y.one('#filemanager-' + this.client_id);
329 var folder_data = {};
331 // normal file list items
335 // archives list items
342 file_data.itemid = folder_data.itemid = zip_data.itemid = options.itemid;
343 file_data.client_id = folder_data.client_id = zip_data.client_id = this.client_id;
345 var foldername_ids = [];
346 if (!list || list.length == 0) {
347 // hide file browser and breadcrumb
348 //container.setStyle('display', 'none');
349 this.empty_filelist(container);
350 if (!path || path.length <= 1) {
351 breadcrumb.setStyle('display', 'none');
355 container.setStyle('display', 'block');
356 breadcrumb.setStyle('display', 'block');
362 // the li html element
363 var htmlid = 'fileitem-'+this.client_id+'-'+count;
365 var fileid = 'filename-'+this.client_id+'-'+count;
367 var action = 'action-' +this.client_id+'-'+count;
369 var html = template.get('innerHTML');
371 html_ids.push('#'+htmlid);
372 html_data[htmlid] = action;
374 list[i].htmlid = htmlid;
375 list[i].fileid = fileid;
376 list[i].action = action;
380 switch (list[i].type) {
383 foldername_ids.push('#'+fileid);
385 folder_ids.push('#'+action);
386 folder_data[action] = list[i];
387 folder_data[fileid] = list[i];
390 file_ids.push('#'+action);
392 file_ids.push('#'+fileid);
393 file_data[action] = list[i];
394 file_data[fileid] = list[i];
400 zip_ids.push('#'+action);
401 zip_ids.push('#'+fileid);
402 zip_data[action] = list[i];
403 zip_data[fileid] = list[i];
409 var fullname = list[i].fullname;
411 if (list[i].sortorder == 1) {
412 html = html.replace('___fullname___', '<strong><a title="'+fullname+'" href="'+url+'" id="'+fileid+'"><img src="'+list[i].icon+'" /> ' + fullname + '</a></strong>');
414 html = html.replace('___fullname___', '<a title="'+fullname+'" href="'+url+'" id="'+fileid+'"><img src="'+list[i].icon+'" /> ' + fullname + '</a>');
416 html = html.replace('___action___', '<span class="fm-menuicon" id="'+action+'"><img alt="â–¶" src="'+M.util.image_url('i/menu')+'" /></span>');
417 html = '<li id="'+htmlid+'">'+html+'</li>';
420 if (!Y.one('#draftfiles-'+this.client_id)) {
421 var filelist = Y.Node.create('<ul id="draftfiles-'+this.client_id+'"></ul>');
422 container.appendChild(filelist);
424 listhtml += this.upload_message();
425 Y.one('#draftfiles-'+this.client_id).set('innerHTML', listhtml);
427 // click normal file menu
428 Y.on('click', this.create_filemenu, file_ids, this, file_data);
429 Y.on('contextmenu', this.create_filemenu, file_ids, this, file_data);
431 Y.on('click', this.create_foldermenu, folder_ids, this, folder_data);
432 Y.on('contextmenu', this.create_foldermenu, folder_ids, this, folder_data);
433 Y.on('contextmenu', this.create_foldermenu, foldername_ids, this, folder_data);
434 // click archievs menu
435 Y.on('click', this.create_zipmenu, zip_ids, this, zip_data);
436 Y.on('contextmenu', this.create_zipmenu, zip_ids, this, zip_data);
438 Y.on('click', this.enter_folder, foldername_ids, this, folder_data);
440 enter_folder: function(e, data) {
441 var node = e.currentTarget;
442 var file = data[node.get('id')];
443 this.refresh(file.filepath);
445 create_filemenu: function(e, data) {
447 var options = this.options;
448 var node = e.currentTarget;
449 var file = data[node.get('id')];
453 {text: M.str.moodle.download, onclick:{fn:open_file_in_new_window, obj:file, scope:this}}
455 function setmainfile(type, ev, obj) {
456 var file = obj[node.get('id')];
457 //Y.one(mainid).set('value', file.filepath+file.filename);
459 params['filepath'] = file.filepath;
460 params['filename'] = file.filename;
462 action: 'setmainfile',
465 callback: function(id, obj, args) {
466 scope.refresh(scope.currentpath);
470 function open_file_in_new_window(type, ev, obj) {
471 // We open in a new window rather than changing the current windows URL as we don't
472 // want to navigate away from the page
473 window.open(obj.url, 'fm-download-file');
475 if (this.enablemainfile && (file.sortorder != 1)) {
476 var mainid = '#id_'+this.enablemainfile;
477 var menu = {text: M.str.repository.setmainfile, onclick:{fn: setmainfile, obj:data, scope:this}};
478 menuitems.push(menu);
480 this.create_menu(e, 'filemenu', menuitems, file, data);
482 create_foldermenu: function(e, data) {
485 var node = e.currentTarget;
486 var fileinfo = data[node.get('id')];
487 // an extra menu item for folder to zip it
488 function archive_folder(type,ev,obj) {
490 params['filepath'] = fileinfo.filepath;
491 params['filename'] = '.';
496 callback: function(id, obj, args) {
497 scope.refresh(obj.filepath);
502 {text: M.str.editor.zip, onclick: {fn: archive_folder, obj: data, scope: this}},
504 this.create_menu(e, 'foldermenu', menuitems, fileinfo, data);
506 create_zipmenu: function(e, data) {
509 var node = e.currentTarget;
510 var fileinfo = data[node.get('id')];
512 function unzip(type, ev, obj) {
514 params['filepath'] = fileinfo.filepath;
515 params['filename'] = fileinfo.fullname;
520 callback: function(id, obj, args) {
521 scope.refresh(obj.filepath);
526 {text: M.str.moodle.download, url:fileinfo.url},
527 {text: M.str.moodle.unzip, onclick: {fn: unzip, obj: data, scope: this}}
529 function setmainfile(type, ev, obj) {
530 var file = obj[node.get('id')];
531 //Y.one(mainid).set('value', file.filepath+file.filename);
533 params['filepath'] = file.filepath;
534 params['filename'] = file.filename;
536 action: 'setmainfile',
539 callback: function(id, obj, args) {
540 scope.refresh(scope.currentpath);
544 if (this.enablemainfile && (fileinfo.sortorder != 1)) {
545 var mainid = '#id_'+this.enablemainfile;
546 var menu = {text: M.str.repository.setmainfile, onclick:{fn: setmainfile, obj:data, scope:this}};
547 menuitems.push(menu);
549 this.create_menu(e, 'zipmenu', menuitems, fileinfo, data);
551 create_menu: function(ev, menuid, menuitems, fileinfo, options) {
552 var position = [ev.pageX, ev.pageY];
554 function remove(type, ev, obj) {
555 var dialog_options = {};
557 dialog_options.message = M.str.repository.confirmdeletefile;
558 dialog_options.scope = this;
561 if (fileinfo.type == 'folder') {
562 params.filename = '.';
563 params.filepath = fileinfo.filepath;
565 params.filename = fileinfo.fullname;
567 dialog_options.callbackargs = [params];
568 dialog_options.callback = function(params) {
573 callback: function(id, obj, args) {
575 scope.refresh(obj.filepath);
576 M.util.set_form_changed();
577 if (scope.filecount < scope.maxfiles && scope.maxfiles!=-1) {
578 var button_addfile = Y.one("#btnadd-"+scope.client_id);
579 button_addfile.setStyle('display', 'inline');
580 button_addfile.on('click', function(e) {
581 var options = scope.filepicker_options;
582 options.formcallback = scope.filepicker_callback;
583 // XXX: magic here, to let filepicker use filemanager scope
584 options.magicscope = scope;
585 options.savepath = scope.currentpath;
586 M.core_filepicker.show(Y, options);
592 M.util.show_confirm_dialog(ev, dialog_options);
594 function rename (type, ev, obj) {
596 var perform = function(e) {
597 var newfilename = Y.one('#fm-rename-input').get('value');
604 if (fileinfo.type == 'folder') {
605 params['filepath'] = fileinfo.filepath;
606 params['filename'] = '.';
607 params['newdirname'] = newfilename;
608 action = 'renamedir';
610 params['filepath'] = fileinfo.filepath;
611 params['filename'] = fileinfo.fullname;
612 params['newfilename'] = newfilename;
619 callback: function(id, obj, args) {
621 alert(M.str.repository.fileexists);
623 scope.refresh(obj.filepath);
624 M.util.set_form_changed();
626 Y.one('#fm-rename-input').set('value', '');
627 scope.rename_dialog.hide();
632 var dialog = Y.one('#fm-rename-dlg');
634 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>');
635 Y.one(document.body).appendChild(dialog);
636 this.rename_dialog = new YAHOO.widget.Dialog("fm-rename-dlg", {
640 constraintoviewport : true
644 var buttons = [ { text:M.str.moodle.rename, handler:perform, isDefault:true},
645 { text:M.str.moodle.cancel, handler:function(){this.cancel();}}];
647 this.rename_dialog.cfg.queueProperty('buttons', buttons);
648 this.rename_dialog.render();
649 this.rename_dialog.show();
650 //var k1 = new YAHOO.util.KeyListener(scope, {keys:13}, {fn:function(){perform();}, correctScope: true});
652 Y.one('#fm-rename-input').set('value', fileinfo.fullname);
654 function move(type, ev, obj) {
656 var itemid = this.options.itemid;
657 // setup move file dialog
659 if (!Y.one('#fm-move-dlg')) {
660 dialog = Y.Node.create('<div id="fm-move-dlg"></div>');
661 Y.one(document.body).appendChild(dialog);
663 dialog = Y.one('#fm-move-dlg');
666 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>');
668 this.movefile_dialog = new YAHOO.widget.Dialog("fm-move-dlg", {
672 constraintoviewport : true
675 var treeview = new YAHOO.widget.TreeView("fm-tree");
677 var dialog = this.movefile_dialog;
679 if (!treeview.targetpath) {
683 if (fileinfo.type == 'folder') {
688 params['filepath'] = fileinfo.filepath;
689 params['filename'] = fileinfo.fullname;
690 params['newfilepath'] = treeview.targetpath;
695 callback: function(id, obj, args) {
702 M.util.set_form_changed();
707 var buttons = [ { text:M.str.moodle.move, handler:_move, isDefault:true },
708 { text:M.str.moodle.cancel, handler:function(){this.cancel();}}];
710 this.movefile_dialog.cfg.queueProperty("buttons", buttons);
711 this.movefile_dialog.render();
713 treeview.subscribe("dblClickEvent", function(e) {
714 // update destidatoin folder
715 this.targetpath = e.node.data.path;
716 var title = Y.one('#fm-move-div');
717 title.set('innerHTML', '<strong>"' + this.targetpath + '"</strong> has been selected.');
720 function loadDataForNode(node, onCompleteCallback) {
722 params['filepath'] = node.data.path;
727 callback: function(id, obj, args) {
729 if (data.length == 0) {
732 for (var i in data) {
733 var textnode = {label: data[i].fullname, path: data[i].filepath, itemid: this.itemid};
734 var tmpNode = new YAHOO.widget.TextNode(textnode, node, false);
740 obj.oncomplete = onCompleteCallback;
744 this.movefile_dialog.subscribe('show', function(){
745 var rootNode = treeview.getRoot();
746 treeview.setDynamicLoad(loadDataForNode);
747 treeview.removeChildren(rootNode);
748 var textnode = {label: M.str.moodle.files, path: '/'};
749 var tmpNode = new YAHOO.widget.TextNode(textnode, rootNode, true);
753 this.movefile_dialog.show();
756 {text: M.str.moodle.rename+'...', onclick: {fn: rename, obj: options, scope: this}},
757 {text: M.str.moodle.move+'...', onclick: {fn: move, obj: options, scope: this}}
759 // delete is reserve word in Javascript
760 shared_items.push({text: M.str.moodle['delete']+'...', onclick: {fn: remove, obj: options, scope: this}});
761 var menu = new YAHOO.widget.Menu(menuid, {xy:position, clicktohide:true});
763 menu.addItems(menuitems);
764 menu.addItems(shared_items);
765 menu.render(document.body);
766 menu.subscribe('hide', function(){
767 this.fireEvent('destroy');
773 // finally init everything needed
774 // kill nonjs filemanager
775 var item = document.getElementById('nonjs-filemanager-'+options.client_id);
776 if (item && !options.usenonjs) {
777 item.parentNode.removeChild(item);
779 // hide loading picture
780 item = document.getElementById('filemanager-loading-'+options.client_id);
782 item.parentNode.removeChild(item);
784 // display filemanager interface
785 item = document.getElementById('filemanager-wrapper-'+options.client_id);
787 item.style.display = '';
790 var manager = new FileManagerHelper(options);
792 filemanager: manager,
793 acceptedtypes: options.accepted_types,
794 clientid: options.client_id,
795 maxfiles: options.maxfiles,
796 maxbytes: options.maxbytes,
797 itemid: options.itemid,
798 repositories: manager.filepicker_options.repositories,
799 containerprefix: '#filemanager-',
801 M.form_dndupload.init(Y, dndoptions);