roles MDL-22048 Fixed use of format_string in front page settings that was causing...
[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
31 *
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
PS
48
49/**
50 * Init and show file picker
51 */
52M.core_filepicker.show = function(Y, options) {
53 if (!M.core_filepicker.instances[options.client_id]) {
54 M.core_filepicker.init(Y, options);
99eaca9d 55 }
4c508047
PS
56 M.core_filepicker.instances[options.client_id].show();
57};
58
59/**
60 * Add new file picker to current instances
61 */
62M.core_filepicker.init = function(Y, options) {
63 var FilePickerHelper = function(options) {
64 FilePickerHelper.superclass.constructor.apply(this, arguments);
65 }
66
67 FilePickerHelper.NAME = "FilePickerHelper";
68 FilePickerHelper.ATTRS = {
99eaca9d
DC
69 options: {},
70 lang: {}
71 };
4c508047
PS
72
73 Y.extend(FilePickerHelper, Y.Base, {
9598d578 74 api: M.cfg.wwwroot+'/repository/repository_ajax.php',
4c508047
PS
75
76 initializer: function(options) {
77 this.options = options;
99eaca9d 78 },
4c508047 79
99eaca9d
DC
80 destructor: function() {
81 },
4c508047 82
99eaca9d
DC
83 request: function(args, redraw) {
84 var api = this.api + '?action='+args.action;
85 var params = {};
86 var scope = this;
87 if (args['scope']) {
88 scope = args['scope'];
89 }
90 params['repo_id']=args.repository_id;
91 params['p'] = args.path?args.path:'';
92 params['page'] = args.page?args.page:'';
93 params['env']=this.options.env;
94 // the form element only accept certain file types
95 params['accepted_types']=this.options.accepted_types;
9598d578 96 params['sesskey']=M.cfg.sesskey;
99eaca9d 97 params['client_id'] = args.client_id;
539d4041 98 params['filearea'] = this.options.filearea?this.options.filearea:'user_draft';
0c4edaa2 99 params['itemid'] = this.options.itemid?this.options.itemid:0;
8a3e6a56 100 params['maxbytes'] = this.options.maxbytes?this.options.maxbytes:-1;
99eaca9d
DC
101 if (args['params']) {
102 for (i in args['params']) {
103 params[i] = args['params'][i];
104 }
105 }
106 var cfg = {
107 method: 'POST',
108 on: {
109 complete: function(id,o,p) {
110 if (!o) {
111 alert('IO FATAL');
112 return;
113 }
4c508047
PS
114 var data = null;
115 try {
116 data = Y.JSON.parse(o.responseText);
117 } catch(e) {
2b728cb5 118 alert(M.str.repository.invalidjson+' - |'+source+'| -'+stripHTML(o.responseText));
4c508047 119 }
1dce6261
DC
120 // error checking
121 if (data.e) {
122 var panel_id = '#panel-'+data.client_id;
123 Y.one(panel_id).set('innerHTML', 'ERROR: '+data.e);
124 return;
125 } else {
126 args.callback(id,data,p);
127 }
99eaca9d
DC
128 }
129 },
130 arguments: {
131 scope: scope
132 },
133 headers: {
134 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
135 'User-Agent': 'MoodleFilePicker/3.0'
136 },
4c508047
PS
137 data: build_querystring(params),
138 context: this
99eaca9d
DC
139 };
140 if (args.form) {
141 cfg.form = args.form;
142 }
143 Y.io(api, cfg);
144 if (redraw) {
145 this.wait('load');
146 }
147 },
4c508047 148
99eaca9d
DC
149 build_tree: function(node, level) {
150 var client_id = this.options.client_id;
151 if(node.children) {
152 node.title = '<i><u>'+node.title+'</u></i>';
153 }
154 var info = {
155 label:node.title,
156 //title:fp_lang.date+' '+node.date+fp_lang.size+' '+node.size,
157 filename:node.title,
158 source:node.source?node.source:'',
159 thumbnail:node.thumbnail,
160 path:node.path?node.path:[]
161 };
162 var tmpNode = new YAHOO.widget.TextNode(info, level, false);
163 //var tooltip = new YAHOO.widget.Tooltip(tmpNode.labelElId, {
164 //context:tmpNode.labelElId, text:info.title});
165 if(node.repo_id) {
166 tmpNode.repo_id=node.repo_id;
167 }else{
168 tmpNode.repo_id=this.active_repo.id;
169 }
170 if(node.children) {
171 if(node.expanded) {
172 tmpNode.expand();
173 }
174 tmpNode.isLeaf = false;
175 tmpNode.client_id = client_id;
176 if (node.path) {
177 tmpNode.path = node.path;
178 } else {
179 tmpNode.path = '';
180 }
181 for(var c in node.children) {
182 this.build_tree(node.children[c], tmpNode);
183 }
184 } else {
185 tmpNode.isLeaf = true;
186 }
187 },
188 view_files: function() {
189 this.viewbar.set('disabled', false);
190 if (this.viewmode == 1) {
191 this.view_as_icons();
192 } else if (this.viewmode ==2) {
193 this.view_as_list();
194 } else {
195 this.view_as_icons();
196 }
197 },
198 view_as_list: function() {
199 var client_id = this.options.client_id;
200 var list = this.filelist;
201 var panel_id = '#panel-'+client_id;
202 this.viewmode = 2;
203 Y.one(panel_id).set('innerHTML', '');
204
205 this.print_header();
206 var tree = Y.Node.create('<div id="treeview-'+client_id+'"></div>');
207 Y.one(panel_id).appendChild(tree);
208 this.treeview = new YAHOO.widget.TreeView('treeview-'+client_id);
209
210 for(k in list) {
211 this.build_tree(list[k], this.treeview.getRoot());
212 }
213 var scope = this;
214 this.treeview.subscribe('clickEvent', function(e){
215 if(e.node.isLeaf){
216 var fileinfo = {};
217 fileinfo['title'] = e.node.data.filename;
218 fileinfo['source'] = e.node.data.source;
219 fileinfo['thumbnail'] = e.node.data.thumbnail;
220 scope.select_file(fileinfo);
221 }
222 });
223 this.treeview.draw();
224 },
225 view_as_icons: function() {
98b15580 226 var scope = this;
99eaca9d
DC
227 var client_id = this.options.client_id;
228 var list = this.filelist;
229 var panel_id = '#panel-'+client_id;
230 this.viewmode = 1;
231 Y.one(panel_id).set('innerHTML', '');
232
233 this.print_header();
234
235 var gridpanel = Y.Node.create('<div id="fp-grid-panel-'+client_id+'"></div>');
236 Y.one('#panel-'+client_id).appendChild(gridpanel);
237 var count = 0;
238 for(var k in list) {
239 var node = list[k];
240 var grid = document.createElement('DIV');
241 grid.className='fp-grid';
242 // the file name
243 var title = document.createElement('DIV');
244 title.id = 'grid-title-'+client_id+'-'+String(count);
245 title.className = 'label';
246 if (node.shorttitle) {
247 node.title = node.shorttitle;
248 }
249 title.innerHTML += '<a href="###"><span>'+node.title+"</span></a>";
250
251 if(node.thumbnail_width){
252 grid.style.width = node.thumbnail_width+'px';
253 title.style.width = (node.thumbnail_width-10)+'px';
254 } else {
255 grid.style.width = title.style.width = '90px';
256 }
257 var frame = document.createElement('DIV');
258 frame.style.textAlign='center';
259 if(node.thumbnail_height){
260 frame.style.height = node.thumbnail_height+'px';
261 }
262 var img = document.createElement('img');
263 img.src = node.thumbnail;
264 if(node.thumbnail_alt) {
265 img.alt = node.thumbnail_alt;
266 }
267 if(node.thumbnail_title) {
268 img.title = node.thumbnail_title;
269 }
270
271 var link = document.createElement('A');
272 link.href='###';
273 link.id = 'img-id-'+client_id+'-'+String(count);
274 if(node.url) {
275 // hide
2b728cb5 276 //grid.innerHTML += '<p><a target="_blank" href="'+node.url+'">'+M.str.repository.preview+'</a></p>';
99eaca9d
DC
277 }
278 link.appendChild(img);
279 frame.appendChild(link);
280 grid.appendChild(frame);
281 grid.appendChild(title);
282 gridpanel.appendChild(grid);
283
284 var y_title = Y.one('#'+title.id);
285 var y_file = Y.one('#'+link.id);
98b15580 286 var dynload = this.active_repo.dynload;
99eaca9d
DC
287 if(node.children) {
288 y_file.on('click', function(e, p) {
98b15580 289 if(dynload) {
98b15580
DC
290 var params = {'path':p.path};
291 scope.list(params);
99eaca9d
DC
292 }else{
293 this.filelist = p.children;
294 this.view_files();
295 }
296 }, this, node);
297 y_title.on('click', function(e, p){
e0b85db4 298 y_file.simulate('click');
99eaca9d
DC
299 }, this, node);
300 } else {
301 var fileinfo = {};
302 fileinfo['title'] = list[k].title;
303 fileinfo['source'] = list[k].source;
304 fileinfo['thumbnail'] = list[k].thumbnail;
1dce6261
DC
305 fileinfo['haslicense'] = list[k].haslicense?true:false;
306 fileinfo['hasauthor'] = list[k].hasauthor?true:false;
99eaca9d
DC
307 y_title.on('click', function(e, args) {
308 this.select_file(args);
309 }, this, fileinfo);
310 y_file.on('click', function(e, args) {
311 this.select_file(args);
312 }, this, fileinfo);
313 }
314 count++;
315 }
99eaca9d
DC
316 },
317 select_file: function(args) {
318 var client_id = this.options.client_id;
319 var thumbnail = Y.one('#fp-grid-panel-'+client_id);
320 if(thumbnail){
321 thumbnail.setStyle('display', 'none');
322 }
323 var header = Y.one('#fp-header-'+client_id);
324 if (header) {
325 header.setStyle('display', 'none');
326 }
327 var footer = Y.one('#fp-footer-'+client_id);
328 if (footer) {
329 footer.setStyle('display', 'none');
330 }
331 var path = Y.one('#path-'+client_id);
332 if(path){
333 path.setStyle('display', 'none');
334 }
335 var panel = Y.one('#panel-'+client_id);
e0b85db4
DC
336 var form_id = 'fp-rename-form-'+client_id;
337 var html = '<div class="fp-rename-form" id="'+form_id+'">';
99eaca9d 338 html += '<p><img src="'+args.thumbnail+'" /></p>';
2b728cb5 339 html += '<p><label for="newname-'+client_id+'">'+M.str.repository.saveas+':</label>';
99eaca9d
DC
340 html += '<input type="text" id="newname-'+client_id+'" value="'+args.title+'" /></p>';
341
342 var le_checked = '';
343 var le_style = '';
344 if (this.options.repositories[this.active_repo.id].return_types == 1) {
345 // support external links only
346 le_checked = 'checked';
347 le_style = ' style="display:none;"';
348 } else if(this.options.repositories[this.active_repo.id].return_types == 2) {
349 // support internal files only
350 le_style = ' style="display:none;"';
351 }
352 if (this.options.externallink && this.options.env == 'editor') {
2b728cb5 353 html += '<p'+le_style+'><input type="checkbox" id="linkexternal-'+client_id+'" value="" '+le_checked+' />'+M.str.repository.linkexternal+'</p>';
99eaca9d 354 }
1dce6261
DC
355
356 if (!args.hasauthor) {
357 // the author of the file
358 html += '<p><label for="text-author">'+M.str.repository.author+' :</label>';
359 html += '<input id="text-author-'+client_id+'" type="text" name="author" value="'+this.options.author+'" />';
360 html += '</p>';
361 }
362
363 if (!args.haslicense) {
364 // the license of the file
365 var licenses = this.options.licenses;
366 html += '<p><label for="select-license-'+client_id+'">'+M.str.repository.chooselicense+' :</label>';
367 html += '<select name="license" id="select-license-'+client_id+'">';
368 for (var i in licenses) {
369 html += '<option value="'+licenses[i].shortname+'">'+licenses[i].fullname+'</option>';
370 }
371 html += '</select></p>';
372 }
373
99eaca9d 374 html += '<p><input type="hidden" id="filesource-'+client_id+'" value="'+args.source+'" />';
2b728cb5
PS
375 html += '<input type="button" id="fp-confirm-'+client_id+'" value="'+M.str.repository.getfile+'" />';
376 html += '<input type="button" id="fp-cancel-'+client_id+'" value="'+M.str.moodle.cancel+'" /></p>';
99eaca9d
DC
377 html += '</div>';
378
379 var getfile_form = Y.Node.create(html);
380 panel.appendChild(getfile_form);
381
382 var getfile = Y.one('#fp-confirm-'+client_id);
383 getfile.on('click', function(e) {
384 var client_id = this.options.client_id;
385 var scope = this;
386 var repository_id = this.active_repo.id;
387 var title = Y.one('#newname-'+client_id).get('value');
388 var filesource = Y.one('#filesource-'+client_id).get('value');
14469892 389 var params = {'title':title, 'source':filesource, 'savepath': this.options.savepath};
1dce6261
DC
390 var license = Y.one('#select-license-'+client_id);
391 if (license) {
392 params['license'] = license.get('value');
393 }
394 var author = Y.one('#text-author-'+client_id);
395 if (author) {
396 params['author'] = author.get('value');
397 }
99eaca9d
DC
398
399 if (this.options.env == 'editor') {
400 var linkexternal = Y.one('#linkexternal-'+client_id).get('checked');
401 if (linkexternal) {
402 params['linkexternal'] = 'yes';
403 }
404 } if (this.options.env == 'url') {
405 params['linkexternal'] = 'yes';
406 }
407
408 this.wait('download', title);
409 this.request({
840912d5
DC
410 action:'download',
411 client_id: client_id,
412 repository_id: repository_id,
413 'params': params,
414 callback: function(id, obj, args) {
e0b85db4 415 if (scope.options.editor_target && scope.options.env=='editor') {
840912d5
DC
416 scope.options.editor_target.value=obj.url;
417 scope.options.editor_target.onchange();
418 }
419 scope.hide();
420 obj.client_id = client_id;
421 var formcallback_scope = null;
411caa63 422 if (args.scope.options.magicscope) {
840912d5
DC
423 formcallback_scope = args.scope.options.magicscope;
424 } else {
425 formcallback_scope = args.scope;
99eaca9d 426 }
840912d5
DC
427 scope.options.formcallback.apply(formcallback_scope, [obj]);
428 }
99eaca9d
DC
429 }, true);
430 }, this);
e0b85db4
DC
431 var elform = Y.one('#'+form_id);
432 elform.on('keydown', function(e) {
433 if (e.keyCode == 13) {
434 getfile.simulate('click');
435 e.preventDefault();
436 }
437 }, this);
99eaca9d
DC
438 var cancel = Y.one('#fp-cancel-'+client_id);
439 cancel.on('click', function(e) {
440 this.view_files();
441 }, this);
442 var treeview = Y.one('#treeview-'+client_id);
443 if (treeview){
444 treeview.setStyle('display', 'none');
445 }
446 },
447 wait: function(type) {
448 var panel = Y.one('#panel-'+this.options.client_id);
449 panel.set('innerHTML', '');
450 var name = '';
451 var str = '<div style="text-align:center">';
452 if(type=='load') {
87ad1edc 453 str += '<img src="'+M.util.image_url('i/loading')+'" />';
2b728cb5 454 str += '<p>'+M.str.repository.loading+'</p>';
99eaca9d 455 }else{
87ad1edc 456 str += '<img src="'+M.util.image_url('i/progressbar')+'" />';
2b728cb5 457 str += '<p>'+M.str.repository.copying+' <strong>'+name+'</strong></p>';
99eaca9d
DC
458 }
459 str += '</div>';
460 try {
461 panel.set('innerHTML', str);
462 } catch(e) {
463 alert(e.toString());
464 }
465 },
466 render: function() {
467 var client_id = this.options.client_id;
468 var filepicker_id = 'filepicker-'+client_id;
469 var fpnode = Y.Node.create('<div class="file-picker" id="'+filepicker_id+'"></div>');
470 Y.one(document.body).appendChild(fpnode);
471 // render file picker panel
472 this.mainui = new YAHOO.widget.Panel(filepicker_id, {
473 draggable: true,
474 close: true,
475 underlay: 'none',
283cf6ec 476 zindex: 9999990,
99eaca9d
DC
477 monitorresize: false,
478 xy: [50, YAHOO.util.Dom.getDocumentScrollTop()+20]
479 });
480 var layout = null;
481 this.mainui.beforeRenderEvent.subscribe(function() {
482 YAHOO.util.Event.onAvailable('layout-'+client_id, function() {
483 layout = new YAHOO.widget.Layout('layout-'+client_id, {
484 height: 480, width: 700,
485 units: [
486 {position: 'top', height: 32, resize: false,
487 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'},
488 {position: 'left', width: 200, resize: true, scroll:true,
489 body:'<ul class="fp-list" id="fp-list-'+client_id+'"></ul>', gutter: '0 5 0 2', minWidth: 150, maxWidth: 300 },
490 {position: 'center', body: '<div class="fp-panel" id="panel-'+client_id+'"></div>',
491 scroll: true, gutter: '0 2 0 0' }
492 ]
493 });
494 layout.render();
495 });
496 });
497 this.mainui.setHeader('File Picker');
498 this.mainui.setBody('<div id="layout-'+client_id+'"></div>');
499 this.mainui.render();
500 this.rendered = true;
501
502 var scope = this;
503 // adding buttons
2b728cb5 504 var view_icons = {label: M.str.repository.iconview, value: 't',
99eaca9d
DC
505 onclick: {
506 fn: function(){
507 scope.view_as_icons();
508 }
509 }
510 };
2b728cb5 511 var view_listing = {label: M.str.repository.listview, value: 'l',
99eaca9d
DC
512 onclick: {
513 fn: function(){
514 scope.view_as_list();
515 }
516 }
517 };
518 this.viewbar = new YAHOO.widget.ButtonGroup({
519 id: 'btngroup-'+client_id,
520 name: 'buttons',
521 disabled: true,
522 container: 'fp-viewbar-'+client_id
523 });
524 this.viewbar.addButtons([view_icons, view_listing]);
525 // processing repository listing
526 var r = this.options.repositories;
527 Y.on('contentready', function(el) {
528 var list = Y.one(el);
529 var count = 0;
530 for (var i in r) {
531 var id = 'repository-'+client_id+'-'+count;
532 var link_id = id + '-link';
533 list.append('<li id="'+id+'"><a class="fp-repo-name" id="'+link_id+'" href="###">'+r[i].name+'</a></li>');
534 Y.one('#'+link_id).prepend('<img src="'+r[i].icon+'" width="16" height="16" />&nbsp;');
535 Y.one('#'+link_id).on('click', function(e, scope, repository_id) {
536 scope.repository_id = repository_id;
537 Y.all(el+' li a').setStyle('backgroundColor', 'transparent');
538 e.currentTarget.setStyle('backgroundColor', '#CCC');
539 this.list({'repo_id':repository_id});
540 }, this /*handler running scope*/, this/*second argument*/, r[i].id/*third argument of handler*/);
541 count++;
542 }
543 }, '#fp-list-'+client_id, this /* handler running scope */, '#fp-list-'+client_id /*first argument of handler*/);
544 },
545 parse_repository_options: function(data) {
546 this.filelist = data.list?data.list:null;
547 this.filepath = data.path?data.path:null;
548 this.active_repo = {};
549 this.active_repo.issearchresult = Boolean(data.issearchresult);
98b15580 550 this.active_repo.dynload = data.dynload?data.dynload:false;
99eaca9d
DC
551 this.active_repo.pages = Number(data.pages?data.pages:null);
552 this.active_repo.page = Number(data.page?data.page:null);
553 this.active_repo.id = data.repo_id?data.repo_id:null;
554 this.active_repo.nosearch = data.nosearch?true:false;
555 this.active_repo.norefresh = data.norefresh?true:false;
556 this.active_repo.nologin = data.nologin?true:false;
557 this.active_repo.help = data.help?data.help:null;
558 this.active_repo.manage = data.manage?data.manage:null;
559 },
560 print_login: function(data) {
561 var client_id = this.options.client_id;
562 var repository_id = data.repo_id;
563 var l = this.logindata = data.login;
564 var loginurl = '';
565 var panel = Y.one('#panel-'+client_id);
566 var action = 'login';
567 if (data['login_btn_action']) {
568 action=data['login_btn_action'];
569 }
570 var form_id = 'fp-form-'+client_id;
571 var download_button_id = 'fp-form-download-button-'+client_id;
572 var search_button_id = 'fp-form-search-button-'+client_id;
573 var login_button_id = 'fp-form-login-button-'+client_id;
574 var popup_button_id = 'fp-form-popup-button-'+client_id;
575
576 var str = '<div class="fp-login-form">';
577 str += '<form id="'+form_id+'">';
578 var has_pop = false;
579 str +='<table width="100%">';
580 for(var k in l) {
581 str +='<tr>';
582 if(l[k].type=='popup') {
583 // pop element
584 loginurl = l[k].url;
2b728cb5
PS
585 str += '<td colspan="2"><p class="fp-popup">'+M.str.repository.popup+'</p>';
586 str += '<p class="fp-popup"><button id="'+popup_button_id+'">'+M.str.repository.login+'</button>';
99eaca9d
DC
587 str += '</p></td>';
588 action = 'popup';
589 }else if(l[k].type=='textarea') {
590 // textarea element
591 str += '<td colspan="2"><p><textarea id="'+l[k].id+'" name="'+l[k].name+'"></textarea></p></td>';
592 }else if(l[k].type=='select') {
593 // select element
594 str += '<td align="right"><label>'+l[k].label+':</label></td>';
595 str += '<td align="left"><select id="'+l[k].id+'" name="'+l[k].name+'">';
596 for (i in l[k].options) {
597 str += '<option value="'+l[k].options[i].value+'">'+l[k].options[i].label+'</option>';
598 }
599 str += '</select></td>';
600 }else{
601 // input element
602 var label_id = '';
603 var field_id = '';
604 var field_value = '';
605 if(l[k].id) {
606 label_id = ' for="'+l[k].id+'"';
607 field_id = ' id="'+l[k].id+'"';
608 }
609 if (l[k].label) {
610 str += '<td align="right" width="30%" valign="center">';
611 str += '<label'+label_id+'>'+l[k].label+'</label> </td>';
612 } else {
613 str += '<td width="30%"></td>';
614 }
615 if(l[k].value) {
616 field_value = ' value="'+l[k].value+'"';
617 }
618 if(l[k].type=='radio'){
619 var list = l[k].value.split('|');
620 var labels = l[k].value_label.split('|');
621 str += '<td align="left">';
622 for(var item in list) {
623 str +='<input type="'+l[k].type+'"'+' name="'+l[k].name+'"'+
624 field_id+' value="'+list[item]+'" />'+labels[item]+'<br />';
625 }
626 str += '</td>';
627 }else{
628 str += '<td align="left">';
629 str += '<input type="'+l[k].type+'"'+' name="'+l[k].name+'"'+field_value+' '+field_id+' />';
630 str += '</td>';
631 }
632 }
633 str +='</tr>';
634 }
635 str +='</table>';
636 str += '</form>';
637
638 // custom lable text
2b728cb5 639 var btn_label = data['login_btn_label']?data['login_btn_label']:M.str.repository.submit;
99eaca9d
DC
640 if (action != 'popup') {
641 str += '<p><input type="button" id="';
642 switch (action) {
643 case 'search':
644 str += search_button_id;
645 break;
646 case 'download':
647 str += download_button_id;
648 break;
649 default:
650 str += login_button_id;
651 break;
652 }
653 str += '" value="'+btn_label+'" /></p>';
654 }
655
656 str += '</div>';
657
658 // insert login form
659 try {
660 panel.set('innerHTML', str);
661 } catch(e) {
2b728cb5 662 alert(e.toString()+M.str.quiz.xhtml);
99eaca9d
DC
663 }
664 // register buttons
665 // process login action
666 var login_button = Y.one('#'+login_button_id);
667 var scope = this;
668 if (login_button) {
669 login_button.on('click', function(){
670 // collect form data
671 var data = this.logindata;
672 var scope = this;
673 var params = {};
674 for (var k in data) {
675 if(data[k].type!='popup') {
676 var el = Y.one('[name='+data[k].name+']');
677 var type = el.get('type');
678 params[data[k].name] = '';
679 if(type == 'checkbox') {
680 params[data[k].name] = el.get('checked');
681 } else {
682 params[data[k].name] = el.get('value');
683 }
684 }
685 }
686 // start ajax request
687 this.request({
688 'params': params,
689 'scope': scope,
690 'action':'signin',
691 'path': '',
692 'client_id': client_id,
693 'repository_id': repository_id,
694 'callback': function(id, o, args) {
695 scope.parse_repository_options(o);
696 scope.view_files();
697 if (o.msg) {
698 // do something
699 }
700 }
701 }, true);
702 }, this);
703 }
704 var search_button = Y.one('#'+search_button_id);
705 if (search_button) {
706 search_button.on('click', function(){
707 var data = this.logindata;
708 var params = {};
709
710 for (var k in data) {
711 if(data[k].type!='popup') {
712 var el = document.getElementsByName(data[k].name)[0];
713 params[data[k].name] = '';
714 if(el.type == 'checkbox') {
715 params[data[k].name] = el.checked;
716 } else if(el.type == 'radio') {
717 var tmp = document.getElementsByName(data[k].name);
718 for(var i in tmp) {
719 if (tmp[i].checked) {
720 params[data[k].name] = tmp[i].value;
721 }
722 }
723 } else {
724 params[data[k].name] = el.value;
725 }
726 }
727 }
728 this.request({
729 scope: scope,
730 action:'search',
731 client_id: client_id,
732 repository_id: repository_id,
733 form: {id: 'fp-form-'+scope.options.client_id,upload:false,useDisabled:true},
734 callback: function(id, o, args) {
735 o.issearchresult = true;
736 scope.parse_repository_options(o);
737 scope.view_files();
738 }
739 }, true);
740 }, this);
741 }
742 var download_button = Y.one('#'+download_button_id);
743 if (download_button) {
744 download_button.on('click', function(){
745 alert('download');
746 });
747 }
748 var popup_button = Y.one('#'+popup_button_id);
749 if (popup_button) {
539d4041
DC
750 popup_button.on('click', function(e){
751 M.core_filepicker.active_filepicker = this;
752 window.open(loginurl, 'repo_auth', 'location=0,status=0,width=500,height=300');
753 e.preventDefault();
99eaca9d
DC
754 }, this);
755 }
e0b85db4
DC
756 var elform = Y.one('#'+form_id);
757 elform.on('keydown', function(e) {
758 if (e.keyCode == 13) {
759 switch (action) {
760 case 'search':
761 search_button.simulate('click');
762 break;
763 default:
764 login_button.simulate('click');
765 break;
766 }
767 e.preventDefault();
768 }
769 }, this);
99eaca9d
DC
770
771 },
772 search: function(args) {
773 var data = this.logindata;
774 var params = {};
775
776 for (var k in data) {
777 if(data[k].type!='popup') {
778 var el = document.getElementsByName(data[k].name)[0];
779 params[data[k].name] = '';
780 if(el.type == 'checkbox') {
781 params[data[k].name] = el.checked;
782 } else if(el.type == 'radio') {
783 var tmp = document.getElementsByName(data[k].name);
784 for(var i in tmp) {
785 if (tmp[i].checked) {
786 params[data[k].name] = tmp[i].value;
787 }
788 }
789 } else {
790 params[data[k].name] = el.value;
791 }
792 }
793 }
794 this.request({
795 scope: scope,
796 action:'search',
797 client_id: client_id,
798 repository_id: repository_id,
799 form: {id: 'fp-form-'+scope.options.client_id,upload:false,useDisabled:true},
800 callback: function(id, o, args) {
801 o.issearchresult = true;
802 scope.parse_repository_options(o);
803 scope.view_files();
804 }
805 }, true);
806 },
807 list: function(args) {
808 var scope = this;
809 if (!args) {
810 args = {};
811 }
812 if (!args.repo_id) {
813 args.repo_id = scope.active_repo.id;
814 }
815 scope.request({
816 action:'list',
817 client_id: scope.options.client_id,
818 repository_id: args.repo_id,
819 path:args.path?args.path:'',
820 page:args.page?args.page:'',
821 callback: function(id, obj, args) {
822 if (obj.login) {
823 scope.viewbar.set('disabled', true);
824 scope.print_login(obj);
825 } else if (obj.upload) {
826 scope.viewbar.set('disabled', true);
827 scope.parse_repository_options(obj);
828 scope.create_upload_form(obj);
829
830 } else if (obj.iframe) {
831
832 } else if (obj.list) {
833 obj.issearchresult = false;
834 scope.viewbar.set('disabled', false);
835 scope.parse_repository_options(obj);
836 scope.view_files();
837 }
838 if (obj.msg) {
839 // TODO: Print message
840 }
841 }
842 }, true);
843 },
844 create_upload_form: function(data) {
845 var client_id = this.options.client_id;
846 Y.one('#panel-'+client_id).set('innerHTML', '');
847
848 this.print_header();
849 var id = data.upload.id+'_'+client_id;
850 var str = '<div id="'+id+'_div" class="fp-upload-form">';
0c4edaa2 851 str += '<form id="'+id+'" method="POST">';
1dce6261
DC
852 str += '<table border=1>';
853 str += '<tr><td>';
854 str += '<label for="'+id+'_file">'+data.upload.label+': </label></td>';
855 str += '<td><input type="file" id="'+id+'_file" name="repo_upload_file" />';
856 str += '<input type="hidden" name="itemid" value="'+this.options.itemid+'" /></tr>';
857 str += '<tr>';
858 str += '<td><label>'+M.str.repository.author+': </label></td>';
859 str += '<td><input type="text" name="author" value="'+this.options.author+'" /></td>';
860 str += '</tr>';
861 str += '<tr>';
862 str += '<td>'+M.str.repository.chooselicense+': </td>';
863 str += '<td>';
864 var licenses = this.options.licenses;
865 str += '<select name="license" id="select-license-'+client_id+'">';
866 for (var i in licenses) {
867 str += '<option value="'+licenses[i].shortname+'">'+licenses[i].fullname+'</option>';
868 }
869 str += '</select>';
870 str += '</td>';
871 str += '</tr></table>';
2b728cb5 872 str += '<div class="fp-upload-btn"><a id="'+id+'_action" href="###" >'+M.str.repository.upload+'...</a></div>';
99eaca9d
DC
873 str += '</form>';
874 str += '</div>';
875 var upload_form = Y.Node.create(str);
876 Y.one('#panel-'+client_id).appendChild(upload_form);
877 var scope = this;
878 Y.one('#'+id+'_action').on('click', function() {
4c508047 879 Y.use('io-upload-iframe', function() {
0c4edaa2
DC
880 scope.request({
881 scope: scope,
882 action:'upload',
883 client_id: client_id,
411caa63 884 params: {'savepath':scope.options.savepath},
0c4edaa2
DC
885 repository_id: scope.active_repo.id,
886 form: {id: id, upload:true},
887 callback: function(id, o, args) {
888 if (scope.options.editor_target&&scope.options.env=='editor') {
889 scope.options.editor_target.value=o.url;
890 scope.options.editor_target.onchange();
891 }
892 scope.hide();
893 o.client_id = client_id;
840912d5 894 var formcallback_scope = null;
75440270 895 if (args.scope.options.magicscope) {
840912d5
DC
896 formcallback_scope = args.scope.options.magicscope;
897 } else {
898 formcallback_scope = args.scope;
899 }
900 scope.options.formcallback.apply(formcallback_scope, [o]);
0c4edaa2
DC
901 }
902 }, true);
903 });
99eaca9d
DC
904 }, this);
905 },
906 print_header: function() {
907 var r = this.active_repo;
908 var scope = this;
909 var client_id = this.options.client_id;
910 var repository_id = this.active_repo.id;
911 var panel = Y.one('#panel-'+client_id);
912 var str = '<div id="fp-header-'+client_id+'">';
913 str += '<div class="fp-toolbar" id="repo-tb-'+client_id+'"></div>';
914 str += '</div>';
915 var head = Y.Node.create(str);
916 panel.appendChild(head);
917 //if(this.active_repo.pages < 8){
918 this.print_paging('header');
919 //}
920
921
922 var toolbar = Y.one('#repo-tb-'+client_id);
923
924 if(!r.nosearch) {
411caa63 925 var html = '<a href="###"><img src="'+M.util.image_url('a/search')+'" /> '+M.str.repository.search+'</a>';
99eaca9d
DC
926 var search = Y.Node.create(html);
927 search.on('click', function() {
928 scope.request({
929 scope: scope,
930 action:'searchform',
931 repository_id: repository_id,
932 callback: function(id, obj, args) {
933 var scope = args.scope;
934 var client_id = scope.options.client_id;
935 var repository_id = scope.active_repo.id;
936 var container = document.getElementById('fp-search-dlg');
937 if(container) {
938 container.innerHTML = '';
939 container.parentNode.removeChild(container);
940 }
941 var container = document.createElement('DIV');
942 container.id = 'fp-search-dlg';
943
944 var dlg_title = document.createElement('DIV');
945 dlg_title.className = 'hd';
946 dlg_title.innerHTML = 'filepicker';
947
948 var dlg_body = document.createElement('DIV');
949 dlg_body.className = 'bd';
950
951 var sform = document.createElement('FORM');
952 sform.method = 'POST';
953 sform.id = "fp-search-form";
954 sform.innerHTML = obj.form;
955
956 dlg_body.appendChild(sform);
957 container.appendChild(dlg_title);
958 container.appendChild(dlg_body);
959 Y.one(document.body).appendChild(container);
960 var search_dialog= null;
961 function dialog_handler() {
962 scope.viewbar.set('disabled', false);
963 scope.request({
964 scope: scope,
965 action:'search',
966 client_id: client_id,
967 repository_id: repository_id,
968 form: {id: 'fp-search-form',upload:false,useDisabled:true},
969 callback: function(id, o, args) {
970 scope.parse_repository_options(o);
971 scope.view_files();
972 }
973 }, true);
974 search_dialog.cancel();
975 }
976
283cf6ec 977 search_dialog = new YAHOO.widget.Dialog("fp-search-dlg", {
99eaca9d
DC
978 postmethod: 'async',
979 draggable: true,
980 width : "30em",
981 fixedcenter : true,
283cf6ec 982 zindex: 9999991,
99eaca9d
DC
983 visible : false,
984 constraintoviewport : true,
985 buttons: [
986 {
2b728cb5 987 text:M.str.repository.submit,
99eaca9d
DC
988 handler:dialog_handler,
989 isDefault:true
990 }, {
2b728cb5 991 text:M.str.moodle.cancel,
99eaca9d
DC
992 handler:function(){
993 this.destroy()
994 }
995 }]
996 });
997 search_dialog.render();
998 search_dialog.show();
999 }
1000 });
1001 },this);
1002 toolbar.appendChild(search);
1003 }
1004 // weather we use cache for this instance, this button will reload listing anyway
1005 if(!r.norefresh) {
2b728cb5 1006 var html = '<a href="###"><img src="'+M.util.image_url('a/refresh')+'" /> '+M.str.repository.refresh+'</a>';
99eaca9d
DC
1007 var refresh = Y.Node.create(html);
1008 refresh.on('click', function() {
1009 this.list();
1010 }, this);
1011 toolbar.appendChild(refresh);
1012 }
1013 if(!r.nologin) {
2b728cb5 1014 var html = '<a href="###"><img src="'+M.util.image_url('a/logout')+'" /> '+M.str.repository.logout+'</a>';
99eaca9d
DC
1015 var logout = Y.Node.create(html);
1016 logout.on('click', function() {
1017 this.request({
1018 action:'logout',
1019 client_id: client_id,
1020 repository_id: repository_id,
1021 path:'',
1022 callback: function(id, obj, args) {
1023 scope.viewbar.set('disabled', true);
1024 scope.print_login(obj);
1025 }
1026 }, true);
1027 }, this);
1028 toolbar.appendChild(logout);
1029 }
1030
1031 if(r.manage) {
1032 var mgr = document.createElement('A');
1033 mgr.href = r.manage;
1034 mgr.target = "_blank";
2b728cb5 1035 mgr.innerHTML = '<img src="'+M.util.image_url('a/setting')+'" /> '+M.str.repository.manageurl;
99eaca9d
DC
1036 toolbar.appendChild(mgr);
1037 }
1038 if(r.help) {
1039 var help = document.createElement('A');
1040 help.href = r.help;
1041 help.target = "_blank";
2b728cb5 1042 help.innerHTML = '<img src="'+M.util.image_url('a/help')+'" /> '+M.str.repository.help;
99eaca9d
DC
1043 toolbar.appendChild(help);
1044 }
1045
1046 // only show in icons view
1047 if (this.viewmode == 1) {
1048 this.print_path();
1049 }
1050 },
1051 get_page_button: function(page) {
1052 var r = this.active_repo;
1053 var css = '';
1054 if (page == r.page) {
1055 css = 'class="cur_page" ';
1056 }
1057 var str = '<a '+css+'href="###" id="repo-page-'+page+'">';
1058 return str;
1059 },
1060 print_paging: function(html_id) {
1061 var client_id = this.options.client_id;
1062 var scope = this;
1063 var r = this.active_repo;
1064 var str = '';
1065 var action = '';
1066 if(r.pages) {
1067 str += '<div class="fp-paging" id="paging-'+html_id+'-'+client_id+'">';
1068 str += this.get_page_button(1)+'1</a> ';
1069
1070 var span = 5;
1071 var ex = (span-1)/2;
1072
1073 if (r.page+ex>=r.pages) {
1074 var max = r.pages;
1075 } else {
1076 if (r.page<span) {
1077 var max = span;
1078 } else {
1079 var max = r.page+ex;
1080 }
1081 }
1082
1083 // won't display upper boundary
1084 if (r.page >= span) {
1085 str += ' ... ';
1086 for(var i=r.page-ex; i<max; i++) {
1087 str += this.get_page_button(i);
1088 str += String(i);
1089 str += '</a> ';
1090 }
1091 } else {
1092 // this very first elements
1093 for(var i = 2; i < max; i++) {
1094 str += this.get_page_button(i);
1095 str += String(i);
1096 str += '</a> ';
1097 }
1098 }
1099
1100 // won't display upper boundary
1101 if (max==r.pages) {
1102 str += this.get_page_button(r.pages)+r.pages+'</a>';
1103 } else {
1104 str += this.get_page_button(max)+max+'</a>';
1105 str += ' ... '+this.get_page_button(r.pages)+r.pages+'</a>';
1106 }
1107 str += '</div>';
1108 }
1109 if (str) {
1110 var a = Y.Node.create(str);
1111 Y.one('#fp-header-'+client_id).appendChild(a);
1112
1113 Y.all('#fp-header-'+client_id+' .fp-paging a').each(
1114 function(node, id) {
1115 node.on('click', function(e) {
1116 var id = node.get('id');
1117 var re = new RegExp("repo-page-(\\d+)", "i");
1118 var result = id.match(re);
1119 var args = {};
1120 args.page = result[1];
1121 if (scope.active_repo.issearchresult) {
1122 scope.request({
1123 scope: scope,
1124 action:'search',
1125 client_id: client_id,
1126 repository_id: r.id,
1127 params: {'page':result[1]},
1128 callback: function(id, o, args) {
1129 o.issearchresult = true;
1130 scope.parse_repository_options(o);
1131 scope.view_files();
1132 }
1133 }, true);
1134
1135 } else {
1136 scope.list(args);
1137 }
1138 });
1139 });
1140 }
1141 },
1142 print_path: function() {
1143 var client_id = this.options.client_id;
1144 if (this.viewmode == 2) {
1145 return;
1146 }
1147 var panel = Y.one('#panel-'+client_id);
1148 var p = this.filepath;
4c508047 1149 if (p && p.length!=0) {
99eaca9d
DC
1150 var path = Y.Node.create('<div id="path-'+client_id+'" class="fp-pathbar"></div>');
1151 panel.appendChild(path);
1152 for(var i = 0; i < p.length; i++) {
1153 var link = document.createElement('A');
1154 link.href = "###";
1155 link.innerHTML = p[i].name;
1156 //link.id = 'path-'+client_id+'-'+repo_id;
1157 var sep = Y.Node.create('<span>/</span>');
1158
1159 path.appendChild(link);
1160 path.appendChild(sep);
1161 }
1162 }
1163 },
1164 hide: function() {
1165 this.mainui.hide();
1166 },
1167 show: function() {
1168 if (this.rendered) {
1169 var panel = Y.one('#panel-'+this.options.client_id);
1170 panel.set('innerHTML', '');
1171 this.mainui.show();
1172 } else {
1173 this.launch();
1174 }
1175 },
1176 launch: function() {
7b42e81a 1177 this.render();
99eaca9d
DC
1178 }
1179 });
4c508047
PS
1180
1181 M.core_filepicker.instances[options.client_id] = new FilePickerHelper(options);
1182};