forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.src.js
1257 lines (1242 loc) · 49.7 KB
/
repository.src.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////////////////
// //
// Don't modify this file unless you know how it works //
// //
///////////////////////////////////////////////////////////////////////////
/**
* repository_client is a javascript class, it contains several static
* methods you can call it directly without creating an instance.
* If you are going to create a file picker, you need create an instance
* repo = new repository_client();
*/
var id2clientid = {};
var id2itemid = {};
var repository_listing = {};
var cached_client_id = {};
var file_extensions = {};
/* when selected a file, filename will be cached in this varible */
var new_filename = '';
// will be used by login form
var cached_id;
var cached_repo_id;
// repository_client has static functions
var repository_client = (function(){
// private static field
var version = '2.0';
var PANEL_BODY_PADDING = (10*2);
// private static methods
function help() {
alert(version);
}
// a hack to fix ie6 bug
function ie6_fix_width(id, width) {
if(YAHOO.env.ua.ie == 6){
var fp_title = document.getElementById('file-picker-'+id);
fp_title.style.width = width;
}
}
function $() {
// public methods of filepicker instance
this.create_filepicker = function(client_id) {
var IE_QUIRKS = (YAHOO.env.ua.ie && document.compatMode == "BackCompat");
var IE_SYNC = (YAHOO.env.ua.ie == 6 || (YAHOO.env.ua.ie == 7 && IE_QUIRKS));
var btn_listing = {label: fp_lang.listview, value: 'l',
onclick: {fn: repository_client.view_as_list, obj:client_id}};
var btn_icons = {label: fp_lang.thumbview, value: 't',
onclick: {fn: repository_client.view_as_icons, obj:client_id}};
var el = document.createElement('DIV');
el.id = 'file-picker-'+client_id;
el.className = 'file-picker';
this.client_id = client_id;
document.body.appendChild(el);
this.filepicker = new YAHOO.widget.Panel('file-picker-' + client_id, {
draggable: true,
close: true,
underlay: 'none',
zindex: 666666,
monitorresize: false,
xy: [50, YAHOO.util.Dom.getDocumentScrollTop()+20]
});
var layout = '';
this.filepicker.client_id = client_id;
this.filepicker.setHeader(fp_lang['title']);
this.filepicker.setBody('<div id="layout-'+client_id+'"></div>');
this.filepicker.beforeRenderEvent.subscribe(function() {
YAHOO.util.Event.onAvailable('layout-'+client_id, function() {
layout = new YAHOO.widget.Layout('layout-'+client_id, {
height: 480, width: 700,
units: [
{position: 'top', height: 32, resize: false,
body:'<div class="yui-buttongroup fp-viewbar" id="repo-viewbar-'+client_id+'"></div><div class="fp-searchbar" id="search-div-'+client_id+'"></div>', gutter: '2'},
{position: 'left', width: 200, resize: true, scroll:true,
body:'<ul class="fp-list" id="repo-list-'+client_id+'"></ul>', gutter: '0 5 0 2', minWidth: 150, maxWidth: 300 },
{position: 'center', body: '<div class="fp-panel" id="panel-'+client_id+'"></div>',
scroll: true, gutter: '0 2 0 0' }
]
});
layout.render();
});
});
if (!YAHOO.env.ua.ie) {
var resize = new YAHOO.util.Resize('file-picker-'+client_id, {
handles: ['br'],
autoRatio: true,
proxy: true,
status: true,
minWidth: 680,
minHeight: 400
});
resize.on('resize', function(args) {
var panelHeight = args.height;
var headerHeight = this.header.offsetHeight; // Content + Padding + Border
var bodyHeight = (panelHeight - headerHeight);
var bodyContentHeight = (IE_QUIRKS) ? bodyHeight : bodyHeight - PANEL_BODY_PADDING;
YAHOO.util.Dom.setStyle(this.body, 'height', bodyContentHeight + 'px');
ie6_fix_width(this.client_id, '680px');
if (IE_SYNC) {
this.sizeUnderlay();
this.syncIframe();
}
layout.set('height', bodyContentHeight);
layout.set('width', (args.width - PANEL_BODY_PADDING));
layout.resize();
}, this.filepicker, true);
}
repository_client.fp[client_id].viewbar = new YAHOO.widget.ButtonGroup({
id: 'btngroup-'+client_id,
name: 'buttons',
disabled: true,
container: 'repo-viewbar-'+client_id
});
repository_client.fp[client_id].viewbar.addButtons([btn_icons, btn_listing]);
this.print_listing();
this.filepicker.render();
}
this.init_search = function() {
var searchbar = document.getElementById('search-div-'+this.client_id);
searchbar.innerHTML = '<input id="search-input-'+this.client_id+'" /><button id="search-btn-'+this.client_id+'">'+fp_lang.federatedsearch+'</button>';
var search_btn = new YAHOO.util.Element('search-btn-'+this.client_id);
search_btn.client_id = this.client_id;
var input_keyword = new YAHOO.util.Element('search-input-'+this.client_id);
input_keyword.client_id = this.client_id;
search_btn.fnSearch = function(e) {
var el = new YAHOO.util.Element('search-input-'+this.client_id)
var keyword = el.get('value');
var r = repository_client.fp[this.client_id];
var params = [];
params['s'] = keyword;
params['env']=r.env;
params['accepted_types'] = r.accepted_types;
params['sesskey']=moodle_cfg.sesskey;
params['ctx_id']=fp_config.contextid;
params['client_id']=this.client_id;
repository_client.loading(this.client_id, 'load');
var trans = YAHOO.util.Connect.asyncRequest('POST',
moodle_cfg.wwwroot+'/repository/ws.php?action=gsearch', this.search_cb, repository_client.postdata(params));
}
search_btn.on('contentReady', function() {
search_btn.on('click', this.fnSearch, this.input_keyword);
});
search_btn.search_cb={
success: function(o) {
var data = repository_client.parse_json(o.responseText, 'global_search_cb');
var panel = new YAHOO.util.Element('panel-'+data.client_id);
if(!data.list || data.list.length<1){
panel.get('element').innerHTML = fp_lang.noresult;
return;
}
var r = repository_client.fp[data.client_id];
r.view_staus = 0;
r.fs = data.list;
if (data.gsearch) {
// will be used to hide header and footer
r.gsearch = true;
}
if(r.view_staus) {
repository_client.view_as_list(data.client_id, data.list);
} else {
repository_client.view_as_icons(data.client_id, data.list);
}
var el = new YAHOO.util.Element('search-input-'+data.client_id)
el.set('value', '');
}
}
input_keyword.on('contentReady', function() {
var scope = document.getElementById('search-input-'+this.client_id);
var k1 = new YAHOO.util.KeyListener(scope, {keys:13}, {fn:function(){this.fnSearch()},scope:search_btn, correctScope: true});
k1.enable();
});
}
this.print_listing = function() {
var container = new YAHOO.util.Element('repo-list-'+this.client_id);
container.set('innerHTML', '');
container.on('contentReady', function() {
this.init_search();
for(var i in repository_listing[this.client_id]) {
var repo = repository_listing[this.client_id][i];
var support = false;
if(this.env=='editor' && this.accepted_types != '*'){
if(repo.supported_types!='*'){
for (var j in repo.supported_types){
if(mdl_in_array(repo.supported_types[j], this.accepted_types)){
support = true;
}
}
}
}else{
support = true;
}
if(repo.supported_types == '*' || support){
var li = document.createElement('li');
li.id = 'repo-'+this.client_id+'-'+repo.id;
var icon = document.createElement('img');
icon.src = repo.icon;
icon.width = '16';
icon.height = '16';
var link = document.createElement('a');
link.href = '###';
link.id = 'repo-call-'+this.client_id+'-'+repo.id;
link.appendChild(icon);
link.className = 'fp-repo-name';
link.innerHTML += ' '+repo.name;
link.onclick = function() {
var re = new RegExp("repo-call-(\\w+)-(\\d+)", "i");
var result = this.id.match(re);
var client_id = result[1];
var repo_id = result[2];
// high light currect selected repository
for(var cc in repository_listing[client_id]){
var tmp_id = 'repo-call-'+client_id+'-'+ cc;
var el = document.getElementById(tmp_id);
if(el){
el.style.background = 'transparent';
}
}
this.style.background = '#CCC';
repository_client.req(client_id, repo_id, '');
}
li.appendChild(link);
container.appendChild(li);
repo = null;
}
}
}, this, true);
}
this.show = function(){
this.print_listing();
var panel = new YAHOO.util.Element('panel-'+this.filepicker.client_id);
panel.get('element').innerHTML = '';
this.filepicker.show();
}
this.hide = function(){
this.filepicker.hide();
}
}
// all filepicker instances
$.fp = {};
return $;
})();
// public static method
// may be called outside yui
repository_client.req = function(client_id, id, path, page) {
repository_client.loading(client_id, 'load');
this.fp[client_id].viewbar.set('disabled', false);
var r = repository_client.fp[client_id];
var params = [];
params['p'] = path;
params['env']=r.env;
params['sesskey']=moodle_cfg.sesskey;
params['ctx_id']=fp_config.contextid;
params['client_id'] = client_id;
params['repo_id']=id;
params['itemid'] = repository_client.fp[client_id].itemid;
if (!!page) { // convert page to boolean value
params['page']=page;
}
params['accepted_types'] = r.accepted_types;
var trans = YAHOO.util.Connect.asyncRequest('POST', moodle_cfg.wwwroot+'/repository/ws.php?action=list', this.req_cb, this.postdata(params));
}
repository_client.req_cb = {
success: function(o){
var data = repository_client.parse_json(o.responseText, 'req_cb');
var repo = repository_client.fp[data.client_id];
repo.viewbar.set('disabled', false);
var panel = new YAHOO.util.Element('panel-'+data.client_id);
if(data && data.e) {
panel.get('element').innerHTML = '<div class="fp-error">'+data.e+'</div>';
return;
}
// save data
repo.fs = data;
if(!data) {
return;
}else if(data.iframe) {
repository_client.view_iframe(data.client_id);
}else if(data.login) {
repo.viewbar.set('disabled', true);
repository_client.print_login(data.client_id, data);
}else if(data.list) {
if(repo.view_status) {
repository_client.view_as_list(data.client_id, data.list);
} else {
repository_client.view_as_icons(data.client_id, data.list);
}
}
if (data.msg) {
repository_client.print_msg(data.msg, data.client_id);
}
}
}
repository_client.print_msg = function(msg, client_id) {
var panel = new YAHOO.util.Element('panel-'+client_id);
panel.get('element').innerHTML = '<div class="fp-msg">'+msg+'</div>'+panel.get('element').innerHTML;
}
repository_client.view_iframe = function(client_id) {
var fs = repository_client.fp[client_id].fs;
var panel = new YAHOO.util.Element('panel-'+client_id);
panel.get('element').innerHTML = "<iframe frameborder=\"0\" width=\"98%\" height=\"400px\" src=\""+fs.iframe+"\" />";
}
repository_client.req_search_results = function(client_id, id, path, page) {
repository_client.loading(client_id, 'load');
this.fp[client_id].viewbar.set('disabled', false);
var r = repository_client.fp[client_id];
var params = [];
params['p'] = path;
params['env']=r.env;
params['sesskey']=moodle_cfg.sesskey;
params['ctx_id']=fp_config.contextid;
params['client_id'] = client_id;
params['search_paging']='true';
params['repo_id']=id;
if (!!page) { // convert page to boolean value
params['page']=page;
}
params['accepted_types'] = r.accepted_types;
var trans = YAHOO.util.Connect.asyncRequest('POST', moodle_cfg.wwwroot+'/repository/ws.php?action=search', this.req_cb, this.postdata(params));
}
repository_client.print_login = function(id, data) {
var login = data.login;
var panel = new YAHOO.util.Element('panel-'+id);
var action = 'login';
if (data['login_btn_action']) {
action=data['login_btn_action'];
}
var str = '<div class="fp-login-form" onkeypress="repository_client.login_keypress(event,\''+action+'\')">';
var has_pop = false;
this.fp[id].login = login;
str +='<table width="100%">';
for(var k in login) {
str +='<tr>';
if(login[k].type=='popup') {
str += '<td colspan="2"><p class="fp-popup">'+fp_lang.popup+'</p>';
str += '<p class="fp-popup"><button onclick="repository_client.popup(\''+id+'\', \''+login[k].url+'\')">'+fp_lang.login+'</button>';
str += '</p></td>';
has_pop = true;
}else if(login[k].type=='textarea') {
str += '<td colspan="2"><p><textarea id="'+login[k].id+'" name="'+login[k].name+'"></textarea></p></td>';
}else if(login[k].type=='select') {
str += '<td align="right"><label>'+login[k].label+':</label></td><td align="left"><select id="'+login[k].id+'" name="'+login[k].name+'">';
for (i in login[k].options) {
str += '<option value="'+login[k].options[i].value+'">'+login[k].options[i].label+'</option>';
}
str += '</select></td>';
}else{
var label_id = '';
var field_id = '';
var field_value = '';
if(login[k].id) {
label_id = ' for="'+login[k].id+'"';
field_id = ' id="'+login[k].id+'"';
}
if (login[k].label) {
str += '<td align="right" valign="top"><label'+label_id+'>'+login[k].label+'</label> </td>';
} else {
str += '<td></td>';
}
if(login[k].value) {
field_value = ' value="'+login[k].value+'"';
}
if(login[k].type=='radio'){
var list = login[k].value.split('|');
var labels = login[k].value_label.split('|');
str += '<td align="left">';
for(var item in list) {
str +='<input type="'+login[k].type+'"'+' name="'+login[k].name+'"'+
field_id+' value="'+list[item]+'" />'+labels[item]+'<br />';
}
str += '</td>';
}else{
str += '<td align="left"><input type="'+login[k].type+'"'+' name="'+login[k].name+'"'+field_value+' '+field_id+' /></td>';
}
}
str +='</tr>';
}
str +='</table>';
var btn_label = data['login_btn_label']?data['login_btn_label']:fp_lang.submit;
if (data['login_btn_action'] == 'search') {
str += '<p><input type="button" onclick="repository_client.search(\''+id+'\', \''+data.repo_id+'\')" value="'+btn_label+'" /></p>';
} else if(data['login_btn_action'] == 'download') {
str += '<p><input type="button" onclick="repository_client.download(\''+id+'\', \''+data.repo_id+'\')" value="'+btn_label+'" /></p>';
} else {
if(!has_pop) {
str += '<p><input type="button" onclick="repository_client.login(\''+id+'\', \''+data.repo_id+'\')" value="'+btn_label+'" /></p>';
}
}
str += '</div>';
try {
panel.get('element').innerHTML = str;
} catch(e) {
alert(e.toString()+fp_lang.xhtml);
}
cached_id=id;
cached_repo_id=data.repo_id;
}
repository_client.login = function(id, repo_id) {
var params = [];
var data = this.fp[id].login;
for (var k in data) {
if(data[k].type!='popup') {
var el = document.getElementsByName(data[k].name)[0];
params[data[k].name] = '';
if(el.type == 'checkbox') {
params[data[k].name] = el.checked;
} else {
params[data[k].name] = el.value;
}
}
}
params['env'] = this.fp[id].env;
params['repo_id'] = repo_id;
params['client_id'] = id;
params['ctx_id'] = fp_config.contextid;
params['sesskey'] = moodle_cfg.sesskey;
params['accepted_types'] = this.fp[id].accepted_types;
this.loading(id, 'load');
var trans = YAHOO.util.Connect.asyncRequest('POST',
moodle_cfg.wwwroot+'/repository/ws.php?action=sign', this.req_cb, this.postdata(params));
}
repository_client.login_keypress = function(evt,action) {
evt = (evt) ? evt : ((window.event) ? window.event : "")
var key = evt.keyCode?evt.keyCode:evt.which;
if(key == 13 || key == 10){
if(action=='search'){
repository_client.search(cached_id, cached_repo_id);
} else if (action=='download') {
repository_client.download(cached_id, cached_repo_id);
} else {
repository_client.login(cached_id, cached_repo_id);
}
}
}
repository_client.search = function(id, repo_id) {
var params = [];
var data = this.fp[id].login;
for (var k in data) {
if(data[k].type!='popup') {
var el = document.getElementsByName(data[k].name)[0];
params[data[k].name] = '';
if(el.type == 'checkbox') {
params[data[k].name] = el.checked;
} else if(el.type == 'radio') {
var tmp = document.getElementsByName(data[k].name);
for(var i in tmp) {
if (tmp[i].checked) {
params[data[k].name] = tmp[i].value;
}
}
} else {
params[data[k].name] = el.value;
}
}
}
params['env'] = this.fp[id].env;
params['repo_id'] = repo_id;
params['client_id'] = id;
params['ctx_id'] = fp_config.contextid;
params['sesskey'] = moodle_cfg.sesskey;
params['accepted_types'] = this.fp[id].accepted_types;
this.loading(id, 'load');
var trans = YAHOO.util.Connect.asyncRequest('POST',
moodle_cfg.wwwroot+'/repository/ws.php?action=search', this.req_cb, this.postdata(params));
}
repository_client.loading = function(id, type, name) {
var panel = new YAHOO.util.Element('panel-'+id);
panel.get('element').innerHTML = '';
var str = '<div style="text-align:center">';
if(type=='load') {
str += '<img src="'+fp_config.icons.loading+'" />';
str += '<p>'+fp_lang.loading+'</p>';
}else{
str += '<img src="'+fp_config.icons.progressbar+'" />';
str += '<p>'+fp_lang.copying+' <strong>'+name+'</strong></p>';
}
str += '</div>';
try {
panel.get('element').innerHTML = str;
} catch(e) {
alert(e.toString());
}
}
repository_client.view_as_list = function(client_id, data) {
if (typeof client_id == 'object') {
// click button
client_id = data;
list = repository_client.fp[client_id].fs.list;
} else if(!data) {
// from viewfiles
list = repository_client.fp[client_id].fs.list;
}else{
// from callback
list = data;
}
var panel = new YAHOO.util.Element('panel-'+client_id);
var fp = repository_client.fp[client_id];
fp.view_status = 1;
fp.viewbar.check(1);
repository_client.print_header(client_id);
panel.get('element').innerHTML += '<div id="treediv-'+client_id+'"></div>';
var tree = new YAHOO.widget.TreeView('treediv-'+client_id);
tree.dynload = function (node, fnLoadComplete) {
var callback = {
success: function(o) {
var json = repository_client.parse_json(o.responseText, 'dynload');
for(k in json.list) {
repository_client.buildtree(json.client_id, json.list[k], node);
}
o.argument.fnLoadComplete();
},
failure:function(oResponse) {
alert(fp_lang.error+' - |dynload| -');
oResponse.argument.fnLoadComplete();
},
argument:{"node":node, "fnLoadComplete": fnLoadComplete}
}
var fp = repository_client.fp[node.client_id];
var params = [];
params['p']=node.path;
params['env']=fp.env;
params['sesskey']=moodle_cfg.sesskey;
params['ctx_id']=fp_config.contextid;
params['repo_id']=fp.fs.repo_id;
params['client_id']=node.client_id;
params['accepted_types']=fp.accepted_types;
var trans = YAHOO.util.Connect.asyncRequest('POST',
moodle_cfg.wwwroot+'/repository/ws.php?action=list',callback,repository_client.postdata(params));
}
tree.dynload.client_id = client_id;
if(fp.fs.dynload) {
tree.setDynamicLoad(tree.dynload, 1);
} else {
}
for(k in list) {
repository_client.buildtree(client_id, list[k], tree.getRoot());
}
tree.draw();
tree.subscribe('clickEvent', function(e){
if(e.node.isLeaf){
repository_client.select_file(e.node.data.filename, e.node.data.value, e.node.data.icon, client_id, e.node.repo_id);
}
});
repository_client.print_footer(client_id);
}
repository_client.buildtree = function(client_id, node, level) {
var fs = repository_client.fp[client_id].fs;
if(node.children) {
node.title = '<i><u>'+node.title+'</u></i>';
}
var info = {
label:node.title,
title:fp_lang.date+' '+node.date+fp_lang.size+' '+node.size,
filename:node.title,
value:node.source,
icon:node.thumbnail,
path:node.path
};
var tmpNode = new YAHOO.widget.TextNode(info, level, false);
var tooltip = new YAHOO.widget.Tooltip(tmpNode.labelElId, {
context:tmpNode.labelElId, text:info.title});
if(node.repo_id) {
tmpNode.repo_id=node.repo_id;
}else{
tmpNode.repo_id=fs.repo_id;
}
if(node.children) {
if(node.expanded) {
tmpNode.expand();
}
tmpNode.isLeaf = false;
tmpNode.client_id = client_id;
if (node.path) {
tmpNode.path = node.path;
} else {
tmpNode.path = '';
}
for(var c in node.children) {
this.buildtree(client_id, node.children[c], tmpNode);
}
} else {
tmpNode.isLeaf = true;
}
}
repository_client.select_file = function(oldname, url, icon, client_id, repo_id) {
if (repository_client.files[client_id] == undefined) {
repository_client.files[client_id] = 0;
}
if (repository_client.files[client_id] >= repository_client.fp[client_id].maxfiles &&
repository_client.fp[client_id].maxfiles != -1)
{
alert('Only '+repository_client.fp[client_id].maxfiles+' files are allowed!');
return false;
}
var thumbnail = document.getElementById('fp-grid-panel-'+client_id);
if(thumbnail){
thumbnail.style.display = 'none';
}
var header = document.getElementById('fp-header-'+client_id);
header.style.display = 'none';
var footer = document.getElementById('fp-footer-'+client_id);
footer.style.display = 'none';
var pathbar = document.getElementById('path-'+client_id);
if(pathbar){
pathbar.style.display = 'none';
}
var panel = new YAHOO.util.Element('panel-'+client_id);
var html = '<div class="fp-rename-form">';
html += '<p><img src="'+icon+'" /></p>';
html += '<p><label for="newname-'+client_id+'">'+fp_lang.saveas+'</label>';
html += '<input type="text" id="newname-'+client_id+'" value="" /></p>';
html += '<p><input type="hidden" id="fileurl-'+client_id+'" value="'+url+'" />';
html += '<input type="button" onclick="repository_client.download(\''+client_id+'\', \''+repo_id+'\')" value="'+fp_lang.downbtn+'" />';
html += '<input type="button" onclick="repository_client.viewfiles(\''+client_id+'\')" value="'+fp_lang.cancel+'" /></p>';
html += '</div>';
panel.get('element').innerHTML += html;
/* to deal with double quote, single quote, we need to use javascript change value */
document.getElementById('newname-'+client_id).value = oldname;
var tree = document.getElementById('treediv-'+client_id);
if(tree){
tree.style.display = 'none';
}
}
repository_client.paging = function(client_id, id) {
var str = '';
this.fp[client_id].view_staus = 0;
var fs = this.fp[client_id].fs;
if(fs.pages) {
str += '<div class="fp-paging" id="paging-'+id+'-'+client_id+'">';
if(!fs.search_result){
var action = 'req';
} else {
var action = 'req_search_results';
}
// print the first element anyway
str += this.get_page_btn(client_id, action, 1)+'1</a> ';
var span = 5;
var ex = (span-1)/2;
if (fs.page+ex>=fs.pages) {
var max = fs.pages;
} else {
if (fs.page<span) {
var max = span;
} else {
var max = fs.page+ex;
}
}
// won't display upper boundary
if (fs.page >= span) {
str += ' ... ';
for(var i=fs.page-ex; i<max; i++) {
str += this.get_page_btn(client_id, action, i);
str += String(i);
str += '</a> ';
}
} else {
// this very first elements
for(var i = 2; i < max; i++) {
str += this.get_page_btn(client_id, action, i);
str += String(i);
str += '</a> ';
}
}
// won't display upper boundary
if (max==fs.pages) {
str += this.get_page_btn(client_id, action, fs.pages)+fs.pages+'</a>';
} else {
str += repository_client.get_page_btn(client_id, action, max)+max+'</a>';
str += ' ... '+repository_client.get_page_btn(client_id, action, fs.pages)+fs.pages+'</a>';
}
str += '</div>';
}
return str;
}
repository_client.get_page_btn = function(client_id, type, page) {
var fs = this.fp[client_id].fs;
var css = '';
if (page == fs.page) {
css = 'class="cur_page"';
}
str = '<a '+css+' onclick="repository_client.'+type+'(\''+client_id+'\','+fs.repo_id+', '+page+', '+page+')" href="###">';
return str;
}
repository_client.path = function(client_id) {
var fs = this.fp[client_id].fs;
// if this is listing view
if(this.fp[client_id].view_status == 1) {
return;
}
var panel = new YAHOO.util.Element('panel-'+client_id);
var p = fs.path;
if(p && p.length!=0) {
var oDiv = document.createElement('DIV');
oDiv.id = "path-"+client_id;
oDiv.className = "fp-pathbar";
panel.get('element').appendChild(oDiv);
for(var i = 0; i < fs.path.length; i++) {
var link = document.createElement('A');
link.href = "###";
link.innerHTML = fs.path[i].name;
link.id = 'path-'+client_id+'-'+fs.repo_id;
link.path=fs.path[i].path;
link.onclick = function() {
var re = new RegExp("path-(\\w+)-(\\d+)", "i");
var result = this.id.match(re);
var client_id = result[1];
var repo_id = result[2];
repository_client.req(client_id, repo_id, this.path);
}
var sep = document.createElement('SPAN');
sep.innerHTML = '/';
oDiv.appendChild(link);
oDiv.appendChild(sep);
}
}
}
repository_client.print_header = function(client_id) {
var panel = new YAHOO.util.Element('panel-'+client_id);
var str = '<div id="fp-header-'+client_id+'">';
str += '<div class="fp-toolbar" id="repo-tb-'+client_id+'"></div>';
if(this.fp[client_id].fs.pages < 8){
str += this.paging(client_id, 'header');
}
str += '</div>';
panel.set('innerHTML', str);
this.path(client_id);
}
repository_client.view_as_icons = function(client_id, data) {
var list = null;
if (typeof client_id == 'object') {
// click button
client_id = data;
list = repository_client.fp[client_id].fs.list;
} else if(!data) {
// from viewfiles
list = repository_client.fp[client_id].fs.list;
}else{
// from callback
list = data;
}
var fp = repository_client.fp[client_id];
fp.view_status = 0;
fp.viewbar.check(0);
var container = document.getElementById('panel-'+client_id);
var panel = document.createElement('DIV');
panel.id = 'fp-grid-panel-'+client_id;
repository_client.print_header(client_id);
var count = 0;
for(k in list) {
// the container
var el = document.createElement('div');
el.className='fp-grid';
// the file name
var title = document.createElement('div');
title.id = 'grid-title-'+String(count);
title.className = 'label';
if (list[k].shorttitle) {
list[k].title = list[k].shorttitle;
}
title.innerHTML += '<a href="###"><span>'+list[k].title+"</span></a>";
if(list[k].thumbnail_width){
el.style.width = list[k].thumbnail_width+'px';
title.style.width = (list[k].thumbnail_width-10)+'px';
} else {
el.style.width = title.style.width = '80px';
}
var frame = document.createElement('DIV');
frame.style.textAlign='center';
if(list[k].thumbnail_height){
frame.style.height = list[k].thumbnail_height+'px';
}
var img = document.createElement('img');
img.src = list[k].thumbnail;
if(list[k].thumbnail_alt) {
img.alt = list[k].thumbnail_alt;
}
if(list[k].thumbnail_title) {
img.title = list[k].thumbnail_title;
}
var link = document.createElement('A');
link.href='###';
link.id = 'img-id-'+String(count);
if(list[k].url) {
el.innerHTML += '<p><a target="_blank" href="'+list[k].url+'">'+fp_lang.preview+'</a></p>';
}
link.appendChild(img);
frame.appendChild(link);
el.appendChild(frame);
if (fp.fs.draftfiles && !list[k].children) {
var delbtn = document.createElement('A');
delbtn.href = '###';
delbtn.innerHTML = "[X]";
delbtn.id = 'del-id-'+String(count);
el.appendChild(delbtn);
delbtn.itemid=fp.itemid;
delbtn.client_id=client_id;
delbtn.title=list[k].title;
delbtn.repo_id=fp.fs.repo_id;
delbtn.onclick = function() {
if (confirm('Sure?')) {
var params = [];
params['client_id'] = this.client_id;
params['itemid'] = this.itemid;
params['title'] = this.title;
var trans = YAHOO.util.Connect.asyncRequest('POST',
moodle_cfg.wwwroot+'/repository/ws.php?action=delete',
this,
repository_client.postdata(params)
);
}
}
delbtn.success = function(o) {
repository_client.req(o.responseText, this.repo_id);
}
}
el.appendChild(title);
panel.appendChild(el);
if(list[k].children) {
var folder = new YAHOO.util.Element(link.id);
folder.path = list[k].path;
var el_title = new YAHOO.util.Element(title.id);
folder.fs = list[k].children;
folder.on('contentReady', function() {
this.on('click', function() {
if(fp.fs.dynload) {
var fs = repository_client.fp[client_id].fs;
var params = [];
params['p'] = this.path;
params['env'] = repository_client.fp[client_id].env;
params['repo_id'] = fs.repo_id;
params['ctx_id'] = fp_config.contextid;
params['sesskey']= moodle_cfg.sesskey;
params['accepted_types'] = repository_client.fp[client_id].accepted_types;
params['client_id'] = client_id;
repository_client.loading(client_id, 'load');
var trans = YAHOO.util.Connect.asyncRequest('POST',
moodle_cfg.wwwroot+'/repository/ws.php?action=list', repository_client.req_cb, repository_client.postdata(params));
}else{
repository_client.view_as_icons(client_id, this.fs);
}
});
});
el_title.on('contentReady', function() {
this.on('click', function(){
folder.fireEvent('click');
});
});
} else {
var el_title = new YAHOO.util.Element(title.id);
var file = new YAHOO.util.Element(link.id);
el_title.filename = file.filename = list[k].title;
el_title.value = file.value = list[k].source;
el_title.icon = file.icon = list[k].thumbnail;
if(fp.fs.repo_id) {
el_title.repo_id = file.repo_id = fp.fs.repo_id;
}else if(list[k].repo_id) {
el_title.repo_id = file.repo_id = list[k].repo_id;
}else{
el_title.repo_id = file.repo_id = '';
}
file.on('contentReady', function() {
this.on('click', function() {
repository_client.select_file(this.filename, this.value, this.icon, client_id, this.repo_id);
});
});
el_title.on('contentReady', function() {
this.on('click', function() {
repository_client.select_file(this.filename, this.value, this.icon, client_id, this.repo_id);
});
});
}
count++;
}
if (list.length == 0 && !fp.fs.upload) {
panel.innerHTML = '<div class="fp-error">'+fp_lang.emptylist+'</div>';
}
container.appendChild(panel);
repository_client.print_footer(client_id);
}
repository_client.print_footer = function(client_id) {
var fs = this.fp[client_id].fs;
var panel = document.getElementById('panel-'+client_id);
var footer = document.createElement('DIV');
footer.id = 'fp-footer-'+client_id;
footer.innerHTML += this.create_upload_form(client_id);
footer.innerHTML += this.paging(client_id, 'footer');
panel.appendChild(footer);
// add repository manage buttons here
var oDiv = document.getElementById('repo-tb-'+client_id);
if(this.fp[client_id].gsearch) {
// reset
this.fp[client_id].gsearch='';
return;
}
if(!fs.nosearch) {
var img = document.createElement('img');
img.src = fp_config.icons.search;
var search = document.createElement('A');
search.href = '###';
search.appendChild(document.createTextNode(fp_lang.search));
oDiv.appendChild(img);
oDiv.appendChild(search);
search.onclick = function() {
repository_client.search_form(client_id, fs.repo_id);
}
}
// weather we use cache for this instance, this button will reload listing anyway
if(!fs.norefresh) {
var img = document.createElement('img');
img.src = fp_config.icons.refresh;
var ccache = document.createElement('A');
ccache.href = "###";
ccache.appendChild(document.createTextNode(fp_lang.refresh));
oDiv.appendChild(img);
oDiv.appendChild(ccache);
ccache.onclick = function() {
var params = [];
params['env']=fs.env;
params['sesskey']=moodle_cfg.sesskey;
params['ctx_id']=fp_config.contextid;
params['repo_id']=fs.repo_id;
params['client_id']=client_id;
repository_client.loading(client_id, 'load');
var trans = YAHOO.util.Connect.asyncRequest('POST',
moodle_cfg.wwwroot+'/repository/ws.php?action=ccache', repository_client.req_cb, repository_client.postdata(params));
}
}
if(fs.manage) {
var mgr = document.createElement('A');
mgr.innerHTML = '<img src="'+fp_config.icons.setting+'" /> '+fp_lang.mgr;
mgr.href = fs.manage;
mgr.target = "_blank";
oDiv.appendChild(mgr);
}
if(!fs.nologin) {
var logout = document.createElement('A');
logout.href = '###';
logout.innerHTML = '<img src="'+fp_config.icons.logout+'" /> '+fp_lang.logout;
oDiv.appendChild(logout);
logout.onclick = function() {
repository_client.logout(client_id, fs.repo_id);
}
}
if(fs.help) {
var help = document.createElement('A');
help.href = fs.help;
help.target = "_blank";
help.innerHTML = '<img src="'+fp_config.icons.help+'" /> '+fp_lang['help'];
oDiv.appendChild(help);
}
}
repository_client.postdata = function(obj) {
var str = '';
for(k in obj) {
if(obj[k] instanceof Array) {
for(i in obj[k]) {
str += (encodeURIComponent(k) +'[]='+encodeURIComponent(obj[k][i]));
str += '&';
}
} else {
str += encodeURIComponent(k) +'='+encodeURIComponent(obj[k]);
str += '&';
}
}
return str;
}
repository_client.popup = function(client_id, url) {
window.open(url,'repo_auth', 'location=0,status=0,scrollbars=0,width=500,height=300');
cached_client_id = client_id;
return true;
}
function repository_callback(id) {
repository_client.req(cached_client_id, id, '');
}
repository_client.logout = function(client_id, repo_id) {
var params = [];
params['repo_id'] = repo_id;
params['client_id'] = client_id;
var trans = YAHOO.util.Connect.asyncRequest('POST', moodle_cfg.wwwroot+'/repository/ws.php?action=logout',
repository_client.req_cb, repository_client.postdata(params));
}
repository_client.download = function(client_id, repo_id) {
var fp = repository_client.fp[client_id];
var title = document.getElementById('newname-'+client_id).value;
new_filename = title;
var file = document.getElementById('fileurl-'+client_id).value;
repository_client.loading(client_id, 'download', title);
var params = [];
if(fp.itemid){
params['itemid']=fp.itemid;
}
params['env']=fp.env;
params['file']=file;