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