webservice MDL-21351 add an overview page to facilitate the web service administration
[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/>.
15
16/**
17 * This file contains javascript code used to manage files in draft area
18 *
19 * @since 2.0
20 * @package filemanager
21 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
56a7bf68 25/**
494bf5c8 26 * Namespaces used by filemanager
56a7bf68 27 */
494bf5c8 28YAHOO.namespace('moodle.filemanager');
56a7bf68 29
494bf5c8
DC
30// three dialog box we will used later
31YAHOO.moodle.filemanager.movefile_dialog = null;
32YAHOO.moodle.filemanager.rename_dialog = null;
33YAHOO.moodle.filemanager.mkdir_dialog = null;
34
35
36// an object used to record filemanager instances' data,
37// we use it quite often
56a7bf68 38var fm_cfg = {};
9598d578 39fm_cfg.api = M.cfg.wwwroot + '/files/files_ajax.php';
56a7bf68 40
41// initialize file manager
42var filemanager = (function(){
43 function _filemanager() {
44 this.init = function(client_id, options) {
45 this.client_id = client_id;
494bf5c8
DC
46
47 // setup move file dialog
48 var dialog = null;
49 if (!YAHOO.moodle.filemanager.movefile_dialog) {
50 dialog = document.createElement('DIV');
51 dialog.id = 'fm-move-dlg';
52 document.body.appendChild(dialog);
53 YAHOO.moodle.filemanager.movefile_dialog = new YAHOO.widget.Dialog("fm-move-dlg", {
54 width : "600px",
55 fixedcenter : true,
56 visible : false,
57 constraintoviewport : true
58 });
59 } else {
60 dialog = document.getElementById('fm-move-div');
61 }
62
63 dialog.innerHTML = '<div class="hd"></div><div class="bd"><div id="fm-move-div">'+mstr.repository.nopathselected+'</div><div id="fm-tree"></div></div>';
64
65 YAHOO.moodle.filemanager.movefile_dialog.render();
66 // generate filemanager html
56a7bf68 67 html_compiler(client_id, options);
1420162a 68 }
69 }
56a7bf68 70 return _filemanager;
71})();
72
56a7bf68 73
494bf5c8
DC
74/**
75 * This function will be called by filepicker once it got the file successfully
76 */
77function filemanager_callback(params) {
78 var client_id = params.client_id;
79 fm_refresh(params.filepath, fm_cfg[client_id]);
80 fm_cfg[client_id].currentfiles++;
494bf5c8
DC
81
82 if (fm_cfg[client_id].currentfiles>=fm_cfg[client_id].maxfiles
83 && fm_cfg[client_id].maxfiles!=-1) {
84 var addfilebutton = document.getElementById('btnadd-'+client_id);
85 if (addfilebutton) {
86 addfilebutton.style.display = 'none';
87 }
71588976 88 }
1420162a 89}
56a7bf68 90
99eaca9d
DC
91var fm_filepickers = {};
92function fp_filepicker_callback(params) {
93 var html = '<a href="'+params['file']+'">'+params['title']+'</a>';
94 document.getElementById('file_info_'+params['client_id']).innerHTML = html;
95}
96
97function fm_init_filepicker(id, options) {
7b42e81a 98 YUI(M.yui.loader).use("filepicker", function (Y) {
99eaca9d
DC
99 options.formcallback = filemanager_callback;
100 if (!pickers[options.client_id]) {
101 fm_filepickers[options.client_id] = new Y.filepicker(options);
102 }
103 Y.one('#'+id).on('click', function(e, client_id) {
104 fm_filepickers[options.client_id].show();
105 }, this, options.client_id);
106 });
107}
494bf5c8
DC
108/**
109 * Setup options to launch file picker.
110 * Fired by add file button.
111 */
112function fm_launch_filepicker(target, options) {
1420162a 113 var picker = document.createElement('DIV');
56a7bf68 114 picker.id = 'file-picker-'+options.client_id;
1420162a 115 picker.className = 'file-picker';
116 document.body.appendChild(picker);
494bf5c8
DC
117
118 var target=document.getElementById(target);
1420162a 119 var params = {};
120 params.env = 'filemanager';
56a7bf68 121 params.itemid = options.itemid;
122 params.maxfiles = options.maxfiles;
123 params.maxbytes = options.maxbytes;
124 params.savepath = options.savepath;
494bf5c8
DC
125 params.target = target;
126 // setup filemanager callback
56a7bf68 127 params.callback = filemanager_callback;
128 var fp = open_filepicker(options.client_id, params);
1420162a 129 return false;
71588976 130}
56a7bf68 131
494bf5c8
DC
132/**
133 * This function will create a dialog of creating new folder
134 * Fired by 'make a folder' button
135 */
136function fm_create_folder(e, client_id, itemid) {
137 // deal with ajax response
138 var mkdir_ajax_callback = {
56a7bf68 139 success: function(o) {
140 var result = json_decode(o.responseText);
494bf5c8
DC
141 YAHOO.moodle.filemanager.mkdir_dialog.hide();
142 fm_refresh(result.filepath, fm_cfg[client_id]);
56a7bf68 143 }
144 }
494bf5c8
DC
145 // a function used to perform an ajax request
146 var perform_ajax_mkdir = function(e) {
56a7bf68 147 var foldername = document.getElementById('fm-newname').value;
148 if (!foldername) {
149 return;
150 }
151 var params = [];
152 params['itemid'] = itemid;
153 params['newdirname'] = foldername;
9598d578 154 params['sesskey'] = M.cfg.sesskey;
56a7bf68 155 params['filepath'] = fm_cfg[client_id].currentpath;
156 var trans = YAHOO.util.Connect.asyncRequest('POST',
494bf5c8 157 fm_cfg.api+'?action=mkdir', mkdir_ajax_callback, build_querystring(params));
56a7bf68 158 YAHOO.util.Event.preventDefault(e);
159 }
494bf5c8 160 // create dialog html element
56a7bf68 161 if (!document.getElementById('fm-mkdir-dlg')) {
162 var el = document.createElement('DIV');
163 el.id = 'fm-mkdir-dlg';
164 el.innerHTML = '<div class="hd">'+mstr.repository.entername+'</div><div class="bd"><input type="text" id="fm-newname" /></div>';
165 document.body.appendChild(el);
4bb7f787 166 var x = YAHOO.util.Event.getPageX(e);
167 var y = YAHOO.util.Event.getPageY(e);
494bf5c8 168 YAHOO.moodle.filemanager.mkdir_dialog = new YAHOO.widget.Dialog("fm-mkdir-dlg", {
56a7bf68 169 width: "300px",
56a7bf68 170 visible: true,
4bb7f787 171 x:y,
172 y:y,
56a7bf68 173 constraintoviewport : true
174 });
175
176 }
494bf5c8 177 var buttons = [ { text:mstr.moodle.ok, handler:perform_ajax_mkdir, isDefault:true },
56a7bf68 178 { text:mstr.moodle.cancel, handler:function(){this.cancel();}}];
179
494bf5c8
DC
180 YAHOO.moodle.filemanager.mkdir_dialog.cfg.queueProperty("buttons", buttons);
181 YAHOO.moodle.filemanager.mkdir_dialog.render();
182 YAHOO.moodle.filemanager.mkdir_dialog.show();
56a7bf68 183
494bf5c8
DC
184 // presss 'enter' key to perform ajax request
185 var k1 = new YAHOO.util.KeyListener(document.getElementById('fm-mkdir-dlg'), {keys:13}, {fn:function(){perform_ajax_mkdir();}, correctScope: true});
56a7bf68 186 k1.enable();
187
188 document.getElementById('fm-newname').value = '';
189}
190
191// generate html
192function html_compiler(client_id, options) {
193 var list = options.list;
194 var breadcrumb = document.getElementById('fm-path-'+client_id);
195 var count = 0;
494bf5c8 196 // build breadcrumb
56a7bf68 197 if (options.path) {
494bf5c8 198 // empty breadcrumb
56a7bf68 199 breadcrumb.innerHTML = '';
200 var count = 0;
201 for(var p in options.path) {
4bb7f787 202 var sep = document.createElement('SPAN');
203 sep.innerHTML = ' â–¶ ';
204 if (count==0) {
22f6b8df 205 sep.innerHTML = mstr.moodle.path + ': ';
4bb7f787 206 } else {
207 sep.innerHTML = ' â–¶ ';
208 }
56a7bf68 209 count++;
494bf5c8 210
56a7bf68 211 var pathid = 'fm-path-node-'+client_id;
212 pathid += ('-'+count);
213
494bf5c8
DC
214 var pathnode = document.createElement('A');
215 pathnode.id = pathid;
216 pathnode.innerHTML = options.path[p].name;
217 pathnode.href = '###';
56a7bf68 218 breadcrumb.appendChild(sep);
494bf5c8 219 breadcrumb.appendChild(pathnode);
56a7bf68 220
221 var args = {};
222 args.itemid = options.itemid;
223 args.requestpath = options.path[p].path;
224 args.client_id = client_id;
225
494bf5c8 226 YAHOO.util.Event.addListener(pathid, 'click', fm_click_breadcrumb, args);
56a7bf68 227 }
228 }
494bf5c8 229 var template = document.getElementById('fm-template');
56a7bf68 230 var container = document.getElementById('filemanager-' + client_id);
494bf5c8 231 var listhtml = '';
56a7bf68 232
494bf5c8 233 // folder list items
56a7bf68 234 var folder_ids = [];
494bf5c8 235 var folder_data = {};
56a7bf68 236
494bf5c8 237 // normal file list items
56a7bf68 238 var file_ids = [];
239 var file_data = {};
494bf5c8
DC
240
241 // archives list items
242 var zip_ids = [];
243 var zip_data = {};
244
56a7bf68 245 var html_ids = [];
246 var html_data = {};
494bf5c8 247
56a7bf68 248 file_data.itemid = folder_data.itemid = zip_data.itemid = options.itemid;
249 file_data.client_id = folder_data.client_id = zip_data.client_id = options.client_id;
250
56a7bf68 251 var foldername_ids = [];
252 if (list.length == 0) {
253 // hide file browser and breadcrumb
254 container.style.display='none';
255 if (options.path.length <= 1) {
6f5e0852 256 breadcrumb.style.display='none';
56a7bf68 257 }
258 return;
259 } else {
260 container.style.display='block';
6f5e0852 261 breadcrumb.style.display='block';
56a7bf68 262 }
494bf5c8
DC
263
264 var count = 0;
56a7bf68 265 for(var i in list) {
266 count++;
494bf5c8 267 // the li html element
56a7bf68 268 var htmlid = 'fileitem-'+client_id+'-'+count;
494bf5c8 269 // link to file
56a7bf68 270 var fileid = 'filename-'+client_id+'-'+count;
494bf5c8 271 // file menu
56a7bf68 272 var action = 'action-' +client_id+'-'+count;
494bf5c8 273
56a7bf68 274 var html = template.innerHTML;
275
276 html_ids.push(htmlid);
277 html_data[htmlid] = action;
278
279 list[i].htmlid = htmlid;
280 list[i].fileid = fileid;
281 list[i].action = action;
494bf5c8 282
56a7bf68 283 var url = "###";
494bf5c8 284 // check main file
56a7bf68 285 var ismainfile = false;
bc6f7235 286 if (fm_cfg[client_id].mainfilename && (fm_cfg[client_id].mainfilename.toLowerCase() == list[i].fullname.toLowerCase())) {
56a7bf68 287 ismainfile = true;
288 }
494bf5c8 289
56a7bf68 290 switch (list[i].type) {
291 case 'folder':
292 foldername_ids.push(fileid);
293 folder_ids.push(action);
294 folder_data[action] = list[i];
295 folder_data[fileid] = list[i];
296 break;
297 case 'file':
298 file_ids.push(action);
299 file_data[action] = list[i];
300 if (list[i].url) {
301 url = list[i].url;
302 }
303 break;
304 case 'zip':
305 zip_ids.push(action);
306 zip_data[action] = list[i];
307 if (list[i].url) {
308 url = list[i].url;
309 }
310 break;
311 }
6f5e0852 312 var fullname = list[i].fullname;
494bf5c8
DC
313
314 // add green tick to main file
56a7bf68 315 if (ismainfile) {
9598d578 316 fullname = "<strong>"+list[i].fullname+"</strong> <img src='"+M.cfg.wwwroot+"/pix/i/tick_green_small.gif"+"' />";
56a7bf68 317 }
494bf5c8 318
56a7bf68 319 html = html.replace('___fullname___', '<a href="'+url+'" id="'+fileid+'"><img src="'+list[i].icon+'" /> ' + fullname + '</a>');
9598d578 320 html = html.replace('___action___', '<a style="display:none" href="###" id="'+action+'"><img alt="â–¶" src="'+M.cfg.wwwroot+'/pix/i/settings.gif'+'" /></a>');
56a7bf68 321 html = '<li id="'+htmlid+'">'+html+'</li>';
322 listhtml += html;
323 }
56a7bf68 324
494bf5c8 325 container.innerHTML = '<ul id="draftfiles-'+client_id+'">' + listhtml + '</ul>';
56a7bf68 326
494bf5c8
DC
327 // click normal file menu
328 YAHOO.util.Event.addListener(file_ids, 'click', fm_create_filemenu, file_data);
329 // click folder menu
330 YAHOO.util.Event.addListener(folder_ids, 'click', fm_create_foldermenu, folder_data);
331 // click archievs menu
332 YAHOO.util.Event.addListener(zip_ids, 'click', fm_create_zipmenu, zip_data);
333 // when mouse moveover every menu
56a7bf68 334 YAHOO.util.Event.addListener(html_ids, 'mouseover', fm_mouseover_menu, html_data);
335 YAHOO.util.Event.addListener(html_ids, 'mouseout', fm_mouseout_menu, html_data);
494bf5c8
DC
336 // click folder name
337 YAHOO.util.Event.addListener(foldername_ids,'click', fm_click_folder, folder_data);
56a7bf68 338}
339
494bf5c8 340function fm_refresh(path, args) {
56a7bf68 341 var params = [];
342 params['itemid'] = args.itemid;
494bf5c8 343 params['filepath'] = path;
9598d578 344 params['sesskey'] = M.cfg.sesskey;
56a7bf68 345 this.cb = {
346 success: function(o) {
347 var data = json_decode(o.responseText);
348 for(var key in data) {
349 this.options[key] = data[key];
350 }
351 html_compiler(this.client_id, this.options);
352 }
353 }
354 this.cb.options = args;
355 this.cb.client_id = args.client_id;
356
494bf5c8 357 fm_cfg[args.client_id].currentpath = params['filepath'];
56a7bf68 358 fm_loading('filemanager-'+args.client_id, 'fm-prgressbar');
359 var trans = YAHOO.util.Connect.asyncRequest('POST',
494bf5c8 360 fm_cfg.api+'?action=list', this.cb, build_querystring(params));
56a7bf68 361}
362
494bf5c8
DC
363// display menu when mouse over
364function fm_mouseover_menu(ev, args) {
365 this.style.backgroundColor = '#0066EE';
366 var menu = args[this.id];
367 menu = document.getElementById(menu);
368 menu.style.display = 'inline';
369}
370
371// hide menu when mouse over
372function fm_mouseout_menu(ev, args) {
373 this.style.backgroundColor = 'transparent';
374 var menu = args[this.id];
375 menu = document.getElementById(menu);
376 menu.style.display = 'none';
56a7bf68 377}
378
494bf5c8 379function fm_click_breadcrumb(ev, args) {
56a7bf68 380 var params = [];
381 params['itemid'] = args.itemid;
9598d578 382 params['sesskey'] = M.cfg.sesskey;
494bf5c8 383 params['filepath'] = args.requestpath;
56a7bf68 384 this.cb = {
385 success: function(o) {
386 var data = json_decode(o.responseText);
387 for(var key in data) {
388 this.options[key] = data[key];
389 }
390 html_compiler(this.client_id, this.options);
391 }
392 }
393 this.cb.options = args;
394 this.cb.client_id = args.client_id;
395
494bf5c8 396 fm_cfg[args.client_id].currentpath = args.requestpath;
56a7bf68 397 fm_loading('filemanager-'+args.client_id, 'fm-prgressbar');
398 var trans = YAHOO.util.Connect.asyncRequest('POST',
494bf5c8 399 fm_cfg.api+'?action=list', this.cb, build_querystring(params));
56a7bf68 400}
401
494bf5c8
DC
402function fm_click_folder(ev, args) {
403 var file = args[this.id];
404 fm_refresh(file.filepath, args);
405}
406
407function fm_create_foldermenu(e, data) {
56a7bf68 408 var file = data[this.id];
494bf5c8 409 // an extra menu item for folder to zip it
56a7bf68 410 this.zip = function(type, ev, obj) {
411 this.cb = {
412 success: function(o) {
413 var result = json_decode(o.responseText);
414 if (result) {
494bf5c8 415 fm_refresh(result.filepath, fm_cfg[this.client_id]);
56a7bf68 416 }
417 }
418 }
419 this.cb.client_id = obj.client_id;
420 this.cb.file = this.file;
421 var params = [];
422 params['itemid'] = obj.itemid;
423 params['filepath'] = this.file.filepath;
424 params['filename'] = '.';
9598d578 425 params['sesskey'] = M.cfg.sesskey;
56a7bf68 426 fm_loading('filemanager-'+obj.client_id, 'fm-prgressbar');
427 var trans = YAHOO.util.Connect.asyncRequest('POST',
494bf5c8 428 fm_cfg.api+'?action=zip', this.cb, build_querystring(params));
56a7bf68 429 }
430 this.zip.file = file;
431 var menuitems = [
432 {text: mstr.editor.zip, onclick: {fn: this.zip, obj: data, scope: this.zip}},
433 ];
494bf5c8 434 fm_create_menu(e, 'foldermenu', menuitems, file, data);
56a7bf68 435}
436
494bf5c8 437function fm_create_filemenu(e, data) {
56a7bf68 438 var file = data[this.id];
439
440 var menuitems = [
441 {text: mstr.moodle.download, url:file.url}
442 ];
494bf5c8 443 fm_create_menu(e, 'filemenu', menuitems, file, data);
56a7bf68 444}
445
494bf5c8 446function fm_create_zipmenu(e, data) {
56a7bf68 447 var file = data[this.id];
448 this.unzip = function(type, ev, obj) {
449 this.cb = {
450 success:function(o) {
451 var result = json_decode(o.responseText);
452 if (result) {
494bf5c8 453 fm_refresh(result.filepath, fm_cfg[this.client_id]);
56a7bf68 454 }
455 }
456 }
457 this.cb.client_id = obj.client_id;
458 var params = [];
459 params['itemid'] = obj.itemid;
460 params['filepath'] = this.file.filepath;
461 params['filename'] = this.file.fullname;
9598d578 462 params['sesskey'] = M.cfg.sesskey;
56a7bf68 463 fm_loading('filemanager-'+obj.client_id, 'fm-prgressbar');
464 var trans = YAHOO.util.Connect.asyncRequest('POST',
494bf5c8 465 fm_cfg.api+'?action=unzip', this.cb, build_querystring(params));
56a7bf68 466 }
467 this.unzip.file = file;
468
469 var menuitems = [
470 {text: mstr.moodle.download, url:file.url},
471 {text: mstr.moodle.unzip, onclick: {fn: this.unzip, obj: data, scope: this.unzip}}
472 ];
494bf5c8 473 fm_create_menu(e, 'zipmenu', menuitems, file, data);
56a7bf68 474}
475
494bf5c8 476function fm_create_menu(ev, menuid, menuitems, file, options) {
56a7bf68 477 var position = YAHOO.util.Event.getXY(ev);
478 var el = document.getElementById(menuid);
479 var menu = new YAHOO.widget.Menu(menuid, {xy:position});
480
481 this.remove = function(type, ev, obj) {
482 var args = {};
483 args.message = mstr.repository.confirmdeletefile;
484 args.callback = function() {
485 var params = {};
486 if (this.file.type == 'folder') {
487 params['filename'] = '.';
488 params['filepath'] = this.file.fullname;
489 } else {
490 params['filename'] = this.file.fullname;
491 params['filepath'] = fm_cfg[this.client_id].currentpath;
492 }
493 params['itemid'] = this.itemid;
9598d578 494 params['sesskey'] = M.cfg.sesskey;
56a7bf68 495 fm_loading('filemanager-'+this.client_id, 'fm-prgressbar');
496 var trans = YAHOO.util.Connect.asyncRequest('POST',
494bf5c8 497 fm_cfg.api+'?action=delete', this.cb, build_querystring(params));
56a7bf68 498 }
499 var dlg = confirm_dialog(ev, args);
500 dlg.file = file;
501 dlg.client_id = obj.client_id;
502 dlg.itemid = obj.itemid;
503 dlg.cb = {
504 success: function(o) {
505 var result = json_decode(o.responseText);
506 if (!result) {
507 alert(mstr.error.cannotdeletefile);
508 }
509 fm_cfg[this.client_id].currentfiles--;
510 if (fm_cfg[this.client_id].currentfiles<fm_cfg[this.client_id].maxfiles) {
511 var btn = document.getElementById('btnadd-'+this.client_id);
512 btn.style.display = 'inline';
494bf5c8
DC
513 btn.onclick = function(e) {
514 this.options.savepath = this.options.currentpath;
515 fm_launch_filepicker(this.options.target, this.options);
516 }
517 btn.options = fm_cfg[this.client_id];
56a7bf68 518 }
494bf5c8 519 fm_refresh(result.filepath, fm_cfg[this.client_id]);
56a7bf68 520 }
521 }
522 dlg.cb.file = this.file;
523 dlg.cb.client_id = obj.client_id;
524 }
525 this.remove.file = file;
526
527 this.rename = function(type, ev, obj) {
528 var file = this.file;
529 var rename_cb = {
530 success: function(o) {
531 var result = json_decode(o.responseText);
532 if (result) {
533 var el = document.getElementById(file.fileid);
534 el.innerHTML = this.newfilename;
535 // update filename
536 file.fullname = this.newfilename;
537 file.filepath = result.filepath;
494bf5c8 538 YAHOO.moodle.filemanager.rename_dialog.hide();
56a7bf68 539 }
540 }
541 }
542 var perform = function(e) {
543 var newfilename = document.getElementById('fm-rename-input').value;
544 if (!newfilename) {
545 return;
546 }
547
548 var action = '';
549 var params = [];
550 params['itemid'] = obj.itemid;
551 if (file.type == 'folder') {
552 params['filepath'] = file.filepath;
553 params['filename'] = '.';
554 action = 'renamedir';
555 } else {
556 params['filepath'] = file.filepath;
557 params['filename'] = file.fullname;
558 action = 'rename';
559 }
560 params['newfilename'] = newfilename;
561
9598d578 562 params['sesskey'] = M.cfg.sesskey;
56a7bf68 563 rename_cb.newfilename = newfilename;
564 var trans = YAHOO.util.Connect.asyncRequest('POST',
494bf5c8 565 fm_cfg.api+'?action='+action, rename_cb, build_querystring(params));
56a7bf68 566 }
567
568 var scope = document.getElementById('fm-rename-dlg');
569 if (!scope) {
570 var el = document.createElement('DIV');
571 el.id = 'fm-rename-dlg';
572 el.innerHTML = '<div class="hd">'+mstr.repository.enternewname+'</div><div class="bd"><input type="text" id="fm-rename-input" /></div>';
573 document.body.appendChild(el);
494bf5c8 574 YAHOO.moodle.filemanager.rename_dialog = new YAHOO.widget.Dialog("fm-rename-dlg", {
56a7bf68 575 width: "300px",
576 fixedcenter: true,
577 visible: true,
578 constraintoviewport : true
579 });
580
581 }
582 var buttons = [ { text:mstr.moodle.rename, handler:perform, isDefault:true },
583 { text:mstr.moodle.cancel, handler:function(){this.cancel();}}];
584
494bf5c8
DC
585 YAHOO.moodle.filemanager.rename_dialog.cfg.queueProperty("buttons", buttons);
586 YAHOO.moodle.filemanager.rename_dialog.render();
587 YAHOO.moodle.filemanager.rename_dialog.show();
56a7bf68 588
589 var k1 = new YAHOO.util.KeyListener(scope, {keys:13}, {fn:function(){perform();}, correctScope: true});
590 k1.enable();
591
592 document.getElementById('fm-rename-input').value = file.fullname;
593 }
594 this.rename.file = file;
595
596 this.move = function(type, ev, obj) {
597 var tree = new YAHOO.widget.TreeView("fm-tree");
598 var file = this.file;
599
600 this.asyncMove = function(e) {
601 if (!tree.targetpath) {
602 return;
603 }
604 var cb = {
605 success : function(o) {
606 var result = json_decode(o.responseText);
607 var p = '/';
608 if (result) {
609 p = result.filepath;
610 }
494bf5c8 611 fm_refresh(result.filepath, fm_cfg[obj.client_id]);
56a7bf68 612 this.dlg.cancel();
613 }
614 }
615 cb.dlg = this;
616 var params = {};
617 if (file.type == 'folder') {
618 alert('Moving folder is not supported yet');
619 return;
620 action = 'movedir';
621 } else {
622 action = 'movefile';
623 }
624 params['filepath'] = file.filepath;
625 params['filename'] = file.fullname;
626 params['itemid'] = obj.itemid;
9598d578 627 params['sesskey'] = M.cfg.sesskey;
56a7bf68 628 params['newfilepath'] = tree.targetpath;
629 fm_loading('filemanager-'+obj.client_id, 'fm-prgressbar');
630 var trans = YAHOO.util.Connect.asyncRequest('POST',
494bf5c8 631 fm_cfg.api+'?action='+action, cb, build_querystring(params));
56a7bf68 632 }
633
634 var buttons = [ { text:mstr.moodle.move, handler:this.asyncMove, isDefault:true },
635 { text:mstr.moodle.cancel, handler:function(){this.cancel();}}];
636
494bf5c8 637 YAHOO.moodle.filemanager.movefile_dialog.cfg.queueProperty("buttons", buttons);
56a7bf68 638
639
640 tree.subscribe("dblClickEvent", function(e) {
641 // update destidatoin folder
642 this.targetpath = e.node.data.path;
643 var el = document.getElementById('fm-move-div');
644 el.innerHTML = '<strong>"' + this.targetpath + '"</strong> has been selected.';
645 YAHOO.util.Event.preventDefault(e);
646 });
647
648 this.loadDataForNode = function(node, onCompleteCallback) {
649 this.cb = {
650 success: function(o) {
651 var data = json_decode(o.responseText);
652 data = data.children;
653 if (data.length == 0) {
654 // so it is empty
655 } else {
656 for (var i in data) {
657 var textnode = {label: data[i].fullname, path: data[i].filepath, itemid: this.itemid};
658 var tmpNode = new YAHOO.widget.TextNode(textnode, node, false);
659 }
660 }
661 this.complete();
662 }
663 }
664 var params = {};
665 params['itemid'] = node.data.itemid;
666 params['filepath'] = node.data.path;
9598d578 667 params['sesskey'] = M.cfg.sesskey;
56a7bf68 668 var trans = YAHOO.util.Connect.asyncRequest('POST',
494bf5c8 669 fm_cfg.api+'?action=dir', this.cb, build_querystring(params));
56a7bf68 670 this.cb.complete = onCompleteCallback;
671 this.cb.itemid = node.data.itemid;
672 }
673 this.loadDataForNode.itemid = obj.itemid;
674
494bf5c8 675 YAHOO.moodle.filemanager.movefile_dialog.subscribe('show', function(){
56a7bf68 676
677 var el = document.getElementById('fm-move-div');
678 el.innerHTML = '<div class="hd"></div><div class="bd"><div id="fm-move-div">'+mstr.repository.nopathselected+'</div><div id="fm-tree"></div></div>';
679
680 var rootNode = tree.getRoot();
681 tree.setDynamicLoad(this.loadDataForNode);
6f5e0852 682 tree.removeChildren(rootNode);
56a7bf68 683 var textnode = {label: "Files", path: '/', itemid: obj.itemid};
684 var tmpNode = new YAHOO.widget.TextNode(textnode, rootNode, true);
685 tree.draw();
686
687 }, this, true);
688
494bf5c8
DC
689 YAHOO.moodle.filemanager.movefile_dialog.render();
690 YAHOO.moodle.filemanager.movefile_dialog.show();
56a7bf68 691 }
692 this.move.file = file;
693 var shared_items = [
694 {text: mstr.moodle.rename+'...', onclick: {fn: this.rename, obj: options, scope: this.rename}},
695 {text: mstr.moodle.move+'...', onclick: {fn: this.move, obj: options, scope: this.move}},
696 {text: mstr.moodle['delete']+'...', onclick: {fn: this.remove, obj: options, scope: this.remove}}
697 ];
698 menu.addItems(menuitems);
699 menu.addItems(shared_items);
700 if (fm_cfg[options.client_id].mainfile && (file.type!='folder')) {
701 this.set_mainfile = function(type, ev, obj) {
702 if (fm_cfg[obj.client_id].mainfile) {
bc6f7235 703 var mainfile = document.getElementById(fm_cfg[obj.client_id].mainfile+'-id');
56a7bf68 704 mainfile.value = this.file.filepath+this.file.fullname;
bc6f7235 705 document.getElementById(fm_cfg[obj.client_id].mainfile+'-label').innerHTML = mainfile.value;
56a7bf68 706 }
707 fm_cfg[obj.client_id].mainfilename = this.file.fullname;
494bf5c8 708 fm_refresh(fm_cfg[obj.client_id].currentpath, fm_cfg[obj.client_id]);
bc6f7235 709
56a7bf68 710 }
711 this.set_mainfile.file = file;
712 menu.addItem({text: mstr.resource.setmainfile, onclick: {fn: this.set_mainfile, obj: options, scope: this.set_mainfile}});
713 }
714 menu.render(document.body);
715 menu.show();
716 menu.subscribe('hide', function(){
717 this.destroy();
718 });
719}
720
494bf5c8
DC
721/**
722 * setup file manager options and initialize filemanger itself
723 */
724function launch_filemanager(client_id, options) {
725 // setup options & parameters
56a7bf68 726 if (!options) {
727 options = {};
728 }
bc6f7235 729 fm_cfg[client_id] = {};
730 fm_cfg[client_id] = options;
bc6f7235 731 fm_cfg[client_id].currentpath = '/';
494bf5c8
DC
732 if (fm_cfg[client_id].filecount) {
733 fm_cfg[client_id].currentfiles = fm_cfg[client_id].filecount;
734 } else {
735 fm_cfg[client_id].currentfiles = 0;
736 }
737 //
bc6f7235 738 if (options.mainfile) {
739 var mainfilename = document.getElementById(options.mainfile+'-id');
740 if (mainfilename.value) {
741 var re = new RegExp(".*\/(.*)$", "i");
742 var result = mainfilename.value.match(re);
743 document.getElementById(options.mainfile+'-label').innerHTML = mainfilename.value;
744 fm_cfg[client_id].mainfilename = result[1];
745 } else {
746 fm_cfg[client_id].mainfilename = '';
747 }
748 }
494bf5c8 749 // setup filemanager
bc6f7235 750 var fm = new filemanager();
751 fm.init(client_id, options);
494bf5c8
DC
752 // setup toolbar
753 fm_setup_buttons(client_id, options);
56a7bf68 754}
755
494bf5c8
DC
756/**
757 * Set up buttons
758 */
759function fm_setup_buttons(client_id, options) {
760 var button_download = document.getElementById("btndwn-"+client_id);
761 var button_create = document.getElementById("btncrt-"+client_id);
762 var button_addfile = document.getElementById("btnadd-"+client_id);
56a7bf68 763
494bf5c8 764 // setup 'add file' button
4bb7f787 765 // if maxfiles == -1, the no limit
494bf5c8
DC
766 if (fm_cfg[client_id].filecount >= fm_cfg[client_id].maxfiles
767 && fm_cfg[client_id].maxfiles!=-1) {
768 button_addfile.style.display = 'none';
4bb7f787 769 } else {
494bf5c8 770 button_addfile.onclick = function(e) {
4bb7f787 771 this.options.savepath = this.options.currentpath;
772 fm_launch_filepicker(this.options.target, this.options);
773 }
494bf5c8 774 button_addfile.options = fm_cfg[client_id];
56a7bf68 775 }
494bf5c8
DC
776
777 // setup 'make a folder' button
be4487f4 778 if (fm_cfg[client_id].subdirs) {
494bf5c8
DC
779 button_create.onclick = function(e) {
780 fm_create_folder(e, this.options.client_id, this.options.itemid);
be4487f4 781 }
494bf5c8 782 button_create.options = fm_cfg[client_id];
be4487f4 783 } else {
494bf5c8 784 button_create.style.display = 'none';
56a7bf68 785 }
494bf5c8
DC
786
787 // setup 'download this folder' button
788 // NOTE: popup window must be enabled to perform download process
789 button_download.onclick = function() {
790 var downloaddir_callback = {
56a7bf68 791 success:function(o) {
792 var result = json_decode(o.responseText);
494bf5c8 793 fm_refresh(result.filepath, fm_cfg[this.client_id]);
6f5e0852 794 var win = window.open(result.fileurl, 'fm-download-folder');
56a7bf68 795 if (!win) {
796 alert(mstr.repository.popupblockeddownload);
797 }
798 }
799 };
494bf5c8 800 downloaddir_callback.client_id = this.options.client_id;
56a7bf68 801 var params = [];
802 params['itemid'] = this.options.itemid;
9598d578 803 params['sesskey'] = M.cfg.sesskey;
56a7bf68 804 params['filepath'] = this.options.currentpath;
494bf5c8 805 // perform downloaddir ajax request
56a7bf68 806 var trans = YAHOO.util.Connect.asyncRequest('POST',
494bf5c8 807 fm_cfg.api+'?action=downloaddir', downloaddir_callback, build_querystring(params));
56a7bf68 808 }
494bf5c8 809 button_download.options = fm_cfg[client_id];
56a7bf68 810}
811
494bf5c8
DC
812/**
813 * Print a progress bar
814 */
56a7bf68 815function fm_loading(container, id) {
816
817 if (!document.getElementById(id)) {
818 var el = document.createElement('DIV');
819 el.id = id;
820 el.style.backgroundColor = "white";
821 var container = document.getElementById(container);
822 container.innerHTML = '';
823 container.appendChild(el);
824 }
825
826 var loading = new YAHOO.widget.Module(id, {visible:false});
9598d578 827 loading.setBody('<div style="text-align:center"><img alt="'+mstr.repository.loading+'" src="'+M.cfg.wwwroot+'/pix/i/progressbar.gif" /></div>');
56a7bf68 828 loading.render();
829 loading.show();
830
831 return loading;
832}