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