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