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