webservice MDL-21351 add an overview page to facilitate the web service administration
[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
44YUI.add('filepicker', function(Y) {
45 function filepicker (args) {
46 filepicker.superclass.constructor.apply(this, arguments);
47 }
48 filepicker.NAME = "FilePicker";
49 filepicker.ATTRS = {
50 options: {},
51 lang: {}
52 };
53 filepicker.json_decode = function(string, source) {
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 }
62 Y.extend(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 }
98 var data = filepicker.json_decode(o.responseText);
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 }
281 //if (list.length == 0 && !this.upload) {
282 //panel.innerHTML = '<div class="fp-error">'+mstr.repository.emptylist+'</div>';
283 //}
284 //container.appendChild(panel);
285 //repository_client.print_footer(client_id);
286 },
287 select_file: function(args) {
288 var client_id = this.options.client_id;
289 var thumbnail = Y.one('#fp-grid-panel-'+client_id);
290 if(thumbnail){
291 thumbnail.setStyle('display', 'none');
292 }
293 var header = Y.one('#fp-header-'+client_id);
294 if (header) {
295 header.setStyle('display', 'none');
296 }
297 var footer = Y.one('#fp-footer-'+client_id);
298 if (footer) {
299 footer.setStyle('display', 'none');
300 }
301 var path = Y.one('#path-'+client_id);
302 if(path){
303 path.setStyle('display', 'none');
304 }
305 var panel = Y.one('#panel-'+client_id);
306 var html = '<div class="fp-rename-form">';
307 html += '<p><img src="'+args.thumbnail+'" /></p>';
308 html += '<p><label for="newname-'+client_id+'">'+mstr.repository.saveas+'</label>';
309 html += '<input type="text" id="newname-'+client_id+'" value="'+args.title+'" /></p>';
310
311 var le_checked = '';
312 var le_style = '';
313 if (this.options.repositories[this.active_repo.id].return_types == 1) {
314 // support external links only
315 le_checked = 'checked';
316 le_style = ' style="display:none;"';
317 } else if(this.options.repositories[this.active_repo.id].return_types == 2) {
318 // support internal files only
319 le_style = ' style="display:none;"';
320 }
321 if (this.options.externallink && this.options.env == 'editor') {
322 html += '<p'+le_style+'><input type="checkbox" id="linkexternal-'+client_id+'" value="" '+le_checked+' />'+mstr.repository.linkexternal+'</p>';
323 }
324 html += '<p><input type="hidden" id="filesource-'+client_id+'" value="'+args.source+'" />';
325 html += '<input type="button" id="fp-confirm-'+client_id+'" value="'+mstr.repository.getfile+'" />';
326 html += '<input type="button" id="fp-cancel-'+client_id+'" value="'+mstr.moodle.cancel+'" /></p>';
327 html += '</div>';
328
329 var getfile_form = Y.Node.create(html);
330 panel.appendChild(getfile_form);
331
332 var getfile = Y.one('#fp-confirm-'+client_id);
333 getfile.on('click', function(e) {
334 var client_id = this.options.client_id;
335 var scope = this;
336 var repository_id = this.active_repo.id;
337 var title = Y.one('#newname-'+client_id).get('value');
338 var filesource = Y.one('#filesource-'+client_id).get('value');
339 var params = {'title':title, 'file':filesource};
340
341 if (this.options.env == 'editor') {
342 var linkexternal = Y.one('#linkexternal-'+client_id).get('checked');
343 if (linkexternal) {
344 params['linkexternal'] = 'yes';
345 }
346 } if (this.options.env == 'url') {
347 params['linkexternal'] = 'yes';
348 }
349
350 this.wait('download', title);
351 this.request({
352 action:'download',
353 client_id: client_id,
354 repository_id: repository_id,
355 'params': params,
356 callback: function(id, obj, args) {
357 if (scope.options.editor_target&&scope.options.env=='editor') {
358 scope.options.editor_target.value=obj.url;
359 scope.options.editor_target.onchange();
360 }
361 scope.hide();
0c4edaa2 362 obj.client_id = client_id;
99eaca9d
DC
363 scope.options.formcallback(obj);
364 }
365 }, true);
366 }, this);
367 var cancel = Y.one('#fp-cancel-'+client_id);
368 cancel.on('click', function(e) {
369 this.view_files();
370 }, this);
371 var treeview = Y.one('#treeview-'+client_id);
372 if (treeview){
373 treeview.setStyle('display', 'none');
374 }
375 },
376 wait: function(type) {
377 var panel = Y.one('#panel-'+this.options.client_id);
378 panel.set('innerHTML', '');
379 var name = '';
380 var str = '<div style="text-align:center">';
381 if(type=='load') {
382 str += '<img src="'+this.options.pix.loading+'" />';
383 str += '<p>'+mstr.repository.loading+'</p>';
384 }else{
385 str += '<img src="'+this.options.pix.progressbar+'" />';
386 str += '<p>'+mstr.repository.copying+' <strong>'+name+'</strong></p>';
387 }
388 str += '</div>';
389 try {
390 panel.set('innerHTML', str);
391 } catch(e) {
392 alert(e.toString());
393 }
394 },
395 render: function() {
396 var client_id = this.options.client_id;
397 var filepicker_id = 'filepicker-'+client_id;
398 var fpnode = Y.Node.create('<div class="file-picker" id="'+filepicker_id+'"></div>');
399 Y.one(document.body).appendChild(fpnode);
400 // render file picker panel
401 this.mainui = new YAHOO.widget.Panel(filepicker_id, {
402 draggable: true,
403 close: true,
404 underlay: 'none',
405 zindex: 9999999,
406 monitorresize: false,
407 xy: [50, YAHOO.util.Dom.getDocumentScrollTop()+20]
408 });
409 var layout = null;
410 this.mainui.beforeRenderEvent.subscribe(function() {
411 YAHOO.util.Event.onAvailable('layout-'+client_id, function() {
412 layout = new YAHOO.widget.Layout('layout-'+client_id, {
413 height: 480, width: 700,
414 units: [
415 {position: 'top', height: 32, resize: false,
416 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'},
417 {position: 'left', width: 200, resize: true, scroll:true,
418 body:'<ul class="fp-list" id="fp-list-'+client_id+'"></ul>', gutter: '0 5 0 2', minWidth: 150, maxWidth: 300 },
419 {position: 'center', body: '<div class="fp-panel" id="panel-'+client_id+'"></div>',
420 scroll: true, gutter: '0 2 0 0' }
421 ]
422 });
423 layout.render();
424 });
425 });
426 this.mainui.setHeader('File Picker');
427 this.mainui.setBody('<div id="layout-'+client_id+'"></div>');
428 this.mainui.render();
429 this.rendered = true;
430
431 var scope = this;
432 // adding buttons
433 var view_icons = {label: mstr.repository.iconview, value: 't',
434 onclick: {
435 fn: function(){
436 scope.view_as_icons();
437 }
438 }
439 };
440 var view_listing = {label: mstr.repository.listview, value: 'l',
441 onclick: {
442 fn: function(){
443 scope.view_as_list();
444 }
445 }
446 };
447 this.viewbar = new YAHOO.widget.ButtonGroup({
448 id: 'btngroup-'+client_id,
449 name: 'buttons',
450 disabled: true,
451 container: 'fp-viewbar-'+client_id
452 });
453 this.viewbar.addButtons([view_icons, view_listing]);
454 // processing repository listing
455 var r = this.options.repositories;
456 Y.on('contentready', function(el) {
457 var list = Y.one(el);
458 var count = 0;
459 for (var i in r) {
460 var id = 'repository-'+client_id+'-'+count;
461 var link_id = id + '-link';
462 list.append('<li id="'+id+'"><a class="fp-repo-name" id="'+link_id+'" href="###">'+r[i].name+'</a></li>');
463 Y.one('#'+link_id).prepend('<img src="'+r[i].icon+'" width="16" height="16" />&nbsp;');
464 Y.one('#'+link_id).on('click', function(e, scope, repository_id) {
465 scope.repository_id = repository_id;
466 Y.all(el+' li a').setStyle('backgroundColor', 'transparent');
467 e.currentTarget.setStyle('backgroundColor', '#CCC');
468 this.list({'repo_id':repository_id});
469 }, this /*handler running scope*/, this/*second argument*/, r[i].id/*third argument of handler*/);
470 count++;
471 }
472 }, '#fp-list-'+client_id, this /* handler running scope */, '#fp-list-'+client_id /*first argument of handler*/);
473 },
474 parse_repository_options: function(data) {
475 this.filelist = data.list?data.list:null;
476 this.filepath = data.path?data.path:null;
477 this.active_repo = {};
478 this.active_repo.issearchresult = Boolean(data.issearchresult);
479 this.active_repo.pages = Number(data.pages?data.pages:null);
480 this.active_repo.page = Number(data.page?data.page:null);
481 this.active_repo.id = data.repo_id?data.repo_id:null;
482 this.active_repo.nosearch = data.nosearch?true:false;
483 this.active_repo.norefresh = data.norefresh?true:false;
484 this.active_repo.nologin = data.nologin?true:false;
485 this.active_repo.help = data.help?data.help:null;
486 this.active_repo.manage = data.manage?data.manage:null;
487 },
488 print_login: function(data) {
489 var client_id = this.options.client_id;
490 var repository_id = data.repo_id;
491 var l = this.logindata = data.login;
492 var loginurl = '';
493 var panel = Y.one('#panel-'+client_id);
494 var action = 'login';
495 if (data['login_btn_action']) {
496 action=data['login_btn_action'];
497 }
498 var form_id = 'fp-form-'+client_id;
499 var download_button_id = 'fp-form-download-button-'+client_id;
500 var search_button_id = 'fp-form-search-button-'+client_id;
501 var login_button_id = 'fp-form-login-button-'+client_id;
502 var popup_button_id = 'fp-form-popup-button-'+client_id;
503
504 var str = '<div class="fp-login-form">';
505 str += '<form id="'+form_id+'">';
506 var has_pop = false;
507 str +='<table width="100%">';
508 for(var k in l) {
509 str +='<tr>';
510 if(l[k].type=='popup') {
511 // pop element
512 loginurl = l[k].url;
513 str += '<td colspan="2"><p class="fp-popup">'+mstr.repository.popup+'</p>';
514 str += '<p class="fp-popup"><button id="'+popup_button_id+'">'+mstr.repository.login+'</button>';
515 str += '</p></td>';
516 action = 'popup';
517 }else if(l[k].type=='textarea') {
518 // textarea element
519 str += '<td colspan="2"><p><textarea id="'+l[k].id+'" name="'+l[k].name+'"></textarea></p></td>';
520 }else if(l[k].type=='select') {
521 // select element
522 str += '<td align="right"><label>'+l[k].label+':</label></td>';
523 str += '<td align="left"><select id="'+l[k].id+'" name="'+l[k].name+'">';
524 for (i in l[k].options) {
525 str += '<option value="'+l[k].options[i].value+'">'+l[k].options[i].label+'</option>';
526 }
527 str += '</select></td>';
528 }else{
529 // input element
530 var label_id = '';
531 var field_id = '';
532 var field_value = '';
533 if(l[k].id) {
534 label_id = ' for="'+l[k].id+'"';
535 field_id = ' id="'+l[k].id+'"';
536 }
537 if (l[k].label) {
538 str += '<td align="right" width="30%" valign="center">';
539 str += '<label'+label_id+'>'+l[k].label+'</label> </td>';
540 } else {
541 str += '<td width="30%"></td>';
542 }
543 if(l[k].value) {
544 field_value = ' value="'+l[k].value+'"';
545 }
546 if(l[k].type=='radio'){
547 var list = l[k].value.split('|');
548 var labels = l[k].value_label.split('|');
549 str += '<td align="left">';
550 for(var item in list) {
551 str +='<input type="'+l[k].type+'"'+' name="'+l[k].name+'"'+
552 field_id+' value="'+list[item]+'" />'+labels[item]+'<br />';
553 }
554 str += '</td>';
555 }else{
556 str += '<td align="left">';
557 str += '<input type="'+l[k].type+'"'+' name="'+l[k].name+'"'+field_value+' '+field_id+' />';
558 str += '</td>';
559 }
560 }
561 str +='</tr>';
562 }
563 str +='</table>';
564 str += '</form>';
565
566 // custom lable text
567 var btn_label = data['login_btn_label']?data['login_btn_label']:mstr.repository.submit;
568 if (action != 'popup') {
569 str += '<p><input type="button" id="';
570 switch (action) {
571 case 'search':
572 str += search_button_id;
573 break;
574 case 'download':
575 str += download_button_id;
576 break;
577 default:
578 str += login_button_id;
579 break;
580 }
581 str += '" value="'+btn_label+'" /></p>';
582 }
583
584 str += '</div>';
585
586 // insert login form
587 try {
588 panel.set('innerHTML', str);
589 } catch(e) {
590 alert(e.toString()+mstr.quiz.xhtml);
591 }
592 // register buttons
593 // process login action
594 var login_button = Y.one('#'+login_button_id);
595 var scope = this;
596 if (login_button) {
597 login_button.on('click', function(){
598 // collect form data
599 var data = this.logindata;
600 var scope = this;
601 var params = {};
602 for (var k in data) {
603 if(data[k].type!='popup') {
604 var el = Y.one('[name='+data[k].name+']');
605 var type = el.get('type');
606 params[data[k].name] = '';
607 if(type == 'checkbox') {
608 params[data[k].name] = el.get('checked');
609 } else {
610 params[data[k].name] = el.get('value');
611 }
612 }
613 }
614 // start ajax request
615 this.request({
616 'params': params,
617 'scope': scope,
618 'action':'signin',
619 'path': '',
620 'client_id': client_id,
621 'repository_id': repository_id,
622 'callback': function(id, o, args) {
623 scope.parse_repository_options(o);
624 scope.view_files();
625 if (o.msg) {
626 // do something
627 }
628 }
629 }, true);
630 }, this);
631 }
632 var search_button = Y.one('#'+search_button_id);
633 if (search_button) {
634 search_button.on('click', function(){
635 var data = this.logindata;
636 var params = {};
637
638 for (var k in data) {
639 if(data[k].type!='popup') {
640 var el = document.getElementsByName(data[k].name)[0];
641 params[data[k].name] = '';
642 if(el.type == 'checkbox') {
643 params[data[k].name] = el.checked;
644 } else if(el.type == 'radio') {
645 var tmp = document.getElementsByName(data[k].name);
646 for(var i in tmp) {
647 if (tmp[i].checked) {
648 params[data[k].name] = tmp[i].value;
649 }
650 }
651 } else {
652 params[data[k].name] = el.value;
653 }
654 }
655 }
656 this.request({
657 scope: scope,
658 action:'search',
659 client_id: client_id,
660 repository_id: repository_id,
661 form: {id: 'fp-form-'+scope.options.client_id,upload:false,useDisabled:true},
662 callback: function(id, o, args) {
663 o.issearchresult = true;
664 scope.parse_repository_options(o);
665 scope.view_files();
666 }
667 }, true);
668 }, this);
669 }
670 var download_button = Y.one('#'+download_button_id);
671 if (download_button) {
672 download_button.on('click', function(){
673 alert('download');
674 });
675 }
676 var popup_button = Y.one('#'+popup_button_id);
677 if (popup_button) {
678 popup_button.on('click', function(){
679 window.open(loginurl, 'repo_auth', 'location=0,status=0,scrollbars=0,width=500,height=300');
680 active_filepicker = this;
681 }, this);
682 }
683
684 },
685 search: function(args) {
686 var data = this.logindata;
687 var params = {};
688
689 for (var k in data) {
690 if(data[k].type!='popup') {
691 var el = document.getElementsByName(data[k].name)[0];
692 params[data[k].name] = '';
693 if(el.type == 'checkbox') {
694 params[data[k].name] = el.checked;
695 } else if(el.type == 'radio') {
696 var tmp = document.getElementsByName(data[k].name);
697 for(var i in tmp) {
698 if (tmp[i].checked) {
699 params[data[k].name] = tmp[i].value;
700 }
701 }
702 } else {
703 params[data[k].name] = el.value;
704 }
705 }
706 }
707 this.request({
708 scope: scope,
709 action:'search',
710 client_id: client_id,
711 repository_id: repository_id,
712 form: {id: 'fp-form-'+scope.options.client_id,upload:false,useDisabled:true},
713 callback: function(id, o, args) {
714 o.issearchresult = true;
715 scope.parse_repository_options(o);
716 scope.view_files();
717 }
718 }, true);
719 },
720 list: function(args) {
721 var scope = this;
722 if (!args) {
723 args = {};
724 }
725 if (!args.repo_id) {
726 args.repo_id = scope.active_repo.id;
727 }
728 scope.request({
729 action:'list',
730 client_id: scope.options.client_id,
731 repository_id: args.repo_id,
732 path:args.path?args.path:'',
733 page:args.page?args.page:'',
734 callback: function(id, obj, args) {
735 if (obj.login) {
736 scope.viewbar.set('disabled', true);
737 scope.print_login(obj);
738 } else if (obj.upload) {
739 scope.viewbar.set('disabled', true);
740 scope.parse_repository_options(obj);
741 scope.create_upload_form(obj);
742
743 } else if (obj.iframe) {
744
745 } else if (obj.list) {
746 obj.issearchresult = false;
747 scope.viewbar.set('disabled', false);
748 scope.parse_repository_options(obj);
749 scope.view_files();
750 }
751 if (obj.msg) {
752 // TODO: Print message
753 }
754 }
755 }, true);
756 },
757 create_upload_form: function(data) {
758 var client_id = this.options.client_id;
759 Y.one('#panel-'+client_id).set('innerHTML', '');
760
761 this.print_header();
762 var id = data.upload.id+'_'+client_id;
763 var str = '<div id="'+id+'_div" class="fp-upload-form">';
0c4edaa2 764 str += '<form id="'+id+'" method="POST">';
99eaca9d
DC
765 str += '<label for="'+id+'_file">'+data.upload.label+': </label>';
766 str += '<input type="file" id="'+id+'_file" name="repo_upload_file" />';
0c4edaa2 767 str += '<input type="hidden" name="itemid" value="'+this.options.itemid+'" />';
99eaca9d
DC
768 str += '<div class="fp-upload-btn"><a id="'+id+'_action" href="###" >'+mstr.repository.upload+'</a></div>';
769 str += '</form>';
770 str += '</div>';
771 var upload_form = Y.Node.create(str);
772 Y.one('#panel-'+client_id).appendChild(upload_form);
773 var scope = this;
774 Y.one('#'+id+'_action').on('click', function() {
0c4edaa2
DC
775 Y.use('io-upload-iframe' ,function() {
776 scope.request({
777 scope: scope,
778 action:'upload',
779 client_id: client_id,
780 params: {'savepath':'/'},
781 repository_id: scope.active_repo.id,
782 form: {id: id, upload:true},
783 callback: function(id, o, args) {
784 if (scope.options.editor_target&&scope.options.env=='editor') {
785 scope.options.editor_target.value=o.url;
786 scope.options.editor_target.onchange();
787 }
788 scope.hide();
789 o.client_id = client_id;
790 scope.options.formcallback(o);
791 }
792 }, true);
793 });
99eaca9d
DC
794 }, this);
795 },
796 print_header: function() {
797 var r = this.active_repo;
798 var scope = this;
799 var client_id = this.options.client_id;
800 var repository_id = this.active_repo.id;
801 var panel = Y.one('#panel-'+client_id);
802 var str = '<div id="fp-header-'+client_id+'">';
803 str += '<div class="fp-toolbar" id="repo-tb-'+client_id+'"></div>';
804 str += '</div>';
805 var head = Y.Node.create(str);
806 panel.appendChild(head);
807 //if(this.active_repo.pages < 8){
808 this.print_paging('header');
809 //}
810
811
812 var toolbar = Y.one('#repo-tb-'+client_id);
813
814 if(!r.nosearch) {
815 var html = '<a href="###"><img src="'+this.options.pix.search+'" /> '+mstr.repository.search+'</a>';
816 var search = Y.Node.create(html);
817 search.on('click', function() {
818 scope.request({
819 scope: scope,
820 action:'searchform',
821 repository_id: repository_id,
822 callback: function(id, obj, args) {
823 var scope = args.scope;
824 var client_id = scope.options.client_id;
825 var repository_id = scope.active_repo.id;
826 var container = document.getElementById('fp-search-dlg');
827 if(container) {
828 container.innerHTML = '';
829 container.parentNode.removeChild(container);
830 }
831 var container = document.createElement('DIV');
832 container.id = 'fp-search-dlg';
833
834 var dlg_title = document.createElement('DIV');
835 dlg_title.className = 'hd';
836 dlg_title.innerHTML = 'filepicker';
837
838 var dlg_body = document.createElement('DIV');
839 dlg_body.className = 'bd';
840
841 var sform = document.createElement('FORM');
842 sform.method = 'POST';
843 sform.id = "fp-search-form";
844 sform.innerHTML = obj.form;
845
846 dlg_body.appendChild(sform);
847 container.appendChild(dlg_title);
848 container.appendChild(dlg_body);
849 Y.one(document.body).appendChild(container);
850 var search_dialog= null;
851 function dialog_handler() {
852 scope.viewbar.set('disabled', false);
853 scope.request({
854 scope: scope,
855 action:'search',
856 client_id: client_id,
857 repository_id: repository_id,
858 form: {id: 'fp-search-form',upload:false,useDisabled:true},
859 callback: function(id, o, args) {
860 scope.parse_repository_options(o);
861 scope.view_files();
862 }
863 }, true);
864 search_dialog.cancel();
865 }
866
867 var search_dialog = new YAHOO.widget.Dialog("fp-search-dlg", {
868 postmethod: 'async',
869 draggable: true,
870 width : "30em",
871 fixedcenter : true,
872 zindex: 766667,
873 visible : false,
874 constraintoviewport : true,
875 buttons: [
876 {
877 text:mstr.repository.submit,
878 handler:dialog_handler,
879 isDefault:true
880 }, {
881 text:mstr.moodle.cancel,
882 handler:function(){
883 this.destroy()
884 }
885 }]
886 });
887 search_dialog.render();
888 search_dialog.show();
889 }
890 });
891 },this);
892 toolbar.appendChild(search);
893 }
894 // weather we use cache for this instance, this button will reload listing anyway
895 if(!r.norefresh) {
896 var html = '<a href="###"><img src="'+this.options.pix.refresh+'" /> '+mstr.repository.refresh+'</a>';
897 var refresh = Y.Node.create(html);
898 refresh.on('click', function() {
899 this.list();
900 }, this);
901 toolbar.appendChild(refresh);
902 }
903 if(!r.nologin) {
904 var html = '<a href="###"><img src="'+this.options.pix.logout+'" /> '+mstr.repository.logout+'</a>';
905 var logout = Y.Node.create(html);
906 logout.on('click', function() {
907 this.request({
908 action:'logout',
909 client_id: client_id,
910 repository_id: repository_id,
911 path:'',
912 callback: function(id, obj, args) {
913 scope.viewbar.set('disabled', true);
914 scope.print_login(obj);
915 }
916 }, true);
917 }, this);
918 toolbar.appendChild(logout);
919 }
920
921 if(r.manage) {
922 var mgr = document.createElement('A');
923 mgr.href = r.manage;
924 mgr.target = "_blank";
925 mgr.innerHTML = '<img src="'+this.options.pix.setting+'" /> '+mstr.repository.manageurl;
926 toolbar.appendChild(mgr);
927 }
928 if(r.help) {
929 var help = document.createElement('A');
930 help.href = r.help;
931 help.target = "_blank";
932 help.innerHTML = '<img src="'+this.options.pix.help+'" /> '+mstr.repository.help;
933 toolbar.appendChild(help);
934 }
935
936 // only show in icons view
937 if (this.viewmode == 1) {
938 this.print_path();
939 }
940 },
941 get_page_button: function(page) {
942 var r = this.active_repo;
943 var css = '';
944 if (page == r.page) {
945 css = 'class="cur_page" ';
946 }
947 var str = '<a '+css+'href="###" id="repo-page-'+page+'">';
948 return str;
949 },
950 print_paging: function(html_id) {
951 var client_id = this.options.client_id;
952 var scope = this;
953 var r = this.active_repo;
954 var str = '';
955 var action = '';
956 if(r.pages) {
957 str += '<div class="fp-paging" id="paging-'+html_id+'-'+client_id+'">';
958 str += this.get_page_button(1)+'1</a> ';
959
960 var span = 5;
961 var ex = (span-1)/2;
962
963 if (r.page+ex>=r.pages) {
964 var max = r.pages;
965 } else {
966 if (r.page<span) {
967 var max = span;
968 } else {
969 var max = r.page+ex;
970 }
971 }
972
973 // won't display upper boundary
974 if (r.page >= span) {
975 str += ' ... ';
976 for(var i=r.page-ex; i<max; i++) {
977 str += this.get_page_button(i);
978 str += String(i);
979 str += '</a> ';
980 }
981 } else {
982 // this very first elements
983 for(var i = 2; i < max; i++) {
984 str += this.get_page_button(i);
985 str += String(i);
986 str += '</a> ';
987 }
988 }
989
990 // won't display upper boundary
991 if (max==r.pages) {
992 str += this.get_page_button(r.pages)+r.pages+'</a>';
993 } else {
994 str += this.get_page_button(max)+max+'</a>';
995 str += ' ... '+this.get_page_button(r.pages)+r.pages+'</a>';
996 }
997 str += '</div>';
998 }
999 if (str) {
1000 var a = Y.Node.create(str);
1001 Y.one('#fp-header-'+client_id).appendChild(a);
1002
1003 Y.all('#fp-header-'+client_id+' .fp-paging a').each(
1004 function(node, id) {
1005 node.on('click', function(e) {
1006 var id = node.get('id');
1007 var re = new RegExp("repo-page-(\\d+)", "i");
1008 var result = id.match(re);
1009 var args = {};
1010 args.page = result[1];
1011 if (scope.active_repo.issearchresult) {
1012 scope.request({
1013 scope: scope,
1014 action:'search',
1015 client_id: client_id,
1016 repository_id: r.id,
1017 params: {'page':result[1]},
1018 callback: function(id, o, args) {
1019 o.issearchresult = true;
1020 scope.parse_repository_options(o);
1021 scope.view_files();
1022 }
1023 }, true);
1024
1025 } else {
1026 scope.list(args);
1027 }
1028 });
1029 });
1030 }
1031 },
1032 print_path: function() {
1033 var client_id = this.options.client_id;
1034 if (this.viewmode == 2) {
1035 return;
1036 }
1037 var panel = Y.one('#panel-'+client_id);
1038 var p = this.filepath;
1039 if(p && p.length!=0) {
1040 var path = Y.Node.create('<div id="path-'+client_id+'" class="fp-pathbar"></div>');
1041 panel.appendChild(path);
1042 for(var i = 0; i < p.length; i++) {
1043 var link = document.createElement('A');
1044 link.href = "###";
1045 link.innerHTML = p[i].name;
1046 //link.id = 'path-'+client_id+'-'+repo_id;
1047 var sep = Y.Node.create('<span>/</span>');
1048
1049 path.appendChild(link);
1050 path.appendChild(sep);
1051 }
1052 }
1053 },
1054 hide: function() {
1055 this.mainui.hide();
1056 },
1057 show: function() {
1058 if (this.rendered) {
1059 var panel = Y.one('#panel-'+this.options.client_id);
1060 panel.set('innerHTML', '');
1061 this.mainui.show();
1062 } else {
1063 this.launch();
1064 }
1065 },
1066 launch: function() {
7b42e81a 1067 this.render();
99eaca9d
DC
1068 }
1069 });
1070 Y.filepicker = filepicker;
1071}, '3.0.0', {requires:['base', 'node', 'json', 'async-queue', 'io']});