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