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