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