1 // YUI3 File Picker module for moodle
2 // Author: Dongsheng Cai <dongsheng@moodle.com>
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
17 * this.options.client_id, the instance id
18 * this.options.contextid
20 * this.options.repositories, stores all repositories displaied in file picker
21 * this.options.formcallback
23 * Active repository options
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
34 * this.filelist, cached filelist
37 * this.filepath, current path
38 * this.logindata, cached login form
41 M.core_filepicker = M.core_filepicker || {};
44 * instances of file pickers used on page
46 M.core_filepicker.instances = M.core_filepicker.instances || {};
47 M.core_filepicker.active_filepicker = null;
50 * Init and show file picker
52 M.core_filepicker.show = function(Y, options) {
53 if (!M.core_filepicker.instances[options.client_id]) {
54 M.core_filepicker.init(Y, options);
56 M.core_filepicker.instances[options.client_id].show();
60 * Add new file picker to current instances
62 M.core_filepicker.init = function(Y, options) {
63 var FilePickerHelper = function(options) {
64 FilePickerHelper.superclass.constructor.apply(this, arguments);
67 FilePickerHelper.NAME = "FilePickerHelper";
68 FilePickerHelper.ATTRS = {
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 = '/';
83 destructor: function() {
86 request: function(args, redraw) {
87 var client_id = args.client_id;
89 var api = this.api + '?action='+args.action;
91 var api = args.api + '?action='+args.action;
96 scope = args['scope'];
98 params['repo_id']=args.repository_id;
99 params['p'] = args.path?args.path:'';
100 params['page'] = args.page?args.page:'';
101 params['env']=this.options.env;
102 // the form element only accept certain file types
103 params['accepted_types']=this.options.accepted_types;
104 params['sesskey'] = M.cfg.sesskey;
105 params['client_id'] = args.client_id;
106 params['itemid'] = this.options.itemid?this.options.itemid:0;
107 params['maxbytes'] = this.options.maxbytes?this.options.maxbytes:-1;
108 if (this.options.context && this.options.context.id) {
109 params['ctx_id'] = this.options.context.id;
111 if (args['params']) {
112 for (i in args['params']) {
113 params[i] = args['params'][i];
116 if (args.action == 'upload') {
118 for(var k in params) {
119 var value = params[k];
120 if(value instanceof Array) {
121 for(var i in value) {
122 list.push(k+'[]='+value[i]);
125 list.push(k+'='+value);
128 params = list.join('&');
130 params = build_querystring(params);
135 complete: function(id,o,p) {
136 var panel_id = '#panel-'+client_id;
143 data = Y.JSON.parse(o.responseText);
145 scope.print_msg(M.str.repository.invalidjson, 'error');
146 Y.one(panel_id).set('innerHTML', 'ERROR: '+M.str.repository.invalidjson+'<pre>'+stripHTML(o.responseText)+'</pre>');
150 if (data && data.error) {
151 scope.print_msg(data.error, 'error');
153 args.onerror(id,data,p);
155 Y.one(panel_id).set('innerHTML', '');
158 } else if (data && data.event) {
159 switch (data.event) {
161 scope.process_existing_file(data);
168 scope.print_msg(data.msg, 'info');
170 args.callback(id,data,p);
178 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
184 cfg.form = args.form;
191 process_existing_file: function(data) {
193 var repository_id = scope.active_repo.id;
194 var client_id = scope.options.client_id;
195 var handleOverwrite = function() {
199 params['existingfilename'] = data.existingfile.filename;
200 params['existingfilepath'] = data.existingfile.filepath;
201 params['newfilename'] = data.newfile.filename;
202 params['newfilepath'] = data.newfile.filepath;
206 'action':'overwrite',
208 'client_id': client_id,
209 'repository_id': repository_id,
210 'callback': function(id, o, args) {
213 // editor needs to update url
214 // filemanager do nothing
215 if (scope.options.editor_target && scope.options.env == 'editor') {
216 scope.options.editor_target.value = data.existingfile.url;
217 scope.options.editor_target.onchange();
218 } else if (scope.options.env === 'filepicker') {
219 var fileinfo = {'client_id':client_id,
220 'url':data.existingfile.url,
221 'file':data.existingfile.filename};
222 scope.options.formcallback.apply(scope, [fileinfo]);
227 var handleRename = function() {
228 if (scope.options.editor_target && scope.options.env == 'editor') {
229 scope.options.editor_target.value = data.newfile.url;
230 scope.options.editor_target.onchange();
234 var formcallback_scope = null;
235 if (scope.options.magicscope) {
236 formcallback_scope = scope.options.magicscope;
238 formcallback_scope = scope;
240 var fileinfo = {'client_id':client_id,
241 'url':data.newfile.url,
242 'file':data.newfile.filename};
243 scope.options.formcallback.apply(formcallback_scope, [fileinfo]);
245 var handleCancel = function() {
249 params['newfilename'] = data.newfile.filename;
250 params['newfilepath'] = data.newfile.filepath;
254 'action':'deletetmpfile',
256 'client_id': client_id,
257 'repository_id': repository_id,
258 'callback': function(id, o, args) {
264 var dialog = new YAHOO.widget.SimpleDialog("dlg", {
268 icon: YAHOO.widget.SimpleDialog.ICON_HELP,
272 buttons: [{ text: M.str.repository.overwrite, handler: handleOverwrite },
273 { text: M.str.repository.renameto + ' "' + data.newfile.filename + '"', handler: handleRename },
274 { text: M.str.moodle.cancel, handler: handleCancel, isDefault: true}]
276 dialog.setHeader(M.str.repository.fileexistsdialogheader);
277 if (scope.options.env == 'editor') {
278 dialog.setBody(M.str.repository.fileexistsdialog_editor);
280 dialog.setBody(M.str.repository.fileexistsdialog_filemanager);
283 dialog.render(document.body);
286 print_msg: function(msg, type) {
287 var client_id = this.options.client_id;
288 var dlg_id = 'fp-msg-dlg-'+client_id;
289 function handleYes() {
292 var icon = YAHOO.widget.SimpleDialog.ICON_INFO;
294 icon = YAHOO.widget.SimpleDialog.ICON_ALARM;
297 this.msg_dlg = new YAHOO.widget.SimpleDialog(dlg_id,
307 constraintoviewport: true,
308 buttons: [{ text:M.str.moodle.ok, handler:handleYes, isDefault:true }]
310 this.msg_dlg.render(document.body);
312 this.msg_dlg.setBody(msg);
314 var header = M.str.moodle.info;
316 header = M.str.moodle.error;
318 this.msg_dlg.setHeader(header);
321 build_tree: function(node, level) {
322 var client_id = this.options.client_id;
323 var dynload = this.active_repo.dynload;
326 //title:fp_lang.date+' '+node.date+fp_lang.size+' '+node.size,
328 source:node.source?node.source:'',
329 thumbnail:node.thumbnail,
330 path:node.path?node.path:[]
332 var tmpNode = new YAHOO.widget.TextNode(info, level, false);
334 tmpNode.repo_id=node.repo_id;
336 tmpNode.repo_id=this.active_repo.id;
343 tmpNode.scope = this;
345 tmpNode.isLeaf = false;
346 tmpNode.client_id = client_id;
348 tmpNode.path = node.path;
352 for(var c in node.children) {
353 this.build_tree(node.children[c], tmpNode);
356 tmpNode.isLeaf = true;
359 view_files: function(page) {
360 var p= page?page:null;
361 if (this.active_repo.issearchresult) {
362 // list view is desiged to display treeview
363 // it is not working well with search result
364 this.view_as_icons();
366 this.viewbar.set('disabled', false);
367 if (this.viewmode == 1) {
368 this.view_as_icons();
369 } else if (this.viewmode == 2) {
370 this.view_as_list(p);
372 this.view_as_icons();
376 treeview_dynload: function(node, cb) {
377 var scope = node.scope;
378 var client_id = scope.options.client_id;
379 var repository_id = scope.active_repo.id;
382 client_id: client_id,
383 repository_id: repository_id,
384 path:node.path?node.path:'',
385 page:node.page?args.page:'',
386 callback: function(id, obj, args) {
387 obj.issearchresult = false;
389 scope.viewbar.set('disabled', false);
390 scope.parse_repository_options(obj);
392 scope.build_tree(list[k], node);
398 view_as_list: function(p) {
402 if (scope.active_repo.page) {
403 page = scope.active_repo.page;
410 client_id: scope.options.client_id,
411 repository_id: scope.active_repo.id,
414 callback: function(id, obj, args) {
415 scope.parse_repository_options(obj);
417 scope.viewbar.set('disabled', true);
418 scope.print_login(obj);
421 var client_id = scope.options.client_id;
422 var dynload = scope.active_repo.dynload;
424 var panel_id = '#panel-'+client_id;
426 Y.one(panel_id).set('innerHTML', '');
428 scope.print_header();
430 var html = '<div class="fp-tree-panel" id="treeview-'+client_id+'">';
431 if (list && list.length==0) {
432 html += '<div class="fp-emptylist mdl-align">' +M.str.repository.nofilesavailable+'</div>';
436 var tree = Y.Node.create(html);
437 Y.one(panel_id).appendChild(tree);
438 if (!list || list.length==0) {
442 scope.treeview = new YAHOO.widget.TreeView('treeview-'+client_id);
444 scope.treeview.setDynamicLoad(scope.treeview_dynload, 1);
448 scope.build_tree(list[k], scope.treeview.getRoot());
450 scope.treeview.subscribe('clickEvent', function(e){
453 fileinfo['title'] = e.node.data.filename;
454 fileinfo['source'] = e.node.data.source;
455 fileinfo['thumbnail'] = e.node.data.thumbnail;
456 scope.select_file(fileinfo);
459 scope.treeview.draw();
463 view_as_icons: function() {
465 var client_id = this.options.client_id;
466 var list = this.filelist;
467 var panel_id = '#panel-'+client_id;
469 Y.one(panel_id).set('innerHTML', '');
473 var html = '<div class="fp-grid-panel" id="fp-grid-panel-'+client_id+'">';
474 if (list && list.length==0) {
475 html += '<div class="fp-emptylist mdl-align">' +M.str.repository.nofilesavailable+'</div>';
479 var gridpanel = Y.Node.create(html);
480 Y.one('#panel-'+client_id).appendChild(gridpanel);
484 var grid = document.createElement('DIV');
485 grid.className='fp-grid';
487 var title = document.createElement('DIV');
488 title.id = 'grid-title-'+client_id+'-'+String(count);
489 title.className = 'label';
490 var filename = node.title;
491 if (node.shorttitle) {
492 filename = node.shorttitle;
494 var filename_id = 'filname-link-'+client_id+'-'+String(count);
495 title.innerHTML += '<a href="###" id="'+filename_id+'" title="'+node.title+'"><span>'+filename+"</span></a>";
498 if(node.thumbnail_width){
499 grid.style.width = node.thumbnail_width+'px';
500 title.style.width = (node.thumbnail_width-10)+'px';
502 grid.style.width = title.style.width = '90px';
504 var frame = document.createElement('DIV');
505 frame.style.textAlign='center';
506 if(node.thumbnail_height){
507 frame.style.height = node.thumbnail_height+'px';
509 var img = document.createElement('img');
510 img.src = node.thumbnail;
511 img.title = node.title;
512 if(node.thumbnail_alt) {
513 img.alt = node.thumbnail_alt;
515 if(node.thumbnail_title) {
516 img.title = node.thumbnail_title;
519 var link = document.createElement('A');
521 link.id = 'img-id-'+client_id+'-'+String(count);
524 //grid.innerHTML += '<p><a target="_blank" href="'+node.url+'">'+M.str.repository.preview+'</a></p>';
526 link.appendChild(img);
527 frame.appendChild(link);
528 grid.appendChild(frame);
529 grid.appendChild(title);
530 gridpanel.appendChild(grid);
532 var y_title = Y.one('#'+title.id);
533 var y_file = Y.one('#'+link.id);
534 var dynload = this.active_repo.dynload;
536 y_file.on('click', function(e, p) {
538 var params = {'path':p.path};
541 this.filelist = p.children;
545 y_title.on('click', function(e, p, id){
546 var icon = Y.one(id);
547 icon.simulate('click');
548 }, this, node, '#'+link.id);
551 fileinfo['title'] = list[k].title;
552 fileinfo['source'] = list[k].source;
553 fileinfo['thumbnail'] = list[k].thumbnail;
554 fileinfo['haslicense'] = list[k].haslicense?true:false;
555 fileinfo['hasauthor'] = list[k].hasauthor?true:false;
556 y_title.on('click', function(e, args) {
557 this.select_file(args);
559 y_file.on('click', function(e, args) {
560 this.select_file(args);
566 select_file: function(args) {
567 var client_id = this.options.client_id;
568 var thumbnail = Y.one('#fp-grid-panel-'+client_id);
570 thumbnail.setStyle('display', 'none');
572 var header = Y.one('#fp-header-'+client_id);
574 header.setStyle('display', 'none');
576 var footer = Y.one('#fp-footer-'+client_id);
578 footer.setStyle('display', 'none');
580 var path = Y.one('#path-'+client_id);
582 path.setStyle('display', 'none');
584 var panel = Y.one('#panel-'+client_id);
585 var form_id = 'fp-rename-form-'+client_id;
586 var html = '<div class="fp-rename-form" id="'+form_id+'">';
587 html += '<p><img src="'+args.thumbnail+'" /></p>';
588 html += '<table width="100%">';
589 html += '<tr><td class="mdl-right"><label for="newname-'+client_id+'">'+M.str.repository.saveas+':</label></td>';
590 html += '<td class="mdl-left"><input type="text" id="newname-'+client_id+'" value="'+args.title+'" /></td></tr>';
594 if (this.options.repositories[this.active_repo.id].return_types == 1) {
595 // support external links only
596 le_checked = 'checked';
597 le_style = ' style="display:none;"';
598 } else if(this.options.repositories[this.active_repo.id].return_types == 2) {
599 // support internal files only
600 le_style = ' style="display:none;"';
602 if ((this.options.externallink && this.options.env == 'editor' && this.options.return_types != 1)) {
603 html += '<tr'+le_style+'><td></td><td class="mdl-left"><input type="checkbox" id="linkexternal-'+client_id+'" value="" '+le_checked+' />'+M.str.repository.linkexternal+'</td></tr>';
606 if (!args.hasauthor) {
607 // the author of the file
608 html += '<tr><td class="mdl-right"><label for="text-author">'+M.str.repository.author+' :</label></td>';
609 html += '<td class="mdl-left"><input id="text-author-'+client_id+'" type="text" name="author" value="'+this.options.author+'" /></td>';
613 if (!args.haslicense) {
614 // the license of the file
615 var licenses = this.options.licenses;
616 html += '<tr><td class="mdl-right"><label for="select-license-'+client_id+'">'+M.str.repository.chooselicense+' :</label></td>';
617 html += '<td class="mdl-left"><select name="license" id="select-license-'+client_id+'">';
618 var recentlicense = YAHOO.util.Cookie.get('recentlicense');
620 this.options.defaultlicense=recentlicense;
622 for (var i in licenses) {
623 if (this.options.defaultlicense==licenses[i].shortname) {
624 var selected = ' selected';
628 html += '<option value="'+licenses[i].shortname+'"'+selected+'>'+licenses[i].fullname+'</option>';
630 html += '</select></td></tr>';
634 html += '<p><input type="hidden" id="filesource-'+client_id+'" value="'+args.source+'" />';
635 html += '<input type="button" id="fp-confirm-'+client_id+'" value="'+M.str.repository.getfile+'" />';
636 html += '<input type="button" id="fp-cancel-'+client_id+'" value="'+M.str.moodle.cancel+'" /></p>';
639 var getfile_form = Y.Node.create(html);
640 panel.appendChild(getfile_form);
642 var getfile = Y.one('#fp-confirm-'+client_id);
643 getfile.on('click', function(e) {
644 var client_id = this.options.client_id;
646 var repository_id = this.active_repo.id;
647 var title = Y.one('#newname-'+client_id).get('value');
648 var filesource = Y.one('#filesource-'+client_id).get('value');
649 var params = {'title':title, 'source':filesource, 'savepath': this.options.savepath};
650 var license = Y.one('#select-license-'+client_id);
652 params['license'] = license.get('value');
653 YAHOO.util.Cookie.set('recentlicense', license.get('value'));
655 var author = Y.one('#text-author-'+client_id);
657 params['author'] = author.get('value');
660 if (this.options.externallink && this.options.env == 'editor') {
661 // in editor, images are stored in '/' only
662 params.savepath = '/';
663 // when image or media button is clicked
664 if ( this.options.return_types != 1 ) {
665 var linkexternal = Y.one('#linkexternal-'+client_id);
666 if (linkexternal && linkexternal.get('checked')) {
667 params['linkexternal'] = 'yes';
670 // when link button in editor clicked
671 params['linkexternal'] = 'yes';
675 if (this.options.env == 'url') {
676 params['linkexternal'] = 'yes';
679 this.wait('download', title);
682 client_id: client_id,
683 repository_id: repository_id,
685 onerror: function(id, obj, args) {
688 callback: function(id, obj, args) {
689 if (scope.options.editor_target && scope.options.env=='editor') {
690 scope.options.editor_target.value=obj.url;
691 scope.options.editor_target.onchange();
694 obj.client_id = client_id;
695 var formcallback_scope = null;
696 if (args.scope.options.magicscope) {
697 formcallback_scope = args.scope.options.magicscope;
699 formcallback_scope = args.scope;
701 scope.options.formcallback.apply(formcallback_scope, [obj]);
705 var elform = Y.one('#'+form_id);
706 elform.on('keydown', function(e) {
707 if (e.keyCode == 13) {
708 getfile.simulate('click');
712 var cancel = Y.one('#fp-cancel-'+client_id);
713 cancel.on('click', function(e) {
716 var treeview = Y.one('#treeview-'+client_id);
718 treeview.setStyle('display', 'none');
721 wait: function(type) {
722 var panel = Y.one('#panel-'+this.options.client_id);
723 panel.set('innerHTML', '');
725 var str = '<div style="text-align:center">';
727 str += '<img src="'+M.util.image_url('i/loading')+'" />';
728 str += '<p>'+M.str.repository.loading+'</p>';
730 str += '<img src="'+M.util.image_url('i/progressbar')+'" />';
731 str += '<p>'+M.str.repository.copying+' <strong>'+name+'</strong></p>';
735 panel.set('innerHTML', str);
741 var client_id = this.options.client_id;
743 var filepicker_id = 'filepicker-'+client_id;
744 var fpnode = Y.Node.create('<div class="file-picker" id="'+filepicker_id+'"></div>');
745 Y.one(document.body).appendChild(fpnode);
746 // render file picker panel
747 this.mainui = new YAHOO.widget.Panel(filepicker_id, {
752 monitorresize: false,
753 xy: [50, YAHOO.util.Dom.getDocumentScrollTop()+20]
756 this.mainui.beforeRenderEvent.subscribe(function() {
757 YAHOO.util.Event.onAvailable('layout-'+client_id, function() {
758 layout = new YAHOO.widget.Layout('layout-'+client_id, {
759 height: 480, width: 700,
761 {position: 'top', height: 32, resize: false,
762 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'},
763 {position: 'left', width: 200, resize: true, scroll:true,
764 body:'<ul class="fp-list" id="fp-list-'+client_id+'"></ul>', gutter: '0 5 0 2', minWidth: 150, maxWidth: 300 },
765 {position: 'center', body: '<div class="fp-panel" id="panel-'+client_id+'"></div>',
766 scroll: true, gutter: '0 2 0 0' }
770 scope.show_recent_repository();
774 this.mainui.setHeader(M.str.repository.filepicker);
775 this.mainui.setBody('<div id="layout-'+client_id+'"></div>');
776 this.mainui.render();
777 this.rendered = true;
781 var view_icons = {label: M.str.repository.iconview, value: 't', 'checked': true,
784 scope.view_as_icons();
788 var view_listing = {label: M.str.repository.listview, value: 'l',
791 scope.view_as_list();
795 this.viewbar = new YAHOO.widget.ButtonGroup({
796 id: 'btngroup-'+client_id,
799 container: 'fp-viewbar-'+client_id
801 this.viewbar.addButtons([view_icons, view_listing]);
802 // processing repository listing
803 var r = this.options.repositories;
804 Y.on('contentready', function(el) {
805 var list = Y.one(el);
807 // Resort the repositories by sortorder
808 var sorted_repositories = new Array();
810 sorted_repositories[r[i].sortorder - 1] = r[i];
812 for (var i in sorted_repositories){
813 repository = sorted_repositories[i];
814 var id = 'repository-'+client_id+'-'+repository.id;
815 var link_id = id + '-link';
816 list.append('<li id="'+id+'"><a class="fp-repo-name" id="'+link_id+'" href="###">'+repository.name+'</a></li>');
817 Y.one('#'+link_id).prepend('<img src="'+repository.icon+'" width="16" height="16" /> ');
818 Y.one('#'+link_id).on('click', function(e, scope, repository_id) {
819 YAHOO.util.Cookie.set('recentrepository', repository_id);
820 scope.repository_id = repository_id;
821 this.list({'repo_id':repository_id});
822 }, this /*handler running scope*/, this/*second argument*/, repository.id/*third argument of handler*/);
826 if (this.options.externallink) {
827 list.set('innerHTML', M.str.repository.norepositoriesexternalavailable);
829 list.set('innerHTML', M.str.repository.norepositoriesavailable);
832 }, '#fp-list-'+client_id, this /* handler running scope */, '#fp-list-'+client_id /*first argument of handler*/);
834 parse_repository_options: function(data) {
835 this.filelist = data.list?data.list:null;
836 this.filepath = data.path?data.path:null;
837 this.active_repo = {};
838 this.active_repo.issearchresult = Boolean(data.issearchresult);
839 this.active_repo.dynload = data.dynload?data.dynload:false;
840 this.active_repo.pages = Number(data.pages?data.pages:null);
841 this.active_repo.page = Number(data.page?data.page:null);
842 this.active_repo.id = data.repo_id?data.repo_id:null;
843 this.active_repo.nosearch = data.nosearch?true:false;
844 this.active_repo.norefresh = data.norefresh?true:false;
845 this.active_repo.nologin = data.nologin?true:false;
846 this.active_repo.logouttext = data.logouttext?data.logouttext:null;
847 this.active_repo.help = data.help?data.help:null;
848 this.active_repo.manage = data.manage?data.manage:null;
850 print_login: function(data) {
851 this.parse_repository_options(data);
852 var client_id = this.options.client_id;
853 var repository_id = data.repo_id;
854 var l = this.logindata = data.login;
856 var panel = Y.one('#panel-'+client_id);
857 var action = 'login';
858 if (data['login_btn_action']) {
859 action=data['login_btn_action'];
861 var form_id = 'fp-form-'+client_id;
862 var download_button_id = 'fp-form-download-button-'+client_id;
863 var search_button_id = 'fp-form-search-button-'+client_id;
864 var login_button_id = 'fp-form-login-button-'+client_id;
865 var popup_button_id = 'fp-form-popup-button-'+client_id;
867 var str = '<div class="fp-login-form">';
868 str += '<form id="'+form_id+'">';
870 str +='<table width="100%">';
873 if(l[k].type=='popup') {
876 str += '<td colspan="2"><p class="fp-popup">'+M.str.repository.popup+'</p>';
877 str += '<p class="fp-popup"><button id="'+popup_button_id+'">'+M.str.repository.login+'</button>';
880 }else if(l[k].type=='textarea') {
882 str += '<td colspan="2"><p><textarea id="'+l[k].id+'" name="'+l[k].name+'"></textarea></p></td>';
883 }else if(l[k].type=='select') {
885 str += '<td align="right"><label>'+l[k].label+':</label></td>';
886 str += '<td align="left"><select id="'+l[k].id+'" name="'+l[k].name+'">';
887 for (i in l[k].options) {
888 str += '<option value="'+l[k].options[i].value+'">'+l[k].options[i].label+'</option>';
890 str += '</select></td>';
895 var field_value = '';
897 label_id = ' for="'+l[k].id+'"';
898 field_id = ' id="'+l[k].id+'"';
901 str += '<td align="right" width="30%" valign="center">';
902 str += '<label'+label_id+'>'+l[k].label+'</label> </td>';
904 str += '<td width="30%"></td>';
907 field_value = ' value="'+l[k].value+'"';
909 if(l[k].type=='radio'){
910 var list = l[k].value.split('|');
911 var labels = l[k].value_label.split('|');
912 str += '<td align="left">';
913 for(var item in list) {
914 str +='<input type="'+l[k].type+'"'+' name="'+l[k].name+'"'+
915 field_id+' value="'+list[item]+'" />'+labels[item]+'<br />';
919 str += '<td align="left">';
920 str += '<input type="'+l[k].type+'"'+' name="'+l[k].name+'"'+field_value+' '+field_id+' />';
930 var btn_label = data['login_btn_label']?data['login_btn_label']:M.str.repository.submit;
931 if (action != 'popup') {
932 str += '<p><input type="button" id="';
935 str += search_button_id;
938 str += download_button_id;
941 str += login_button_id;
944 str += '" value="'+btn_label+'" /></p>';
951 panel.set('innerHTML', str);
953 alert(M.str.repository.xhtmlerror);
956 // process login action
957 var login_button = Y.one('#'+login_button_id);
960 login_button.on('click', function(){
962 var data = this.logindata;
965 for (var k in data) {
966 if(data[k].type!='popup') {
967 var el = Y.one('[name='+data[k].name+']');
968 var type = el.get('type');
969 params[data[k].name] = '';
970 if(type == 'checkbox') {
971 params[data[k].name] = el.get('checked');
973 params[data[k].name] = el.get('value');
977 // start ajax request
983 'client_id': client_id,
984 'repository_id': repository_id,
985 'callback': function(id, o, args) {
986 scope.parse_repository_options(o);
992 var search_button = Y.one('#'+search_button_id);
994 search_button.on('click', function(){
995 var data = this.logindata;
998 for (var k in data) {
999 if(data[k].type!='popup') {
1000 var el = document.getElementsByName(data[k].name)[0];
1001 params[data[k].name] = '';
1002 if(el.type == 'checkbox') {
1003 params[data[k].name] = el.checked;
1004 } else if(el.type == 'radio') {
1005 var tmp = document.getElementsByName(data[k].name);
1007 if (tmp[i].checked) {
1008 params[data[k].name] = tmp[i].value;
1012 params[data[k].name] = el.value;
1019 client_id: client_id,
1020 repository_id: repository_id,
1021 form: {id: 'fp-form-'+scope.options.client_id,upload:false,useDisabled:true},
1022 callback: function(id, o, args) {
1023 o.issearchresult = true;
1024 scope.parse_repository_options(o);
1030 var download_button = Y.one('#'+download_button_id);
1031 if (download_button) {
1032 download_button.on('click', function(){
1036 var popup_button = Y.one('#'+popup_button_id);
1038 popup_button.on('click', function(e){
1039 M.core_filepicker.active_filepicker = this;
1040 window.open(loginurl, 'repo_auth', 'location=0,status=0,width=500,height=300,scrollbars=yes');
1044 var elform = Y.one('#'+form_id);
1045 elform.on('keydown', function(e) {
1046 if (e.keyCode == 13) {
1049 search_button.simulate('click');
1052 login_button.simulate('click');
1060 search: function(args) {
1061 var data = this.logindata;
1064 for (var k in data) {
1065 if(data[k].type!='popup') {
1066 var el = document.getElementsByName(data[k].name)[0];
1067 params[data[k].name] = '';
1068 if(el.type == 'checkbox') {
1069 params[data[k].name] = el.checked;
1070 } else if(el.type == 'radio') {
1071 var tmp = document.getElementsByName(data[k].name);
1073 if (tmp[i].checked) {
1074 params[data[k].name] = tmp[i].value;
1078 params[data[k].name] = el.value;
1085 client_id: client_id,
1086 repository_id: repository_id,
1087 form: {id: 'fp-form-'+scope.options.client_id,upload:false,useDisabled:true},
1088 callback: function(id, o, args) {
1089 o.issearchresult = true;
1090 scope.parse_repository_options(o);
1095 list: function(args) {
1100 if (!args.repo_id) {
1101 args.repo_id = scope.active_repo.id;
1105 client_id: scope.options.client_id,
1106 repository_id: args.repo_id,
1107 path:args.path?args.path:'',
1108 page:args.page?args.page:'',
1109 callback: function(id, obj, args) {
1110 Y.all('#fp-list-'+scope.options.client_id+' li a').setStyle('backgroundColor', 'transparent');
1111 var el = Y.one('#repository-'+scope.options.client_id+'-'+obj.repo_id+'-link');
1113 el.setStyle('backgroundColor', '#AACCEE');
1116 scope.viewbar.set('disabled', true);
1117 scope.print_login(obj);
1118 } else if (obj.upload) {
1119 scope.viewbar.set('disabled', true);
1120 scope.parse_repository_options(obj);
1121 scope.create_upload_form(obj);
1123 } else if (obj.iframe) {
1125 } else if (obj.list) {
1126 obj.issearchresult = false;
1127 scope.viewbar.set('disabled', false);
1128 scope.parse_repository_options(obj);
1134 create_upload_form: function(data) {
1135 var client_id = this.options.client_id;
1136 Y.one('#panel-'+client_id).set('innerHTML', '');
1137 var types = this.options.accepted_types;
1139 this.print_header();
1140 var id = data.upload.id+'_'+client_id;
1141 var str = '<div id="'+id+'_div" class="fp-upload-form mdl-align">';
1142 str += '<form id="'+id+'" enctype="multipart/form-data" method="POST">';
1143 str += '<table width="100%">';
1144 str += '<tr><td class="mdl-right">';
1145 str += '<label for="'+id+'_file">'+data.upload.label+': </label></td>';
1146 str += '<td class="mdl-left"><input type="file" id="'+id+'_file" name="repo_upload_file" />';
1147 str += '<tr><td class="mdl-right"><label for="newname-'+client_id+'">'+M.str.repository.saveas+':</label></td>';
1148 str += '<td class="mdl-left"><input type="text" name="title" id="newname-'+client_id+'" value="" /></td></tr>';
1149 str += '<input type="hidden" name="itemid" value="'+this.options.itemid+'" />';
1150 for (var i in types) {
1151 str += '<input type="hidden" name="accepted_types[]" value="'+types[i]+'" />';
1153 str += '</td></tr><tr>';
1154 str += '<td class="mdl-right"><label>'+M.str.repository.author+': </label></td>';
1155 str += '<td class="mdl-left"><input type="text" name="author" value="'+this.options.author+'" /></td>';
1158 str += '<td class="mdl-right">'+M.str.repository.chooselicense+': </td>';
1159 str += '<td class="mdl-left">';
1160 var licenses = this.options.licenses;
1161 str += '<select name="license" id="select-license-'+client_id+'">';
1162 var recentlicense = YAHOO.util.Cookie.get('recentlicense');
1163 if (recentlicense) {
1164 this.options.defaultlicense=recentlicense;
1166 for (var i in licenses) {
1167 if (this.options.defaultlicense==licenses[i].shortname) {
1168 var selected = ' selected';
1172 str += '<option value="'+licenses[i].shortname+'"'+selected+'>'+licenses[i].fullname+'</option>';
1176 str += '</tr></table>';
1178 str += '<div class="fp-upload-btn"><button id="'+id+'_action">'+M.str.repository.upload+'</button></div>';
1180 var upload_form = Y.Node.create(str);
1181 Y.one('#panel-'+client_id).appendChild(upload_form);
1183 Y.one('#'+id+'_action').on('click', function(e) {
1185 var license = Y.one('#select-license-'+client_id).get('value');
1186 YAHOO.util.Cookie.set('recentlicense', license);
1187 if (!Y.one('#'+id+'_file').get('value')) {
1188 scope.print_msg(M.str.repository.nofilesattached, 'error');
1194 client_id: client_id,
1195 params: {'savepath':scope.options.savepath},
1196 repository_id: scope.active_repo.id,
1197 form: {id: id, upload:true},
1198 onerror: function(id, o, args) {
1199 scope.create_upload_form(data);
1201 callback: function(id, o, args) {
1202 if (scope.options.editor_target&&scope.options.env=='editor') {
1203 scope.options.editor_target.value=o.url;
1204 scope.options.editor_target.onchange();
1207 o.client_id = client_id;
1208 var formcallback_scope = null;
1209 if (args.scope.options.magicscope) {
1210 formcallback_scope = args.scope.options.magicscope;
1212 formcallback_scope = args.scope;
1214 scope.options.formcallback.apply(formcallback_scope, [o]);
1219 print_header: function() {
1220 var r = this.active_repo;
1222 var client_id = this.options.client_id;
1223 var repository_id = this.active_repo.id;
1224 var panel = Y.one('#panel-'+client_id);
1225 var str = '<div id="fp-header-'+client_id+'">';
1226 str += '<div class="fp-toolbar" id="repo-tb-'+client_id+'"></div>';
1228 var head = Y.Node.create(str);
1229 panel.appendChild(head);
1230 //if(this.active_repo.pages < 8){
1231 this.print_paging('header');
1234 var toolbar = Y.one('#repo-tb-'+client_id);
1237 var html = '<a href="###"><img src="'+M.util.image_url('a/search')+'" /> '+M.str.repository.search+'</a>';
1238 var search = Y.Node.create(html);
1239 search.on('click', function() {
1242 action:'searchform',
1243 repository_id: repository_id,
1244 callback: function(id, obj, args) {
1245 var scope = args.scope;
1246 var client_id = scope.options.client_id;
1247 var repository_id = scope.active_repo.id;
1248 var container = document.getElementById('fp-search-dlg');
1250 container.innerHTML = '';
1251 container.parentNode.removeChild(container);
1253 var container = document.createElement('DIV');
1254 container.id = 'fp-search-dlg';
1256 var dlg_title = document.createElement('DIV');
1257 dlg_title.className = 'hd';
1258 dlg_title.innerHTML = M.str.repository.search;
1260 var dlg_body = document.createElement('DIV');
1261 dlg_body.className = 'bd';
1263 var sform = document.createElement('FORM');
1264 sform.method = 'POST';
1265 sform.id = "fp-search-form";
1266 sform.innerHTML = obj.form;
1268 dlg_body.appendChild(sform);
1269 container.appendChild(dlg_title);
1270 container.appendChild(dlg_body);
1271 Y.one(document.body).appendChild(container);
1272 var search_dialog= null;
1273 function dialog_handler() {
1274 scope.viewbar.set('disabled', false);
1278 client_id: client_id,
1279 repository_id: repository_id,
1280 form: {id: 'fp-search-form',upload:false,useDisabled:true},
1281 callback: function(id, o, args) {
1282 scope.parse_repository_options(o);
1286 search_dialog.cancel();
1288 Y.one('#fp-search-form').on('keydown', function(e){
1289 if (e.keyCode == 13) {
1295 search_dialog = new YAHOO.widget.Dialog("fp-search-dlg", {
1296 postmethod: 'async',
1303 constraintoviewport : true,
1306 text:M.str.repository.submit,
1307 handler:dialog_handler,
1310 text:M.str.moodle.cancel,
1316 search_dialog.render();
1317 search_dialog.show();
1321 toolbar.appendChild(search);
1323 // weather we use cache for this instance, this button will reload listing anyway
1325 var html = '<a href="###"><img src="'+M.util.image_url('a/refresh')+'" /> '+M.str.repository.refresh+'</a>';
1326 var refresh = Y.Node.create(html);
1327 refresh.on('click', function() {
1330 toolbar.appendChild(refresh);
1333 var label = r.logouttext?r.logouttext:M.str.repository.logout;
1334 var html = '<a href="###"><img src="'+M.util.image_url('a/logout')+'" /> '+label+'</a>';
1335 var logout = Y.Node.create(html);
1336 logout.on('click', function() {
1339 client_id: client_id,
1340 repository_id: repository_id,
1342 callback: function(id, obj, args) {
1343 scope.viewbar.set('disabled', true);
1344 scope.print_login(obj);
1348 toolbar.appendChild(logout);
1352 var mgr = document.createElement('A');
1353 mgr.href = r.manage;
1354 mgr.target = "_blank";
1355 mgr.innerHTML = '<img src="'+M.util.image_url('a/setting')+'" /> '+M.str.repository.manageurl;
1356 toolbar.appendChild(mgr);
1359 var help = document.createElement('A');
1361 help.target = "_blank";
1362 help.innerHTML = '<img src="'+M.util.image_url('a/help')+'" /> '+M.str.repository.help;
1363 toolbar.appendChild(help);
1368 get_page_button: function(page) {
1369 var r = this.active_repo;
1371 if (page == r.page) {
1372 css = 'class="cur_page" ';
1374 var str = '<a '+css+'href="###" id="repo-page-'+page+'">';
1377 print_paging: function(html_id) {
1378 var client_id = this.options.client_id;
1380 var r = this.active_repo;
1383 var lastpage = r.pages;
1384 var lastpagetext = r.pages;
1385 if (r.pages == -1) {
1386 lastpage = r.page + 1;
1387 lastpagetext = M.str.moodle.next;
1390 str += '<div class="fp-paging" id="paging-'+html_id+'-'+client_id+'">';
1391 str += this.get_page_button(1)+'1</a> ';
1394 var ex = (span-1)/2;
1396 if (r.page+ex>=lastpage) {
1402 var max = r.page+ex;
1406 // won't display upper boundary
1407 if (r.page >= span) {
1409 for(var i=r.page-ex; i<max; i++) {
1410 str += this.get_page_button(i);
1415 // this very first elements
1416 for(var i = 2; i < max; i++) {
1417 str += this.get_page_button(i);
1423 // won't display upper boundary
1424 if (max==lastpage) {
1425 str += this.get_page_button(lastpage)+lastpagetext+'</a>';
1427 str += this.get_page_button(max)+max+'</a>';
1428 str += ' ... '+this.get_page_button(lastpage)+lastpagetext+'</a>';
1433 var a = Y.Node.create(str);
1434 Y.one('#fp-header-'+client_id).appendChild(a);
1436 Y.all('#fp-header-'+client_id+' .fp-paging a').each(
1437 function(node, id) {
1438 node.on('click', function(e) {
1439 var id = node.get('id');
1440 var re = new RegExp("repo-page-(\\d+)", "i");
1441 var result = id.match(re);
1443 args.page = result[1];
1444 if (scope.active_repo.issearchresult) {
1448 client_id: client_id,
1449 repository_id: r.id,
1450 params: {'page':result[1]},
1451 callback: function(id, o, args) {
1452 o.issearchresult = true;
1453 scope.parse_repository_options(o);
1454 scope.view_files(result[1]);
1459 if (scope.viewmode == 2) {
1460 scope.view_as_list(result[1]);
1469 print_path: function() {
1470 var client_id = this.options.client_id;
1471 var panel = Y.one('#panel-'+client_id);
1472 var p = this.filepath;
1473 if (p && p.length!=0) {
1474 var path = Y.Node.create('<div id="path-'+client_id+'" class="fp-pathbar"></div>');
1475 panel.appendChild(path);
1476 for(var i = 0; i < p.length; i++) {
1477 var link_path = p[i].path;
1478 var link = document.createElement('A');
1480 link.innerHTML = p[i].name;
1481 link.id = 'path-node-'+client_id+'-'+i;
1482 var sep = Y.Node.create('<span>/</span>');
1483 path.appendChild(link);
1484 path.appendChild(sep);
1485 Y.one('#'+link.id).on('click', function(Y, path){
1486 this.list({'path':path});
1495 if (this.rendered) {
1496 var panel = Y.one('#panel-'+this.options.client_id);
1497 panel.set('innerHTML', '');
1499 this.show_recent_repository();
1504 launch: function() {
1507 show_recent_repository: function() {
1508 var repository_id = YAHOO.util.Cookie.get('recentrepository');
1509 if (this.options.repositories[repository_id]) {
1510 this.list({'repo_id':repository_id});
1514 var loading = Y.one('#filepicker-loading-'+options.client_id);
1516 loading.setStyle('display', 'none');
1518 M.core_filepicker.instances[options.client_id] = new FilePickerHelper(options);