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');
154 } else if (data && data.event) {
155 switch (data.event) {
157 scope.process_existing_file(data);
164 scope.print_msg(data.msg, 'info');
166 args.callback(id,data,p);
174 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
180 cfg.form = args.form;
187 process_existing_file: function(data) {
189 var repository_id = scope.active_repo.id;
190 var client_id = scope.options.client_id;
191 var handleOverwrite = function() {
195 params['existingfilename'] = data.existingfile.filename;
196 params['existingfilepath'] = data.existingfile.filepath;
197 params['newfilename'] = data.newfile.filename;
198 params['newfilepath'] = data.newfile.filepath;
202 'action':'overwrite',
204 'client_id': client_id,
205 'repository_id': repository_id,
206 'callback': function(id, o, args) {
209 // editor needs to update url
210 // filemanager do nothing
211 if (scope.options.editor_target && scope.options.env == 'editor') {
212 scope.options.editor_target.value = data.existingfile.url;
213 scope.options.editor_target.onchange();
214 } else if (scope.options.env === 'filepicker') {
215 var fileinfo = {'client_id':client_id,
216 'url':data.existingfile.url,
217 'file':data.existingfile.filename};
218 scope.options.formcallback.apply(scope, [fileinfo]);
223 var handleRename = function() {
224 if (scope.options.editor_target && scope.options.env == 'editor') {
225 scope.options.editor_target.value = data.newfile.url;
226 scope.options.editor_target.onchange();
230 var formcallback_scope = null;
231 if (scope.options.magicscope) {
232 formcallback_scope = scope.options.magicscope;
234 formcallback_scope = scope;
236 var fileinfo = {'client_id':client_id,
237 'url':data.newfile.url,
238 'file':data.newfile.filename};
239 scope.options.formcallback.apply(formcallback_scope, [fileinfo]);
241 var handleCancel = function() {
245 params['newfilename'] = data.newfile.filename;
246 params['newfilepath'] = data.newfile.filepath;
250 'action':'deletetmpfile',
252 'client_id': client_id,
253 'repository_id': repository_id,
254 'callback': function(id, o, args) {
260 var dialog = new YAHOO.widget.SimpleDialog("dlg", {
264 icon: YAHOO.widget.SimpleDialog.ICON_HELP,
268 buttons: [{ text: M.str.repository.overwrite, handler: handleOverwrite },
269 { text: M.str.repository.renameto + ' "' + data.newfile.filename + '"', handler: handleRename },
270 { text: M.str.moodle.cancel, handler: handleCancel, isDefault: true}]
272 dialog.setHeader(M.str.repository.fileexistsdialogheader);
273 if (scope.options.env == 'editor') {
274 dialog.setBody(M.str.repository.fileexistsdialog_editor);
276 dialog.setBody(M.str.repository.fileexistsdialog_filemanager);
279 dialog.render(document.body);
282 print_msg: function(msg, type) {
283 var client_id = this.options.client_id;
284 var dlg_id = 'fp-msg-dlg-'+client_id;
285 function handleYes() {
288 var icon = YAHOO.widget.SimpleDialog.ICON_INFO;
290 icon = YAHOO.widget.SimpleDialog.ICON_ALARM;
293 this.msg_dlg = new YAHOO.widget.SimpleDialog(dlg_id,
303 constraintoviewport: true,
304 buttons: [{ text:M.str.moodle.ok, handler:handleYes, isDefault:true }]
306 this.msg_dlg.render(document.body);
308 this.msg_dlg.setBody(msg);
310 var header = M.str.moodle.info;
312 header = M.str.moodle.error;
314 this.msg_dlg.setHeader(type);
317 build_tree: function(node, level) {
318 var client_id = this.options.client_id;
319 var dynload = this.active_repo.dynload;
321 node.title = '<i><u>'+node.title+'</u></i>';
325 //title:fp_lang.date+' '+node.date+fp_lang.size+' '+node.size,
327 source:node.source?node.source:'',
328 thumbnail:node.thumbnail,
329 path:node.path?node.path:[]
331 var tmpNode = new YAHOO.widget.TextNode(info, level, false);
333 tmpNode.repo_id=node.repo_id;
335 tmpNode.repo_id=this.active_repo.id;
342 tmpNode.scope = this;
344 tmpNode.isLeaf = false;
345 tmpNode.client_id = client_id;
347 tmpNode.path = node.path;
351 for(var c in node.children) {
352 this.build_tree(node.children[c], tmpNode);
355 tmpNode.isLeaf = true;
358 view_files: function(page) {
359 var p= page?page:null;
360 if (this.active_repo.issearchresult) {
361 // list view is desiged to display treeview
362 // it is not working well with search result
363 this.view_as_icons();
365 this.viewbar.set('disabled', false);
366 if (this.viewmode == 1) {
367 this.view_as_icons();
368 } else if (this.viewmode == 2) {
369 this.view_as_list(p);
371 this.view_as_icons();
375 treeview_dynload: function(node, cb) {
376 var scope = node.scope;
377 var client_id = scope.options.client_id;
378 var repository_id = scope.active_repo.id;
381 client_id: client_id,
382 repository_id: repository_id,
383 path:node.path?node.path:'',
384 page:node.page?args.page:'',
385 callback: function(id, obj, args) {
386 obj.issearchresult = false;
388 scope.viewbar.set('disabled', false);
389 scope.parse_repository_options(obj);
391 scope.build_tree(list[k], node);
397 view_as_list: function(p) {
401 if (scope.active_repo.page) {
402 page = scope.active_repo.page;
409 client_id: scope.options.client_id,
410 repository_id: scope.active_repo.id,
413 callback: function(id, obj, args) {
414 scope.parse_repository_options(obj);
416 scope.viewbar.set('disabled', true);
417 scope.print_login(obj);
420 var client_id = scope.options.client_id;
421 var dynload = scope.active_repo.dynload;
423 var panel_id = '#panel-'+client_id;
425 Y.one(panel_id).set('innerHTML', '');
427 scope.print_header();
429 var html = '<div class="fp-tree-panel" id="treeview-'+client_id+'">';
430 if (list && list.length==0) {
431 html += '<div class="fp-emptylist mdl-align">' +M.str.repository.nofilesavailable+'</div>';
435 var tree = Y.Node.create(html);
436 Y.one(panel_id).appendChild(tree);
437 if (!list || list.length==0) {
441 scope.treeview = new YAHOO.widget.TreeView('treeview-'+client_id);
443 scope.treeview.setDynamicLoad(scope.treeview_dynload, 1);
447 scope.build_tree(list[k], scope.treeview.getRoot());
449 scope.treeview.subscribe('clickEvent', function(e){
452 fileinfo['title'] = e.node.data.filename;
453 fileinfo['source'] = e.node.data.source;
454 fileinfo['thumbnail'] = e.node.data.thumbnail;
455 scope.select_file(fileinfo);
458 scope.treeview.draw();
462 view_as_icons: function() {
464 var client_id = this.options.client_id;
465 var list = this.filelist;
466 var panel_id = '#panel-'+client_id;
468 Y.one(panel_id).set('innerHTML', '');
472 var html = '<div class="fp-grid-panel" id="fp-grid-panel-'+client_id+'">';
473 if (list && list.length==0) {
474 html += '<div class="fp-emptylist mdl-align">' +M.str.repository.nofilesavailable+'</div>';
478 var gridpanel = Y.Node.create(html);
479 Y.one('#panel-'+client_id).appendChild(gridpanel);
483 var grid = document.createElement('DIV');
484 grid.className='fp-grid';
486 var title = document.createElement('DIV');
487 title.id = 'grid-title-'+client_id+'-'+String(count);
488 title.className = 'label';
489 var filename = node.title;
490 if (node.shorttitle) {
491 filename = node.shorttitle;
493 var filename_id = 'filname-link-'+client_id+'-'+String(count);
494 title.innerHTML += '<a href="###" id="'+filename_id+'" title="'+node.title+'"><span>'+filename+"</span></a>";
497 if(node.thumbnail_width){
498 grid.style.width = node.thumbnail_width+'px';
499 title.style.width = (node.thumbnail_width-10)+'px';
501 grid.style.width = title.style.width = '90px';
503 var frame = document.createElement('DIV');
504 frame.style.textAlign='center';
505 if(node.thumbnail_height){
506 frame.style.height = node.thumbnail_height+'px';
508 var img = document.createElement('img');
509 img.src = node.thumbnail;
510 img.title = node.title;
511 if(node.thumbnail_alt) {
512 img.alt = node.thumbnail_alt;
514 if(node.thumbnail_title) {
515 img.title = node.thumbnail_title;
518 var link = document.createElement('A');
520 link.id = 'img-id-'+client_id+'-'+String(count);
523 //grid.innerHTML += '<p><a target="_blank" href="'+node.url+'">'+M.str.repository.preview+'</a></p>';
525 link.appendChild(img);
526 frame.appendChild(link);
527 grid.appendChild(frame);
528 grid.appendChild(title);
529 gridpanel.appendChild(grid);
531 var y_title = Y.one('#'+title.id);
532 var y_file = Y.one('#'+link.id);
533 var dynload = this.active_repo.dynload;
535 y_file.on('click', function(e, p) {
537 var params = {'path':p.path};
540 this.filelist = p.children;
544 y_title.on('click', function(e, p, id){
545 var icon = Y.one(id);
546 icon.simulate('click');
547 }, this, node, '#'+link.id);
550 fileinfo['title'] = list[k].title;
551 fileinfo['source'] = list[k].source;
552 fileinfo['thumbnail'] = list[k].thumbnail;
553 fileinfo['haslicense'] = list[k].haslicense?true:false;
554 fileinfo['hasauthor'] = list[k].hasauthor?true:false;
555 y_title.on('click', function(e, args) {
556 this.select_file(args);
558 y_file.on('click', function(e, args) {
559 this.select_file(args);
565 select_file: function(args) {
566 var client_id = this.options.client_id;
567 var thumbnail = Y.one('#fp-grid-panel-'+client_id);
569 thumbnail.setStyle('display', 'none');
571 var header = Y.one('#fp-header-'+client_id);
573 header.setStyle('display', 'none');
575 var footer = Y.one('#fp-footer-'+client_id);
577 footer.setStyle('display', 'none');
579 var path = Y.one('#path-'+client_id);
581 path.setStyle('display', 'none');
583 var panel = Y.one('#panel-'+client_id);
584 var form_id = 'fp-rename-form-'+client_id;
585 var html = '<div class="fp-rename-form" id="'+form_id+'">';
586 html += '<p><img src="'+args.thumbnail+'" /></p>';
587 html += '<table width="100%">';
588 html += '<tr><td class="mdl-right"><label for="newname-'+client_id+'">'+M.str.repository.saveas+':</label></td>';
589 html += '<td class="mdl-left"><input type="text" id="newname-'+client_id+'" value="'+args.title+'" /></td></tr>';
593 if (this.options.repositories[this.active_repo.id].return_types == 1) {
594 // support external links only
595 le_checked = 'checked';
596 le_style = ' style="display:none;"';
597 } else if(this.options.repositories[this.active_repo.id].return_types == 2) {
598 // support internal files only
599 le_style = ' style="display:none;"';
601 if ((this.options.externallink && this.options.env == 'editor' && this.options.return_types != 1)) {
602 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>';
605 if (!args.hasauthor) {
606 // the author of the file
607 html += '<tr><td class="mdl-right"><label for="text-author">'+M.str.repository.author+' :</label></td>';
608 html += '<td class="mdl-left"><input id="text-author-'+client_id+'" type="text" name="author" value="'+this.options.author+'" /></td>';
612 if (!args.haslicense) {
613 // the license of the file
614 var licenses = this.options.licenses;
615 html += '<tr><td class="mdl-right"><label for="select-license-'+client_id+'">'+M.str.repository.chooselicense+' :</label></td>';
616 html += '<td class="mdl-left"><select name="license" id="select-license-'+client_id+'">';
617 var recentlicense = YAHOO.util.Cookie.get('recentlicense');
619 this.options.defaultlicense=recentlicense;
621 for (var i in licenses) {
622 if (this.options.defaultlicense==licenses[i].shortname) {
623 var selected = ' selected';
627 html += '<option value="'+licenses[i].shortname+'"'+selected+'>'+licenses[i].fullname+'</option>';
629 html += '</select></td></tr>';
633 html += '<p><input type="hidden" id="filesource-'+client_id+'" value="'+args.source+'" />';
634 html += '<input type="button" id="fp-confirm-'+client_id+'" value="'+M.str.repository.getfile+'" />';
635 html += '<input type="button" id="fp-cancel-'+client_id+'" value="'+M.str.moodle.cancel+'" /></p>';
638 var getfile_form = Y.Node.create(html);
639 panel.appendChild(getfile_form);
641 var getfile = Y.one('#fp-confirm-'+client_id);
642 getfile.on('click', function(e) {
643 var client_id = this.options.client_id;
645 var repository_id = this.active_repo.id;
646 var title = Y.one('#newname-'+client_id).get('value');
647 var filesource = Y.one('#filesource-'+client_id).get('value');
648 var params = {'title':title, 'source':filesource, 'savepath': this.options.savepath};
649 var license = Y.one('#select-license-'+client_id);
651 params['license'] = license.get('value');
652 YAHOO.util.Cookie.set('recentlicense', license.get('value'));
654 var author = Y.one('#text-author-'+client_id);
656 params['author'] = author.get('value');
659 if (this.options.env == 'editor') {
660 // in editor, images are stored in '/' only
661 params.savepath = '/';
662 // when image or media button is clicked
663 if ( this.options.return_types != 1 ) {
664 var linkexternal = Y.one('#linkexternal-'+client_id).get('checked');
666 params['linkexternal'] = 'yes';
669 // when link button in editor clicked
670 params['linkexternal'] = 'yes';
674 if (this.options.env == 'url') {
675 params['linkexternal'] = 'yes';
678 this.wait('download', title);
681 client_id: client_id,
682 repository_id: repository_id,
684 callback: function(id, obj, args) {
685 if (scope.options.editor_target && scope.options.env=='editor') {
686 scope.options.editor_target.value=obj.url;
687 scope.options.editor_target.onchange();
690 obj.client_id = client_id;
691 var formcallback_scope = null;
692 if (args.scope.options.magicscope) {
693 formcallback_scope = args.scope.options.magicscope;
695 formcallback_scope = args.scope;
697 scope.options.formcallback.apply(formcallback_scope, [obj]);
701 var elform = Y.one('#'+form_id);
702 elform.on('keydown', function(e) {
703 if (e.keyCode == 13) {
704 getfile.simulate('click');
708 var cancel = Y.one('#fp-cancel-'+client_id);
709 cancel.on('click', function(e) {
712 var treeview = Y.one('#treeview-'+client_id);
714 treeview.setStyle('display', 'none');
717 wait: function(type) {
718 var panel = Y.one('#panel-'+this.options.client_id);
719 panel.set('innerHTML', '');
721 var str = '<div style="text-align:center">';
723 str += '<img src="'+M.util.image_url('i/loading')+'" />';
724 str += '<p>'+M.str.repository.loading+'</p>';
726 str += '<img src="'+M.util.image_url('i/progressbar')+'" />';
727 str += '<p>'+M.str.repository.copying+' <strong>'+name+'</strong></p>';
731 panel.set('innerHTML', str);
737 var client_id = this.options.client_id;
739 var filepicker_id = 'filepicker-'+client_id;
740 var fpnode = Y.Node.create('<div class="file-picker" id="'+filepicker_id+'"></div>');
741 Y.one(document.body).appendChild(fpnode);
742 // render file picker panel
743 this.mainui = new YAHOO.widget.Panel(filepicker_id, {
748 monitorresize: false,
749 xy: [50, YAHOO.util.Dom.getDocumentScrollTop()+20]
752 this.mainui.beforeRenderEvent.subscribe(function() {
753 YAHOO.util.Event.onAvailable('layout-'+client_id, function() {
754 layout = new YAHOO.widget.Layout('layout-'+client_id, {
755 height: 480, width: 700,
757 {position: 'top', height: 32, resize: false,
758 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'},
759 {position: 'left', width: 200, resize: true, scroll:true,
760 body:'<ul class="fp-list" id="fp-list-'+client_id+'"></ul>', gutter: '0 5 0 2', minWidth: 150, maxWidth: 300 },
761 {position: 'center', body: '<div class="fp-panel" id="panel-'+client_id+'"></div>',
762 scroll: true, gutter: '0 2 0 0' }
766 scope.show_recent_repository();
770 this.mainui.setHeader(M.str.repository.filepicker);
771 this.mainui.setBody('<div id="layout-'+client_id+'"></div>');
772 this.mainui.render();
773 this.rendered = true;
777 var view_icons = {label: M.str.repository.iconview, value: 't', 'checked': true,
780 scope.view_as_icons();
784 var view_listing = {label: M.str.repository.listview, value: 'l',
787 scope.view_as_list();
791 this.viewbar = new YAHOO.widget.ButtonGroup({
792 id: 'btngroup-'+client_id,
795 container: 'fp-viewbar-'+client_id
797 this.viewbar.addButtons([view_icons, view_listing]);
798 // processing repository listing
799 var r = this.options.repositories;
800 Y.on('contentready', function(el) {
801 var list = Y.one(el);
804 var id = 'repository-'+client_id+'-'+r[i].id;
805 var link_id = id + '-link';
806 list.append('<li id="'+id+'"><a class="fp-repo-name" id="'+link_id+'" href="###">'+r[i].name+'</a></li>');
807 Y.one('#'+link_id).prepend('<img src="'+r[i].icon+'" width="16" height="16" /> ');
808 Y.one('#'+link_id).on('click', function(e, scope, repository_id) {
809 YAHOO.util.Cookie.set('recentrepository', repository_id);
810 scope.repository_id = repository_id;
811 this.list({'repo_id':repository_id});
812 }, this /*handler running scope*/, this/*second argument*/, r[i].id/*third argument of handler*/);
816 if (this.options.externallink) {
817 list.set('innerHTML', M.str.repository.norepositoriesexternalavailable);
819 list.set('innerHTML', M.str.repository.norepositoriesavailable);
822 }, '#fp-list-'+client_id, this /* handler running scope */, '#fp-list-'+client_id /*first argument of handler*/);
824 parse_repository_options: function(data) {
825 this.filelist = data.list?data.list:null;
826 this.filepath = data.path?data.path:null;
827 this.active_repo = {};
828 this.active_repo.issearchresult = Boolean(data.issearchresult);
829 this.active_repo.dynload = data.dynload?data.dynload:false;
830 this.active_repo.pages = Number(data.pages?data.pages:null);
831 this.active_repo.page = Number(data.page?data.page:null);
832 this.active_repo.id = data.repo_id?data.repo_id:null;
833 this.active_repo.nosearch = data.nosearch?true:false;
834 this.active_repo.norefresh = data.norefresh?true:false;
835 this.active_repo.nologin = data.nologin?true:false;
836 this.active_repo.logouttext = data.logouttext?data.logouttext:null;
837 this.active_repo.help = data.help?data.help:null;
838 this.active_repo.manage = data.manage?data.manage:null;
840 print_login: function(data) {
841 this.parse_repository_options(data);
842 var client_id = this.options.client_id;
843 var repository_id = data.repo_id;
844 var l = this.logindata = data.login;
846 var panel = Y.one('#panel-'+client_id);
847 var action = 'login';
848 if (data['login_btn_action']) {
849 action=data['login_btn_action'];
851 var form_id = 'fp-form-'+client_id;
852 var download_button_id = 'fp-form-download-button-'+client_id;
853 var search_button_id = 'fp-form-search-button-'+client_id;
854 var login_button_id = 'fp-form-login-button-'+client_id;
855 var popup_button_id = 'fp-form-popup-button-'+client_id;
857 var str = '<div class="fp-login-form">';
858 str += '<form id="'+form_id+'">';
860 str +='<table width="100%">';
863 if(l[k].type=='popup') {
866 str += '<td colspan="2"><p class="fp-popup">'+M.str.repository.popup+'</p>';
867 str += '<p class="fp-popup"><button id="'+popup_button_id+'">'+M.str.repository.login+'</button>';
870 }else if(l[k].type=='textarea') {
872 str += '<td colspan="2"><p><textarea id="'+l[k].id+'" name="'+l[k].name+'"></textarea></p></td>';
873 }else if(l[k].type=='select') {
875 str += '<td align="right"><label>'+l[k].label+':</label></td>';
876 str += '<td align="left"><select id="'+l[k].id+'" name="'+l[k].name+'">';
877 for (i in l[k].options) {
878 str += '<option value="'+l[k].options[i].value+'">'+l[k].options[i].label+'</option>';
880 str += '</select></td>';
885 var field_value = '';
887 label_id = ' for="'+l[k].id+'"';
888 field_id = ' id="'+l[k].id+'"';
891 str += '<td align="right" width="30%" valign="center">';
892 str += '<label'+label_id+'>'+l[k].label+'</label> </td>';
894 str += '<td width="30%"></td>';
897 field_value = ' value="'+l[k].value+'"';
899 if(l[k].type=='radio'){
900 var list = l[k].value.split('|');
901 var labels = l[k].value_label.split('|');
902 str += '<td align="left">';
903 for(var item in list) {
904 str +='<input type="'+l[k].type+'"'+' name="'+l[k].name+'"'+
905 field_id+' value="'+list[item]+'" />'+labels[item]+'<br />';
909 str += '<td align="left">';
910 str += '<input type="'+l[k].type+'"'+' name="'+l[k].name+'"'+field_value+' '+field_id+' />';
920 var btn_label = data['login_btn_label']?data['login_btn_label']:M.str.repository.submit;
921 if (action != 'popup') {
922 str += '<p><input type="button" id="';
925 str += search_button_id;
928 str += download_button_id;
931 str += login_button_id;
934 str += '" value="'+btn_label+'" /></p>';
941 panel.set('innerHTML', str);
943 alert(M.str.repository.xhtmlerror);
946 // process login action
947 var login_button = Y.one('#'+login_button_id);
950 login_button.on('click', function(){
952 var data = this.logindata;
955 for (var k in data) {
956 if(data[k].type!='popup') {
957 var el = Y.one('[name='+data[k].name+']');
958 var type = el.get('type');
959 params[data[k].name] = '';
960 if(type == 'checkbox') {
961 params[data[k].name] = el.get('checked');
963 params[data[k].name] = el.get('value');
967 // start ajax request
973 'client_id': client_id,
974 'repository_id': repository_id,
975 'callback': function(id, o, args) {
976 scope.parse_repository_options(o);
982 var search_button = Y.one('#'+search_button_id);
984 search_button.on('click', function(){
985 var data = this.logindata;
988 for (var k in data) {
989 if(data[k].type!='popup') {
990 var el = document.getElementsByName(data[k].name)[0];
991 params[data[k].name] = '';
992 if(el.type == 'checkbox') {
993 params[data[k].name] = el.checked;
994 } else if(el.type == 'radio') {
995 var tmp = document.getElementsByName(data[k].name);
997 if (tmp[i].checked) {
998 params[data[k].name] = tmp[i].value;
1002 params[data[k].name] = el.value;
1009 client_id: client_id,
1010 repository_id: repository_id,
1011 form: {id: 'fp-form-'+scope.options.client_id,upload:false,useDisabled:true},
1012 callback: function(id, o, args) {
1013 o.issearchresult = true;
1014 scope.parse_repository_options(o);
1020 var download_button = Y.one('#'+download_button_id);
1021 if (download_button) {
1022 download_button.on('click', function(){
1026 var popup_button = Y.one('#'+popup_button_id);
1028 popup_button.on('click', function(e){
1029 M.core_filepicker.active_filepicker = this;
1030 window.open(loginurl, 'repo_auth', 'location=0,status=0,width=500,height=300,scrollbars=yes');
1034 var elform = Y.one('#'+form_id);
1035 elform.on('keydown', function(e) {
1036 if (e.keyCode == 13) {
1039 search_button.simulate('click');
1042 login_button.simulate('click');
1050 search: function(args) {
1051 var data = this.logindata;
1054 for (var k in data) {
1055 if(data[k].type!='popup') {
1056 var el = document.getElementsByName(data[k].name)[0];
1057 params[data[k].name] = '';
1058 if(el.type == 'checkbox') {
1059 params[data[k].name] = el.checked;
1060 } else if(el.type == 'radio') {
1061 var tmp = document.getElementsByName(data[k].name);
1063 if (tmp[i].checked) {
1064 params[data[k].name] = tmp[i].value;
1068 params[data[k].name] = el.value;
1075 client_id: client_id,
1076 repository_id: repository_id,
1077 form: {id: 'fp-form-'+scope.options.client_id,upload:false,useDisabled:true},
1078 callback: function(id, o, args) {
1079 o.issearchresult = true;
1080 scope.parse_repository_options(o);
1085 list: function(args) {
1090 if (!args.repo_id) {
1091 args.repo_id = scope.active_repo.id;
1095 client_id: scope.options.client_id,
1096 repository_id: args.repo_id,
1097 path:args.path?args.path:'',
1098 page:args.page?args.page:'',
1099 callback: function(id, obj, args) {
1100 Y.all('#fp-list-'+scope.options.client_id+' li a').setStyle('backgroundColor', 'transparent');
1101 var el = Y.one('#repository-'+scope.options.client_id+'-'+obj.repo_id+'-link');
1103 el.setStyle('backgroundColor', '#AACCEE');
1106 scope.viewbar.set('disabled', true);
1107 scope.print_login(obj);
1108 } else if (obj.upload) {
1109 scope.viewbar.set('disabled', true);
1110 scope.parse_repository_options(obj);
1111 scope.create_upload_form(obj);
1113 } else if (obj.iframe) {
1115 } else if (obj.list) {
1116 obj.issearchresult = false;
1117 scope.viewbar.set('disabled', false);
1118 scope.parse_repository_options(obj);
1124 create_upload_form: function(data) {
1125 var client_id = this.options.client_id;
1126 Y.one('#panel-'+client_id).set('innerHTML', '');
1127 var types = this.options.accepted_types;
1129 this.print_header();
1130 var id = data.upload.id+'_'+client_id;
1131 var str = '<div id="'+id+'_div" class="fp-upload-form mdl-align">';
1132 str += '<form id="'+id+'" enctype="multipart/form-data" method="POST">';
1133 str += '<table width="100%">';
1134 str += '<tr><td class="mdl-right">';
1135 str += '<label for="'+id+'_file">'+data.upload.label+': </label></td>';
1136 str += '<td class="mdl-left"><input type="file" id="'+id+'_file" name="repo_upload_file" />';
1137 str += '<tr><td class="mdl-right"><label for="newname-'+client_id+'">'+M.str.repository.saveas+':</label></td>';
1138 str += '<td class="mdl-left"><input type="text" name="title" id="newname-'+client_id+'" value="" /></td></tr>';
1139 str += '<input type="hidden" name="itemid" value="'+this.options.itemid+'" />';
1140 for (var i in types) {
1141 str += '<input type="hidden" name="accepted_types[]" value="'+types[i]+'" />';
1143 str += '</td></tr><tr>';
1144 str += '<td class="mdl-right"><label>'+M.str.repository.author+': </label></td>';
1145 str += '<td class="mdl-left"><input type="text" name="author" value="'+this.options.author+'" /></td>';
1148 str += '<td class="mdl-right">'+M.str.repository.chooselicense+': </td>';
1149 str += '<td class="mdl-left">';
1150 var licenses = this.options.licenses;
1151 str += '<select name="license" id="select-license-'+client_id+'">';
1152 var recentlicense = YAHOO.util.Cookie.get('recentlicense');
1153 if (recentlicense) {
1154 this.options.defaultlicense=recentlicense;
1156 for (var i in licenses) {
1157 if (this.options.defaultlicense==licenses[i].shortname) {
1158 var selected = ' selected';
1162 str += '<option value="'+licenses[i].shortname+'"'+selected+'>'+licenses[i].fullname+'</option>';
1166 str += '</tr></table>';
1168 str += '<div class="fp-upload-btn"><button id="'+id+'_action">'+M.str.repository.upload+'</button></div>';
1170 var upload_form = Y.Node.create(str);
1171 Y.one('#panel-'+client_id).appendChild(upload_form);
1173 Y.one('#'+id+'_action').on('click', function(e) {
1175 var license = Y.one('#select-license-'+client_id).get('value');
1176 YAHOO.util.Cookie.set('recentlicense', license);
1177 if (!Y.one('#'+id+'_file').get('value')) {
1178 scope.print_msg(M.str.repository.nofilesattached, 'error');
1181 Y.use('io-upload-iframe', function() {
1185 client_id: client_id,
1186 params: {'savepath':scope.options.savepath},
1187 repository_id: scope.active_repo.id,
1188 form: {id: id, upload:true},
1189 callback: function(id, o, args) {
1190 if (scope.options.editor_target&&scope.options.env=='editor') {
1191 scope.options.editor_target.value=o.url;
1192 scope.options.editor_target.onchange();
1195 o.client_id = client_id;
1196 var formcallback_scope = null;
1197 if (args.scope.options.magicscope) {
1198 formcallback_scope = args.scope.options.magicscope;
1200 formcallback_scope = args.scope;
1202 scope.options.formcallback.apply(formcallback_scope, [o]);
1208 print_header: function() {
1209 var r = this.active_repo;
1211 var client_id = this.options.client_id;
1212 var repository_id = this.active_repo.id;
1213 var panel = Y.one('#panel-'+client_id);
1214 var str = '<div id="fp-header-'+client_id+'">';
1215 str += '<div class="fp-toolbar" id="repo-tb-'+client_id+'"></div>';
1217 var head = Y.Node.create(str);
1218 panel.appendChild(head);
1219 //if(this.active_repo.pages < 8){
1220 this.print_paging('header');
1223 var toolbar = Y.one('#repo-tb-'+client_id);
1226 var html = '<a href="###"><img src="'+M.util.image_url('a/search')+'" /> '+M.str.repository.search+'</a>';
1227 var search = Y.Node.create(html);
1228 search.on('click', function() {
1231 action:'searchform',
1232 repository_id: repository_id,
1233 callback: function(id, obj, args) {
1234 var scope = args.scope;
1235 var client_id = scope.options.client_id;
1236 var repository_id = scope.active_repo.id;
1237 var container = document.getElementById('fp-search-dlg');
1239 container.innerHTML = '';
1240 container.parentNode.removeChild(container);
1242 var container = document.createElement('DIV');
1243 container.id = 'fp-search-dlg';
1245 var dlg_title = document.createElement('DIV');
1246 dlg_title.className = 'hd';
1247 dlg_title.innerHTML = M.str.repository.search;
1249 var dlg_body = document.createElement('DIV');
1250 dlg_body.className = 'bd';
1252 var sform = document.createElement('FORM');
1253 sform.method = 'POST';
1254 sform.id = "fp-search-form";
1255 sform.innerHTML = obj.form;
1257 dlg_body.appendChild(sform);
1258 container.appendChild(dlg_title);
1259 container.appendChild(dlg_body);
1260 Y.one(document.body).appendChild(container);
1261 var search_dialog= null;
1262 function dialog_handler() {
1263 scope.viewbar.set('disabled', false);
1267 client_id: client_id,
1268 repository_id: repository_id,
1269 form: {id: 'fp-search-form',upload:false,useDisabled:true},
1270 callback: function(id, o, args) {
1271 scope.parse_repository_options(o);
1275 search_dialog.cancel();
1277 Y.one('#fp-search-form').on('keydown', function(e){
1278 if (e.keyCode == 13) {
1284 search_dialog = new YAHOO.widget.Dialog("fp-search-dlg", {
1285 postmethod: 'async',
1292 constraintoviewport : true,
1295 text:M.str.repository.submit,
1296 handler:dialog_handler,
1299 text:M.str.moodle.cancel,
1305 search_dialog.render();
1306 search_dialog.show();
1310 toolbar.appendChild(search);
1312 // weather we use cache for this instance, this button will reload listing anyway
1314 var html = '<a href="###"><img src="'+M.util.image_url('a/refresh')+'" /> '+M.str.repository.refresh+'</a>';
1315 var refresh = Y.Node.create(html);
1316 refresh.on('click', function() {
1319 toolbar.appendChild(refresh);
1322 var label = r.logouttext?r.logouttext:M.str.repository.logout;
1323 var html = '<a href="###"><img src="'+M.util.image_url('a/logout')+'" /> '+label+'</a>';
1324 var logout = Y.Node.create(html);
1325 logout.on('click', function() {
1328 client_id: client_id,
1329 repository_id: repository_id,
1331 callback: function(id, obj, args) {
1332 scope.viewbar.set('disabled', true);
1333 scope.print_login(obj);
1337 toolbar.appendChild(logout);
1341 var mgr = document.createElement('A');
1342 mgr.href = r.manage;
1343 mgr.target = "_blank";
1344 mgr.innerHTML = '<img src="'+M.util.image_url('a/setting')+'" /> '+M.str.repository.manageurl;
1345 toolbar.appendChild(mgr);
1348 var help = document.createElement('A');
1350 help.target = "_blank";
1351 help.innerHTML = '<img src="'+M.util.image_url('a/help')+'" /> '+M.str.repository.help;
1352 toolbar.appendChild(help);
1357 get_page_button: function(page) {
1358 var r = this.active_repo;
1360 if (page == r.page) {
1361 css = 'class="cur_page" ';
1363 var str = '<a '+css+'href="###" id="repo-page-'+page+'">';
1366 print_paging: function(html_id) {
1367 var client_id = this.options.client_id;
1369 var r = this.active_repo;
1373 str += '<div class="fp-paging" id="paging-'+html_id+'-'+client_id+'">';
1374 str += this.get_page_button(1)+'1</a> ';
1377 var ex = (span-1)/2;
1379 if (r.page+ex>=r.pages) {
1385 var max = r.page+ex;
1389 // won't display upper boundary
1390 if (r.page >= span) {
1392 for(var i=r.page-ex; i<max; i++) {
1393 str += this.get_page_button(i);
1398 // this very first elements
1399 for(var i = 2; i < max; i++) {
1400 str += this.get_page_button(i);
1406 // won't display upper boundary
1408 str += this.get_page_button(r.pages)+r.pages+'</a>';
1410 str += this.get_page_button(max)+max+'</a>';
1411 str += ' ... '+this.get_page_button(r.pages)+r.pages+'</a>';
1416 var a = Y.Node.create(str);
1417 Y.one('#fp-header-'+client_id).appendChild(a);
1419 Y.all('#fp-header-'+client_id+' .fp-paging a').each(
1420 function(node, id) {
1421 node.on('click', function(e) {
1422 var id = node.get('id');
1423 var re = new RegExp("repo-page-(\\d+)", "i");
1424 var result = id.match(re);
1426 args.page = result[1];
1427 if (scope.active_repo.issearchresult) {
1431 client_id: client_id,
1432 repository_id: r.id,
1433 params: {'page':result[1]},
1434 callback: function(id, o, args) {
1435 o.issearchresult = true;
1436 scope.parse_repository_options(o);
1437 scope.view_files(result[1]);
1442 if (scope.viewmode == 2) {
1443 scope.view_as_list(result[1]);
1452 print_path: function() {
1453 var client_id = this.options.client_id;
1454 var panel = Y.one('#panel-'+client_id);
1455 var p = this.filepath;
1456 if (p && p.length!=0) {
1457 var path = Y.Node.create('<div id="path-'+client_id+'" class="fp-pathbar"></div>');
1458 panel.appendChild(path);
1459 for(var i = 0; i < p.length; i++) {
1460 var link_path = p[i].path;
1461 var link = document.createElement('A');
1463 link.innerHTML = p[i].name;
1464 link.id = 'path-node-'+client_id+'-'+i;
1465 var sep = Y.Node.create('<span>/</span>');
1466 path.appendChild(link);
1467 path.appendChild(sep);
1468 Y.one('#'+link.id).on('click', function(Y, path){
1469 this.list({'path':path});
1478 if (this.rendered) {
1479 var panel = Y.one('#panel-'+this.options.client_id);
1480 panel.set('innerHTML', '');
1482 this.show_recent_repository();
1487 launch: function() {
1490 show_recent_repository: function() {
1491 var repository_id = YAHOO.util.Cookie.get('recentrepository');
1492 if (this.options.repositories[repository_id]) {
1493 this.list({'repo_id':repository_id});
1497 var loading = Y.one('#filepicker-loading-'+options.client_id);
1499 loading.setStyle('display', 'none');
1501 M.core_filepicker.instances[options.client_id] = new FilePickerHelper(options);