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