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