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['filearea'] = this.options.filearea?this.options.filearea:'user_draft';
103 params['itemid'] = this.options.itemid?this.options.itemid:0;
104 params['maxbytes'] = this.options.maxbytes?this.options.maxbytes:-1;
105 if (args['params']) {
106 for (i in args['params']) {
107 params[i] = args['params'][i];
113 complete: function(id,o,p) {
114 var panel_id = '#panel-'+client_id;
121 data = Y.JSON.parse(o.responseText);
123 Y.one(panel_id).set('innerHTML', 'ERROR: '+M.str.repository.invalidjson);
127 if (data && data.e) {
128 Y.one(panel_id).set('innerHTML', 'ERROR: '+data.e);
131 args.callback(id,data,p);
139 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
140 'User-Agent': 'MoodleFilePicker/3.0'
142 data: build_querystring(params),
146 cfg.form = args.form;
154 build_tree: function(node, level) {
155 var client_id = this.options.client_id;
156 var dynload = this.active_repo.dynload;
158 node.title = '<i><u>'+node.title+'</u></i>';
162 //title:fp_lang.date+' '+node.date+fp_lang.size+' '+node.size,
164 source:node.source?node.source:'',
165 thumbnail:node.thumbnail,
166 path:node.path?node.path:[]
168 var tmpNode = new YAHOO.widget.TextNode(info, level, false);
169 //var tooltip = new YAHOO.widget.Tooltip(tmpNode.labelElId, {
170 //context:tmpNode.labelElId, text:info.title});
172 tmpNode.repo_id=node.repo_id;
174 tmpNode.repo_id=this.active_repo.id;
181 tmpNode.scope = this;
183 tmpNode.isLeaf = false;
184 tmpNode.client_id = client_id;
186 tmpNode.path = node.path;
190 for(var c in node.children) {
191 this.build_tree(node.children[c], tmpNode);
194 tmpNode.isLeaf = true;
197 view_files: function() {
198 this.viewbar.set('disabled', false);
199 if (this.viewmode == 1) {
200 this.view_as_icons();
201 } else if (this.viewmode ==2) {
204 this.view_as_icons();
207 treeview_dynload: function(node, cb) {
208 var scope = node.scope;
209 var client_id = scope.options.client_id;
210 var repository_id = scope.active_repo.id;
213 client_id: client_id,
214 repository_id: repository_id,
215 path:node.path?node.path:'',
216 page:node.page?args.page:'',
217 callback: function(id, obj, args) {
218 obj.issearchresult = false;
220 scope.viewbar.set('disabled', false);
221 scope.parse_repository_options(obj);
224 scope.build_tree(list[k], node);
230 view_as_list: function() {
232 var client_id = this.options.client_id;
233 var dynload = this.active_repo.dynload;
234 var list = this.filelist;
235 var panel_id = '#panel-'+client_id;
237 Y.one(panel_id).set('innerHTML', '');
240 var tree = Y.Node.create('<div id="treeview-'+client_id+'"></div>');
241 Y.one(panel_id).appendChild(tree);
242 this.treeview = new YAHOO.widget.TreeView('treeview-'+client_id);
244 this.treeview.setDynamicLoad(this.treeview_dynload, 1);
248 this.build_tree(list[k], this.treeview.getRoot());
251 this.treeview.subscribe('clickEvent', function(e){
254 fileinfo['title'] = e.node.data.filename;
255 fileinfo['source'] = e.node.data.source;
256 fileinfo['thumbnail'] = e.node.data.thumbnail;
257 scope.select_file(fileinfo);
260 this.treeview.draw();
262 view_as_icons: function() {
264 var client_id = this.options.client_id;
265 var list = this.filelist;
266 var panel_id = '#panel-'+client_id;
268 Y.one(panel_id).set('innerHTML', '');
272 var gridpanel = Y.Node.create('<div id="fp-grid-panel-'+client_id+'"></div>');
273 Y.one('#panel-'+client_id).appendChild(gridpanel);
277 var grid = document.createElement('DIV');
278 grid.className='fp-grid';
280 var title = document.createElement('DIV');
281 title.id = 'grid-title-'+client_id+'-'+String(count);
282 title.className = 'label';
283 if (node.shorttitle) {
284 node.title = node.shorttitle;
286 title.innerHTML += '<a href="###"><span>'+node.title+"</span></a>";
288 if(node.thumbnail_width){
289 grid.style.width = node.thumbnail_width+'px';
290 title.style.width = (node.thumbnail_width-10)+'px';
292 grid.style.width = title.style.width = '90px';
294 var frame = document.createElement('DIV');
295 frame.style.textAlign='center';
296 if(node.thumbnail_height){
297 frame.style.height = node.thumbnail_height+'px';
299 var img = document.createElement('img');
300 img.src = node.thumbnail;
301 if(node.thumbnail_alt) {
302 img.alt = node.thumbnail_alt;
304 if(node.thumbnail_title) {
305 img.title = node.thumbnail_title;
308 var link = document.createElement('A');
310 link.id = 'img-id-'+client_id+'-'+String(count);
313 //grid.innerHTML += '<p><a target="_blank" href="'+node.url+'">'+M.str.repository.preview+'</a></p>';
315 link.appendChild(img);
316 frame.appendChild(link);
317 grid.appendChild(frame);
318 grid.appendChild(title);
319 gridpanel.appendChild(grid);
321 var y_title = Y.one('#'+title.id);
322 var y_file = Y.one('#'+link.id);
323 var dynload = this.active_repo.dynload;
325 y_file.on('click', function(e, p) {
327 var params = {'path':p.path};
330 this.filelist = p.children;
334 y_title.on('click', function(e, p){
335 y_file.simulate('click');
339 fileinfo['title'] = list[k].title;
340 fileinfo['source'] = list[k].source;
341 fileinfo['thumbnail'] = list[k].thumbnail;
342 fileinfo['haslicense'] = list[k].haslicense?true:false;
343 fileinfo['hasauthor'] = list[k].hasauthor?true:false;
344 y_title.on('click', function(e, args) {
345 this.select_file(args);
347 y_file.on('click', function(e, args) {
348 this.select_file(args);
354 select_file: function(args) {
355 var client_id = this.options.client_id;
356 var thumbnail = Y.one('#fp-grid-panel-'+client_id);
358 thumbnail.setStyle('display', 'none');
360 var header = Y.one('#fp-header-'+client_id);
362 header.setStyle('display', 'none');
364 var footer = Y.one('#fp-footer-'+client_id);
366 footer.setStyle('display', 'none');
368 var path = Y.one('#path-'+client_id);
370 path.setStyle('display', 'none');
372 var panel = Y.one('#panel-'+client_id);
373 var form_id = 'fp-rename-form-'+client_id;
374 var html = '<div class="fp-rename-form" id="'+form_id+'">';
375 html += '<p><img src="'+args.thumbnail+'" /></p>';
376 html += '<p><label for="newname-'+client_id+'">'+M.str.repository.saveas+':</label>';
377 html += '<input type="text" id="newname-'+client_id+'" value="'+args.title+'" /></p>';
381 if (this.options.repositories[this.active_repo.id].return_types == 1) {
382 // support external links only
383 le_checked = 'checked';
384 le_style = ' style="display:none;"';
385 } else if(this.options.repositories[this.active_repo.id].return_types == 2) {
386 // support internal files only
387 le_style = ' style="display:none;"';
389 if (this.options.externallink && this.options.env == 'editor') {
390 html += '<p'+le_style+'><input type="checkbox" id="linkexternal-'+client_id+'" value="" '+le_checked+' />'+M.str.repository.linkexternal+'</p>';
393 if (!args.hasauthor) {
394 // the author of the file
395 html += '<p><label for="text-author">'+M.str.repository.author+' :</label>';
396 html += '<input id="text-author-'+client_id+'" type="text" name="author" value="'+this.options.author+'" />';
400 if (!args.haslicense) {
401 // the license of the file
402 var licenses = this.options.licenses;
403 html += '<p><label for="select-license-'+client_id+'">'+M.str.repository.chooselicense+' :</label>';
404 html += '<select name="license" id="select-license-'+client_id+'">';
405 for (var i in licenses) {
406 html += '<option value="'+licenses[i].shortname+'">'+licenses[i].fullname+'</option>';
408 html += '</select></p>';
411 html += '<p><input type="hidden" id="filesource-'+client_id+'" value="'+args.source+'" />';
412 html += '<input type="button" id="fp-confirm-'+client_id+'" value="'+M.str.repository.getfile+'" />';
413 html += '<input type="button" id="fp-cancel-'+client_id+'" value="'+M.str.moodle.cancel+'" /></p>';
416 var getfile_form = Y.Node.create(html);
417 panel.appendChild(getfile_form);
419 var getfile = Y.one('#fp-confirm-'+client_id);
420 getfile.on('click', function(e) {
421 var client_id = this.options.client_id;
423 var repository_id = this.active_repo.id;
424 var title = Y.one('#newname-'+client_id).get('value');
425 var filesource = Y.one('#filesource-'+client_id).get('value');
426 var params = {'title':title, 'source':filesource, 'savepath': this.options.savepath};
427 var license = Y.one('#select-license-'+client_id);
429 params['license'] = license.get('value');
431 var author = Y.one('#text-author-'+client_id);
433 params['author'] = author.get('value');
436 if (this.options.env == 'editor') {
437 // in editor, images are stored in '/' only
438 params.savepath = '/';
439 var linkexternal = Y.one('#linkexternal-'+client_id).get('checked');
441 params['linkexternal'] = 'yes';
443 } if (this.options.env == 'url') {
444 params['linkexternal'] = 'yes';
447 this.wait('download', title);
450 client_id: client_id,
451 repository_id: repository_id,
453 callback: function(id, obj, args) {
454 if (scope.options.editor_target && scope.options.env=='editor') {
455 scope.options.editor_target.value=obj.url;
456 scope.options.editor_target.onchange();
459 obj.client_id = client_id;
460 var formcallback_scope = null;
461 if (args.scope.options.magicscope) {
462 formcallback_scope = args.scope.options.magicscope;
464 formcallback_scope = args.scope;
466 scope.options.formcallback.apply(formcallback_scope, [obj]);
470 var elform = Y.one('#'+form_id);
471 elform.on('keydown', function(e) {
472 if (e.keyCode == 13) {
473 getfile.simulate('click');
477 var cancel = Y.one('#fp-cancel-'+client_id);
478 cancel.on('click', function(e) {
481 var treeview = Y.one('#treeview-'+client_id);
483 treeview.setStyle('display', 'none');
486 wait: function(type) {
487 var panel = Y.one('#panel-'+this.options.client_id);
488 panel.set('innerHTML', '');
490 var str = '<div style="text-align:center">';
492 str += '<img src="'+M.util.image_url('i/loading')+'" />';
493 str += '<p>'+M.str.repository.loading+'</p>';
495 str += '<img src="'+M.util.image_url('i/progressbar')+'" />';
496 str += '<p>'+M.str.repository.copying+' <strong>'+name+'</strong></p>';
500 panel.set('innerHTML', str);
506 var client_id = this.options.client_id;
507 var filepicker_id = 'filepicker-'+client_id;
508 var fpnode = Y.Node.create('<div class="file-picker" id="'+filepicker_id+'"></div>');
509 Y.one(document.body).appendChild(fpnode);
510 // render file picker panel
511 this.mainui = new YAHOO.widget.Panel(filepicker_id, {
516 monitorresize: false,
517 xy: [50, YAHOO.util.Dom.getDocumentScrollTop()+20]
520 this.mainui.beforeRenderEvent.subscribe(function() {
521 YAHOO.util.Event.onAvailable('layout-'+client_id, function() {
522 layout = new YAHOO.widget.Layout('layout-'+client_id, {
523 height: 480, width: 700,
525 {position: 'top', height: 32, resize: false,
526 body:'<div class="yui-buttongroup fp-viewbar" id="fp-viewbar-'+client_id+'"></div><div class="fp-searchbar" id="search-div-'+client_id+'"></div>', gutter: '2'},
527 {position: 'left', width: 200, resize: true, scroll:true,
528 body:'<ul class="fp-list" id="fp-list-'+client_id+'"></ul>', gutter: '0 5 0 2', minWidth: 150, maxWidth: 300 },
529 {position: 'center', body: '<div class="fp-panel" id="panel-'+client_id+'"></div>',
530 scroll: true, gutter: '0 2 0 0' }
536 this.mainui.setHeader('File Picker');
537 this.mainui.setBody('<div id="layout-'+client_id+'"></div>');
538 this.mainui.render();
539 this.rendered = true;
543 var view_icons = {label: M.str.repository.iconview, value: 't',
546 scope.view_as_icons();
550 var view_listing = {label: M.str.repository.listview, value: 'l',
553 scope.view_as_list();
557 this.viewbar = new YAHOO.widget.ButtonGroup({
558 id: 'btngroup-'+client_id,
561 container: 'fp-viewbar-'+client_id
563 this.viewbar.addButtons([view_icons, view_listing]);
564 // processing repository listing
565 var r = this.options.repositories;
566 Y.on('contentready', function(el) {
567 var list = Y.one(el);
570 var id = 'repository-'+client_id+'-'+count;
571 var link_id = id + '-link';
572 list.append('<li id="'+id+'"><a class="fp-repo-name" id="'+link_id+'" href="###">'+r[i].name+'</a></li>');
573 Y.one('#'+link_id).prepend('<img src="'+r[i].icon+'" width="16" height="16" /> ');
574 Y.one('#'+link_id).on('click', function(e, scope, repository_id) {
575 scope.repository_id = repository_id;
576 Y.all(el+' li a').setStyle('backgroundColor', 'transparent');
577 e.currentTarget.setStyle('backgroundColor', '#CCC');
578 this.list({'repo_id':repository_id});
579 }, this /*handler running scope*/, this/*second argument*/, r[i].id/*third argument of handler*/);
582 }, '#fp-list-'+client_id, this /* handler running scope */, '#fp-list-'+client_id /*first argument of handler*/);
584 parse_repository_options: function(data) {
585 this.filelist = data.list?data.list:null;
586 this.filepath = data.path?data.path:null;
587 this.active_repo = {};
588 this.active_repo.issearchresult = Boolean(data.issearchresult);
589 this.active_repo.dynload = data.dynload?data.dynload:false;
590 this.active_repo.pages = Number(data.pages?data.pages:null);
591 this.active_repo.page = Number(data.page?data.page:null);
592 this.active_repo.id = data.repo_id?data.repo_id:null;
593 this.active_repo.nosearch = data.nosearch?true:false;
594 this.active_repo.norefresh = data.norefresh?true:false;
595 this.active_repo.nologin = data.nologin?true:false;
596 this.active_repo.help = data.help?data.help:null;
597 this.active_repo.manage = data.manage?data.manage:null;
599 print_login: function(data) {
600 var client_id = this.options.client_id;
601 var repository_id = data.repo_id;
602 var l = this.logindata = data.login;
604 var panel = Y.one('#panel-'+client_id);
605 var action = 'login';
606 if (data['login_btn_action']) {
607 action=data['login_btn_action'];
609 var form_id = 'fp-form-'+client_id;
610 var download_button_id = 'fp-form-download-button-'+client_id;
611 var search_button_id = 'fp-form-search-button-'+client_id;
612 var login_button_id = 'fp-form-login-button-'+client_id;
613 var popup_button_id = 'fp-form-popup-button-'+client_id;
615 var str = '<div class="fp-login-form">';
616 str += '<form id="'+form_id+'">';
618 str +='<table width="100%">';
621 if(l[k].type=='popup') {
624 str += '<td colspan="2"><p class="fp-popup">'+M.str.repository.popup+'</p>';
625 str += '<p class="fp-popup"><button id="'+popup_button_id+'">'+M.str.repository.login+'</button>';
628 }else if(l[k].type=='textarea') {
630 str += '<td colspan="2"><p><textarea id="'+l[k].id+'" name="'+l[k].name+'"></textarea></p></td>';
631 }else if(l[k].type=='select') {
633 str += '<td align="right"><label>'+l[k].label+':</label></td>';
634 str += '<td align="left"><select id="'+l[k].id+'" name="'+l[k].name+'">';
635 for (i in l[k].options) {
636 str += '<option value="'+l[k].options[i].value+'">'+l[k].options[i].label+'</option>';
638 str += '</select></td>';
643 var field_value = '';
645 label_id = ' for="'+l[k].id+'"';
646 field_id = ' id="'+l[k].id+'"';
649 str += '<td align="right" width="30%" valign="center">';
650 str += '<label'+label_id+'>'+l[k].label+'</label> </td>';
652 str += '<td width="30%"></td>';
655 field_value = ' value="'+l[k].value+'"';
657 if(l[k].type=='radio'){
658 var list = l[k].value.split('|');
659 var labels = l[k].value_label.split('|');
660 str += '<td align="left">';
661 for(var item in list) {
662 str +='<input type="'+l[k].type+'"'+' name="'+l[k].name+'"'+
663 field_id+' value="'+list[item]+'" />'+labels[item]+'<br />';
667 str += '<td align="left">';
668 str += '<input type="'+l[k].type+'"'+' name="'+l[k].name+'"'+field_value+' '+field_id+' />';
678 var btn_label = data['login_btn_label']?data['login_btn_label']:M.str.repository.submit;
679 if (action != 'popup') {
680 str += '<p><input type="button" id="';
683 str += search_button_id;
686 str += download_button_id;
689 str += login_button_id;
692 str += '" value="'+btn_label+'" /></p>';
699 panel.set('innerHTML', str);
701 alert(e.toString()+M.str.quiz.xhtml);
704 // process login action
705 var login_button = Y.one('#'+login_button_id);
708 login_button.on('click', function(){
710 var data = this.logindata;
713 for (var k in data) {
714 if(data[k].type!='popup') {
715 var el = Y.one('[name='+data[k].name+']');
716 var type = el.get('type');
717 params[data[k].name] = '';
718 if(type == 'checkbox') {
719 params[data[k].name] = el.get('checked');
721 params[data[k].name] = el.get('value');
725 // start ajax request
731 'client_id': client_id,
732 'repository_id': repository_id,
733 'callback': function(id, o, args) {
734 scope.parse_repository_options(o);
743 var search_button = Y.one('#'+search_button_id);
745 search_button.on('click', function(){
746 var data = this.logindata;
749 for (var k in data) {
750 if(data[k].type!='popup') {
751 var el = document.getElementsByName(data[k].name)[0];
752 params[data[k].name] = '';
753 if(el.type == 'checkbox') {
754 params[data[k].name] = el.checked;
755 } else if(el.type == 'radio') {
756 var tmp = document.getElementsByName(data[k].name);
758 if (tmp[i].checked) {
759 params[data[k].name] = tmp[i].value;
763 params[data[k].name] = el.value;
770 client_id: client_id,
771 repository_id: repository_id,
772 form: {id: 'fp-form-'+scope.options.client_id,upload:false,useDisabled:true},
773 callback: function(id, o, args) {
774 o.issearchresult = true;
775 scope.parse_repository_options(o);
781 var download_button = Y.one('#'+download_button_id);
782 if (download_button) {
783 download_button.on('click', function(){
787 var popup_button = Y.one('#'+popup_button_id);
789 popup_button.on('click', function(e){
790 M.core_filepicker.active_filepicker = this;
791 window.open(loginurl, 'repo_auth', 'location=0,status=0,width=500,height=300');
795 var elform = Y.one('#'+form_id);
796 elform.on('keydown', function(e) {
797 if (e.keyCode == 13) {
800 search_button.simulate('click');
803 login_button.simulate('click');
811 search: function(args) {
812 var data = this.logindata;
815 for (var k in data) {
816 if(data[k].type!='popup') {
817 var el = document.getElementsByName(data[k].name)[0];
818 params[data[k].name] = '';
819 if(el.type == 'checkbox') {
820 params[data[k].name] = el.checked;
821 } else if(el.type == 'radio') {
822 var tmp = document.getElementsByName(data[k].name);
824 if (tmp[i].checked) {
825 params[data[k].name] = tmp[i].value;
829 params[data[k].name] = el.value;
836 client_id: client_id,
837 repository_id: repository_id,
838 form: {id: 'fp-form-'+scope.options.client_id,upload:false,useDisabled:true},
839 callback: function(id, o, args) {
840 o.issearchresult = true;
841 scope.parse_repository_options(o);
846 list: function(args) {
852 args.repo_id = scope.active_repo.id;
856 client_id: scope.options.client_id,
857 repository_id: args.repo_id,
858 path:args.path?args.path:'',
859 page:args.page?args.page:'',
860 callback: function(id, obj, args) {
862 scope.viewbar.set('disabled', true);
863 scope.print_login(obj);
864 } else if (obj.upload) {
865 scope.viewbar.set('disabled', true);
866 scope.parse_repository_options(obj);
867 scope.create_upload_form(obj);
869 } else if (obj.iframe) {
871 } else if (obj.list) {
872 obj.issearchresult = false;
873 scope.viewbar.set('disabled', false);
874 scope.parse_repository_options(obj);
878 // TODO: Print message
883 create_upload_form: function(data) {
884 var client_id = this.options.client_id;
885 Y.one('#panel-'+client_id).set('innerHTML', '');
888 var id = data.upload.id+'_'+client_id;
889 var str = '<div id="'+id+'_div" class="fp-upload-form">';
890 str += '<form id="'+id+'" method="POST">';
891 str += '<table border=1>';
893 str += '<label for="'+id+'_file">'+data.upload.label+': </label></td>';
894 str += '<td><input type="file" id="'+id+'_file" name="repo_upload_file" />';
895 str += '<input type="hidden" name="itemid" value="'+this.options.itemid+'" /></tr>';
897 str += '<td><label>'+M.str.repository.author+': </label></td>';
898 str += '<td><input type="text" name="author" value="'+this.options.author+'" /></td>';
901 str += '<td>'+M.str.repository.chooselicense+': </td>';
903 var licenses = this.options.licenses;
904 str += '<select name="license" id="select-license-'+client_id+'">';
905 for (var i in licenses) {
906 str += '<option value="'+licenses[i].shortname+'">'+licenses[i].fullname+'</option>';
910 str += '</tr></table>';
911 str += '<div class="fp-upload-btn"><a id="'+id+'_action" href="###" >'+M.str.repository.upload+'...</a></div>';
914 var upload_form = Y.Node.create(str);
915 Y.one('#panel-'+client_id).appendChild(upload_form);
917 Y.one('#'+id+'_action').on('click', function() {
918 Y.use('io-upload-iframe', function() {
922 client_id: client_id,
923 params: {'savepath':scope.options.savepath},
924 repository_id: scope.active_repo.id,
925 form: {id: id, upload:true},
926 callback: function(id, o, args) {
927 if (scope.options.editor_target&&scope.options.env=='editor') {
928 scope.options.editor_target.value=o.url;
929 scope.options.editor_target.onchange();
932 o.client_id = client_id;
933 var formcallback_scope = null;
934 if (args.scope.options.magicscope) {
935 formcallback_scope = args.scope.options.magicscope;
937 formcallback_scope = args.scope;
939 scope.options.formcallback.apply(formcallback_scope, [o]);
945 print_header: function() {
946 var r = this.active_repo;
948 var client_id = this.options.client_id;
949 var repository_id = this.active_repo.id;
950 var panel = Y.one('#panel-'+client_id);
951 var str = '<div id="fp-header-'+client_id+'">';
952 str += '<div class="fp-toolbar" id="repo-tb-'+client_id+'"></div>';
954 var head = Y.Node.create(str);
955 panel.appendChild(head);
956 //if(this.active_repo.pages < 8){
957 this.print_paging('header');
961 var toolbar = Y.one('#repo-tb-'+client_id);
964 var html = '<a href="###"><img src="'+M.util.image_url('a/search')+'" /> '+M.str.repository.search+'</a>';
965 var search = Y.Node.create(html);
966 search.on('click', function() {
970 repository_id: repository_id,
971 callback: function(id, obj, args) {
972 var scope = args.scope;
973 var client_id = scope.options.client_id;
974 var repository_id = scope.active_repo.id;
975 var container = document.getElementById('fp-search-dlg');
977 container.innerHTML = '';
978 container.parentNode.removeChild(container);
980 var container = document.createElement('DIV');
981 container.id = 'fp-search-dlg';
983 var dlg_title = document.createElement('DIV');
984 dlg_title.className = 'hd';
985 dlg_title.innerHTML = 'filepicker';
987 var dlg_body = document.createElement('DIV');
988 dlg_body.className = 'bd';
990 var sform = document.createElement('FORM');
991 sform.method = 'POST';
992 sform.id = "fp-search-form";
993 sform.innerHTML = obj.form;
995 dlg_body.appendChild(sform);
996 container.appendChild(dlg_title);
997 container.appendChild(dlg_body);
998 Y.one(document.body).appendChild(container);
999 var search_dialog= null;
1000 function dialog_handler() {
1001 scope.viewbar.set('disabled', false);
1005 client_id: client_id,
1006 repository_id: repository_id,
1007 form: {id: 'fp-search-form',upload:false,useDisabled:true},
1008 callback: function(id, o, args) {
1009 scope.parse_repository_options(o);
1013 search_dialog.cancel();
1016 search_dialog = new YAHOO.widget.Dialog("fp-search-dlg", {
1017 postmethod: 'async',
1023 constraintoviewport : true,
1026 text:M.str.repository.submit,
1027 handler:dialog_handler,
1030 text:M.str.moodle.cancel,
1036 search_dialog.render();
1037 search_dialog.show();
1041 toolbar.appendChild(search);
1043 // weather we use cache for this instance, this button will reload listing anyway
1045 var html = '<a href="###"><img src="'+M.util.image_url('a/refresh')+'" /> '+M.str.repository.refresh+'</a>';
1046 var refresh = Y.Node.create(html);
1047 refresh.on('click', function() {
1050 toolbar.appendChild(refresh);
1053 var html = '<a href="###"><img src="'+M.util.image_url('a/logout')+'" /> '+M.str.repository.logout+'</a>';
1054 var logout = Y.Node.create(html);
1055 logout.on('click', function() {
1058 client_id: client_id,
1059 repository_id: repository_id,
1061 callback: function(id, obj, args) {
1062 scope.viewbar.set('disabled', true);
1063 scope.print_login(obj);
1067 toolbar.appendChild(logout);
1071 var mgr = document.createElement('A');
1072 mgr.href = r.manage;
1073 mgr.target = "_blank";
1074 mgr.innerHTML = '<img src="'+M.util.image_url('a/setting')+'" /> '+M.str.repository.manageurl;
1075 toolbar.appendChild(mgr);
1078 var help = document.createElement('A');
1080 help.target = "_blank";
1081 help.innerHTML = '<img src="'+M.util.image_url('a/help')+'" /> '+M.str.repository.help;
1082 toolbar.appendChild(help);
1085 // only show in icons view
1086 if (this.viewmode == 1) {
1090 get_page_button: function(page) {
1091 var r = this.active_repo;
1093 if (page == r.page) {
1094 css = 'class="cur_page" ';
1096 var str = '<a '+css+'href="###" id="repo-page-'+page+'">';
1099 print_paging: function(html_id) {
1100 var client_id = this.options.client_id;
1102 var r = this.active_repo;
1106 str += '<div class="fp-paging" id="paging-'+html_id+'-'+client_id+'">';
1107 str += this.get_page_button(1)+'1</a> ';
1110 var ex = (span-1)/2;
1112 if (r.page+ex>=r.pages) {
1118 var max = r.page+ex;
1122 // won't display upper boundary
1123 if (r.page >= span) {
1125 for(var i=r.page-ex; i<max; i++) {
1126 str += this.get_page_button(i);
1131 // this very first elements
1132 for(var i = 2; i < max; i++) {
1133 str += this.get_page_button(i);
1139 // won't display upper boundary
1141 str += this.get_page_button(r.pages)+r.pages+'</a>';
1143 str += this.get_page_button(max)+max+'</a>';
1144 str += ' ... '+this.get_page_button(r.pages)+r.pages+'</a>';
1149 var a = Y.Node.create(str);
1150 Y.one('#fp-header-'+client_id).appendChild(a);
1152 Y.all('#fp-header-'+client_id+' .fp-paging a').each(
1153 function(node, id) {
1154 node.on('click', function(e) {
1155 var id = node.get('id');
1156 var re = new RegExp("repo-page-(\\d+)", "i");
1157 var result = id.match(re);
1159 args.page = result[1];
1160 if (scope.active_repo.issearchresult) {
1164 client_id: client_id,
1165 repository_id: r.id,
1166 params: {'page':result[1]},
1167 callback: function(id, o, args) {
1168 o.issearchresult = true;
1169 scope.parse_repository_options(o);
1181 print_path: function() {
1182 var client_id = this.options.client_id;
1183 if (this.viewmode == 2) {
1186 var panel = Y.one('#panel-'+client_id);
1187 var p = this.filepath;
1188 if (p && p.length!=0) {
1189 var path = Y.Node.create('<div id="path-'+client_id+'" class="fp-pathbar"></div>');
1190 panel.appendChild(path);
1191 for(var i = 0; i < p.length; i++) {
1192 var link_path = p[i].path;
1193 var link = document.createElement('A');
1195 link.innerHTML = p[i].name;
1196 link.id = 'path-node-'+client_id+'-'+i;
1197 var sep = Y.Node.create('<span>/</span>');
1198 path.appendChild(link);
1199 path.appendChild(sep);
1200 Y.one('#'+link.id).on('click', function(Y, path){
1201 this.list({'path':path});
1210 if (this.rendered) {
1211 var panel = Y.one('#panel-'+this.options.client_id);
1212 panel.set('innerHTML', '');
1218 launch: function() {
1223 M.core_filepicker.instances[options.client_id] = new FilePickerHelper(options);