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, onclick:{fn:open_file_in_new_window, obj:file, scope:this}}
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 function open_file_in_new_window(type, ev, obj) {
475 // We open in a new window rather than changing the current windows URL as we don't
476 // want to navigate away from the page
477 window.open(obj.url, 'fm-download-file');
479 if (this.enablemainfile && (file.sortorder != 1)) {
480 var mainid = '#id_'+this.enablemainfile;
481 var menu = {text: M.str.repository.setmainfile, onclick:{fn: setmainfile, obj:data, scope:this}};
482 menuitems.push(menu);
484 this.create_menu(e, 'filemenu', menuitems, file, data);
486 create_foldermenu: function(e, data) {
489 var node = e.currentTarget;
490 var fileinfo = data[node.get('id')];
491 // an extra menu item for folder to zip it
492 function archive_folder(type,ev,obj) {
494 params['filepath'] = fileinfo.filepath;
495 params['filename'] = '.';
500 callback: function(id, obj, args) {
501 scope.refresh(obj.filepath);
506 {text: M.str.editor.zip, onclick: {fn: archive_folder, obj: data, scope: this}},
508 this.create_menu(e, 'foldermenu', menuitems, fileinfo, data);
510 create_zipmenu: function(e, data) {
513 var node = e.currentTarget;
514 var fileinfo = data[node.get('id')];
516 function unzip(type, ev, obj) {
518 params['filepath'] = fileinfo.filepath;
519 params['filename'] = fileinfo.fullname;
524 callback: function(id, obj, args) {
525 scope.refresh(obj.filepath);
530 {text: M.str.moodle.download, url:fileinfo.url},
531 {text: M.str.moodle.unzip, onclick: {fn: unzip, obj: data, scope: this}}
533 function setmainfile(type, ev, obj) {
534 var file = obj[node.get('id')];
535 //Y.one(mainid).set('value', file.filepath+file.filename);
537 params['filepath'] = file.filepath;
538 params['filename'] = file.filename;
540 action: 'setmainfile',
543 callback: function(id, obj, args) {
544 scope.refresh(scope.currentpath);
548 if (this.enablemainfile && (fileinfo.sortorder != 1)) {
549 var mainid = '#id_'+this.enablemainfile;
550 var menu = {text: M.str.repository.setmainfile, onclick:{fn: setmainfile, obj:data, scope:this}};
551 menuitems.push(menu);
553 this.create_menu(e, 'zipmenu', menuitems, fileinfo, data);
555 create_menu: function(ev, menuid, menuitems, fileinfo, options) {
556 var position = [ev.pageX, ev.pageY];
558 function remove(type, ev, obj) {
559 var dialog_options = {};
561 dialog_options.message = M.str.repository.confirmdeletefile;
562 dialog_options.scope = this;
565 if (fileinfo.type == 'folder') {
566 params.filename = '.';
567 params.filepath = fileinfo.filepath;
569 params.filename = fileinfo.fullname;
571 dialog_options.callbackargs = [params];
572 dialog_options.callback = function(params) {
577 callback: function(id, obj, args) {
579 scope.refresh(obj.filepath);
580 if (typeof M.core_formchangechecker != 'undefined') {
581 M.core_formchangechecker.set_form_changed();
583 if (scope.filecount < scope.maxfiles && scope.maxfiles!=-1) {
584 var button_addfile = Y.one("#btnadd-"+scope.client_id);
585 button_addfile.setStyle('display', 'inline');
586 button_addfile.on('click', function(e) {
587 var options = scope.filepicker_options;
588 options.formcallback = scope.filepicker_callback;
589 // XXX: magic here, to let filepicker use filemanager scope
590 options.magicscope = scope;
591 options.savepath = scope.currentpath;
592 M.core_filepicker.show(Y, options);
598 M.util.show_confirm_dialog(ev, dialog_options);
600 function rename (type, ev, obj) {
602 var perform = function(e) {
603 var newfilename = Y.one('#fm-rename-input').get('value');
610 if (fileinfo.type == 'folder') {
611 params['filepath'] = fileinfo.filepath;
612 params['filename'] = '.';
613 params['newdirname'] = newfilename;
614 action = 'renamedir';
616 params['filepath'] = fileinfo.filepath;
617 params['filename'] = fileinfo.fullname;
618 params['newfilename'] = newfilename;
625 callback: function(id, obj, args) {
627 alert(M.str.repository.fileexists);
629 scope.refresh(obj.filepath);
630 if (typeof M.core_formchangechecker != 'undefined') {
631 M.core_formchangechecker.set_form_changed();
634 Y.one('#fm-rename-input').set('value', '');
635 scope.rename_dialog.hide();
640 var dialog = Y.one('#fm-rename-dlg');
642 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>');
643 Y.one(document.body).appendChild(dialog);
644 this.rename_dialog = new YAHOO.widget.Dialog("fm-rename-dlg", {
648 constraintoviewport : true
652 var buttons = [ { text:M.str.moodle.rename, handler:perform, isDefault:true},
653 { text:M.str.moodle.cancel, handler:function(){this.cancel();}}];
655 this.rename_dialog.cfg.queueProperty('buttons', buttons);
656 this.rename_dialog.render();
657 this.rename_dialog.show();
658 //var k1 = new YAHOO.util.KeyListener(scope, {keys:13}, {fn:function(){perform();}, correctScope: true});
660 Y.one('#fm-rename-input').set('value', fileinfo.fullname);
662 function move(type, ev, obj) {
664 var itemid = this.options.itemid;
665 // setup move file dialog
667 if (!Y.one('#fm-move-dlg')) {
668 dialog = Y.Node.create('<div id="fm-move-dlg"></div>');
669 Y.one(document.body).appendChild(dialog);
671 dialog = Y.one('#fm-move-dlg');
674 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>');
676 this.movefile_dialog = new YAHOO.widget.Dialog("fm-move-dlg", {
680 constraintoviewport : true
683 var treeview = new YAHOO.widget.TreeView("fm-tree");
685 var dialog = this.movefile_dialog;
687 if (!treeview.targetpath) {
691 if (fileinfo.type == 'folder') {
696 params['filepath'] = fileinfo.filepath;
697 params['filename'] = fileinfo.fullname;
698 params['newfilepath'] = treeview.targetpath;
703 callback: function(id, obj, args) {
710 if (typeof M.core_formchangechecker != 'undefined') {
711 M.core_formchangechecker.set_form_changed();
717 var buttons = [ { text:M.str.moodle.move, handler:_move, isDefault:true },
718 { text:M.str.moodle.cancel, handler:function(){this.cancel();}}];
720 this.movefile_dialog.cfg.queueProperty("buttons", buttons);
721 this.movefile_dialog.render();
723 treeview.subscribe("dblClickEvent", function(e) {
724 // update destidatoin folder
725 this.targetpath = e.node.data.path;
726 var title = Y.one('#fm-move-div');
727 title.set('innerHTML', '<strong>"' + this.targetpath + '"</strong> has been selected.');
730 function loadDataForNode(node, onCompleteCallback) {
732 params['filepath'] = node.data.path;
737 callback: function(id, obj, args) {
739 if (data.length == 0) {
742 for (var i in data) {
743 var textnode = {label: data[i].fullname, path: data[i].filepath, itemid: this.itemid};
744 var tmpNode = new YAHOO.widget.TextNode(textnode, node, false);
750 obj.oncomplete = onCompleteCallback;
754 this.movefile_dialog.subscribe('show', function(){
755 var rootNode = treeview.getRoot();
756 treeview.setDynamicLoad(loadDataForNode);
757 treeview.removeChildren(rootNode);
758 var textnode = {label: M.str.moodle.files, path: '/'};
759 var tmpNode = new YAHOO.widget.TextNode(textnode, rootNode, true);
763 this.movefile_dialog.show();
766 {text: M.str.moodle.rename+'...', onclick: {fn: rename, obj: options, scope: this}},
767 {text: M.str.moodle.move+'...', onclick: {fn: move, obj: options, scope: this}}
769 // delete is reserve word in Javascript
770 shared_items.push({text: M.str.moodle['delete']+'...', onclick: {fn: remove, obj: options, scope: this}});
771 var menu = new YAHOO.widget.Menu(menuid, {xy:position, clicktohide:true});
773 menu.addItems(menuitems);
774 menu.addItems(shared_items);
775 menu.render(document.body);
776 menu.subscribe('hide', function(){
777 this.fireEvent('destroy');
783 // finally init everything needed
784 // kill nonjs filemanager
785 var item = document.getElementById('nonjs-filemanager-'+options.client_id);
786 if (item && !options.usenonjs) {
787 item.parentNode.removeChild(item);
789 // hide loading picture
790 item = document.getElementById('filemanager-loading-'+options.client_id);
792 item.parentNode.removeChild(item);
794 // display filemanager interface
795 item = document.getElementById('filemanager-wrapper-'+options.client_id);
797 item.style.display = '';
800 var manager = new FileManagerHelper(options);
802 filemanager: manager,
803 acceptedtypes: options.accepted_types,
804 clientid: options.client_id,
805 maxfiles: options.maxfiles,
806 maxbytes: options.maxbytes,
807 itemid: options.itemid,
808 repositories: manager.filepicker_options.repositories,
809 containerprefix: '#filemanager-',
811 M.form_dndupload.init(Y, dndoptions);