publish course MDL-19315 refactor the publish index page + add unpublish operation
[moodle.git] / lib / form / filemanager.js
CommitLineData
494bf5c8
DC
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/>.
494bf5c8 15/**
494bf5c8 16 *
840912d5
DC
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
554cd8fc
DC
26 * this.filecount, how many files in this filemanager
27 * this.maxfiles
28 * this.maxbytes
840912d5
DC
29 *
30 * FileManager options:
31 * =====
32 * this.options.currentpath
33 * this.options.itemid
494bf5c8 34 */
56a7bf68 35
4c508047
PS
36
37M.form_filemanager = {};
38
39/**
40 * This fucntion is called for each file picker on page.
41 */
42M.form_filemanager.init = function(Y, options) {
43 var FileManagerHelper = function(options) {
44 FileManagerHelper.superclass.constructor.apply(this, arguments);
56a7bf68 45 }
4c508047
PS
46 FileManagerHelper.NAME = "FileManager";
47 FileManagerHelper.ATTRS = {
840912d5
DC
48 options: {},
49 lang: {}
50 };
4c508047
PS
51
52 Y.extend(FileManagerHelper, Y.Base, {
840912d5
DC
53 api: M.cfg.wwwroot+'/files/files_ajax.php',
54 menus: {},
4c508047
PS
55 initializer: function(options) {
56 this.options = options;
f45dfeeb
DC
57 if (options.mainfile) {
58 this.mainfile = options.mainfile;
59 }
4c508047 60 this.client_id = options.client_id;
840912d5 61 this.currentpath = '/';
4c508047
PS
62 this.maxfiles = options.maxfiles;
63 this.maxbytes = options.maxbytes;
133fd70b
DC
64
65 this.filepicker_options = options.filepicker?options.filepicker:{};
66 this.filepicker_options.client_id = this.client_id;
67 this.filepicker_options.maxfiles = this.maxfiles;
68 this.filepicker_options.maxbytes = this.maxbytes;
69 this.filepicker_options.env = 'filemanager';
70 this.filepicker_options.filearea = options.filearea;
71 this.filepicker_options.itemid = options.itemid;
72
73 this.filearea = options.filearea?options.filearea:'user_draft';
4c508047
PS
74 if (options.filecount) {
75 this.filecount = options.filecount;
554cd8fc
DC
76 } else {
77 this.filecount = 0;
78 }
840912d5
DC
79 this.setup_buttons();
80 this.render();
81 },
4c508047 82
840912d5
DC
83 request: function(args) {
84 var api = this.api + '?action='+args.action;
85 var params = {};
86 var scope = this;
87 if (args['scope']) {
88 scope = args['scope'];
89 }
4c508047 90 params['sesskey'] = M.cfg.sesskey;
133fd70b 91 params['filearea'] = this.filearea;
840912d5
DC
92 params['client_id'] = this.client_id;
93 params['filepath'] = this.currentpath;
94 params['itemid'] = this.options.itemid?this.options.itemid:0;
95 if (args['params']) {
96 for (i in args['params']) {
97 params[i] = args['params'][i];
56a7bf68 98 }
56a7bf68 99 }
840912d5
DC
100 var cfg = {
101 method: 'POST',
102 on: {
103 complete: function(id,o,p) {
104 if (!o) {
105 alert('IO FATAL');
106 return;
107 }
384ab39a 108 var data = Y.JSON.parse(o.responseText);
840912d5
DC
109 args.callback(id,data,p);
110 }
111 },
112 arguments: {
113 scope: scope
114 },
115 headers: {
116 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
117 'User-Agent': 'MoodleFileManager/3.0'
118 },
119 data: build_querystring(params)
120 };
121 if (args.form) {
122 cfg.form = args.form;
56a7bf68 123 }
840912d5
DC
124 Y.io(api, cfg);
125 },
126 filepicker_callback: function(obj) {
554cd8fc
DC
127 var button_addfile = Y.one("#btnadd-"+this.client_id);
128 this.filecount++;
1cea3c80
DC
129 if (this.filecount > 0) {
130 Y.one("#btndwn-"+this.client_id).setStyle('display', 'inline');
131 }
554cd8fc
DC
132 if (this.filecount >= this.maxfiles && this.maxfiles!=-1) {
133 button_addfile.setStyle('display', 'none');
134 }
840912d5
DC
135 this.refresh(this.currentpath);
136 },
137 refresh: function(filepath) {
138 var scope = this;
59eeb81b 139 this.currentpath = filepath;
840912d5
DC
140 if (!filepath) {
141 filepath = this.currentpath;
142 } else {
143 this.currentpath = filepath;
56a7bf68 144 }
840912d5
DC
145 this.request({
146 action: 'list',
147 scope: scope,
148 params: {'filepath':filepath},
149 callback: function(id, obj, args) {
150 scope.options = obj;
151 scope.render(obj);
56a7bf68 152 }
840912d5
DC
153 });
154 },
155 setup_buttons: function() {
4c508047
PS
156 var button_download = Y.one("#btndwn-"+this.client_id);
157 var button_create = Y.one("#btncrt-"+this.client_id);
158 var button_addfile = Y.one("#btnadd-"+this.client_id);
840912d5
DC
159
160 // setup 'add file' button
161 // if maxfiles == -1, the no limit
554cd8fc
DC
162 if (this.filecount >= this.maxfiles
163 && this.maxfiles!=-1) {
164 button_addfile.setStyle('display', 'none');
165 } else {
166 button_addfile.on('click', function(e) {
167 var options = this.filepicker_options;
4c508047
PS
168 options.formcallback = this.filepicker_callback;
169 // XXX: magic here, to let filepicker use filemanager scope
170 options.magicscope = this;
171 options.savepath = this.currentpath;
59eeb81b 172 M.core_filepicker.show(Y, options);
554cd8fc
DC
173 }, this);
174 }
840912d5
DC
175
176 // setup 'make a folder' button
177 if (this.options.subdirs) {
178 button_create.on('click',function(e) {
179 var scope = this;
180 // a function used to perform an ajax request
181 function perform_action(e) {
182 var foldername = Y.one('#fm-newname').get('value');
183 if (!foldername) {
184 return;
185 }
186 scope.request({
187 action:'mkdir',
188 params: {filepath:scope.currentpath, newdirname:foldername},
189 callback: function(id, obj, args) {
190 var filepath = obj.filepath;
191 scope.mkdir_dialog.hide();
192 scope.refresh(filepath);
193 Y.one('#fm-newname').set('value', '');
194 }
195 });
196 }
197 if (!Y.one('#fm-mkdir-dlg')) {
2b728cb5 198 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>');
840912d5
DC
199 Y.one(document.body).appendChild(dialog);
200 this.mkdir_dialog = new YAHOO.widget.Dialog("fm-mkdir-dlg", {
201 width: "300px",
202 visible: true,
203 x:e.pageX,
204 y:e.pageY,
205 constraintoviewport : true
206 });
56a7bf68 207
840912d5 208 }
2b728cb5
PS
209 var buttons = [ { text:M.str.moodle.ok, handler:perform_action, isDefault:true },
210 { text:M.str.moodle.cancel, handler:function(){this.cancel();}}];
56a7bf68 211
840912d5
DC
212 this.mkdir_dialog.cfg.queueProperty("buttons", buttons);
213 this.mkdir_dialog.render();
214 this.mkdir_dialog.show();
215 }, this);
56a7bf68 216 } else {
840912d5 217 button_create.setStyle('display', 'none');
56a7bf68 218 }
840912d5
DC
219
220 // setup 'download this folder' button
221 // NOTE: popup window must be enabled to perform download process
222 button_download.on('click',function() {
223 var scope = this;
224 // perform downloaddir ajax request
225 this.request({
226 action: 'downloaddir',
227 scope: scope,
228 callback: function(id, obj, args) {
7210e887
DC
229 if (obj) {
230 scope.refresh(obj.filepath);
231 var win = window.open(obj.fileurl, 'fm-download-folder');
232 if (!win) {
233 alert(M.str.repository.popupblockeddownload);
234 }
235 } else {
236 alert(M.str.repository.draftareanofiles);
840912d5
DC
237 }
238 }
239 });
240 }, this);
241 },
242 render: function() {
840912d5
DC
243 var options = this.options;
244 var path = this.options.path;
245 var list = this.options.list;
4c508047 246 var breadcrumb = Y.one('#fm-path-'+this.client_id);
840912d5
DC
247 // build breadcrumb
248 if (path) {
249 // empty breadcrumb
250 breadcrumb.set('innerHTML', '');
251 var count = 0;
252 for(var p in path) {
253 var arrow = '';
254 if (count==0) {
2b728cb5 255 arrow = Y.Node.create('<span>'+M.str.moodle.path + ': </span>');
840912d5
DC
256 } else {
257 arrow = Y.Node.create('<span> â–¶ </span>');
494bf5c8 258 }
840912d5
DC
259 count++;
260
4c508047 261 var pathid = 'fm-path-node-'+this.client_id;
840912d5
DC
262 pathid += ('-'+count);
263
264 var crumb = Y.Node.create('<a href="###" id="'+pathid+'">'+path[p].name+'</a>');
265 breadcrumb.appendChild(arrow);
266 breadcrumb.appendChild(crumb);
267
268 var args = {};
269 args.requestpath = path[p].path;
4c508047 270 args.client_id = this.client_id;
840912d5
DC
271 Y.one('#'+pathid).on('click', function(e, args) {
272 var scope = this;
273 var params = {};
274 params['filepath'] = args.requestpath;
59eeb81b 275 this.currentpath = args.requestpath;
840912d5
DC
276 this.request({
277 action: 'list',
278 scope: scope,
279 params: params,
280 callback: function(id, obj, args) {
281 scope.options = obj;
282 scope.render(obj);
283 }
284 });
285 }, this, args);
56a7bf68 286 }
56a7bf68 287 }
840912d5 288 var template = Y.one('#fm-template');
4c508047 289 var container = Y.one('#filemanager-' + this.client_id);
840912d5
DC
290 var listhtml = '';
291
292 // folder list items
293 var folder_ids = [];
294 var folder_data = {};
295
296 // normal file list items
297 var file_ids = [];
298 var file_data = {};
299
300 // archives list items
301 var zip_ids = [];
302 var zip_data = {};
303
304 var html_ids = [];
305 var html_data = {};
306
307 file_data.itemid = folder_data.itemid = zip_data.itemid = options.itemid;
308 file_data.client_id = folder_data.client_id = zip_data.client_id = this.client_id;
309
310 var foldername_ids = [];
311 if (!list || list.length == 0) {
312 // hide file browser and breadcrumb
313 container.setStyle('display', 'none');
314 if (!path || path.length <= 1) {
315 breadcrumb.setStyle('display', 'none');
56a7bf68 316 }
56a7bf68 317 return;
56a7bf68 318 } else {
840912d5
DC
319 container.setStyle('display', 'block');
320 breadcrumb.setStyle('display', 'block');
56a7bf68 321 }
56a7bf68 322
840912d5
DC
323 var count = 0;
324 for(var i in list) {
325 count++;
326 // the li html element
4c508047 327 var htmlid = 'fileitem-'+this.client_id+'-'+count;
840912d5 328 // link to file
4c508047 329 var fileid = 'filename-'+this.client_id+'-'+count;
840912d5 330 // file menu
4c508047 331 var action = 'action-' +this.client_id+'-'+count;
840912d5
DC
332
333 var html = template.get('innerHTML');
334
335 html_ids.push('#'+htmlid);
336 html_data[htmlid] = action;
337
338 list[i].htmlid = htmlid;
339 list[i].fileid = fileid;
340 list[i].action = action;
341
342 var url = "###";
343 // check main file
f45dfeeb 344 var ismainfile = false;
4c508047 345 //if (fm_cfg[this.client_id].mainfilename && (fm_cfg[this.client_id].mainfilename.toLowerCase() == list[i].fullname.toLowerCase())) {
840912d5
DC
346 //ismainfile = true;
347 //}
348
349 switch (list[i].type) {
350 case 'folder':
351 // click folder name
352 foldername_ids.push('#'+fileid);
353 // click folder menu
354 folder_ids.push('#'+action);
355 folder_data[action] = list[i];
356 folder_data[fileid] = list[i];
357 break;
358 case 'file':
359 file_ids.push('#'+action);
360 file_data[action] = list[i];
361 if (list[i].url) {
362 url = list[i].url;
363 }
364 break;
365 case 'zip':
366 zip_ids.push('#'+action);
367 zip_data[action] = list[i];
368 if (list[i].url) {
369 url = list[i].url;
370 }
371 break;
372 }
373 var fullname = list[i].fullname;
56a7bf68 374
840912d5
DC
375 // add green tick to main file
376 //if (ismainfile) {
377 //fullname = "<strong>"+list[i].fullname+"</strong> <img src='"+M.cfg.wwwroot+"/pix/i/tick_green_small.gif"+"' />";
378 //}
56a7bf68 379
840912d5 380 html = html.replace('___fullname___', '<a href="'+url+'" id="'+fileid+'"><img src="'+list[i].icon+'" /> ' + fullname + '</a>');
1345cc53 381 html = html.replace('___action___', '<a href="###" id="'+action+'"><img alt="â–¶" src="'+M.cfg.wwwroot+'/pix/t/expanded.png'+'" /></a>');
840912d5
DC
382 html = '<li id="'+htmlid+'">'+html+'</li>';
383 listhtml += html;
384 }
4c508047
PS
385 if (!Y.one('#draftfiles-'+this.client_id)) {
386 var filelist = Y.Node.create('<ul id="draftfiles-'+this.client_id+'"></ul>');
840912d5 387 container.appendChild(filelist);
56a7bf68 388 }
4c508047 389 Y.one('#draftfiles-'+this.client_id).set('innerHTML', listhtml);
840912d5
DC
390
391 // click normal file menu
392 Y.on('click', this.create_filemenu, file_ids, this, file_data);
393 // click folder menu
394 Y.on('click', this.create_foldermenu, folder_ids, this, folder_data);
395 // click archievs menu
396 Y.on('click', this.create_zipmenu, zip_ids, this, zip_data);
397 // when mouse moveover every menu
398 function mouseover_menu(e, obj) {
399 var node = e.currentTarget;
400 node.setStyle('backgroundColor', '#0066EE');
401 var menu = Y.one('#'+obj[node.get('id')]);
402 menu.setStyle('display', 'inline');
403 }
404 function mouseout_menu(e, obj) {
405 var node = e.currentTarget;
406 node.setStyle('backgroundColor', 'transparent');
407 var menu = Y.one('#'+obj[node.get('id')]);
408 menu.setStyle('display', 'none');
409 }
1345cc53
DC
410 //Y.on('mouseover', mouseover_menu, html_ids, this, html_data);
411 //Y.on('mouseout', mouseout_menu, html_ids, this, html_data);
840912d5
DC
412 // click folder name
413 Y.on('click', this.enter_folder, foldername_ids, this, folder_data);
414 },
415 enter_folder: function(e, data) {
416 var node = e.currentTarget;
417 var file = data[node.get('id')];
418 this.refresh(file.filepath);
419 },
420 create_filemenu: function(e, data) {
f45dfeeb 421 var options = this.options;
840912d5
DC
422 var node = e.currentTarget;
423 var file = data[node.get('id')];
424
425 var menuitems = [
2b728cb5 426 {text: M.str.moodle.download, url:file.url}
840912d5 427 ];
f45dfeeb
DC
428 function setmainfile(type, ev, obj) {
429 var file = obj[node.get('id')];
430 Y.one(mainid).set('value', file.filepath+file.filename);
431 }
432 if (this.mainfile) {
433 var mainid = '#id_'+this.mainfile;
434 var menu = {text: M.str.repository.setmainfile, onclick:{fn: setmainfile, obj:data, scope:this}};
435 menuitems.push(menu);
436 }
840912d5
DC
437 this.create_menu(e, 'filemenu', menuitems, file, data);
438 },
439 create_foldermenu: function(e, data) {
440 var scope = this;
441 var node = e.currentTarget;
442 var fileinfo = data[node.get('id')];
443 // an extra menu item for folder to zip it
444 function archive_folder(type,ev,obj) {
445 var params = {};
446 params['filepath'] = fileinfo.filepath;
447 params['filename'] = '.';
448 this.request({
449 action: 'zip',
450 scope: scope,
451 params: params,
452 callback: function(id, obj, args) {
453 scope.refresh(obj.filepath);
56a7bf68 454 }
840912d5 455 });
56a7bf68 456 }
840912d5 457 var menuitems = [
2b728cb5 458 {text: M.str.editor.zip, onclick: {fn: archive_folder, obj: data, scope: this}},
840912d5
DC
459 ];
460 this.create_menu(e, 'foldermenu', menuitems, fileinfo, data);
461 },
462 create_zipmenu: function(e, data) {
463 var scope = this;
464 var node = e.currentTarget;
465 var fileinfo = data[node.get('id')];
466
467 function unzip(type, ev, obj) {
468 var params = {};
469 params['filepath'] = fileinfo.filepath;
470 params['filename'] = fileinfo.fullname;
471 this.request({
472 action: 'unzip',
473 scope: scope,
474 params: params,
475 callback: function(id, obj, args) {
476 scope.refresh(obj.filepath);
477 }
478 });
56a7bf68 479 }
840912d5 480 var menuitems = [
2b728cb5
PS
481 {text: M.str.moodle.download, url:fileinfo.url},
482 {text: M.str.moodle.unzip, onclick: {fn: unzip, obj: data, scope: this}}
840912d5
DC
483 ];
484 this.create_menu(e, 'zipmenu', menuitems, fileinfo, data);
485 },
486 create_menu: function(ev, menuid, menuitems, fileinfo, options) {
487 var position = [ev.pageX, ev.pageY];
488 var scope = this;
489 function remove(type, ev, obj) {
490 var dialog_options = {};
491 var params = {};
2b728cb5 492 dialog_options.message = M.str.repository.confirmdeletefile;
840912d5
DC
493 dialog_options.scope = this;
494 var filename = '';
495 var filepath = '';
496 if (fileinfo.type == 'folder') {
497 params.filename = '.';
9a6606b8 498 params.filepath = fileinfo.filepath;
840912d5
DC
499 } else {
500 params.filename = fileinfo.fullname;
501 }
20fb563e
PS
502 dialog_options.callbackargs = [params];
503 dialog_options.callback = function(params) {
840912d5
DC
504 this.request({
505 action: 'delete',
20fb563e 506 scope: this,
840912d5
DC
507 params: params,
508 callback: function(id, obj, args) {
554cd8fc 509 scope.filecount--;
840912d5 510 scope.refresh(obj.filepath);
554cd8fc
DC
511 if (scope.filecount < scope.maxfiles && scope.maxfiles!=-1) {
512 var button_addfile = Y.one("#btnadd-"+scope.client_id);
513 button_addfile.setStyle('display', 'inline');
26032c2d
DC
514 button_addfile.on('click', function(e) {
515 var options = scope.filepicker_options;
516 options.formcallback = scope.filepicker_callback;
517 // XXX: magic here, to let filepicker use filemanager scope
518 options.magicscope = scope;
519 options.savepath = scope.currentpath;
26032c2d
DC
520 M.core_filepicker.show(Y, options);
521 }, this);
554cd8fc 522 }
56a7bf68 523 }
840912d5 524 });
56a7bf68 525 }
20fb563e 526 M.util.show_confirm_dialog(ev, dialog_options);
56a7bf68 527 }
840912d5
DC
528 function rename (type, ev, obj) {
529 var scope = this;
530 var perform = function(e) {
531 var newfilename = Y.one('#fm-rename-input').get('value');
532 if (!newfilename) {
533 return;
534 }
56a7bf68 535
840912d5
DC
536 var action = '';
537 var params = {};
538 if (fileinfo.type == 'folder') {
539 params['filepath'] = fileinfo.filepath;
540 params['filename'] = '.';
541 action = 'renamedir';
542 } else {
543 params['filepath'] = fileinfo.filepath;
544 params['filename'] = fileinfo.fullname;
545 action = 'rename';
546 }
547 params['newfilename'] = newfilename;
548 scope.request({
549 action: action,
550 scope: scope,
551 params: params,
552 callback: function(id, obj, args) {
553 scope.refresh(obj.filepath);
554 Y.one('#fm-rename-input').set('value', '');
555 scope.rename_dialog.hide();
556 }
557 });
558 }
56a7bf68 559
840912d5
DC
560 var dialog = Y.one('#fm-rename-dlg');
561 if (!dialog) {
2b728cb5 562 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>');
840912d5
DC
563 Y.one(document.body).appendChild(dialog);
564 this.rename_dialog = new YAHOO.widget.Dialog("fm-rename-dlg", {
565 width: "300px",
566 fixedcenter: true,
567 visible: true,
568 constraintoviewport : true
569 });
56a7bf68 570
840912d5 571 }
2b728cb5
PS
572 var buttons = [ { text:M.str.moodle.rename, handler:perform, isDefault:true},
573 { text:M.str.moodle.cancel, handler:function(){this.cancel();}}];
840912d5
DC
574
575 this.rename_dialog.cfg.queueProperty('buttons', buttons);
576 this.rename_dialog.render();
577 this.rename_dialog.show();
578 //var k1 = new YAHOO.util.KeyListener(scope, {keys:13}, {fn:function(){perform();}, correctScope: true});
579 //k1.enable();
580 Y.one('#fm-rename-input').set('value', fileinfo.fullname);
56a7bf68 581 }
840912d5
DC
582 function move(type, ev, obj) {
583 var scope = this;
584 var itemid = this.options.itemid;
585 // setup move file dialog
586 var dialog = null;
587 if (!Y.one('#fm-move-dlg')) {
588 dialog = Y.Node.create('<div id="fm-move-dlg"></div>');
589 Y.one(document.body).appendChild(dialog);
590 } else {
591 dialog = Y.one('#fm-move-dlg');
592 }
56a7bf68 593
2b728cb5 594 dialog.set('innerHTML', '<div class="hd">Moving</div><div class="bd"><div id="fm-move-div">'+M.str.repository.nopathselected+'</div><div id="fm-tree"></div></div>');
56a7bf68 595
840912d5
DC
596 this.movefile_dialog = new YAHOO.widget.Dialog("fm-move-dlg", {
597 width : "600px",
598 fixedcenter : true,
599 visible : false,
600 constraintoviewport : true
601 });
494bf5c8 602
840912d5 603 var treeview = new YAHOO.widget.TreeView("fm-tree");
494bf5c8 604
59eeb81b 605 var dialog = this.movefile_dialog;
840912d5
DC
606 function _move(e) {
607 if (!treeview.targetpath) {
608 return;
609 }
610 var params = {};
611 if (fileinfo.type == 'folder') {
612 alert('Moving folder is not supported yet');
613 action = 'movedir';
614 return;
615 } else {
616 action = 'movefile';
617 }
618 params['filepath'] = fileinfo.filepath;
619 params['filename'] = fileinfo.fullname;
620 params['newfilepath'] = treeview.targetpath;
621 scope.request({
622 action: action,
623 scope: scope,
624 params: params,
625 callback: function(id, obj, args) {
626 var p = '/';
627 if (obj) {
59eeb81b 628 p = obj.filepath;
840912d5 629 }
59eeb81b
DC
630 dialog.cancel();
631 scope.refresh(p);
840912d5
DC
632 }
633 });
56a7bf68 634 }
56a7bf68 635
2b728cb5
PS
636 var buttons = [ { text:M.str.moodle.move, handler:_move, isDefault:true },
637 { text:M.str.moodle.cancel, handler:function(){this.cancel();}}];
840912d5
DC
638
639 this.movefile_dialog.cfg.queueProperty("buttons", buttons);
640 this.movefile_dialog.render();
641
642 treeview.subscribe("dblClickEvent", function(e) {
643 // update destidatoin folder
644 this.targetpath = e.node.data.path;
645 var title = Y.one('#fm-move-div');
646 title.set('innerHTML', '<strong>"' + this.targetpath + '"</strong> has been selected.');
647 });
648
649 function loadDataForNode(node, onCompleteCallback) {
650 var params = {};
651 params['filepath'] = node.data.path;
652 var obj = {
653 action: 'dir',
654 scope: scope,
655 params: params,
656 callback: function(id, obj, args) {
657 data = obj.children;
658 if (data.length == 0) {
659 // so it is empty
660 } else {
661 for (var i in data) {
662 var textnode = {label: data[i].fullname, path: data[i].filepath, itemid: this.itemid};
663 var tmpNode = new YAHOO.widget.TextNode(textnode, node, false);
664 }
665 }
666 this.oncomplete();
667 }
668 };
669 obj.oncomplete = onCompleteCallback;
670 scope.request(obj);
671 }
56a7bf68 672
840912d5
DC
673 this.movefile_dialog.subscribe('show', function(){
674 var rootNode = treeview.getRoot();
675 treeview.setDynamicLoad(loadDataForNode);
676 treeview.removeChildren(rootNode);
677 var textnode = {label: "Files", path: '/'};
678 var tmpNode = new YAHOO.widget.TextNode(textnode, rootNode, true);
679 treeview.draw();
680 }, this, true);
56a7bf68 681
840912d5
DC
682 this.movefile_dialog.show();
683 }
59eeb81b
DC
684 if (fileinfo.type!='folder') {
685 var shared_items = [
686 {text: M.str.moodle.rename+'...', onclick: {fn: rename, obj: options, scope: this}},
687 {text: M.str.moodle.move+'...', onclick: {fn: move, obj: options, scope: this}}
688 ];
689 } else {
690 var shared_items = [];
691 }
692 // delete is reserve word in Javascript
693 shared_items.push({text: M.str.moodle['delete']+'...', onclick: {fn: remove, obj: options, scope: this}});
840912d5
DC
694 var menu = new YAHOO.widget.Menu(menuid, {xy:position, clicktohide:true});
695 menu.clearContent();
696 menu.addItems(menuitems);
697 menu.addItems(shared_items);
698 menu.render(document.body);
699 menu.subscribe('hide', function(){
700 this.fireEvent('destroy');
701 });
702 menu.show();
703 }
704 });
4c508047
PS
705
706 // finally init everything needed
707
708 var item = document.getElementById('nonjs-filemanager-'+options.client_id);
709 if (item) {
710 item.parentNode.removeChild(item);
711 }
712 item = document.getElementById('filemanager-wrapper-'+options.client_id);
713 if (item) {
714 item.style.display = '';
715 }
716
717 new FileManagerHelper(options);
718};