MDL-28666 Extends repository to support references
[moodle.git] / repository / filepicker.js
CommitLineData
99eaca9d
DC
1// YUI3 File Picker module for moodle
2// Author: Dongsheng Cai <dongsheng@moodle.com>
3
4/**
5 *
6 * File Picker UI
7 * =====
99eaca9d 8 * this.rendered, it tracks if YUI Panel rendered
99eaca9d 9 * this.api, stores the URL to make ajax request
99eaca9d
DC
10 * this.mainui, YUI Panel
11 * this.treeview, YUI Treeview
12 * this.viewbar, a button group to switch view mode
13 * this.viewmode, store current view mode
14 *
15 * Filepicker options:
16 * =====
99eaca9d
DC
17 * this.options.client_id, the instance id
18 * this.options.contextid
19 * this.options.itemid
20 * this.options.repositories, stores all repositories displaied in file picker
21 * this.options.formcallback
22 *
23 * Active repository options
24 * =====
25 * this.active_repo.id
26 * this.active_repo.nosearch
27 * this.active_repo.norefresh
28 * this.active_repo.nologin
29 * this.active_repo.help
30 * this.active_repo.manage
8cbef19e 31 *
99eaca9d
DC
32 * Server responses
33 * =====
34 * this.filelist, cached filelist
35 * this.pages
36 * this.page
37 * this.filepath, current path
38 * this.logindata, cached login form
39 */
40
4c508047 41M.core_filepicker = M.core_filepicker || {};
99eaca9d 42
4c508047
PS
43/**
44 * instances of file pickers used on page
45 */
46M.core_filepicker.instances = M.core_filepicker.instances || {};
539d4041 47M.core_filepicker.active_filepicker = null;
4c508047 48
67233725
DC
49/**
50 * Set selected file info
51 *
52 * @parma object file info
53 */
54M.core_filepicker.select_file = function(file) {
55 var fileinfo = {};
56 fileinfo['title'] = file.title;
57 fileinfo['source'] = file.reference;
58 fileinfo['thumbnail'] = file.thumbnail;
59 M.core_filepicker.active_filepicker.select_file(fileinfo);
60}
61
4c508047
PS
62/**
63 * Init and show file picker
64 */
65M.core_filepicker.show = function(Y, options) {
66 if (!M.core_filepicker.instances[options.client_id]) {
8cbef19e 67 M.core_filepicker.init(Y, options);
99eaca9d 68 }
4c508047
PS
69 M.core_filepicker.instances[options.client_id].show();
70};
71
72/**
73 * Add new file picker to current instances
74 */
75M.core_filepicker.init = function(Y, options) {
76 var FilePickerHelper = function(options) {
77 FilePickerHelper.superclass.constructor.apply(this, arguments);
8cbef19e 78 };
4c508047
PS
79
80 FilePickerHelper.NAME = "FilePickerHelper";
81 FilePickerHelper.ATTRS = {
99eaca9d
DC
82 options: {},
83 lang: {}
84 };
4c508047
PS
85
86 Y.extend(FilePickerHelper, Y.Base, {
9598d578 87 api: M.cfg.wwwroot+'/repository/repository_ajax.php',
7f9358fc 88 cached_responses: {},
4c508047
PS
89
90 initializer: function(options) {
91 this.options = options;
392d5fd4
DC
92 if (!this.options.savepath) {
93 this.options.savepath = '/';
94 }
99eaca9d 95 },
4c508047 96
99eaca9d
DC
97 destructor: function() {
98 },
4c508047 99
99eaca9d 100 request: function(args, redraw) {
c26855ff 101 var client_id = args.client_id;
f392caba
DC
102 if (!args.api) {
103 var api = this.api + '?action='+args.action;
104 } else {
105 var api = args.api + '?action='+args.action;
106 }
99eaca9d
DC
107 var params = {};
108 var scope = this;
109 if (args['scope']) {
110 scope = args['scope'];
111 }
112 params['repo_id']=args.repository_id;
113 params['p'] = args.path?args.path:'';
114 params['page'] = args.page?args.page:'';
115 params['env']=this.options.env;
116 // the form element only accept certain file types
117 params['accepted_types']=this.options.accepted_types;
e35194be 118 params['sesskey'] = M.cfg.sesskey;
99eaca9d 119 params['client_id'] = args.client_id;
0c4edaa2 120 params['itemid'] = this.options.itemid?this.options.itemid:0;
8a3e6a56 121 params['maxbytes'] = this.options.maxbytes?this.options.maxbytes:-1;
be85f7ab
DC
122 if (this.options.context && this.options.context.id) {
123 params['ctx_id'] = this.options.context.id;
124 }
99eaca9d
DC
125 if (args['params']) {
126 for (i in args['params']) {
127 params[i] = args['params'][i];
128 }
129 }
64654e78
DC
130 if (args.action == 'upload') {
131 var list = [];
132 for(var k in params) {
133 var value = params[k];
134 if(value instanceof Array) {
135 for(var i in value) {
136 list.push(k+'[]='+value[i]);
137 }
138 } else {
139 list.push(k+'='+value);
140 }
141 }
142 params = list.join('&');
143 } else {
144 params = build_querystring(params);
145 }
99eaca9d
DC
146 var cfg = {
147 method: 'POST',
148 on: {
149 complete: function(id,o,p) {
c26855ff 150 var panel_id = '#panel-'+client_id;
99eaca9d
DC
151 if (!o) {
152 alert('IO FATAL');
153 return;
154 }
4c508047
PS
155 var data = null;
156 try {
157 data = Y.JSON.parse(o.responseText);
158 } catch(e) {
879b4f9a
DC
159 scope.print_msg(M.str.repository.invalidjson, 'error');
160 Y.one(panel_id).set('innerHTML', 'ERROR: '+M.str.repository.invalidjson+'<pre>'+stripHTML(o.responseText)+'</pre>');
c26855ff 161 return;
4c508047 162 }
1dce6261 163 // error checking
e35194be
DC
164 if (data && data.error) {
165 scope.print_msg(data.error, 'error');
a5159b86
MG
166 if (args.onerror) {
167 args.onerror(id,data,p);
168 } else {
169 Y.one(panel_id).set('innerHTML', '');
170 }
1dce6261 171 return;
f392caba
DC
172 } else if (data && data.event) {
173 switch (data.event) {
174 case 'fileexists':
175 scope.process_existing_file(data);
176 break;
177 default:
178 break;
179 }
1dce6261 180 } else {
879b4f9a
DC
181 if (data.msg) {
182 scope.print_msg(data.msg, 'info');
183 }
7f9358fc
MG
184 // cache result if applicable
185 if (args.action != 'upload' && data.allowcaching) {
186 scope.cached_responses[params] = data;
187 }
188 // invoke callback
1dce6261
DC
189 args.callback(id,data,p);
190 }
99eaca9d
DC
191 }
192 },
193 arguments: {
194 scope: scope
195 },
196 headers: {
bd2db41a 197 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
99eaca9d 198 },
64654e78 199 data: params,
4c508047 200 context: this
99eaca9d
DC
201 };
202 if (args.form) {
203 cfg.form = args.form;
204 }
7f9358fc
MG
205 // check if result of the same request has been already cached. If not, request it
206 // (never applicable in case of form submission and/or upload action):
207 if (!args.form && args.action != 'upload' && scope.cached_responses[params]) {
208 args.callback(null, scope.cached_responses[params], {scope: scope})
209 } else {
210 Y.io(api, cfg);
211 if (redraw) {
212 this.wait('load');
213 }
99eaca9d
DC
214 }
215 },
f392caba
DC
216 process_existing_file: function(data) {
217 var scope = this;
218 var repository_id = scope.active_repo.id;
219 var client_id = scope.options.client_id;
220 var handleOverwrite = function() {
221 // overwrite
222 var dialog = this;
223 var params = {}
224 params['existingfilename'] = data.existingfile.filename;
225 params['existingfilepath'] = data.existingfile.filepath;
226 params['newfilename'] = data.newfile.filename;
227 params['newfilepath'] = data.newfile.filepath;
228 scope.request({
229 'params': params,
230 'scope': scope,
231 'action':'overwrite',
232 'path': '',
233 'client_id': client_id,
234 'repository_id': repository_id,
235 'callback': function(id, o, args) {
236 dialog.cancel();
237 scope.hide();
238 // editor needs to update url
239 // filemanager do nothing
c8ad2c12
SH
240 if (scope.options.editor_target && scope.options.env == 'editor') {
241 scope.options.editor_target.value = data.existingfile.url;
f392caba 242 scope.options.editor_target.onchange();
1ae299f5 243 } else if (scope.options.env === 'filepicker') {
913b3cb3
JP
244 var fileinfo = {'client_id':client_id,
245 'url':data.existingfile.url,
246 'file':data.existingfile.filename};
247 scope.options.formcallback.apply(scope, [fileinfo]);
f392caba
DC
248 }
249 }
250 }, true);
251 }
252 var handleRename = function() {
c8ad2c12
SH
253 if (scope.options.editor_target && scope.options.env == 'editor') {
254 scope.options.editor_target.value = data.newfile.url;
f392caba
DC
255 scope.options.editor_target.onchange();
256 }
257 this.cancel();
258 scope.hide();
f392caba
DC
259 var formcallback_scope = null;
260 if (scope.options.magicscope) {
261 formcallback_scope = scope.options.magicscope;
262 } else {
263 formcallback_scope = scope;
264 }
794cc7e1
JP
265 var fileinfo = {'client_id':client_id,
266 'url':data.newfile.url,
267 'file':data.newfile.filename};
268 scope.options.formcallback.apply(formcallback_scope, [fileinfo]);
f392caba
DC
269 }
270 var handleCancel = function() {
271 // Delete tmp file
272 var dialog = this;
273 var params = {};
274 params['newfilename'] = data.newfile.filename;
275 params['newfilepath'] = data.newfile.filepath;
276 scope.request({
277 'params': params,
278 'scope': scope,
279 'action':'deletetmpfile',
280 'path': '',
281 'client_id': client_id,
282 'repository_id': repository_id,
283 'callback': function(id, o, args) {
284 scope.hide();
285 dialog.cancel();
286 }
287 }, true);
288 }
289 var dialog = new YAHOO.widget.SimpleDialog("dlg", {
290 width: "50em",
291 fixedcenter: true,
f392caba
DC
292 close: false,
293 icon: YAHOO.widget.SimpleDialog.ICON_HELP,
294 visible: true,
1448c595 295 zIndex: 9999993,
f392caba
DC
296 draggable: true,
297 buttons: [{ text: M.str.repository.overwrite, handler: handleOverwrite },
298 { text: M.str.repository.renameto + ' "' + data.newfile.filename + '"', handler: handleRename },
c8ad2c12 299 { text: M.str.moodle.cancel, handler: handleCancel, isDefault: true}]
f392caba
DC
300 });
301 dialog.setHeader(M.str.repository.fileexistsdialogheader);
c8ad2c12 302 if (scope.options.env == 'editor') {
f392caba
DC
303 dialog.setBody(M.str.repository.fileexistsdialog_editor);
304 } else {
305 dialog.setBody(M.str.repository.fileexistsdialog_filemanager);
306 }
307
308 dialog.render(document.body);
309 dialog.show();
310 },
879b4f9a
DC
311 print_msg: function(msg, type) {
312 var client_id = this.options.client_id;
313 var dlg_id = 'fp-msg-dlg-'+client_id;
314 function handleYes() {
315 this.hide();
316 }
317 var icon = YAHOO.widget.SimpleDialog.ICON_INFO;
318 if (type=='error') {
319 icon = YAHOO.widget.SimpleDialog.ICON_ALARM;
320 }
321 if (!this.msg_dlg) {
8cbef19e 322 this.msg_dlg = new YAHOO.widget.SimpleDialog(dlg_id,
879b4f9a
DC
323 { width: "300px",
324 fixedcenter: true,
325 visible: true,
326 draggable: true,
327 close: true,
328 text: msg,
329 modal: false,
330 icon: icon,
331 zindex: 9999992,
332 constraintoviewport: true,
333 buttons: [{ text:M.str.moodle.ok, handler:handleYes, isDefault:true }]
334 });
335 this.msg_dlg.render(document.body);
336 } else {
337 this.msg_dlg.setBody(msg);
338 }
339 var header = M.str.moodle.info;
340 if (type=='error') {
341 header = M.str.moodle.error;
342 }
1628fd3b 343 this.msg_dlg.setHeader(header);
879b4f9a 344 this.msg_dlg.show();
879b4f9a 345 },
99eaca9d
DC
346 build_tree: function(node, level) {
347 var client_id = this.options.client_id;
1fb0173e 348 var dynload = this.active_repo.dynload;
99eaca9d
DC
349 var info = {
350 label:node.title,
351 //title:fp_lang.date+' '+node.date+fp_lang.size+' '+node.size,
352 filename:node.title,
353 source:node.source?node.source:'',
354 thumbnail:node.thumbnail,
355 path:node.path?node.path:[]
356 };
203bbcbe 357 var tmpNode = new YAHOO.widget.TextNode(info, level, false);
99eaca9d
DC
358 if(node.repo_id) {
359 tmpNode.repo_id=node.repo_id;
360 }else{
361 tmpNode.repo_id=this.active_repo.id;
362 }
363 if(node.children) {
364 if(node.expanded) {
365 tmpNode.expand();
366 }
1fb0173e
DC
367 if (dynload) {
368 tmpNode.scope = this;
369 }
99eaca9d
DC
370 tmpNode.isLeaf = false;
371 tmpNode.client_id = client_id;
372 if (node.path) {
373 tmpNode.path = node.path;
374 } else {
375 tmpNode.path = '';
376 }
377 for(var c in node.children) {
378 this.build_tree(node.children[c], tmpNode);
379 }
380 } else {
381 tmpNode.isLeaf = true;
382 }
383 },
60a4bf98
DC
384 view_files: function(page) {
385 var p= page?page:null;
386 if (this.active_repo.issearchresult) {
387 // list view is desiged to display treeview
388 // it is not working well with search result
99eaca9d 389 this.view_as_icons();
99eaca9d 390 } else {
67233725
DC
391 if (this.objecttag) {
392 // handle cancle action for repo using iframe
393 this.create_object_container(this.objecttag);
60a4bf98 394 } else {
67233725
DC
395 this.viewbar.set('disabled', false);
396 if (this.viewmode == 1) {
397 this.view_as_icons();
398 } else if (this.viewmode == 2) {
399 this.view_as_list(p);
400 } else {
401 this.view_as_icons();
402 }
60a4bf98 403 }
99eaca9d
DC
404 }
405 },
1fb0173e
DC
406 treeview_dynload: function(node, cb) {
407 var scope = node.scope;
408 var client_id = scope.options.client_id;
409 var repository_id = scope.active_repo.id;
410 scope.request({
411 action:'list',
412 client_id: client_id,
413 repository_id: repository_id,
414 path:node.path?node.path:'',
415 page:node.page?args.page:'',
416 callback: function(id, obj, args) {
417 obj.issearchresult = false;
418 var list = obj.list;
419 scope.viewbar.set('disabled', false);
420 scope.parse_repository_options(obj);
1fb0173e
DC
421 for(k in list) {
422 scope.build_tree(list[k], node);
423 }
424 cb();
425 }
426 }, false);
427 },
60a4bf98 428 view_as_list: function(p) {
1fb0173e 429 var scope = this;
60a4bf98
DC
430 var page = null;
431 if (!p) {
432 if (scope.active_repo.page) {
433 page = scope.active_repo.page;
434 }
435 } else {
436 page = p;
437 }
f312e878
DC
438 scope.request({
439 action:'list',
440 client_id: scope.options.client_id,
441 repository_id: scope.active_repo.id,
442 path:'',
60a4bf98 443 page:page,
f312e878 444 callback: function(id, obj, args) {
60a4bf98
DC
445 scope.parse_repository_options(obj);
446 if (obj.login) {
447 scope.viewbar.set('disabled', true);
448 scope.print_login(obj);
449 return;
450 }
f312e878
DC
451 var client_id = scope.options.client_id;
452 var dynload = scope.active_repo.dynload;
453 var list = obj.list;
454 var panel_id = '#panel-'+client_id;
455 scope.viewmode = 2;
456 Y.one(panel_id).set('innerHTML', '');
99eaca9d 457
f312e878 458 scope.print_header();
879b4f9a 459
f312e878 460 var html = '<div class="fp-tree-panel" id="treeview-'+client_id+'">';
60a4bf98 461 if (list && list.length==0) {
f312e878
DC
462 html += '<div class="fp-emptylist mdl-align">' +M.str.repository.nofilesavailable+'</div>';
463 }
464 html += '</div>';
879b4f9a 465
f312e878
DC
466 var tree = Y.Node.create(html);
467 Y.one(panel_id).appendChild(tree);
60a4bf98 468 if (!list || list.length==0) {
f312e878
DC
469 return;
470 }
879b4f9a 471
f312e878
DC
472 scope.treeview = new YAHOO.widget.TreeView('treeview-'+client_id);
473 if (dynload) {
474 scope.treeview.setDynamicLoad(scope.treeview_dynload, 1);
475 }
99eaca9d 476
f312e878
DC
477 for(k in list) {
478 scope.build_tree(list[k], scope.treeview.getRoot());
479 }
480 scope.treeview.subscribe('clickEvent', function(e){
481 if(e.node.isLeaf){
482 var fileinfo = {};
483 fileinfo['title'] = e.node.data.filename;
484 fileinfo['source'] = e.node.data.source;
485 fileinfo['thumbnail'] = e.node.data.thumbnail;
486 scope.select_file(fileinfo);
487 }
488 });
489 scope.treeview.draw();
99eaca9d 490 }
f312e878 491 }, true);
99eaca9d
DC
492 },
493 view_as_icons: function() {
98b15580 494 var scope = this;
99eaca9d
DC
495 var client_id = this.options.client_id;
496 var list = this.filelist;
497 var panel_id = '#panel-'+client_id;
498 this.viewmode = 1;
499 Y.one(panel_id).set('innerHTML', '');
500
501 this.print_header();
502
879b4f9a 503 var html = '<div class="fp-grid-panel" id="fp-grid-panel-'+client_id+'">';
60a4bf98 504 if (list && list.length==0) {
f312e878 505 html += '<div class="fp-emptylist mdl-align">' +M.str.repository.nofilesavailable+'</div>';
879b4f9a
DC
506 }
507 html += '</div>';
508
509 var gridpanel = Y.Node.create(html);
99eaca9d
DC
510 Y.one('#panel-'+client_id).appendChild(gridpanel);
511 var count = 0;
512 for(var k in list) {
513 var node = list[k];
514 var grid = document.createElement('DIV');
515 grid.className='fp-grid';
516 // the file name
517 var title = document.createElement('DIV');
518 title.id = 'grid-title-'+client_id+'-'+String(count);
519 title.className = 'label';
f00340e2 520 var filename = node.title;
99eaca9d 521 if (node.shorttitle) {
f00340e2 522 filename = node.shorttitle;
99eaca9d 523 }
f00340e2
DC
524 var filename_id = 'filname-link-'+client_id+'-'+String(count);
525 title.innerHTML += '<a href="###" id="'+filename_id+'" title="'+node.title+'"><span>'+filename+"</span></a>";
8cbef19e 526
99eaca9d
DC
527
528 if(node.thumbnail_width){
529 grid.style.width = node.thumbnail_width+'px';
530 title.style.width = (node.thumbnail_width-10)+'px';
531 } else {
532 grid.style.width = title.style.width = '90px';
533 }
534 var frame = document.createElement('DIV');
535 frame.style.textAlign='center';
536 if(node.thumbnail_height){
537 frame.style.height = node.thumbnail_height+'px';
538 }
539 var img = document.createElement('img');
540 img.src = node.thumbnail;
f00340e2 541 img.title = node.title;
99eaca9d
DC
542 if(node.thumbnail_alt) {
543 img.alt = node.thumbnail_alt;
544 }
545 if(node.thumbnail_title) {
546 img.title = node.thumbnail_title;
547 }
548
549 var link = document.createElement('A');
550 link.href='###';
551 link.id = 'img-id-'+client_id+'-'+String(count);
552 if(node.url) {
8cbef19e 553 // hide
2b728cb5 554 //grid.innerHTML += '<p><a target="_blank" href="'+node.url+'">'+M.str.repository.preview+'</a></p>';
99eaca9d
DC
555 }
556 link.appendChild(img);
557 frame.appendChild(link);
558 grid.appendChild(frame);
559 grid.appendChild(title);
560 gridpanel.appendChild(grid);
561
562 var y_title = Y.one('#'+title.id);
563 var y_file = Y.one('#'+link.id);
98b15580 564 var dynload = this.active_repo.dynload;
99eaca9d
DC
565 if(node.children) {
566 y_file.on('click', function(e, p) {
98b15580 567 if(dynload) {
98b15580
DC
568 var params = {'path':p.path};
569 scope.list(params);
99eaca9d
DC
570 }else{
571 this.filelist = p.children;
572 this.view_files();
573 }
574 }, this, node);
2e93bc38
DC
575 y_title.on('click', function(e, p, id){
576 var icon = Y.one(id);
577 icon.simulate('click');
578 }, this, node, '#'+link.id);
99eaca9d
DC
579 } else {
580 var fileinfo = {};
581 fileinfo['title'] = list[k].title;
582 fileinfo['source'] = list[k].source;
583 fileinfo['thumbnail'] = list[k].thumbnail;
1dce6261
DC
584 fileinfo['haslicense'] = list[k].haslicense?true:false;
585 fileinfo['hasauthor'] = list[k].hasauthor?true:false;
99eaca9d
DC
586 y_title.on('click', function(e, args) {
587 this.select_file(args);
588 }, this, fileinfo);
589 y_file.on('click', function(e, args) {
590 this.select_file(args);
591 }, this, fileinfo);
592 }
593 count++;
594 }
99eaca9d
DC
595 },
596 select_file: function(args) {
597 var client_id = this.options.client_id;
598 var thumbnail = Y.one('#fp-grid-panel-'+client_id);
599 if(thumbnail){
600 thumbnail.setStyle('display', 'none');
601 }
602 var header = Y.one('#fp-header-'+client_id);
603 if (header) {
604 header.setStyle('display', 'none');
605 }
606 var footer = Y.one('#fp-footer-'+client_id);
607 if (footer) {
608 footer.setStyle('display', 'none');
609 }
610 var path = Y.one('#path-'+client_id);
611 if(path){
612 path.setStyle('display', 'none');
613 }
614 var panel = Y.one('#panel-'+client_id);
67233725 615 panel.set('innerHTML', '');
e0b85db4
DC
616 var form_id = 'fp-rename-form-'+client_id;
617 var html = '<div class="fp-rename-form" id="'+form_id+'">';
99eaca9d 618 html += '<p><img src="'+args.thumbnail+'" /></p>';
308d948b
DC
619 html += '<table width="100%">';
620 html += '<tr><td class="mdl-right"><label for="newname-'+client_id+'">'+M.str.repository.saveas+':</label></td>';
621 html += '<td class="mdl-left"><input type="text" id="newname-'+client_id+'" value="'+args.title+'" /></td></tr>';
99eaca9d
DC
622
623 var le_checked = '';
624 var le_style = '';
67233725
DC
625 var returntypes = this.options.repositories[this.active_repo.id].return_types;
626
627 if (returntypes == 1) {
628 // support link only
629 html += '<input type="checkbox" id="linkexternal-'+client_id+'" style="display:none" value="checked" checked />';
630 } else if (returntypes == 4) {
631 html += '<input type="checkbox" id="filereference-'+client_id+'" style="display:none" value="checked" checked />';
632 } else {
633 if (((returntypes & 1) == 1) && ((this.options.return_types & 1) == 1)) {
634 // support external links
635 html += '<tr><td></td><td class="mdl-left"><input type="checkbox" id="linkexternal-'+client_id+'" value="" />'+M.str.repository.linkexternal+'</td></tr>';
636 }
637 // form element support file reference and repository support it too
638 if (((returntypes & 4) == 4) && ((this.options.return_types & 4) == 4)) {
639 // file reference
640 html += '<tr><td></td><td class="mdl-left"><input type="checkbox" id="filereference-'+client_id+'" value="" />'+M.str.repository.uselatestfile+'</td></tr>';
641 }
642
643 if (((returntypes & 4) != 4) && ((returntypes & 1) != 1)) {
644 html += '<input type="checkbox" id="linkexternal-'+client_id+'" style="display:none" value="checked" />';
645 html += '<input type="checkbox" id="filereference-'+client_id+'" style="display:none" value="checked" />';
646 }
99eaca9d 647 }
1dce6261
DC
648
649 if (!args.hasauthor) {
650 // the author of the file
308d948b
DC
651 html += '<tr><td class="mdl-right"><label for="text-author">'+M.str.repository.author+' :</label></td>';
652 html += '<td class="mdl-left"><input id="text-author-'+client_id+'" type="text" name="author" value="'+this.options.author+'" /></td>';
653 html += '</tr>';
1dce6261
DC
654 }
655
656 if (!args.haslicense) {
657 // the license of the file
658 var licenses = this.options.licenses;
308d948b
DC
659 html += '<tr><td class="mdl-right"><label for="select-license-'+client_id+'">'+M.str.repository.chooselicense+' :</label></td>';
660 html += '<td class="mdl-left"><select name="license" id="select-license-'+client_id+'">';
a756cb37
DC
661 var recentlicense = YAHOO.util.Cookie.get('recentlicense');
662 if (recentlicense) {
663 this.options.defaultlicense=recentlicense;
664 }
1dce6261 665 for (var i in licenses) {
308d948b
DC
666 if (this.options.defaultlicense==licenses[i].shortname) {
667 var selected = ' selected';
668 } else {
669 var selected = '';
670 }
671 html += '<option value="'+licenses[i].shortname+'"'+selected+'>'+licenses[i].fullname+'</option>';
1dce6261 672 }
308d948b 673 html += '</select></td></tr>';
1dce6261 674 }
308d948b 675 html += '</table>';
1dce6261 676
99eaca9d 677 html += '<p><input type="hidden" id="filesource-'+client_id+'" value="'+args.source+'" />';
2b728cb5
PS
678 html += '<input type="button" id="fp-confirm-'+client_id+'" value="'+M.str.repository.getfile+'" />';
679 html += '<input type="button" id="fp-cancel-'+client_id+'" value="'+M.str.moodle.cancel+'" /></p>';
99eaca9d
DC
680 html += '</div>';
681
682 var getfile_form = Y.Node.create(html);
683 panel.appendChild(getfile_form);
684
685 var getfile = Y.one('#fp-confirm-'+client_id);
686 getfile.on('click', function(e) {
687 var client_id = this.options.client_id;
688 var scope = this;
689 var repository_id = this.active_repo.id;
690 var title = Y.one('#newname-'+client_id).get('value');
691 var filesource = Y.one('#filesource-'+client_id).get('value');
14469892 692 var params = {'title':title, 'source':filesource, 'savepath': this.options.savepath};
1dce6261
DC
693 var license = Y.one('#select-license-'+client_id);
694 if (license) {
695 params['license'] = license.get('value');
a756cb37 696 YAHOO.util.Cookie.set('recentlicense', license.get('value'));
1dce6261
DC
697 }
698 var author = Y.one('#text-author-'+client_id);
392d5fd4 699 if (author){
1dce6261
DC
700 params['author'] = author.get('value');
701 }
99eaca9d 702
400d228e 703 if (this.options.externallink && this.options.env == 'editor') {
392d5fd4
DC
704 // in editor, images are stored in '/' only
705 params.savepath = '/';
766514a0 706 // when image or media button is clicked
67233725 707 if (this.options.return_types != 1) {
400d228e
MG
708 var linkexternal = Y.one('#linkexternal-'+client_id);
709 if (linkexternal && linkexternal.get('checked')) {
766514a0
DC
710 params['linkexternal'] = 'yes';
711 }
712 } else {
713 // when link button in editor clicked
99eaca9d
DC
714 params['linkexternal'] = 'yes';
715 }
766514a0
DC
716 }
717
718 if (this.options.env == 'url') {
99eaca9d
DC
719 params['linkexternal'] = 'yes';
720 }
721
67233725
DC
722 if (this.options.return_types != 4) {
723 var usefilereference = Y.one('#filereference-'+client_id);
724 if (usefilereference && usefilereference.get('checked')) {
725 params['usefilereference'] = 'yes';
726 }
727 } else {
728 params['usefilereference'] = 'yes';
729 }
730
99eaca9d
DC
731 this.wait('download', title);
732 this.request({
840912d5
DC
733 action:'download',
734 client_id: client_id,
735 repository_id: repository_id,
736 'params': params,
a5159b86
MG
737 onerror: function(id, obj, args) {
738 scope.view_files();
739 },
840912d5 740 callback: function(id, obj, args) {
e0b85db4 741 if (scope.options.editor_target && scope.options.env=='editor') {
840912d5
DC
742 scope.options.editor_target.value=obj.url;
743 scope.options.editor_target.onchange();
744 }
745 scope.hide();
746 obj.client_id = client_id;
747 var formcallback_scope = null;
411caa63 748 if (args.scope.options.magicscope) {
840912d5
DC
749 formcallback_scope = args.scope.options.magicscope;
750 } else {
751 formcallback_scope = args.scope;
99eaca9d 752 }
840912d5
DC
753 scope.options.formcallback.apply(formcallback_scope, [obj]);
754 }
99eaca9d
DC
755 }, true);
756 }, this);
e0b85db4
DC
757 var elform = Y.one('#'+form_id);
758 elform.on('keydown', function(e) {
759 if (e.keyCode == 13) {
760 getfile.simulate('click');
8cbef19e 761 e.preventDefault();
e0b85db4
DC
762 }
763 }, this);
99eaca9d
DC
764 var cancel = Y.one('#fp-cancel-'+client_id);
765 cancel.on('click', function(e) {
766 this.view_files();
767 }, this);
768 var treeview = Y.one('#treeview-'+client_id);
769 if (treeview){
770 treeview.setStyle('display', 'none');
771 }
772 },
773 wait: function(type) {
774 var panel = Y.one('#panel-'+this.options.client_id);
775 panel.set('innerHTML', '');
776 var name = '';
777 var str = '<div style="text-align:center">';
778 if(type=='load') {
87ad1edc 779 str += '<img src="'+M.util.image_url('i/loading')+'" />';
2b728cb5 780 str += '<p>'+M.str.repository.loading+'</p>';
99eaca9d 781 }else{
87ad1edc 782 str += '<img src="'+M.util.image_url('i/progressbar')+'" />';
2b728cb5 783 str += '<p>'+M.str.repository.copying+' <strong>'+name+'</strong></p>';
99eaca9d
DC
784 }
785 str += '</div>';
786 try {
787 panel.set('innerHTML', str);
788 } catch(e) {
789 alert(e.toString());
790 }
791 },
792 render: function() {
793 var client_id = this.options.client_id;
a756cb37 794 var scope = this;
99eaca9d
DC
795 var filepicker_id = 'filepicker-'+client_id;
796 var fpnode = Y.Node.create('<div class="file-picker" id="'+filepicker_id+'"></div>');
797 Y.one(document.body).appendChild(fpnode);
798 // render file picker panel
799 this.mainui = new YAHOO.widget.Panel(filepicker_id, {
800 draggable: true,
801 close: true,
802 underlay: 'none',
283cf6ec 803 zindex: 9999990,
99eaca9d
DC
804 monitorresize: false,
805 xy: [50, YAHOO.util.Dom.getDocumentScrollTop()+20]
806 });
807 var layout = null;
808 this.mainui.beforeRenderEvent.subscribe(function() {
809 YAHOO.util.Event.onAvailable('layout-'+client_id, function() {
810 layout = new YAHOO.widget.Layout('layout-'+client_id, {
811 height: 480, width: 700,
812 units: [
813 {position: 'top', height: 32, resize: false,
814 body:'<div class="yui-buttongroup fp-viewbar" id="fp-viewbar-'+client_id+'"></div><div class="fp-searchbar" id="search-div-'+client_id+'"></div>', gutter: '2'},
815 {position: 'left', width: 200, resize: true, scroll:true,
816 body:'<ul class="fp-list" id="fp-list-'+client_id+'"></ul>', gutter: '0 5 0 2', minWidth: 150, maxWidth: 300 },
817 {position: 'center', body: '<div class="fp-panel" id="panel-'+client_id+'"></div>',
818 scroll: true, gutter: '0 2 0 0' }
819 ]
820 });
821 layout.render();
a756cb37 822 scope.show_recent_repository();
99eaca9d
DC
823 });
824 });
934291d6 825
1c309299 826 this.mainui.setHeader(M.str.repository.filepicker);
99eaca9d
DC
827 this.mainui.setBody('<div id="layout-'+client_id+'"></div>');
828 this.mainui.render();
829 this.rendered = true;
830
831 var scope = this;
832 // adding buttons
60a4bf98 833 var view_icons = {label: M.str.repository.iconview, value: 't', 'checked': true,
99eaca9d
DC
834 onclick: {
835 fn: function(){
836 scope.view_as_icons();
837 }
838 }
839 };
2b728cb5 840 var view_listing = {label: M.str.repository.listview, value: 'l',
99eaca9d
DC
841 onclick: {
842 fn: function(){
843 scope.view_as_list();
844 }
845 }
846 };
847 this.viewbar = new YAHOO.widget.ButtonGroup({
848 id: 'btngroup-'+client_id,
849 name: 'buttons',
850 disabled: true,
851 container: 'fp-viewbar-'+client_id
852 });
853 this.viewbar.addButtons([view_icons, view_listing]);
854 // processing repository listing
855 var r = this.options.repositories;
856 Y.on('contentready', function(el) {
857 var list = Y.one(el);
858 var count = 0;
36ef6643
ARN
859 // Resort the repositories by sortorder
860 var sorted_repositories = new Array();
99eaca9d 861 for (var i in r) {
36ef6643
ARN
862 sorted_repositories[r[i].sortorder - 1] = r[i];
863 }
864 for (var i in sorted_repositories){
865 repository = sorted_repositories[i];
866 var id = 'repository-'+client_id+'-'+repository.id;
99eaca9d 867 var link_id = id + '-link';
36ef6643
ARN
868 list.append('<li id="'+id+'"><a class="fp-repo-name" id="'+link_id+'" href="###">'+repository.name+'</a></li>');
869 Y.one('#'+link_id).prepend('<img src="'+repository.icon+'" width="16" height="16" />&nbsp;');
99eaca9d 870 Y.one('#'+link_id).on('click', function(e, scope, repository_id) {
a756cb37 871 YAHOO.util.Cookie.set('recentrepository', repository_id);
99eaca9d 872 scope.repository_id = repository_id;
99eaca9d 873 this.list({'repo_id':repository_id});
36ef6643 874 }, this /*handler running scope*/, this/*second argument*/, repository.id/*third argument of handler*/);
99eaca9d
DC
875 count++;
876 }
f00340e2
DC
877 if (count==0) {
878 if (this.options.externallink) {
e35194be 879 list.set('innerHTML', M.str.repository.norepositoriesexternalavailable);
f00340e2
DC
880 } else {
881 list.set('innerHTML', M.str.repository.norepositoriesavailable);
882 }
883 }
99eaca9d
DC
884 }, '#fp-list-'+client_id, this /* handler running scope */, '#fp-list-'+client_id /*first argument of handler*/);
885 },
886 parse_repository_options: function(data) {
887 this.filelist = data.list?data.list:null;
888 this.filepath = data.path?data.path:null;
67233725 889 this.objecttag = data.object?data.object:null;
99eaca9d
DC
890 this.active_repo = {};
891 this.active_repo.issearchresult = Boolean(data.issearchresult);
98b15580 892 this.active_repo.dynload = data.dynload?data.dynload:false;
99eaca9d
DC
893 this.active_repo.pages = Number(data.pages?data.pages:null);
894 this.active_repo.page = Number(data.page?data.page:null);
895 this.active_repo.id = data.repo_id?data.repo_id:null;
896 this.active_repo.nosearch = data.nosearch?true:false;
897 this.active_repo.norefresh = data.norefresh?true:false;
898 this.active_repo.nologin = data.nologin?true:false;
fdfb9cbe 899 this.active_repo.logouttext = data.logouttext?data.logouttext:null;
99eaca9d
DC
900 this.active_repo.help = data.help?data.help:null;
901 this.active_repo.manage = data.manage?data.manage:null;
902 },
903 print_login: function(data) {
97b151ea 904 this.parse_repository_options(data);
99eaca9d
DC
905 var client_id = this.options.client_id;
906 var repository_id = data.repo_id;
907 var l = this.logindata = data.login;
908 var loginurl = '';
909 var panel = Y.one('#panel-'+client_id);
910 var action = 'login';
911 if (data['login_btn_action']) {
912 action=data['login_btn_action'];
913 }
914 var form_id = 'fp-form-'+client_id;
915 var download_button_id = 'fp-form-download-button-'+client_id;
916 var search_button_id = 'fp-form-search-button-'+client_id;
917 var login_button_id = 'fp-form-login-button-'+client_id;
918 var popup_button_id = 'fp-form-popup-button-'+client_id;
919
920 var str = '<div class="fp-login-form">';
921 str += '<form id="'+form_id+'">';
922 var has_pop = false;
923 str +='<table width="100%">';
924 for(var k in l) {
925 str +='<tr>';
926 if(l[k].type=='popup') {
927 // pop element
928 loginurl = l[k].url;
2b728cb5
PS
929 str += '<td colspan="2"><p class="fp-popup">'+M.str.repository.popup+'</p>';
930 str += '<p class="fp-popup"><button id="'+popup_button_id+'">'+M.str.repository.login+'</button>';
99eaca9d
DC
931 str += '</p></td>';
932 action = 'popup';
933 }else if(l[k].type=='textarea') {
934 // textarea element
935 str += '<td colspan="2"><p><textarea id="'+l[k].id+'" name="'+l[k].name+'"></textarea></p></td>';
936 }else if(l[k].type=='select') {
937 // select element
938 str += '<td align="right"><label>'+l[k].label+':</label></td>';
939 str += '<td align="left"><select id="'+l[k].id+'" name="'+l[k].name+'">';
940 for (i in l[k].options) {
941 str += '<option value="'+l[k].options[i].value+'">'+l[k].options[i].label+'</option>';
942 }
943 str += '</select></td>';
944 }else{
945 // input element
946 var label_id = '';
947 var field_id = '';
948 var field_value = '';
949 if(l[k].id) {
950 label_id = ' for="'+l[k].id+'"';
951 field_id = ' id="'+l[k].id+'"';
952 }
953 if (l[k].label) {
954 str += '<td align="right" width="30%" valign="center">';
955 str += '<label'+label_id+'>'+l[k].label+'</label> </td>';
956 } else {
957 str += '<td width="30%"></td>';
958 }
959 if(l[k].value) {
960 field_value = ' value="'+l[k].value+'"';
961 }
962 if(l[k].type=='radio'){
963 var list = l[k].value.split('|');
964 var labels = l[k].value_label.split('|');
965 str += '<td align="left">';
966 for(var item in list) {
967 str +='<input type="'+l[k].type+'"'+' name="'+l[k].name+'"'+
968 field_id+' value="'+list[item]+'" />'+labels[item]+'<br />';
969 }
970 str += '</td>';
971 }else{
972 str += '<td align="left">';
973 str += '<input type="'+l[k].type+'"'+' name="'+l[k].name+'"'+field_value+' '+field_id+' />';
974 str += '</td>';
975 }
976 }
977 str +='</tr>';
978 }
979 str +='</table>';
980 str += '</form>';
981
982 // custom lable text
2b728cb5 983 var btn_label = data['login_btn_label']?data['login_btn_label']:M.str.repository.submit;
99eaca9d
DC
984 if (action != 'popup') {
985 str += '<p><input type="button" id="';
986 switch (action) {
987 case 'search':
988 str += search_button_id;
989 break;
990 case 'download':
991 str += download_button_id;
992 break;
993 default:
994 str += login_button_id;
995 break;
996 }
997 str += '" value="'+btn_label+'" /></p>';
998 }
999
1000 str += '</div>';
1001
1002 // insert login form
1003 try {
1004 panel.set('innerHTML', str);
1005 } catch(e) {
a6b53a7c 1006 alert(M.str.repository.xhtmlerror);
99eaca9d
DC
1007 }
1008 // register buttons
1009 // process login action
1010 var login_button = Y.one('#'+login_button_id);
1011 var scope = this;
1012 if (login_button) {
1013 login_button.on('click', function(){
1014 // collect form data
1015 var data = this.logindata;
1016 var scope = this;
1017 var params = {};
1018 for (var k in data) {
1019 if(data[k].type!='popup') {
1020 var el = Y.one('[name='+data[k].name+']');
1021 var type = el.get('type');
1022 params[data[k].name] = '';
1023 if(type == 'checkbox') {
1024 params[data[k].name] = el.get('checked');
1025 } else {
1026 params[data[k].name] = el.get('value');
1027 }
1028 }
1029 }
1030 // start ajax request
1031 this.request({
1032 'params': params,
1033 'scope': scope,
1034 'action':'signin',
1035 'path': '',
1036 'client_id': client_id,
1037 'repository_id': repository_id,
1038 'callback': function(id, o, args) {
1039 scope.parse_repository_options(o);
1040 scope.view_files();
99eaca9d
DC
1041 }
1042 }, true);
1043 }, this);
1044 }
1045 var search_button = Y.one('#'+search_button_id);
1046 if (search_button) {
1047 search_button.on('click', function(){
1048 var data = this.logindata;
1049 var params = {};
1050
1051 for (var k in data) {
1052 if(data[k].type!='popup') {
1053 var el = document.getElementsByName(data[k].name)[0];
1054 params[data[k].name] = '';
1055 if(el.type == 'checkbox') {
1056 params[data[k].name] = el.checked;
1057 } else if(el.type == 'radio') {
1058 var tmp = document.getElementsByName(data[k].name);
1059 for(var i in tmp) {
1060 if (tmp[i].checked) {
1061 params[data[k].name] = tmp[i].value;
1062 }
1063 }
1064 } else {
1065 params[data[k].name] = el.value;
1066 }
1067 }
1068 }
1069 this.request({
1070 scope: scope,
1071 action:'search',
1072 client_id: client_id,
1073 repository_id: repository_id,
1074 form: {id: 'fp-form-'+scope.options.client_id,upload:false,useDisabled:true},
1075 callback: function(id, o, args) {
1076 o.issearchresult = true;
1077 scope.parse_repository_options(o);
1078 scope.view_files();
1079 }
1080 }, true);
1081 }, this);
1082 }
1083 var download_button = Y.one('#'+download_button_id);
1084 if (download_button) {
1085 download_button.on('click', function(){
1086 alert('download');
1087 });
1088 }
1089 var popup_button = Y.one('#'+popup_button_id);
1090 if (popup_button) {
539d4041
DC
1091 popup_button.on('click', function(e){
1092 M.core_filepicker.active_filepicker = this;
5ba5e378 1093 window.open(loginurl, 'repo_auth', 'location=0,status=0,width=500,height=300,scrollbars=yes');
8cbef19e 1094 e.preventDefault();
99eaca9d
DC
1095 }, this);
1096 }
e0b85db4
DC
1097 var elform = Y.one('#'+form_id);
1098 elform.on('keydown', function(e) {
1099 if (e.keyCode == 13) {
1100 switch (action) {
1101 case 'search':
1102 search_button.simulate('click');
1103 break;
1104 default:
1105 login_button.simulate('click');
1106 break;
1107 }
8cbef19e 1108 e.preventDefault();
e0b85db4
DC
1109 }
1110 }, this);
99eaca9d
DC
1111
1112 },
1113 search: function(args) {
1114 var data = this.logindata;
1115 var params = {};
1116
1117 for (var k in data) {
1118 if(data[k].type!='popup') {
1119 var el = document.getElementsByName(data[k].name)[0];
1120 params[data[k].name] = '';
1121 if(el.type == 'checkbox') {
1122 params[data[k].name] = el.checked;
1123 } else if(el.type == 'radio') {
1124 var tmp = document.getElementsByName(data[k].name);
1125 for(var i in tmp) {
1126 if (tmp[i].checked) {
1127 params[data[k].name] = tmp[i].value;
1128 }
1129 }
1130 } else {
1131 params[data[k].name] = el.value;
1132 }
1133 }
1134 }
1135 this.request({
1136 scope: scope,
1137 action:'search',
1138 client_id: client_id,
1139 repository_id: repository_id,
1140 form: {id: 'fp-form-'+scope.options.client_id,upload:false,useDisabled:true},
1141 callback: function(id, o, args) {
1142 o.issearchresult = true;
1143 scope.parse_repository_options(o);
1144 scope.view_files();
1145 }
1146 }, true);
1147 },
1148 list: function(args) {
1149 var scope = this;
1150 if (!args) {
1151 args = {};
1152 }
1153 if (!args.repo_id) {
1154 args.repo_id = scope.active_repo.id;
1155 }
1156 scope.request({
1157 action:'list',
1158 client_id: scope.options.client_id,
1159 repository_id: args.repo_id,
1160 path:args.path?args.path:'',
1161 page:args.page?args.page:'',
1162 callback: function(id, obj, args) {
32737311
DC
1163 Y.all('#fp-list-'+scope.options.client_id+' li a').setStyle('backgroundColor', 'transparent');
1164 var el = Y.one('#repository-'+scope.options.client_id+'-'+obj.repo_id+'-link');
1165 if (el) {
1166 el.setStyle('backgroundColor', '#AACCEE');
1167 }
99eaca9d
DC
1168 if (obj.login) {
1169 scope.viewbar.set('disabled', true);
1170 scope.print_login(obj);
1171 } else if (obj.upload) {
1172 scope.viewbar.set('disabled', true);
1173 scope.parse_repository_options(obj);
1174 scope.create_upload_form(obj);
8cbef19e 1175
67233725
DC
1176 } else if (obj.object) {
1177 M.core_filepicker.active_filepicker = scope;
1178 scope.viewbar.set('disabled', true);
1179 scope.parse_repository_options(obj);
1180 scope.create_object_container(obj.object);
99eaca9d
DC
1181
1182 } else if (obj.list) {
1183 obj.issearchresult = false;
1184 scope.viewbar.set('disabled', false);
1185 scope.parse_repository_options(obj);
1186 scope.view_files();
1187 }
99eaca9d
DC
1188 }
1189 }, true);
1190 },
67233725
DC
1191 create_object_container: function(data) {
1192 var client_id = this.options.client_id;
1193 Y.one('#panel-'+client_id).set('innerHTML', '');
1194 var types = this.options.accepted_types;
1195 var panel = Y.one('#panel-'+client_id);
1196 var str = '<object data="'+data.src+'" type="'+data.type+'" width="98%" height="98%" id="container_object" class="fp-object-container mdl-align">';
1197 str += '</object>';
1198 var container = Y.Node.create(str);
1199 Y.one('#panel-'+client_id).appendChild(container);
1200 },
99eaca9d
DC
1201 create_upload_form: function(data) {
1202 var client_id = this.options.client_id;
1203 Y.one('#panel-'+client_id).set('innerHTML', '');
e35194be 1204 var types = this.options.accepted_types;
99eaca9d
DC
1205
1206 this.print_header();
1207 var id = data.upload.id+'_'+client_id;
308d948b 1208 var str = '<div id="'+id+'_div" class="fp-upload-form mdl-align">';
a8f3f4cc 1209 str += '<form id="'+id+'" enctype="multipart/form-data" method="POST">';
308d948b
DC
1210 str += '<table width="100%">';
1211 str += '<tr><td class="mdl-right">';
1dce6261 1212 str += '<label for="'+id+'_file">'+data.upload.label+': </label></td>';
308d948b 1213 str += '<td class="mdl-left"><input type="file" id="'+id+'_file" name="repo_upload_file" />';
0bd269b7
DC
1214 str += '<tr><td class="mdl-right"><label for="newname-'+client_id+'">'+M.str.repository.saveas+':</label></td>';
1215 str += '<td class="mdl-left"><input type="text" name="title" id="newname-'+client_id+'" value="" /></td></tr>';
e35194be
DC
1216 str += '<input type="hidden" name="itemid" value="'+this.options.itemid+'" />';
1217 for (var i in types) {
1218 str += '<input type="hidden" name="accepted_types[]" value="'+types[i]+'" />';
1219 }
1220 str += '</td></tr><tr>';
308d948b
DC
1221 str += '<td class="mdl-right"><label>'+M.str.repository.author+': </label></td>';
1222 str += '<td class="mdl-left"><input type="text" name="author" value="'+this.options.author+'" /></td>';
1dce6261
DC
1223 str += '</tr>';
1224 str += '<tr>';
308d948b
DC
1225 str += '<td class="mdl-right">'+M.str.repository.chooselicense+': </td>';
1226 str += '<td class="mdl-left">';
1dce6261
DC
1227 var licenses = this.options.licenses;
1228 str += '<select name="license" id="select-license-'+client_id+'">';
a756cb37
DC
1229 var recentlicense = YAHOO.util.Cookie.get('recentlicense');
1230 if (recentlicense) {
1231 this.options.defaultlicense=recentlicense;
1232 }
1dce6261 1233 for (var i in licenses) {
308d948b
DC
1234 if (this.options.defaultlicense==licenses[i].shortname) {
1235 var selected = ' selected';
1236 } else {
1237 var selected = '';
1238 }
1239 str += '<option value="'+licenses[i].shortname+'"'+selected+'>'+licenses[i].fullname+'</option>';
1dce6261
DC
1240 }
1241 str += '</select>';
1242 str += '</td>';
1243 str += '</tr></table>';
99eaca9d 1244 str += '</form>';
b09b6ed8 1245 str += '<div class="fp-upload-btn"><button id="'+id+'_action">'+M.str.repository.upload+'</button></div>';
99eaca9d
DC
1246 str += '</div>';
1247 var upload_form = Y.Node.create(str);
1248 Y.one('#panel-'+client_id).appendChild(upload_form);
1249 var scope = this;
46325d3e 1250 Y.one('#'+id+'_action').on('click', function(e) {
8cbef19e 1251 e.preventDefault();
a756cb37
DC
1252 var license = Y.one('#select-license-'+client_id).get('value');
1253 YAHOO.util.Cookie.set('recentlicense', license);
2a03b97b 1254 if (!Y.one('#'+id+'_file').get('value')) {
ce6297d2 1255 scope.print_msg(M.str.repository.nofilesattached, 'error');
2a03b97b
DC
1256 return false;
1257 }
9d753c38
PS
1258 scope.request({
1259 scope: scope,
1260 action:'upload',
1261 client_id: client_id,
1262 params: {'savepath':scope.options.savepath},
1263 repository_id: scope.active_repo.id,
1264 form: {id: id, upload:true},
a5159b86
MG
1265 onerror: function(id, o, args) {
1266 scope.create_upload_form(data);
1267 },
9d753c38
PS
1268 callback: function(id, o, args) {
1269 if (scope.options.editor_target&&scope.options.env=='editor') {
1270 scope.options.editor_target.value=o.url;
1271 scope.options.editor_target.onchange();
0c4edaa2 1272 }
9d753c38
PS
1273 scope.hide();
1274 o.client_id = client_id;
1275 var formcallback_scope = null;
1276 if (args.scope.options.magicscope) {
1277 formcallback_scope = args.scope.options.magicscope;
1278 } else {
1279 formcallback_scope = args.scope;
1280 }
1281 scope.options.formcallback.apply(formcallback_scope, [o]);
1282 }
1283 }, true);
99eaca9d
DC
1284 }, this);
1285 },
1286 print_header: function() {
1287 var r = this.active_repo;
1288 var scope = this;
1289 var client_id = this.options.client_id;
1290 var repository_id = this.active_repo.id;
1291 var panel = Y.one('#panel-'+client_id);
1292 var str = '<div id="fp-header-'+client_id+'">';
1293 str += '<div class="fp-toolbar" id="repo-tb-'+client_id+'"></div>';
1294 str += '</div>';
1295 var head = Y.Node.create(str);
1296 panel.appendChild(head);
1297 //if(this.active_repo.pages < 8){
1298 this.print_paging('header');
1299 //}
1300
99eaca9d
DC
1301 var toolbar = Y.one('#repo-tb-'+client_id);
1302
1303 if(!r.nosearch) {
411caa63 1304 var html = '<a href="###"><img src="'+M.util.image_url('a/search')+'" /> '+M.str.repository.search+'</a>';
99eaca9d
DC
1305 var search = Y.Node.create(html);
1306 search.on('click', function() {
1307 scope.request({
1308 scope: scope,
1309 action:'searchform',
1310 repository_id: repository_id,
1311 callback: function(id, obj, args) {
1312 var scope = args.scope;
1313 var client_id = scope.options.client_id;
1314 var repository_id = scope.active_repo.id;
1315 var container = document.getElementById('fp-search-dlg');
1316 if(container) {
1317 container.innerHTML = '';
1318 container.parentNode.removeChild(container);
1319 }
1320 var container = document.createElement('DIV');
1321 container.id = 'fp-search-dlg';
1322
1323 var dlg_title = document.createElement('DIV');
1324 dlg_title.className = 'hd';
e4e9e853 1325 dlg_title.innerHTML = M.str.repository.search;
99eaca9d
DC
1326
1327 var dlg_body = document.createElement('DIV');
1328 dlg_body.className = 'bd';
1329
1330 var sform = document.createElement('FORM');
1331 sform.method = 'POST';
1332 sform.id = "fp-search-form";
1333 sform.innerHTML = obj.form;
1334
1335 dlg_body.appendChild(sform);
1336 container.appendChild(dlg_title);
1337 container.appendChild(dlg_body);
1338 Y.one(document.body).appendChild(container);
1339 var search_dialog= null;
1340 function dialog_handler() {
1341 scope.viewbar.set('disabled', false);
1342 scope.request({
1343 scope: scope,
1344 action:'search',
1345 client_id: client_id,
1346 repository_id: repository_id,
1347 form: {id: 'fp-search-form',upload:false,useDisabled:true},
1348 callback: function(id, o, args) {
1349 scope.parse_repository_options(o);
1350 scope.view_files();
1351 }
1352 }, true);
1353 search_dialog.cancel();
1354 }
e4e9e853
DC
1355 Y.one('#fp-search-form').on('keydown', function(e){
1356 if (e.keyCode == 13) {
1357 dialog_handler();
1358 e.preventDefault();
1359 }
1360 }, this);
99eaca9d 1361
283cf6ec 1362 search_dialog = new YAHOO.widget.Dialog("fp-search-dlg", {
99eaca9d
DC
1363 postmethod: 'async',
1364 draggable: true,
1365 width : "30em",
e4e9e853 1366 modal: true,
99eaca9d 1367 fixedcenter : true,
283cf6ec 1368 zindex: 9999991,
99eaca9d
DC
1369 visible : false,
1370 constraintoviewport : true,
1371 buttons: [
1372 {
2b728cb5 1373 text:M.str.repository.submit,
99eaca9d
DC
1374 handler:dialog_handler,
1375 isDefault:true
1376 }, {
2b728cb5 1377 text:M.str.moodle.cancel,
99eaca9d
DC
1378 handler:function(){
1379 this.destroy()
1380 }
1381 }]
1382 });
1383 search_dialog.render();
1384 search_dialog.show();
1385 }
1386 });
1387 },this);
1388 toolbar.appendChild(search);
1389 }
1390 // weather we use cache for this instance, this button will reload listing anyway
1391 if(!r.norefresh) {
2b728cb5 1392 var html = '<a href="###"><img src="'+M.util.image_url('a/refresh')+'" /> '+M.str.repository.refresh+'</a>';
99eaca9d
DC
1393 var refresh = Y.Node.create(html);
1394 refresh.on('click', function() {
1395 this.list();
1396 }, this);
1397 toolbar.appendChild(refresh);
1398 }
1399 if(!r.nologin) {
fdfb9cbe
DC
1400 var label = r.logouttext?r.logouttext:M.str.repository.logout;
1401 var html = '<a href="###"><img src="'+M.util.image_url('a/logout')+'" /> '+label+'</a>';
99eaca9d
DC
1402 var logout = Y.Node.create(html);
1403 logout.on('click', function() {
1404 this.request({
1405 action:'logout',
1406 client_id: client_id,
1407 repository_id: repository_id,
1408 path:'',
1409 callback: function(id, obj, args) {
1410 scope.viewbar.set('disabled', true);
1411 scope.print_login(obj);
1412 }
1413 }, true);
1414 }, this);
1415 toolbar.appendChild(logout);
1416 }
1417
1418 if(r.manage) {
1419 var mgr = document.createElement('A');
1420 mgr.href = r.manage;
1421 mgr.target = "_blank";
2b728cb5 1422 mgr.innerHTML = '<img src="'+M.util.image_url('a/setting')+'" /> '+M.str.repository.manageurl;
99eaca9d
DC
1423 toolbar.appendChild(mgr);
1424 }
1425 if(r.help) {
1426 var help = document.createElement('A');
1427 help.href = r.help;
1428 help.target = "_blank";
2b728cb5 1429 help.innerHTML = '<img src="'+M.util.image_url('a/help')+'" /> '+M.str.repository.help;
99eaca9d
DC
1430 toolbar.appendChild(help);
1431 }
1432
ea44dd2e 1433 this.print_path();
99eaca9d
DC
1434 },
1435 get_page_button: function(page) {
1436 var r = this.active_repo;
1437 var css = '';
1438 if (page == r.page) {
1439 css = 'class="cur_page" ';
1440 }
1441 var str = '<a '+css+'href="###" id="repo-page-'+page+'">';
1442 return str;
1443 },
1444 print_paging: function(html_id) {
1445 var client_id = this.options.client_id;
1446 var scope = this;
1447 var r = this.active_repo;
1448 var str = '';
1449 var action = '';
8ebbe295
EL
1450 var lastpage = r.pages;
1451 var lastpagetext = r.pages;
20ee5084 1452 if (r.pages == -1) {
8ebbe295
EL
1453 lastpage = r.page + 1;
1454 lastpagetext = M.str.moodle.next;
20ee5084 1455 }
8ebbe295 1456 if (lastpage > 1) {
99eaca9d
DC
1457 str += '<div class="fp-paging" id="paging-'+html_id+'-'+client_id+'">';
1458 str += this.get_page_button(1)+'1</a> ';
1459
1460 var span = 5;
1461 var ex = (span-1)/2;
1462
20ee5084
MG
1463 if (r.page+ex>=lastpage) {
1464 var max = lastpage;
99eaca9d
DC
1465 } else {
1466 if (r.page<span) {
1467 var max = span;
1468 } else {
1469 var max = r.page+ex;
1470 }
1471 }
1472
1473 // won't display upper boundary
1474 if (r.page >= span) {
1475 str += ' ... ';
1476 for(var i=r.page-ex; i<max; i++) {
1477 str += this.get_page_button(i);
1478 str += String(i);
1479 str += '</a> ';
1480 }
1481 } else {
1482 // this very first elements
1483 for(var i = 2; i < max; i++) {
1484 str += this.get_page_button(i);
1485 str += String(i);
1486 str += '</a> ';
1487 }
1488 }
1489
1490 // won't display upper boundary
20ee5084
MG
1491 if (max==lastpage) {
1492 str += this.get_page_button(lastpage)+lastpagetext+'</a>';
99eaca9d
DC
1493 } else {
1494 str += this.get_page_button(max)+max+'</a>';
20ee5084 1495 str += ' ... '+this.get_page_button(lastpage)+lastpagetext+'</a>';
99eaca9d
DC
1496 }
1497 str += '</div>';
1498 }
1499 if (str) {
1500 var a = Y.Node.create(str);
1501 Y.one('#fp-header-'+client_id).appendChild(a);
1502
1503 Y.all('#fp-header-'+client_id+' .fp-paging a').each(
1504 function(node, id) {
1505 node.on('click', function(e) {
1506 var id = node.get('id');
1507 var re = new RegExp("repo-page-(\\d+)", "i");
1508 var result = id.match(re);
1509 var args = {};
1510 args.page = result[1];
1511 if (scope.active_repo.issearchresult) {
1512 scope.request({
1513 scope: scope,
1514 action:'search',
1515 client_id: client_id,
1516 repository_id: r.id,
1517 params: {'page':result[1]},
1518 callback: function(id, o, args) {
1519 o.issearchresult = true;
1520 scope.parse_repository_options(o);
60a4bf98 1521 scope.view_files(result[1]);
99eaca9d
DC
1522 }
1523 }, true);
1524
1525 } else {
60a4bf98
DC
1526 if (scope.viewmode == 2) {
1527 scope.view_as_list(result[1]);
1528 } else {
1529 scope.list(args);
1530 }
99eaca9d
DC
1531 }
1532 });
1533 });
1534 }
1535 },
1536 print_path: function() {
1537 var client_id = this.options.client_id;
99eaca9d
DC
1538 var panel = Y.one('#panel-'+client_id);
1539 var p = this.filepath;
4c508047 1540 if (p && p.length!=0) {
99eaca9d
DC
1541 var path = Y.Node.create('<div id="path-'+client_id+'" class="fp-pathbar"></div>');
1542 panel.appendChild(path);
1543 for(var i = 0; i < p.length; i++) {
4e0d044f 1544 var link_path = p[i].path;
99eaca9d
DC
1545 var link = document.createElement('A');
1546 link.href = "###";
1547 link.innerHTML = p[i].name;
4e0d044f 1548 link.id = 'path-node-'+client_id+'-'+i;
99eaca9d 1549 var sep = Y.Node.create('<span>/</span>');
99eaca9d
DC
1550 path.appendChild(link);
1551 path.appendChild(sep);
c26855ff
DC
1552 Y.one('#'+link.id).on('click', function(Y, path){
1553 this.list({'path':path});
1554 }, this, link_path)
99eaca9d
DC
1555 }
1556 }
1557 },
1558 hide: function() {
1559 this.mainui.hide();
1560 },
1561 show: function() {
1562 if (this.rendered) {
1563 var panel = Y.one('#panel-'+this.options.client_id);
1564 panel.set('innerHTML', '');
1565 this.mainui.show();
a756cb37 1566 this.show_recent_repository();
99eaca9d
DC
1567 } else {
1568 this.launch();
1569 }
1570 },
1571 launch: function() {
7b42e81a 1572 this.render();
a756cb37
DC
1573 },
1574 show_recent_repository: function() {
1575 var repository_id = YAHOO.util.Cookie.get('recentrepository');
1576 if (this.options.repositories[repository_id]) {
1577 this.list({'repo_id':repository_id});
1578 }
99eaca9d
DC
1579 }
1580 });
bb496de7
DC
1581 var loading = Y.one('#filepicker-loading-'+options.client_id);
1582 if (loading) {
1583 loading.setStyle('display', 'none');
1584 }
4c508047
PS
1585 M.core_filepicker.instances[options.client_id] = new FilePickerHelper(options);
1586};