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