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
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
33 * FileManager options:
35 * this.options.currentpath
40 M.form_filemanager = {templates:{}};
42 M.form_filemanager.set_templates = function(Y, templates) {
43 M.form_filemanager.templates = templates;
47 * This fucntion is called for each file picker on page.
49 M.form_filemanager.init = function(Y, options) {
50 var FileManagerHelper = function(options) {
51 FileManagerHelper.superclass.constructor.apply(this, arguments);
53 FileManagerHelper.NAME = "FileManager";
54 FileManagerHelper.ATTRS = {
59 Y.extend(FileManagerHelper, Y.Base, {
60 api: M.cfg.wwwroot+'/repository/draftfiles_ajax.php',
62 initializer: function(options) {
63 this.options = options;
64 if (options.mainfile) {
65 this.enablemainfile = options.mainfile;
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;
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;
91 this.dndcontainer = this.filemanager.one('.filemanager-container');
92 if (!this.dndcontainer.get('id')) {
93 this.dndcontainer.generateID();
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);
102 // initialize 'select file' panel
103 this.selectnode = Y.Node.createWithFilesSkin(M.form_filemanager.templates.fileselectlayout);
104 this.selectnode.generateID();
105 this.selectui = new Y.Panel({
106 srcNode : this.selectnode,
113 this.selectui.plug(Y.Plugin.Drag,{handles:['#'+this.selectnode.get('id')+' .yui3-widget-hd']});
114 this.selectui.hide();
115 this.setup_select_file();
116 // setup buttons onclick events
117 this.setup_buttons();
118 // set event handler for lazy loading of thumbnails
119 this.filemanager.one('.fp-content').on(['scroll','resize'], this.content_scrolled, this);
121 this.viewmode = 1; // TODO take from cookies?
122 this.filemanager.all('.fp-vb-icons,.fp-vb-tree,.fp-vb-details').removeClass('checked')
123 this.filemanager.all('.fp-vb-icons').addClass('checked')
124 this.refresh(this.currentpath); // MDL-31113 get latest list from server
128 this.filemanager.addClass('fm-updating');
130 request: function(args, redraw) {
131 var api = this.api + '?action='+args.action;
135 scope = args['scope'];
137 params['sesskey'] = M.cfg.sesskey;
138 params['client_id'] = this.client_id;
139 params['filepath'] = this.currentpath;
140 params['itemid'] = this.options.itemid?this.options.itemid:0;
141 if (args['params']) {
142 for (i in args['params']) {
143 params[i] = args['params'][i];
149 complete: function(id,o,p) {
156 data = Y.JSON.parse(o.responseText);
158 scope.print_msg(M.str.repository.invalidjson, 'error');
159 Y.error(M.str.repository.invalidjson+":\n"+o.responseText);
162 if (data && data.tree && scope.set_current_tree) {
163 scope.set_current_tree(data.tree);
165 args.callback(id,data,p);
172 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
173 'User-Agent': 'MoodleFileManager/3.0'
175 data: build_querystring(params)
178 cfg.form = args.form;
185 filepicker_callback: function(obj) {
187 this.check_buttons();
188 this.refresh(this.currentpath);
189 if (typeof M.core_formchangechecker != 'undefined') {
190 M.core_formchangechecker.set_form_changed();
193 check_buttons: function() {
194 if (this.filecount>0) {
195 this.filemanager.removeClass('fm-nofiles');
197 this.filemanager.addClass('fm-nofiles');
199 if (this.filecount >= this.maxfiles && this.maxfiles!=-1) {
200 this.filemanager.addClass('fm-maxfiles');
203 this.filemanager.removeClass('fm-maxfiles');
206 refresh: function(filepath) {
208 this.currentpath = filepath;
210 filepath = this.currentpath;
212 this.currentpath = filepath;
217 params: {'filepath':filepath},
218 callback: function(id, obj, args) {
219 scope.filecount = obj.filecount;
220 scope.check_buttons();
222 scope.lazyloading = {};
227 /** displays message in a popup */
228 print_msg: function(msg, type) {
229 var header = M.str.moodle.error;
230 if (type != 'error') {
231 type = 'info'; // one of only two types excepted
232 header = M.str.moodle.info;
235 this.msg_dlg_node = Y.Node.createWithFilesSkin(M.form_filemanager.templates.message);
236 var nodeid = this.msg_dlg_node.generateID();
238 this.msg_dlg = new Y.Panel({
239 srcNode : this.msg_dlg_node,
246 this.msg_dlg.plug(Y.Plugin.Drag,{handles:['#'+nodeid+' .yui3-widget-hd']});
247 this.msg_dlg_node.one('.fp-msg-butok').on('click', function(e) {
253 this.msg_dlg.set('headerContent', header);
254 this.msg_dlg_node.removeClass('fp-msg-info').removeClass('fp-msg-error').addClass('fp-msg-'+type)
255 this.msg_dlg_node.one('.fp-msg-text').setContent(msg);
258 is_disabled: function() {
259 return this.filemanager.ancestor('.fitem.disabled') != null;
261 setup_buttons: function() {
262 var button_download = this.filemanager.one('.fp-btn-download');
263 var button_create = this.filemanager.one('.fp-btn-mkdir');
264 var button_addfile = this.filemanager.one('.fp-btn-add');
266 // setup 'add file' button
267 button_addfile.on('click', this.show_filepicker, this);
269 var dndarrow = this.filemanager.one('.dndupload-arrow');
271 dndarrow.on('click', this.show_filepicker, this);
274 // setup 'make a folder' button
275 if (this.options.subdirs) {
276 button_create.on('click',function(e) {
278 if (this.is_disabled()) {
282 // a function used to perform an ajax request
283 var perform_action = function(e) {
285 var foldername = Y.one('#fm-newname-'+scope.client_id).get('value');
287 scope.mkdir_dialog.hide();
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();
304 if (!this.mkdir_dialog) {
305 var node = Y.Node.createWithFilesSkin(M.form_filemanager.templates.mkdir);
306 this.mkdir_dialog = new Y.Panel({
314 this.mkdir_dialog.plug(Y.Plugin.Drag,{handles:['.yui3-widget-hd']});
315 node.one('.fp-dlg-butcreate').on('click', perform_action, this);
316 node.one('input').set('id', 'fm-newname-'+this.client_id).
317 on('keydown', function(e){
318 if (e.keyCode == 13) {Y.bind(perform_action, this)(e);}
320 node.all('.fp-dlg-butcancel').on('click', function(e){e.preventDefault();this.mkdir_dialog.hide();}, this);
321 node.all('.fp-dlg-curpath').set('id', 'fm-curpath-'+this.client_id);
323 this.mkdir_dialog.show();
324 Y.one('#fm-newname-'+scope.client_id).focus();
325 Y.all('#fm-curpath-'+scope.client_id).setContent(this.currentpath)
328 this.filemanager.addClass('fm-nomkdir');
331 // setup 'download this folder' button
332 button_download.on('click',function(e) {
334 if (this.is_disabled()) {
338 // perform downloaddir ajax request
340 action: 'downloaddir',
342 callback: function(id, obj, args) {
344 scope.refresh(obj.filepath);
345 node = Y.Node.create('<iframe></iframe>').setStyles({
346 visibility : 'hidden',
350 node.set('src', obj.fileurl);
351 Y.one('body').appendChild(node);
353 scope.print_msg(M.str.repository.draftareanofiles, 'error');
359 this.filemanager.all('.fp-vb-icons,.fp-vb-tree,.fp-vb-details').
360 on('click', function(e) {
362 var viewbar = this.filemanager.one('.fp-viewbar')
363 if (!this.is_disabled() && (!viewbar || !viewbar.hasClass('disabled'))) {
364 this.filemanager.all('.fp-vb-icons,.fp-vb-tree,.fp-vb-details').removeClass('checked')
365 if (e.currentTarget.hasClass('fp-vb-tree')) {
367 } else if (e.currentTarget.hasClass('fp-vb-details')) {
372 e.currentTarget.addClass('checked')
378 show_filepicker: function (e) {
379 // if maxfiles == -1, the no limit
381 if (this.is_disabled()) {
384 var options = this.filepicker_options;
385 options.formcallback = this.filepicker_callback;
386 // XXX: magic here, to let filepicker use filemanager scope
387 options.magicscope = this;
388 options.savepath = this.currentpath;
389 M.core_filepicker.show(Y, options);
392 print_path: function() {
393 var p = this.options.path;
394 this.pathbar.setContent('').addClass('empty');
395 if (p && p.length!=0 && this.viewmode != 2) {
396 for(var i = 0; i < p.length; i++) {
397 var el = this.pathnode.cloneNode(true);
398 this.pathbar.appendChild(el);
401 el.addClass('first');
403 if (i == p.length-1) {
412 el.one('.fp-path-folder-name').setContent(p[i].name).
413 on('click', function(e, path) {
415 if (!this.is_disabled()) {
420 this.pathbar.removeClass('empty');
423 get_filepath: function(obj) {
424 if (obj.path && obj.path.length) {
425 return obj.path[obj.path.length-1].path;
429 treeview_dynload: function(node, cb) {
430 var retrieved_children = {};
432 for (var i in node.children) {
433 retrieved_children[node.children[i].path] = node.children[i];
436 if (!node.path || node.path == '/') {
437 // this is a root pseudo folder
438 node.fileinfo.filepath = '/';
439 node.fileinfo.type = 'folder';
440 node.fileinfo.fullname = node.fileinfo.title;
441 node.fileinfo.filename = '.';
445 params: {filepath:node.path?node.path:''},
447 callback: function(id, obj, args) {
449 var scope = args.scope;
450 // check that user did not leave the view mode before recieving this response
451 if (!(scope.viewmode == 2 && node && node.getChildrenEl())) {
454 if (cb != null) { // (in manual mode do not update current path)
456 scope.currentpath = node.path?node.path:'/';
458 node.highlight(false);
459 node.origlist = obj.list ? obj.list : null;
460 node.origpath = obj.path ? obj.path : null;
463 if (list[k].type == 'folder' && retrieved_children[list[k].filepath]) {
464 // if this child is a folder and has already been retrieved
465 retrieved_children[list[k].filepath].fileinfo = list[k];
466 node.children[node.children.length] = retrieved_children[list[k].filepath];
468 // append new file to the list
469 scope.view_files([list[k]]);
475 // invoke callback requested by TreeView component
478 scope.content_scrolled();
482 content_scrolled: function(e) {
483 setTimeout(Y.bind(function() {
484 if (this.processingimages) {return;}
485 this.processingimages = true;
487 fpcontent = this.filemanager.one('.fp-content'),
488 fpcontenty = fpcontent.getY(),
489 fpcontentheight = fpcontent.getStylePx('height'),
490 is_node_visible = function(node) {
491 var offset = node.getY()-fpcontenty;
492 if (offset <= fpcontentheight && (offset >=0 || offset+node.getStylePx('height')>=0)) {
497 // replace src for visible images that need to be lazy-loaded
498 if (scope.lazyloading) {
499 fpcontent.all('img').each( function(node) {
500 if (node.get('id') && scope.lazyloading[node.get('id')] && is_node_visible(node)) {
501 node.setImgRealSrc(scope.lazyloading);
505 this.processingimages = false;
508 view_files: function(appendfiles) {
509 this.filemanager.removeClass('fm-updating').removeClass('fm-noitems');
510 if ((appendfiles == null) && (!this.options.list || this.options.list.length == 0) && this.viewmode != 2) {
511 this.filemanager.addClass('fm-noitems');
514 var list = (appendfiles != null) ? appendfiles : this.options.list;
515 var element_template;
516 if (this.viewmode == 2 || this.viewmode == 3) {
517 element_template = Y.Node.create(M.form_filemanager.templates.listfilename);
520 element_template = Y.Node.create(M.form_filemanager.templates.iconfilename);
523 viewmode : this.viewmode,
524 appendonly : appendfiles != null,
525 filenode : element_template,
526 callbackcontext : this,
527 callback : function(e, node) {
528 if (e.preventDefault) { e.preventDefault(); }
529 if (node.type == 'folder') {
530 this.refresh(node.filepath);
532 this.select_file(node);
535 rightclickcallback : function(e, node) {
536 if (e.preventDefault) { e.preventDefault(); }
537 this.select_file(node);
539 classnamecallback : function(node) {
541 if (node.type == 'folder' || (!node.type && !node.filename)) {
542 classname = classname + ' fp-folder';
544 if (node.filename || node.filepath || (node.path && node.path != '/')) {
545 classname = classname + ' fp-hascontextmenu';
548 classname = classname + ' fp-isreference';
551 classname = classname + ' fp-hasreferences';
553 if (node.originalmissing) {
554 classname = classname + ' fp-originalmissing';
556 if (node.sortorder == 1) { classname = classname + ' fp-mainfile';}
557 return Y.Lang.trim(classname);
560 if (this.viewmode == 2) {
561 options.dynload = true;
562 options.filepath = this.options.path;
563 options.treeview_dynload = this.treeview_dynload;
564 options.norootrightclick = true;
565 options.callback = function(e, node) {
566 // TODO MDL-32736 e is not an event here but an object with properties 'event' and 'node'
567 if (!node.fullname) {return;}
568 if (node.type != 'folder') {
569 if (e.node.parent && e.node.parent.origpath) {
570 // set the current path
571 this.options.path = e.node.parent.origpath;
572 this.options.list = e.node.parent.origlist;
575 this.currentpath = node.filepath;
576 this.select_file(node);
578 // save current path and filelist (in case we want to jump to other viewmode)
579 this.options.path = e.node.origpath;
580 this.options.list = e.node.origlist;
581 this.currentpath = node.filepath;
583 //this.content_scrolled();
587 if (!this.lazyloading) {
590 this.filemanager.one('.fp-content').fp_display_filelist(options, list, this.lazyloading);
591 this.content_scrolled();
593 populate_licenses_select: function(node) {
598 var licenses = this.options.licenses;
599 for (var i in licenses) {
600 var option = Y.Node.create('<option/>').
601 set('value', licenses[i].shortname).
602 setContent(licenses[i].fullname);
603 node.appendChild(option)
606 set_current_tree: function(tree) {
607 var appendfilepaths = function(list, node) {
608 if (!node || !node.children || !node.children.length) {return;}
609 for (var i in node.children) {
610 list[list.length] = node.children[i].filepath;
611 appendfilepaths(list, node.children[i]);
615 appendfilepaths(list, tree);
616 var selectnode = this.selectnode;
617 node = selectnode.one('.fp-path select');
619 for (var i in list) {
620 node.appendChild(Y.Node.create('<option/>').
621 set('value', list[i]).setContent(list[i]))
624 update_file: function(confirmed) {
625 var selectnode = this.selectnode;
626 var fileinfo = this.selectui.fileinfo;
628 var newfilename = Y.Lang.trim(selectnode.one('.fp-saveas input').get('value'));
629 var filenamechanged = (newfilename && newfilename != fileinfo.fullname);
630 var pathselect = selectnode.one('.fp-path select'),
631 pathindex = pathselect.get('selectedIndex'),
632 targetpath = pathselect.get("options").item(pathindex).get('value');
633 var filepathchanged = (targetpath != this.get_parent_folder_name(fileinfo));
634 var newauthor = Y.Lang.trim(selectnode.one('.fp-author input').get('value'));
635 var authorchanged = (newauthor != Y.Lang.trim(fileinfo.author));
636 var licenseselect = selectnode.one('.fp-license select'),
637 licenseindex = licenseselect.get('selectedIndex'),
638 newlicense = licenseselect.get("options").item(licenseindex).get('value');
639 var licensechanged = (newlicense != fileinfo.license);
642 var dialog_options = {callback:this.update_file, callbackargs:[true], scope:this};
643 if (fileinfo.type == 'folder') {
645 this.print_msg(M.str.repository.entername, 'error');
648 if (filenamechanged || filepathchanged) {
650 dialog_options.message = M.str.repository.confirmrenamefolder;
651 this.show_confirm_dialog(dialog_options);
654 params = {filepath:fileinfo.filepath, newdirname:newfilename, newfilepath:targetpath};
655 action = 'updatedir';
659 this.print_msg(M.str.repository.enternewname, 'error');
662 if ((filenamechanged || filepathchanged) && !confirmed && fileinfo.refcount) {
663 dialog_options.message = M.util.get_string('confirmrenamefile', 'repository', fileinfo.refcount);
664 this.show_confirm_dialog(dialog_options);
667 if (filenamechanged || filepathchanged || licensechanged || authorchanged) {
668 params = {filepath:fileinfo.filepath, filename:fileinfo.fullname,
669 newfilename:newfilename, newfilepath:targetpath,
670 newlicense:newlicense, newauthor:newauthor};
671 action = 'updatefile';
676 this.selectui.hide();
679 selectnode.addClass('loading');
684 callback: function(id, obj, args) {
686 selectnode.removeClass('loading');
687 args.scope.print_msg(obj.error, 'error');
689 args.scope.selectui.hide();
690 args.scope.refresh((obj && obj.filepath) ? obj.filepath : '/');
691 if (typeof M.core_formchangechecker != 'undefined') {
692 M.core_formchangechecker.set_form_changed();
699 * Displays a confirmation dialog
700 * Expected attributes in dialog_options: message, callback, callbackargs(optional), scope(optional)
702 show_confirm_dialog: function(dialog_options) {
703 // instead of M.util.show_confirm_dialog(e, dialog_options);
704 if (!this.confirm_dlg) {
705 this.confirm_dlg_node = Y.Node.createWithFilesSkin(M.form_filemanager.templates.confirmdialog);
706 var node = this.confirm_dlg_node;
708 this.confirm_dlg = new Y.Panel({
717 this.confirm_dlg.plug(Y.Plugin.Drag,{handles:['#'+node.get('id')+' .yui3-widget-hd']});
718 var handle_confirm = function(ev) {
719 var dlgopt = this.confirm_dlg.dlgopt;
721 this.confirm_dlg.hide();
722 if (dlgopt.callback) {
723 if (dlgopt.callbackargs) {
724 dlgopt.callback.apply(dlgopt.scope || this, dlgopt.callbackargs);
726 dlgopt.callback.apply(dlgopt.scope || this);
730 var handle_cancel = function(ev) {
732 this.confirm_dlg.hide();
734 node.one('.fp-dlg-butconfirm').on('click', handle_confirm, this);
735 node.one('.fp-dlg-butcancel').on('click', handle_cancel, this);
737 this.confirm_dlg.dlgopt = dialog_options;
738 this.confirm_dlg_node.one('.fp-dlg-text').setContent(dialog_options.message);
739 this.confirm_dlg.show();
741 setup_select_file: function() {
742 var selectnode = this.selectnode;
743 // bind labels with corresponding inputs
744 selectnode.all('.fp-saveas,.fp-path,.fp-author,.fp-license').each(function (node) {
745 node.all('label').set('for', node.one('input,select').generateID());
747 this.populate_licenses_select(selectnode.one('.fp-license select'));
748 // register event on clicking buttons
749 selectnode.one('.fp-file-update').on('click', function(e) {
753 selectnode.all('form').on('keydown', function(e) {
754 if (e.keyCode == 13) {
759 selectnode.one('.fp-file-download').on('click', function(e) {
761 if (this.selectui.fileinfo.type != 'folder') {
762 node = Y.Node.create('<iframe></iframe>').setStyles({
763 visibility : 'hidden',
767 node.set('src', this.selectui.fileinfo.url);
768 Y.one('body').appendChild(node);
771 selectnode.one('.fp-file-delete').on('click', function(e) {
773 var dialog_options = {};
775 var fileinfo = this.selectui.fileinfo;
776 dialog_options.scope = this;
777 params.filepath = fileinfo.filepath;
778 if (fileinfo.type == 'folder') {
779 params.filename = '.';
780 dialog_options.message = M.str.repository.confirmdeletefolder;
782 params.filename = fileinfo.fullname;
783 if (fileinfo.refcount) {
784 dialog_options.message = M.util.get_string('confirmdeletefilewithhref', 'repository', fileinfo.refcount);
786 dialog_options.message = M.str.repository.confirmdeletefile;
789 dialog_options.callbackargs = [params];
790 dialog_options.callback = function(params) {
791 //selectnode.addClass('loading');
796 callback: function(id, obj, args) {
797 //args.scope.selectui.hide();
798 args.scope.filecount--;
799 args.scope.refresh(obj.filepath);
800 if (typeof M.core_formchangechecker != 'undefined') {
801 M.core_formchangechecker.set_form_changed();
806 this.selectui.hide(); // TODO remove this after confirm dialog is replaced with YUI3
807 this.show_confirm_dialog(dialog_options);
809 selectnode.one('.fp-file-zip').on('click', function(e) {
812 var fileinfo = this.selectui.fileinfo;
813 if (fileinfo.type != 'folder') {
814 // this button should not even be shown
817 params['filepath'] = fileinfo.filepath;
818 params['filename'] = '.';
819 selectnode.addClass('loading');
824 callback: function(id, obj, args) {
825 args.scope.selectui.hide();
826 args.scope.refresh(obj.filepath);
830 selectnode.one('.fp-file-unzip').on('click', function(e) {
833 var fileinfo = this.selectui.fileinfo;
834 if (fileinfo.type != 'zip') {
835 // this button should not even be shown
838 params['filepath'] = fileinfo.filepath;
839 params['filename'] = fileinfo.fullname;
840 selectnode.addClass('loading');
845 callback: function(id, obj, args) {
846 args.scope.selectui.hide();
847 args.scope.refresh(obj.filepath);
851 selectnode.one('.fp-file-setmain').on('click', function(e) {
854 var fileinfo = this.selectui.fileinfo;
855 if (!this.enablemainfile || fileinfo.type == 'folder') {
856 // this button should not even be shown for folders or when mainfile is disabled
859 params['filepath'] = fileinfo.filepath;
860 params['filename'] = fileinfo.fullname;
861 selectnode.addClass('loading');
863 action: 'setmainfile',
866 callback: function(id, obj, args) {
867 args.scope.selectui.hide();
868 args.scope.refresh(fileinfo.filepath);
872 selectnode.all('.fp-file-cancel').on('click', function(e) {
874 // TODO if changed asked to confirm, the same with close button
875 this.selectui.hide();
878 get_parent_folder_name: function(node) {
879 if (node.type != 'folder' || node.filepath.length < node.fullname.length+1) {
880 return node.filepath;
882 var basedir = node.filepath.substr(0, node.filepath.length - node.fullname.length - 1);
883 var lastdir = node.filepath.substr(node.filepath.length - node.fullname.length - 2);
884 if (lastdir == '/' + node.fullname + '/') {
887 return node.filepath;
889 select_file: function(node) {
890 if (this.is_disabled()) {
893 var selectnode = this.selectnode;
894 selectnode.removeClass('loading').removeClass('fp-folder').
895 removeClass('fp-file').removeClass('fp-zip').removeClass('fp-cansetmain');
896 if (node.type == 'folder' || node.type == 'zip') {
897 selectnode.addClass('fp-'+node.type);
899 selectnode.addClass('fp-file');
901 if (this.enablemainfile && (node.sortorder != 1) && node.type == 'file') {
902 selectnode.addClass('fp-cansetmain');
904 this.selectui.fileinfo = node;
905 selectnode.one('.fp-saveas input').set('value', node.fullname);
906 var foldername = this.get_parent_folder_name(node);
907 selectnode.all('.fp-author input').set('value', node.author ? node.author : '');
908 selectnode.all('.fp-license select option[selected]').set('selected', false);
909 selectnode.all('.fp-license select option[value='+node.license+']').set('selected', true);
910 selectnode.all('.fp-path select option[selected]').set('selected', false);
911 selectnode.all('.fp-path select option').each(function(el){
912 if (el.get('value') == foldername) {
913 el.set('selected', true);
916 selectnode.all('.fp-author input, .fp-license select').set('disabled',(node.type == 'folder')?'disabled':'');
917 // display static information about a file (when known)
918 var attrs = ['datemodified','datecreated','size','dimensions','original','reflist'];
919 for (var i in attrs) {
920 if (selectnode.one('.fp-'+attrs[i])) {
921 var value = (node[attrs[i]+'_f']) ? node[attrs[i]+'_f'] : (node[attrs[i]] ? node[attrs[i]] : '');
922 selectnode.one('.fp-'+attrs[i]).addClassIf('fp-unknown', ''+value == '')
923 .one('.fp-value').setContent(value);
927 var imgnode = Y.Node.create('<img/>').
928 set('src', node.realthumbnail ? node.realthumbnail : node.thumbnail).
929 setStyle('maxHeight', ''+(node.thumbnail_height ? node.thumbnail_height : 90)+'px').
930 setStyle('maxWidth', ''+(node.thumbnail_width ? node.thumbnail_width : 90)+'px');
931 selectnode.one('.fp-thumbnail').setContent('').appendChild(imgnode);
932 // load original location if applicable
933 if (node.isref && !node.original) {
934 selectnode.one('.fp-original').removeClass('fp-unknown').addClass('fp-loading');
936 action: 'getoriginal',
938 params: {'filepath':node.filepath,'filename':node.fullname},
939 callback: function(id, obj, args) {
940 // check if we did not select another file meanwhile
941 var scope = args.scope;
942 if (scope.selectui.fileinfo && node &&
943 scope.selectui.fileinfo.filepath == node.filepath &&
944 scope.selectui.fileinfo.fullname == node.fullname) {
945 selectnode.one('.fp-original').removeClass('fp-loading');
947 node.original = obj.original;
948 selectnode.one('.fp-original .fp-value').setContent(node.original);
950 selectnode.one('.fp-original .fp-value').setContent(M.str.repository.unknownsource);
956 // load references list if applicable
957 selectnode.one('.fp-refcount').setContent(node.refcount ? M.util.get_string('referencesexist', 'repository', node.refcount) : '');
958 if (node.refcount && !node.reflist) {
959 selectnode.one('.fp-reflist').removeClass('fp-unknown').addClass('fp-loading');
961 action: 'getreferences',
963 params: {'filepath':node.filepath,'filename':node.fullname},
964 callback: function(id, obj, args) {
965 // check if we did not select another file meanwhile
966 var scope = args.scope;
967 if (scope.selectui.fileinfo && node &&
968 scope.selectui.fileinfo.filepath == node.filepath &&
969 scope.selectui.fileinfo.fullname == node.fullname) {
970 selectnode.one('.fp-reflist').removeClass('fp-loading');
971 if (obj.references) {
973 for (var i in obj.references) {
974 node.reflist += '<li>'+obj.references[i]+'</li>';
976 selectnode.one('.fp-reflist .fp-value').setContent(node.reflist);
978 selectnode.one('.fp-reflist .fp-value').setContent('');
985 this.selectui.show();
993 // finally init everything needed
994 // hide loading picture, display filemanager interface
995 var filemanager = Y.one('#filemanager-'+options.client_id);
996 filemanager.removeClass('fm-loading').addClass('fm-loaded');
998 var manager = new FileManagerHelper(options);
1000 filemanager: manager,
1001 acceptedtypes: options.filepicker.accepted_types,
1002 clientid: options.client_id,
1003 author: options.author,
1004 maxfiles: options.maxfiles,
1005 maxbytes: options.maxbytes,
1006 itemid: options.itemid,
1007 repositories: manager.filepicker_options.repositories,
1008 containerid: manager.dndcontainer.get('id')
1010 M.form_dndupload.init(Y, dndoptions);