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;
88 var api = this.api + '?action='+args.action;
92 scope = args['scope'];
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['itemid'] = this.options.itemid?this.options.itemid:0;
103 params['maxbytes'] = this.options.maxbytes?this.options.maxbytes:-1;
104 if (this.options.context && this.options.context.id) {
105 params['ctx_id'] = this.options.context.id;
107 if (args['params']) {
108 for (i in args['params']) {
109 params[i] = args['params'][i];
112 if (args.action == 'upload') {
114 for(var k in params) {
115 var value = params[k];
116 if(value instanceof Array) {
117 for(var i in value) {
118 list.push(k+'[]='+value[i]);
121 list.push(k+'='+value);
124 params = list.join('&');
126 params = build_querystring(params);
131 complete: function(id,o,p) {
132 var panel_id = '#panel-'+client_id;
139 data = Y.JSON.parse(o.responseText);
141 scope.print_msg(M.str.repository.invalidjson, 'error');
142 Y.one(panel_id).set('innerHTML', 'ERROR: '+M.str.repository.invalidjson+'<pre>'+stripHTML(o.responseText)+'</pre>');
146 if (data && data.error) {
147 scope.print_msg(data.error, 'error');
152 scope.print_msg(data.msg, 'info');
154 args.callback(id,data,p);
162 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
168 cfg.form = args.form;
175 print_msg: function(msg, type) {
176 var client_id = this.options.client_id;
177 var dlg_id = 'fp-msg-dlg-'+client_id;
178 function handleYes() {
181 var icon = YAHOO.widget.SimpleDialog.ICON_INFO;
183 icon = YAHOO.widget.SimpleDialog.ICON_ALARM;
186 this.msg_dlg = new YAHOO.widget.SimpleDialog(dlg_id,
196 constraintoviewport: true,
197 buttons: [{ text:M.str.moodle.ok, handler:handleYes, isDefault:true }]
199 this.msg_dlg.render(document.body);
201 this.msg_dlg.setBody(msg);
203 var header = M.str.moodle.info;
205 header = M.str.moodle.error;
207 this.msg_dlg.setHeader(type);
210 build_tree: function(node, level) {
211 var client_id = this.options.client_id;
212 var dynload = this.active_repo.dynload;
214 node.title = '<i><u>'+node.title+'</u></i>';
218 //title:fp_lang.date+' '+node.date+fp_lang.size+' '+node.size,
220 source:node.source?node.source:'',
221 thumbnail:node.thumbnail,
222 path:node.path?node.path:[]
224 var tmpNode = new YAHOO.widget.TextNode(info, level, false);
226 tmpNode.repo_id=node.repo_id;
228 tmpNode.repo_id=this.active_repo.id;
235 tmpNode.scope = this;
237 tmpNode.isLeaf = false;
238 tmpNode.client_id = client_id;
240 tmpNode.path = node.path;
244 for(var c in node.children) {
245 this.build_tree(node.children[c], tmpNode);
248 tmpNode.isLeaf = true;
251 view_files: function() {
252 this.viewbar.set('disabled', false);
253 if (this.viewmode == 1) {
254 this.view_as_icons();
255 } else if (this.viewmode ==2) {
258 this.view_as_icons();
261 treeview_dynload: function(node, cb) {
262 var scope = node.scope;
263 var client_id = scope.options.client_id;
264 var repository_id = scope.active_repo.id;
267 client_id: client_id,
268 repository_id: repository_id,
269 path:node.path?node.path:'',
270 page:node.page?args.page:'',
271 callback: function(id, obj, args) {
272 obj.issearchresult = false;
274 scope.viewbar.set('disabled', false);
275 scope.parse_repository_options(obj);
277 scope.build_tree(list[k], node);
283 view_as_list: function() {
287 client_id: scope.options.client_id,
288 repository_id: scope.active_repo.id,
291 callback: function(id, obj, args) {
292 var client_id = scope.options.client_id;
293 var dynload = scope.active_repo.dynload;
295 var panel_id = '#panel-'+client_id;
297 Y.one(panel_id).set('innerHTML', '');
299 scope.print_header();
301 var html = '<div class="fp-tree-panel" id="treeview-'+client_id+'">';
302 if (list.length==0) {
303 html += '<div class="fp-emptylist mdl-align">' +M.str.repository.nofilesavailable+'</div>';
307 var tree = Y.Node.create(html);
308 Y.one(panel_id).appendChild(tree);
309 if (list.length==0) {
313 scope.treeview = new YAHOO.widget.TreeView('treeview-'+client_id);
315 scope.treeview.setDynamicLoad(scope.treeview_dynload, 1);
319 scope.build_tree(list[k], scope.treeview.getRoot());
321 scope.treeview.subscribe('clickEvent', function(e){
324 fileinfo['title'] = e.node.data.filename;
325 fileinfo['source'] = e.node.data.source;
326 fileinfo['thumbnail'] = e.node.data.thumbnail;
327 scope.select_file(fileinfo);
330 scope.treeview.draw();
334 view_as_icons: function() {
336 var client_id = this.options.client_id;
337 var list = this.filelist;
338 var panel_id = '#panel-'+client_id;
340 Y.one(panel_id).set('innerHTML', '');
344 var html = '<div class="fp-grid-panel" id="fp-grid-panel-'+client_id+'">';
345 if (list.length==0) {
346 html += '<div class="fp-emptylist mdl-align">' +M.str.repository.nofilesavailable+'</div>';
350 var gridpanel = Y.Node.create(html);
351 Y.one('#panel-'+client_id).appendChild(gridpanel);
355 var grid = document.createElement('DIV');
356 grid.className='fp-grid';
358 var title = document.createElement('DIV');
359 title.id = 'grid-title-'+client_id+'-'+String(count);
360 title.className = 'label';
361 var filename = node.title;
362 if (node.shorttitle) {
363 filename = node.shorttitle;
365 var filename_id = 'filname-link-'+client_id+'-'+String(count);
366 title.innerHTML += '<a href="###" id="'+filename_id+'" title="'+node.title+'"><span>'+filename+"</span></a>";
369 if(node.thumbnail_width){
370 grid.style.width = node.thumbnail_width+'px';
371 title.style.width = (node.thumbnail_width-10)+'px';
373 grid.style.width = title.style.width = '90px';
375 var frame = document.createElement('DIV');
376 frame.style.textAlign='center';
377 if(node.thumbnail_height){
378 frame.style.height = node.thumbnail_height+'px';
380 var img = document.createElement('img');
381 img.src = node.thumbnail;
382 img.title = node.title;
383 if(node.thumbnail_alt) {
384 img.alt = node.thumbnail_alt;
386 if(node.thumbnail_title) {
387 img.title = node.thumbnail_title;
390 var link = document.createElement('A');
392 link.id = 'img-id-'+client_id+'-'+String(count);
395 //grid.innerHTML += '<p><a target="_blank" href="'+node.url+'">'+M.str.repository.preview+'</a></p>';
397 link.appendChild(img);
398 frame.appendChild(link);
399 grid.appendChild(frame);
400 grid.appendChild(title);
401 gridpanel.appendChild(grid);
403 var y_title = Y.one('#'+title.id);
404 var y_file = Y.one('#'+link.id);
405 var dynload = this.active_repo.dynload;
407 y_file.on('click', function(e, p) {
409 var params = {'path':p.path};
412 this.filelist = p.children;
416 y_title.on('click', function(e, p, id){
417 var icon = Y.one(id);
418 icon.simulate('click');
419 }, this, node, '#'+link.id);
422 fileinfo['title'] = list[k].title;
423 fileinfo['source'] = list[k].source;
424 fileinfo['thumbnail'] = list[k].thumbnail;
425 fileinfo['haslicense'] = list[k].haslicense?true:false;
426 fileinfo['hasauthor'] = list[k].hasauthor?true:false;
427 y_title.on('click', function(e, args) {
428 this.select_file(args);
430 y_file.on('click', function(e, args) {
431 this.select_file(args);
437 select_file: function(args) {
438 var client_id = this.options.client_id;
439 var thumbnail = Y.one('#fp-grid-panel-'+client_id);
441 thumbnail.setStyle('display', 'none');
443 var header = Y.one('#fp-header-'+client_id);
445 header.setStyle('display', 'none');
447 var footer = Y.one('#fp-footer-'+client_id);
449 footer.setStyle('display', 'none');
451 var path = Y.one('#path-'+client_id);
453 path.setStyle('display', 'none');
455 var panel = Y.one('#panel-'+client_id);
456 var form_id = 'fp-rename-form-'+client_id;
457 var html = '<div class="fp-rename-form" id="'+form_id+'">';
458 html += '<p><img src="'+args.thumbnail+'" /></p>';
459 html += '<table width="100%">';
460 html += '<tr><td class="mdl-right"><label for="newname-'+client_id+'">'+M.str.repository.saveas+':</label></td>';
461 html += '<td class="mdl-left"><input type="text" id="newname-'+client_id+'" value="'+args.title+'" /></td></tr>';
465 if (this.options.repositories[this.active_repo.id].return_types == 1) {
466 // support external links only
467 le_checked = 'checked';
468 le_style = ' style="display:none;"';
469 } else if(this.options.repositories[this.active_repo.id].return_types == 2) {
470 // support internal files only
471 le_style = ' style="display:none;"';
473 if ((this.options.externallink && this.options.env == 'editor' && this.options.return_types != 1)) {
474 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>';
477 if (!args.hasauthor) {
478 // the author of the file
479 html += '<tr><td class="mdl-right"><label for="text-author">'+M.str.repository.author+' :</label></td>';
480 html += '<td class="mdl-left"><input id="text-author-'+client_id+'" type="text" name="author" value="'+this.options.author+'" /></td>';
484 if (!args.haslicense) {
485 // the license of the file
486 var licenses = this.options.licenses;
487 html += '<tr><td class="mdl-right"><label for="select-license-'+client_id+'">'+M.str.repository.chooselicense+' :</label></td>';
488 html += '<td class="mdl-left"><select name="license" id="select-license-'+client_id+'">';
489 var recentlicense = YAHOO.util.Cookie.get('recentlicense');
491 this.options.defaultlicense=recentlicense;
493 for (var i in licenses) {
494 if (this.options.defaultlicense==licenses[i].shortname) {
495 var selected = ' selected';
499 html += '<option value="'+licenses[i].shortname+'"'+selected+'>'+licenses[i].fullname+'</option>';
501 html += '</select></td></tr>';
505 html += '<p><input type="hidden" id="filesource-'+client_id+'" value="'+args.source+'" />';
506 html += '<input type="button" id="fp-confirm-'+client_id+'" value="'+M.str.repository.getfile+'" />';
507 html += '<input type="button" id="fp-cancel-'+client_id+'" value="'+M.str.moodle.cancel+'" /></p>';
510 var getfile_form = Y.Node.create(html);
511 panel.appendChild(getfile_form);
513 var getfile = Y.one('#fp-confirm-'+client_id);
514 getfile.on('click', function(e) {
515 var client_id = this.options.client_id;
517 var repository_id = this.active_repo.id;
518 var title = Y.one('#newname-'+client_id).get('value');
519 var filesource = Y.one('#filesource-'+client_id).get('value');
520 var params = {'title':title, 'source':filesource, 'savepath': this.options.savepath};
521 var license = Y.one('#select-license-'+client_id);
523 params['license'] = license.get('value');
524 YAHOO.util.Cookie.set('recentlicense', license.get('value'));
526 var author = Y.one('#text-author-'+client_id);
528 params['author'] = author.get('value');
531 if (this.options.env == 'editor') {
532 // in editor, images are stored in '/' only
533 params.savepath = '/';
534 // when image or media button is clicked
535 if ( this.options.return_types != 1 ) {
536 var linkexternal = Y.one('#linkexternal-'+client_id).get('checked');
538 params['linkexternal'] = 'yes';
541 // when link button in editor clicked
542 params['linkexternal'] = 'yes';
546 if (this.options.env == 'url') {
547 params['linkexternal'] = 'yes';
550 this.wait('download', title);
553 client_id: client_id,
554 repository_id: repository_id,
556 callback: function(id, obj, args) {
557 if (scope.options.editor_target && scope.options.env=='editor') {
558 scope.options.editor_target.value=obj.url;
559 scope.options.editor_target.onchange();
562 obj.client_id = client_id;
563 var formcallback_scope = null;
564 if (args.scope.options.magicscope) {
565 formcallback_scope = args.scope.options.magicscope;
567 formcallback_scope = args.scope;
569 scope.options.formcallback.apply(formcallback_scope, [obj]);
573 var elform = Y.one('#'+form_id);
574 elform.on('keydown', function(e) {
575 if (e.keyCode == 13) {
576 getfile.simulate('click');
580 var cancel = Y.one('#fp-cancel-'+client_id);
581 cancel.on('click', function(e) {
584 var treeview = Y.one('#treeview-'+client_id);
586 treeview.setStyle('display', 'none');
589 wait: function(type) {
590 var panel = Y.one('#panel-'+this.options.client_id);
591 panel.set('innerHTML', '');
593 var str = '<div style="text-align:center">';
595 str += '<img src="'+M.util.image_url('i/loading')+'" />';
596 str += '<p>'+M.str.repository.loading+'</p>';
598 str += '<img src="'+M.util.image_url('i/progressbar')+'" />';
599 str += '<p>'+M.str.repository.copying+' <strong>'+name+'</strong></p>';
603 panel.set('innerHTML', str);
609 var client_id = this.options.client_id;
611 var filepicker_id = 'filepicker-'+client_id;
612 var fpnode = Y.Node.create('<div class="file-picker" id="'+filepicker_id+'"></div>');
613 Y.one(document.body).appendChild(fpnode);
614 // render file picker panel
615 this.mainui = new YAHOO.widget.Panel(filepicker_id, {
620 monitorresize: false,
621 xy: [50, YAHOO.util.Dom.getDocumentScrollTop()+20]
624 this.mainui.beforeRenderEvent.subscribe(function() {
625 YAHOO.util.Event.onAvailable('layout-'+client_id, function() {
626 layout = new YAHOO.widget.Layout('layout-'+client_id, {
627 height: 480, width: 700,
629 {position: 'top', height: 32, resize: false,
630 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'},
631 {position: 'left', width: 200, resize: true, scroll:true,
632 body:'<ul class="fp-list" id="fp-list-'+client_id+'"></ul>', gutter: '0 5 0 2', minWidth: 150, maxWidth: 300 },
633 {position: 'center', body: '<div class="fp-panel" id="panel-'+client_id+'"></div>',
634 scroll: true, gutter: '0 2 0 0' }
638 scope.show_recent_repository();
642 this.mainui.setHeader(M.str.repository.filepicker);
643 this.mainui.setBody('<div id="layout-'+client_id+'"></div>');
644 this.mainui.render();
645 this.rendered = true;
649 var view_icons = {label: M.str.repository.iconview, value: 't',
652 scope.view_as_icons();
656 var view_listing = {label: M.str.repository.listview, value: 'l',
659 scope.view_as_list();
663 this.viewbar = new YAHOO.widget.ButtonGroup({
664 id: 'btngroup-'+client_id,
667 container: 'fp-viewbar-'+client_id
669 this.viewbar.addButtons([view_icons, view_listing]);
670 // processing repository listing
671 var r = this.options.repositories;
672 Y.on('contentready', function(el) {
673 var list = Y.one(el);
676 var id = 'repository-'+client_id+'-'+r[i].id;
677 var link_id = id + '-link';
678 list.append('<li id="'+id+'"><a class="fp-repo-name" id="'+link_id+'" href="###">'+r[i].name+'</a></li>');
679 Y.one('#'+link_id).prepend('<img src="'+r[i].icon+'" width="16" height="16" /> ');
680 Y.one('#'+link_id).on('click', function(e, scope, repository_id) {
681 YAHOO.util.Cookie.set('recentrepository', repository_id);
682 scope.repository_id = repository_id;
683 this.list({'repo_id':repository_id});
684 }, this /*handler running scope*/, this/*second argument*/, r[i].id/*third argument of handler*/);
688 if (this.options.externallink) {
689 list.set('innerHTML', M.str.repository.norepositoriesexternalavailable);
691 list.set('innerHTML', M.str.repository.norepositoriesavailable);
694 }, '#fp-list-'+client_id, this /* handler running scope */, '#fp-list-'+client_id /*first argument of handler*/);
696 parse_repository_options: function(data) {
697 this.filelist = data.list?data.list:null;
698 this.filepath = data.path?data.path:null;
699 this.active_repo = {};
700 this.active_repo.issearchresult = Boolean(data.issearchresult);
701 this.active_repo.dynload = data.dynload?data.dynload:false;
702 this.active_repo.pages = Number(data.pages?data.pages:null);
703 this.active_repo.page = Number(data.page?data.page:null);
704 this.active_repo.id = data.repo_id?data.repo_id:null;
705 this.active_repo.nosearch = data.nosearch?true:false;
706 this.active_repo.norefresh = data.norefresh?true:false;
707 this.active_repo.nologin = data.nologin?true:false;
708 this.active_repo.logouttext = data.logouttext?data.logouttext:null;
709 this.active_repo.help = data.help?data.help:null;
710 this.active_repo.manage = data.manage?data.manage:null;
712 print_login: function(data) {
713 this.parse_repository_options(data);
714 var client_id = this.options.client_id;
715 var repository_id = data.repo_id;
716 var l = this.logindata = data.login;
718 var panel = Y.one('#panel-'+client_id);
719 var action = 'login';
720 if (data['login_btn_action']) {
721 action=data['login_btn_action'];
723 var form_id = 'fp-form-'+client_id;
724 var download_button_id = 'fp-form-download-button-'+client_id;
725 var search_button_id = 'fp-form-search-button-'+client_id;
726 var login_button_id = 'fp-form-login-button-'+client_id;
727 var popup_button_id = 'fp-form-popup-button-'+client_id;
729 var str = '<div class="fp-login-form">';
730 str += '<form id="'+form_id+'">';
732 str +='<table width="100%">';
735 if(l[k].type=='popup') {
738 str += '<td colspan="2"><p class="fp-popup">'+M.str.repository.popup+'</p>';
739 str += '<p class="fp-popup"><button id="'+popup_button_id+'">'+M.str.repository.login+'</button>';
742 }else if(l[k].type=='textarea') {
744 str += '<td colspan="2"><p><textarea id="'+l[k].id+'" name="'+l[k].name+'"></textarea></p></td>';
745 }else if(l[k].type=='select') {
747 str += '<td align="right"><label>'+l[k].label+':</label></td>';
748 str += '<td align="left"><select id="'+l[k].id+'" name="'+l[k].name+'">';
749 for (i in l[k].options) {
750 str += '<option value="'+l[k].options[i].value+'">'+l[k].options[i].label+'</option>';
752 str += '</select></td>';
757 var field_value = '';
759 label_id = ' for="'+l[k].id+'"';
760 field_id = ' id="'+l[k].id+'"';
763 str += '<td align="right" width="30%" valign="center">';
764 str += '<label'+label_id+'>'+l[k].label+'</label> </td>';
766 str += '<td width="30%"></td>';
769 field_value = ' value="'+l[k].value+'"';
771 if(l[k].type=='radio'){
772 var list = l[k].value.split('|');
773 var labels = l[k].value_label.split('|');
774 str += '<td align="left">';
775 for(var item in list) {
776 str +='<input type="'+l[k].type+'"'+' name="'+l[k].name+'"'+
777 field_id+' value="'+list[item]+'" />'+labels[item]+'<br />';
781 str += '<td align="left">';
782 str += '<input type="'+l[k].type+'"'+' name="'+l[k].name+'"'+field_value+' '+field_id+' />';
792 var btn_label = data['login_btn_label']?data['login_btn_label']:M.str.repository.submit;
793 if (action != 'popup') {
794 str += '<p><input type="button" id="';
797 str += search_button_id;
800 str += download_button_id;
803 str += login_button_id;
806 str += '" value="'+btn_label+'" /></p>';
813 panel.set('innerHTML', str);
815 alert(e.toString()+M.str.quiz.xhtml);
818 // process login action
819 var login_button = Y.one('#'+login_button_id);
822 login_button.on('click', function(){
824 var data = this.logindata;
827 for (var k in data) {
828 if(data[k].type!='popup') {
829 var el = Y.one('[name='+data[k].name+']');
830 var type = el.get('type');
831 params[data[k].name] = '';
832 if(type == 'checkbox') {
833 params[data[k].name] = el.get('checked');
835 params[data[k].name] = el.get('value');
839 // start ajax request
845 'client_id': client_id,
846 'repository_id': repository_id,
847 'callback': function(id, o, args) {
848 scope.parse_repository_options(o);
854 var search_button = Y.one('#'+search_button_id);
856 search_button.on('click', function(){
857 var data = this.logindata;
860 for (var k in data) {
861 if(data[k].type!='popup') {
862 var el = document.getElementsByName(data[k].name)[0];
863 params[data[k].name] = '';
864 if(el.type == 'checkbox') {
865 params[data[k].name] = el.checked;
866 } else if(el.type == 'radio') {
867 var tmp = document.getElementsByName(data[k].name);
869 if (tmp[i].checked) {
870 params[data[k].name] = tmp[i].value;
874 params[data[k].name] = el.value;
881 client_id: client_id,
882 repository_id: repository_id,
883 form: {id: 'fp-form-'+scope.options.client_id,upload:false,useDisabled:true},
884 callback: function(id, o, args) {
885 o.issearchresult = true;
886 scope.parse_repository_options(o);
892 var download_button = Y.one('#'+download_button_id);
893 if (download_button) {
894 download_button.on('click', function(){
898 var popup_button = Y.one('#'+popup_button_id);
900 popup_button.on('click', function(e){
901 M.core_filepicker.active_filepicker = this;
902 window.open(loginurl, 'repo_auth', 'location=0,status=0,width=500,height=300,scrollbars=yes');
906 var elform = Y.one('#'+form_id);
907 elform.on('keydown', function(e) {
908 if (e.keyCode == 13) {
911 search_button.simulate('click');
914 login_button.simulate('click');
922 search: function(args) {
923 var data = this.logindata;
926 for (var k in data) {
927 if(data[k].type!='popup') {
928 var el = document.getElementsByName(data[k].name)[0];
929 params[data[k].name] = '';
930 if(el.type == 'checkbox') {
931 params[data[k].name] = el.checked;
932 } else if(el.type == 'radio') {
933 var tmp = document.getElementsByName(data[k].name);
935 if (tmp[i].checked) {
936 params[data[k].name] = tmp[i].value;
940 params[data[k].name] = el.value;
947 client_id: client_id,
948 repository_id: repository_id,
949 form: {id: 'fp-form-'+scope.options.client_id,upload:false,useDisabled:true},
950 callback: function(id, o, args) {
951 o.issearchresult = true;
952 scope.parse_repository_options(o);
957 list: function(args) {
963 args.repo_id = scope.active_repo.id;
967 client_id: scope.options.client_id,
968 repository_id: args.repo_id,
969 path:args.path?args.path:'',
970 page:args.page?args.page:'',
971 callback: function(id, obj, args) {
972 Y.all('#fp-list-'+scope.options.client_id+' li a').setStyle('backgroundColor', 'transparent');
973 var el = Y.one('#repository-'+scope.options.client_id+'-'+obj.repo_id+'-link');
975 el.setStyle('backgroundColor', '#AACCEE');
978 scope.viewbar.set('disabled', true);
979 scope.print_login(obj);
980 } else if (obj.upload) {
981 scope.viewbar.set('disabled', true);
982 scope.parse_repository_options(obj);
983 scope.create_upload_form(obj);
985 } else if (obj.iframe) {
987 } else if (obj.list) {
988 obj.issearchresult = false;
989 scope.viewbar.set('disabled', false);
990 scope.parse_repository_options(obj);
996 create_upload_form: function(data) {
997 var client_id = this.options.client_id;
998 Y.one('#panel-'+client_id).set('innerHTML', '');
999 var types = this.options.accepted_types;
1001 this.print_header();
1002 var id = data.upload.id+'_'+client_id;
1003 var str = '<div id="'+id+'_div" class="fp-upload-form mdl-align">';
1004 str += '<form id="'+id+'" method="POST">';
1005 str += '<table width="100%">';
1006 str += '<tr><td class="mdl-right">';
1007 str += '<label for="'+id+'_file">'+data.upload.label+': </label></td>';
1008 str += '<td class="mdl-left"><input type="file" id="'+id+'_file" name="repo_upload_file" />';
1009 str += '<tr><td class="mdl-right"><label for="newname-'+client_id+'">'+M.str.repository.saveas+':</label></td>';
1010 str += '<td class="mdl-left"><input type="text" name="title" id="newname-'+client_id+'" value="" /></td></tr>';
1011 str += '<input type="hidden" name="itemid" value="'+this.options.itemid+'" />';
1012 for (var i in types) {
1013 str += '<input type="hidden" name="accepted_types[]" value="'+types[i]+'" />';
1015 str += '</td></tr><tr>';
1016 str += '<td class="mdl-right"><label>'+M.str.repository.author+': </label></td>';
1017 str += '<td class="mdl-left"><input type="text" name="author" value="'+this.options.author+'" /></td>';
1020 str += '<td class="mdl-right">'+M.str.repository.chooselicense+': </td>';
1021 str += '<td class="mdl-left">';
1022 var licenses = this.options.licenses;
1023 str += '<select name="license" id="select-license-'+client_id+'">';
1024 var recentlicense = YAHOO.util.Cookie.get('recentlicense');
1025 if (recentlicense) {
1026 this.options.defaultlicense=recentlicense;
1028 for (var i in licenses) {
1029 if (this.options.defaultlicense==licenses[i].shortname) {
1030 var selected = ' selected';
1034 str += '<option value="'+licenses[i].shortname+'"'+selected+'>'+licenses[i].fullname+'</option>';
1038 str += '</tr></table>';
1040 str += '<div class="fp-upload-btn"><button id="'+id+'_action">'+M.str.repository.upload+'</button></div>';
1042 var upload_form = Y.Node.create(str);
1043 Y.one('#panel-'+client_id).appendChild(upload_form);
1045 Y.one('#'+id+'_action').on('click', function(e) {
1047 var license = Y.one('#select-license-'+client_id).get('value');
1048 YAHOO.util.Cookie.set('recentlicense', license);
1049 if (!Y.one('#'+id+'_file').get('value')) {
1050 scope.print_msg(M.str.repository.nofilesattached, 'error');
1053 Y.use('io-upload-iframe', function() {
1057 client_id: client_id,
1058 params: {'savepath':scope.options.savepath},
1059 repository_id: scope.active_repo.id,
1060 form: {id: id, upload:true},
1061 callback: function(id, o, args) {
1062 if (scope.options.editor_target&&scope.options.env=='editor') {
1063 scope.options.editor_target.value=o.url;
1064 scope.options.editor_target.onchange();
1067 o.client_id = client_id;
1068 var formcallback_scope = null;
1069 if (args.scope.options.magicscope) {
1070 formcallback_scope = args.scope.options.magicscope;
1072 formcallback_scope = args.scope;
1074 scope.options.formcallback.apply(formcallback_scope, [o]);
1080 print_header: function() {
1081 var r = this.active_repo;
1083 var client_id = this.options.client_id;
1084 var repository_id = this.active_repo.id;
1085 var panel = Y.one('#panel-'+client_id);
1086 var str = '<div id="fp-header-'+client_id+'">';
1087 str += '<div class="fp-toolbar" id="repo-tb-'+client_id+'"></div>';
1089 var head = Y.Node.create(str);
1090 panel.appendChild(head);
1091 //if(this.active_repo.pages < 8){
1092 this.print_paging('header');
1096 var toolbar = Y.one('#repo-tb-'+client_id);
1099 var html = '<a href="###"><img src="'+M.util.image_url('a/search')+'" /> '+M.str.repository.search+'</a>';
1100 var search = Y.Node.create(html);
1101 search.on('click', function() {
1104 action:'searchform',
1105 repository_id: repository_id,
1106 callback: function(id, obj, args) {
1107 var scope = args.scope;
1108 var client_id = scope.options.client_id;
1109 var repository_id = scope.active_repo.id;
1110 var container = document.getElementById('fp-search-dlg');
1112 container.innerHTML = '';
1113 container.parentNode.removeChild(container);
1115 var container = document.createElement('DIV');
1116 container.id = 'fp-search-dlg';
1118 var dlg_title = document.createElement('DIV');
1119 dlg_title.className = 'hd';
1120 dlg_title.innerHTML = 'filepicker';
1122 var dlg_body = document.createElement('DIV');
1123 dlg_body.className = 'bd';
1125 var sform = document.createElement('FORM');
1126 sform.method = 'POST';
1127 sform.id = "fp-search-form";
1128 sform.innerHTML = obj.form;
1130 dlg_body.appendChild(sform);
1131 container.appendChild(dlg_title);
1132 container.appendChild(dlg_body);
1133 Y.one(document.body).appendChild(container);
1134 var search_dialog= null;
1135 function dialog_handler() {
1136 scope.viewbar.set('disabled', false);
1140 client_id: client_id,
1141 repository_id: repository_id,
1142 form: {id: 'fp-search-form',upload:false,useDisabled:true},
1143 callback: function(id, o, args) {
1144 scope.parse_repository_options(o);
1148 search_dialog.cancel();
1151 search_dialog = new YAHOO.widget.Dialog("fp-search-dlg", {
1152 postmethod: 'async',
1158 constraintoviewport : true,
1161 text:M.str.repository.submit,
1162 handler:dialog_handler,
1165 text:M.str.moodle.cancel,
1171 search_dialog.render();
1172 search_dialog.show();
1176 toolbar.appendChild(search);
1178 // weather we use cache for this instance, this button will reload listing anyway
1180 var html = '<a href="###"><img src="'+M.util.image_url('a/refresh')+'" /> '+M.str.repository.refresh+'</a>';
1181 var refresh = Y.Node.create(html);
1182 refresh.on('click', function() {
1185 toolbar.appendChild(refresh);
1188 var label = r.logouttext?r.logouttext:M.str.repository.logout;
1189 var html = '<a href="###"><img src="'+M.util.image_url('a/logout')+'" /> '+label+'</a>';
1190 var logout = Y.Node.create(html);
1191 logout.on('click', function() {
1194 client_id: client_id,
1195 repository_id: repository_id,
1197 callback: function(id, obj, args) {
1198 scope.viewbar.set('disabled', true);
1199 scope.print_login(obj);
1203 toolbar.appendChild(logout);
1207 var mgr = document.createElement('A');
1208 mgr.href = r.manage;
1209 mgr.target = "_blank";
1210 mgr.innerHTML = '<img src="'+M.util.image_url('a/setting')+'" /> '+M.str.repository.manageurl;
1211 toolbar.appendChild(mgr);
1214 var help = document.createElement('A');
1216 help.target = "_blank";
1217 help.innerHTML = '<img src="'+M.util.image_url('a/help')+'" /> '+M.str.repository.help;
1218 toolbar.appendChild(help);
1223 get_page_button: function(page) {
1224 var r = this.active_repo;
1226 if (page == r.page) {
1227 css = 'class="cur_page" ';
1229 var str = '<a '+css+'href="###" id="repo-page-'+page+'">';
1232 print_paging: function(html_id) {
1233 var client_id = this.options.client_id;
1235 var r = this.active_repo;
1239 str += '<div class="fp-paging" id="paging-'+html_id+'-'+client_id+'">';
1240 str += this.get_page_button(1)+'1</a> ';
1243 var ex = (span-1)/2;
1245 if (r.page+ex>=r.pages) {
1251 var max = r.page+ex;
1255 // won't display upper boundary
1256 if (r.page >= span) {
1258 for(var i=r.page-ex; i<max; i++) {
1259 str += this.get_page_button(i);
1264 // this very first elements
1265 for(var i = 2; i < max; i++) {
1266 str += this.get_page_button(i);
1272 // won't display upper boundary
1274 str += this.get_page_button(r.pages)+r.pages+'</a>';
1276 str += this.get_page_button(max)+max+'</a>';
1277 str += ' ... '+this.get_page_button(r.pages)+r.pages+'</a>';
1282 var a = Y.Node.create(str);
1283 Y.one('#fp-header-'+client_id).appendChild(a);
1285 Y.all('#fp-header-'+client_id+' .fp-paging a').each(
1286 function(node, id) {
1287 node.on('click', function(e) {
1288 var id = node.get('id');
1289 var re = new RegExp("repo-page-(\\d+)", "i");
1290 var result = id.match(re);
1292 args.page = result[1];
1293 if (scope.active_repo.issearchresult) {
1297 client_id: client_id,
1298 repository_id: r.id,
1299 params: {'page':result[1]},
1300 callback: function(id, o, args) {
1301 o.issearchresult = true;
1302 scope.parse_repository_options(o);
1314 print_path: function() {
1315 var client_id = this.options.client_id;
1316 var panel = Y.one('#panel-'+client_id);
1317 var p = this.filepath;
1318 if (p && p.length!=0) {
1319 var path = Y.Node.create('<div id="path-'+client_id+'" class="fp-pathbar"></div>');
1320 panel.appendChild(path);
1321 for(var i = 0; i < p.length; i++) {
1322 var link_path = p[i].path;
1323 var link = document.createElement('A');
1325 link.innerHTML = p[i].name;
1326 link.id = 'path-node-'+client_id+'-'+i;
1327 var sep = Y.Node.create('<span>/</span>');
1328 path.appendChild(link);
1329 path.appendChild(sep);
1330 Y.one('#'+link.id).on('click', function(Y, path){
1331 this.list({'path':path});
1340 if (this.rendered) {
1341 var panel = Y.one('#panel-'+this.options.client_id);
1342 panel.set('innerHTML', '');
1344 this.show_recent_repository();
1349 launch: function() {
1352 show_recent_repository: function() {
1353 var repository_id = YAHOO.util.Cookie.get('recentrepository');
1354 if (this.options.repositories[repository_id]) {
1355 this.list({'repo_id':repository_id});
1359 var loading = Y.one('#filepicker-loading-'+options.client_id);
1361 loading.setStyle('display', 'none');
1363 M.core_filepicker.instances[options.client_id] = new FilePickerHelper(options);