-
Notifications
You must be signed in to change notification settings - Fork 13
/
jaofiletree.js
179 lines (166 loc) · 7.26 KB
/
jaofiletree.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
// jQuery File Tree Plugin
//
// Version 1.0
//
// Base on the work of Cory S.N. LaViska A Beautiful Site (http://abeautifulsite.net/)
// Dual-licensed under the GNU General Public License and the MIT License
// Icons from famfamfam silk icon set thanks to http://www.famfamfam.com/lab/icons/silk/
//
// Usage : $('#jao').jaofiletree(options);
//
// Author: Damien Barrère
// Website: http://www.crac-design.com
(function( $ ) {
var options = {
'root' : '/',
'script' : 'connectors/jaoconnector.php',
'showroot' : 'root',
'onclick' : function(elem,type,file){},
'oncheck' : function(elem,checked,type,file){},
'usecheckboxes' : true, //can be true files dirs or false
'expandSpeed' : 500,
'collapseSpeed' : 500,
'expandEasing' : null,
'collapseEasing' : null,
'canselect' : true
};
var methods = {
init : function( o ) {
if($(this).length==0){
return;
}
$this = $(this);
$.extend(options,o);
if(options.showroot!=''){
checkboxes = '';
if(options.usecheckboxes===true || options.usecheckboxes==='dirs'){
checkboxes = '<input type="checkbox" />';
}
$this.html('<ul class="jaofiletree"><li class="drive directory collapsed selected">'+checkboxes+'<a href="#" data-file="'+options.root+'" data-type="dir">'+options.showroot+'</a></li></ul>');
}
openfolder(options.root);
},
open : function(dir){
openfolder(dir);
},
close : function(dir){
closedir(dir);
},
getchecked : function(){
var list = new Array();
var ik = 0;
$this.find('input:checked + a').each(function(){
list[ik] = {
type : $(this).attr('data-type'),
file : $(this).attr('data-file')
}
ik++;
});
return list;
},
getselected : function(){
var list = new Array();
var ik = 0;
$this.find('li.selected > a').each(function(){
list[ik] = {
type : $(this).attr('data-type'),
file : $(this).attr('data-file')
}
ik++;
});
return list;
}
};
openfolder = function(dir) {
if($this.find('a[data-file="'+dir+'"]').parent().hasClass('expanded')){
return;
}
var ret;
ret = $.ajax({
url : options.script,
data : {dir : dir},
context : $this,
dataType: 'json',
beforeSend : function(){this.find('a[data-file="'+dir+'"]').parent().addClass('wait');}
}).done(function(datas) {
ret = '<ul class="jaofiletree" style="display: none">';
for(ij=0; ij<datas.length; ij++){
if(datas[ij].type=='dir'){
classe = 'directory collapsed';
}else{
classe = 'file ext_'+datas[ij].ext;
}
ret += '<li class="'+classe+'">'
if(options.usecheckboxes===true || (options.usecheckboxes==='dirs' && datas[ij].type=='dir') || (options.usecheckboxes==='files' && datas[ij].type=='file')){
ret += '<input type="checkbox" data-file="'+dir+datas[ij].file+'" data-type="'+datas[ij].type+'"/>';
}
else{
ret += '<input disabled="disabled" type="checkbox" data-file="'+dir+datas[ij].file+'" data-type="'+datas[ij].type+'"/>';
}
ret += '<a href="#" data-file="'+dir+datas[ij].file+'/" data-type="'+datas[ij].type+'">'+datas[ij].file+'</a>';
ret += '</li>';
}
ret += '</ul>';
this.find('a[data-file="'+dir+'"]').parent().removeClass('wait').removeClass('collapsed').addClass('expanded');
this.find('a[data-file="'+dir+'"]').after(ret);
this.find('a[data-file="'+dir+'"]').next().slideDown(options.expandSpeed,options.expandEasing);
if(options.usecheckboxes){
this.find('li input[type="checkbox"]').attr('checked',null);
this.find('a[data-file="'+dir+'"]').prev(':not(:disabled)').attr('checked','checked');
this.find('a[data-file="'+dir+'"] + ul li input[type="checkbox"]:not(:disabled)').attr('checked','checked');
}
setevents();
}).done(function(){
//Trigger custom event
$this.trigger('afteropen');
$this.trigger('afterupdate');
});
}
closedir = function(dir) {
$this.find('a[data-file="'+dir+'"]').next().slideUp(options.collapseSpeed,options.collapseEasing,function(){$(this).remove();});
$this.find('a[data-file="'+dir+'"]').parent().removeClass('expanded').addClass('collapsed');
setevents();
//Trigger custom event
$this.trigger('afterclose');
$this.trigger('afterupdate');
}
setevents = function(){
$this.find('li a').unbind('click');
//Bind userdefined function on click an element
$this.find('li a').bind('click', function() {
options.onclick(this, $(this).attr('data-type'),$(this).attr('data-file'));
if(options.usecheckboxes && $(this).attr('data-type')=='file'){
$this.find('li input[type="checkbox"]').attr('checked',null);
$(this).prev(':not(:disabled)').attr('checked','checked');
$(this).prev(':not(:disabled)').trigger('check');
}
if(options.canselect){
$this.find('li').removeClass('selected');
$(this).parent().addClass('selected');
}
return false;
});
//Bind checkbox check/uncheck
$this.find('li input[type="checkbox"]').bind('change', function() {
options.oncheck(this,$(this).is(':checked'), $(this).next().attr('data-type'),$(this).next().attr('data-file'));
if($(this).is(':checked')){
$this.trigger('check');
}else{
$this.trigger('uncheck');
}
});
//Bind for collapse or expand elements
$this.find('li.directory.collapsed a').bind('click', function() {methods.open($(this).attr('data-file'));return false;});
$this.find('li.directory.expanded a').bind('click', function() {methods.close($(this).attr('data-file'));return false;});
}
$.fn.jaofiletree = function( method ) {
// Method calling logic
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
//error
}
};
})( jQuery );