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