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