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