forked from r043v/jTree
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjtree.js
More file actions
295 lines (255 loc) · 11.2 KB
/
jtree.js
File metadata and controls
295 lines (255 loc) · 11.2 KB
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
(function($)
{ function openfolder(t,ul,opt)
{ t.attr("data-state","open"); ul.slideDown(opt.openSpeed);
if(opt.multiFolder == "siblings")
{ var sbul = t.parent().parent().find("ul");
var allpul = t.parents("span+ul");
var toclose = allpul.parent().parent().find("ul:visible").not(allpul).not(sbul);
sbulch = sbul.not(ul).children().children("ul:visible");
toclose = toclose.add(sbulch).add(sbulch.find("ul:visible"));
toclose.slideUp(opt.closeSpeed).prev().removeAttr("data-state");
} else if(!opt.multiFolder) t.parent().parent().children("li").children("ul:visible").not(ul).slideUp(opt.closeSpeed).prev().removeAttr("data-state");
}
function closeFolder(t,ul,opt)
{ if(opt.multiFolder == "siblings")
{ var toclose = ul.add( ul.children().children("ul").find("ul:visible") );
toclose.slideUp(opt.closeSpeed).prev().removeAttr("data-state");
} else { ul.slideUp(opt.closeSpeed); t.removeAttr("data-state"); }
}
function setIcons(set,opt)
{ set.each(function()
{ var t = $(this);
var ul = t.next();
if(!ul.length)
{ var ext = t.html().split(".").pop();
var icon = opt.icon;
var type = "none";
for(l in opt.icons){ l=opt.icons[l]; if( $.inArray(ext, l.ext) > -1 ){ type=l.type; icon=l.icon; break; } }
t.data('ext',ext).data('type',type).css("background-image","url("+opt.iconFolder+icon+")");
}
});
}
$.expr[':'].jtreeFiles = function(t){return ($(t).next("ul").length == 0);}
$.expr[':'].jtreeFolder = function(t){return ($(t).next("ul").length == 1);}
$.expr[':'].jtreeOpen = function(t){ return $(t).attr("data-state") == "open";}
$.expr[':'].jtreeClose = function(t){ return $(t).attr("data-state") != "open";}
$.expr[':'].jtreeIs = function(t,index,meta){return ($.inArray($(t).data("ext"),meta[3].split(','))>-1);}
$.expr[':'].jtreeOf = function(t,index,meta){return ($.inArray($(t).data("type"),meta[3].split(','))>-1);}
$.expr[':'].jtreeNot = function(t,index,meta){return ($.inArray($(t).data("ext"),meta[3].split(','))<=-1);}
$.expr[':'].jtreeNotOf = function(t,index,meta){return ($.inArray($(t).data("type"),meta[3].split(','))<=-1);}
$.expr[':'].jtreeEmpty = function(t){ return $(t).next("ul:empty").length; }
$.expr[':'].jtreeFill = function(t){ return $(t).next("ul:parent").length; }
$.expr[':'].jtreeNameHas = function(t,index,meta){return ($(t).filter(':contains('+meta[3]+')').length == 1)}
$.expr[':'].jtreeNotNameHas = function(t,index,meta){return ($(t).filter(':contains('+meta[3]+')').length == 0)}
$.expr[':'].jtreeNameIs = function(t,index,meta){return ($(t).text() == meta[3]);}
$.expr[':'].jtreeNotNameIs = function(t,index,meta){return ($(t).text() != meta[3]);}
$.expr[':'].jtreeMNameIs = function(t,index,meta){return ($.inArray($(t).text(),meta[3].split(','))>-1);}
$.expr[':'].jtreeMNotNameIs = function(t,index,meta){return ($.inArray($(t).text(),meta[3].split(','))<=-1);}
$.fn.jtree = function(options) // create the tree
{ var opt = $.extend({}, $.fn.jtree.dopt, options);
return this.each(function()
{ var span = $(this).find("span:not([data-style=unclosable])");
if(opt.autoToggle)
{ span.click(function()
{ var t = $(this);
var ul = t.next();
if(ul.length == 1)
{ if(t.attr('data-state') != 'open') openfolder(t,ul,opt); else closeFolder(t,ul,opt);
}
});
}
});
};
$.fn.jtreeFolders = function(filter) // get all folders from current folder
{ var t = $(this); if(t.is("span")) t = t.next();
if(typeof(filter) == "string") return t.find('span+ul').prev(filter); return t.find('span+ul').prev();
};
$.fn.jtreeFiles = function(filter) // get all files from current folder
{ var t = $(this); if(t.is("span")) t = t.next();
var span = t.find('span'); span = span.not(span.next().prev());
if(typeof(filter) == "string") return span.filter(filter); return span;
};
$.fn.jtreeAll = function(filter) // get all entry, files and folders, from current folder
{ if(typeof(filter) == "string") return $(this).find("span"+filter); return $(this).find("span");
}
$.fn.jtreeChildFolders = function(filter) // get all folders inside current folder
{ var t = $(this); if(t.is("span")) t = t.next();
var f = t.children('li').children('span+ul'); if(typeof(filter) == "string") return f.prev(filter); return f.prev();
};
$.fn.jtreeChildFiles = function(filter) // get all files inside current folder
{ var t = $(this); if(t.is("span")) t = t.next(); t = t.children('li');
var span = t.children('span'); span = span.not(span.next('ul').prev());
if(typeof(filter) == "string") return span.filter(filter); return span;
};
$.fn.jtreeChildAll = function(filter) // get all files and folders inside current folder
{ var t = $(this); if(t.is("span")) t = t.next();
if(typeof(filter) == "string") return t.children('li>span'+filter); return t.children('li>span');
}
$.fn.jtreeOpened = function() // is current folder open ?
{ return ($(this).attr('data-state') == 'open');
}
$.fn.jtreeClose = function(options) // close current folder
{ var t = $(this); if(t.attr('data-style') == 'unclosable') return this;
var opt = $.extend({}, $.fn.jtree.dopt, options);
closeFolder(t,t.next(),opt);
return this;
}
$.fn.jtreeOpen = function(options) // open current folder
{ var opt = $.extend({}, $.fn.jtree.dopt, options);
var t = $(this); var ul = t.next(); if(ul.length != 1) return this;
openfolder(t,ul,opt); return this;
}
$.fn.jtreeToggle = function(options) // toggle current folder
{ var opt = $.extend({}, $.fn.jtree.dopt, options);
var t = $(this);
var ul = t.next();
if(ul.length != 1) return this;
if(t.attr('data-state') != 'open') openfolder(t,ul,opt);
else { if(t.is('[data-style=unclosable]')) return this; closeFolder(t,ul,opt); }
return this;
}
$.fn.jtreeRemove = function(options)
{ var opt = $.extend({}, $.fn.jtree.dopt, options);
var li = $(this).parent(); var ul = li.parent().prev();
var hide = li.filter(":hidden"); li = li.not(hide); hide.remove();
li.addClass('todel').slideUp(opt.removeTime,function(){ $(this).remove(); }); return ul;
}
$.fn.jtreeRemoveAllEmpty = function(options)
{ var t = $(this); var found=0;
t.find("li:not(.todel)>ul:empty").parent().addClass('todel');
do{ found=0; ul = $.unique( t.find('li:not(.todel) > ul > li.todel').parent('ul') );
ul.each(function(){ $t=$(this); if($t.children('li:not(.todel)').length == 0){ $.unique($t.parent('li')).addClass('todel'); found=1; }});
} while(found);
t.find('li.todel').children('span').jtreeRemove(options);
}
$.fn.jtreeCreateFile = function(name,options)
{ return $(this).jtreeInsert('file',name,options);
}
$.fn.jtreeCreateFolder = function(name,options)
{ return $(this).jtreeInsert('folder',name,options);
}
$.fn.jtreeMoveTo = function(folder,options,callback)
{ var opt = $.extend({}, $.fn.jtree.dopt, options);
var t = $(this);
if(!t.length || !folder.length || t.is(folder)) return false;
var ul = t.next();
var li = t.parent();
var ftogo = folder.next(); if(!ftogo.length) ftogo=folder.parent().parent();
if(ul.length) // folder
{ if(ul.parent().parent().is(ftogo) || ul.find(folder).length) return false;
var type = "folder";
} else { // file
if(li.parent().is(ftogo)) return false;
var type = "file";
}
if($.isFunction(callback))
{ var fname = t.text();
var oldpath = t.jtreeGetPath();
var newpath = ftogo.prev().jtreeGetPath();
if(newpath != '') newpath += '/'; newpath += fname;
if(false === callback(oldpath,newpath)) return false;
}
li.slideUp(opt.removeTime,function(){ li.detach(); folder.jtreeInsert(type,li,options); });
return ftogo.prev();//li.children("span");
}
$.fn.jtreeInsert = function(type,name,options)
{ var opt = $.extend({}, $.fn.jtree.dopt, options);
var t = $(this); var ul = t.next();
if(!ul.length){ ul = t.parent().parent(); t = ul.prev(); }
var li = ul.children("li"); var out = false;
var list = li.children("span+ul").prev();
if(type != "folder")
{ list = li.children("span").not(list);
var endhtml = '</span></li>';
} else var endhtml = '</span><ul></ul></li>';
if(typeof(name) == "string")
{ var d = $('<li><span>'+name+endhtml).jtree(opt);
var filename = name;
} else { var d=name; var filename = d.children("span").text(); }
d.hide();
if(list.length)
{ list.each(function()
{ var $t = $(this); if($t.text() > filename){ out = d.insertBefore($t.parent()); return false; }
}); if(out === false) out = d.insertAfter(list.last().parent());
} else { if(type != "folder") out = d.appendTo(ul); else out = d.prependTo(ul); }
return out.slideDown(opt.appendTime).children("span");
}
$.fn.jtreeParent = function() // get parent folder
{ var t = $(this); if(t.is("ul.jtree>li>span")) return this;
return t.parent().parent().prev();
}
$.fn.jtreeParentList = function() // get parent ul
{ return $(this).parent().parent();
}
$.fn.jtreeGetList = function() // get child ul
{ return $(this).next();
}
$.fn.jtreeSelect = function(path)
{ var t = $(this);
//t.css("color","blue");
if(t.is("ul.jtree"))
{ var root = t.children();
} else var root = t.parentsUntil('ul.jtree').last();
root = root.children("span").next();
var p = path.split('/'); p = $.grep(p,function(n){ return(n); });
var nb = p.length;
var start = 0; if(p[0] == "root") start=1;
for(var n=start;n<nb-1;n++)
{ var child = root.children('li').children('span+ul').prev("span:jtreeNameIs("+p[n]+")").last();
if(child.length) root = child.next(); else return root.prev();
}
child = root.children('li').children('span:jtreeNameIs('+p[nb-1]+')').last();
if(child.length) return child;
return root.prev();
}
$.fn.jtreeReverse = [].reverse;
$.fn.jtreeOpenTo = function(options)
{ var t = $(this);
var opt = $.extend({}, $.fn.jtree.dopt, options);
var ul = t.next("ul").add(t.parentsUntil('ul.jtree','ul'));
ul = ul.prev().filter(":jtreeClose").next(); if(!ul.length) ul=t.next("ul");
ul.each(function(i)
{ var t = $(this);
openfolder(t.prev(),t.delay(opt.multiOpenDelay*i),opt);
});
return this;
}
$.fn.jtreeGoTo = function(path,options)
{ return $(this).jtreeSelect(path).jtreeOpenTo(options);
}
$.fn.jtreeSearchFile = function(name,mode)
{ var set = $(this).find('span:contains('+name+')'); set = set.not(set.next("ul").prev());
if(mode == "exact") return set.filter(':jtreeNameIs('+name+')');
return set;
}
$.fn.jtreeSearchFolder = function(name,mode)
{ var set = $(this).find('span:contains('+name+')').next("ul").prev();
if(mode == "exact") return set.filter(':jtreeNameIs('+name+')');
return set;
}
$.fn.jtreeSearch = function(name,mode)
{ var set = $(this).find('span:contains('+name+')');
if(mode == "exact") return set.filter(':jtreeNameIs('+name+')');
return set;
}
$.fn.jtreeGetPath = function() // get current entry complete path
{ var t = $(this); if(t.is("ul.jtree>li>span")) return '';
var p = t.parents("span+ul").not("ul.jtree>li>ul").prev();
var path = ''; p.each(function(){ path = $(this).html()+'/'+path; });
return path+t.html();
}
$.fn.jtree.dopt = {
autoToggle:true,
multiFolder:"siblings",
iconFolder:'/img/icons/',
icons: [ { type:"image", icon:"images.png", ext:[ "jpg","jpeg","gif","png","bmp" ] },
{ type:"source", icon:"page_code.png", ext:[ "htm","html","php","txt" ] }
],
openSpeed:500,
closeSpeed:500,
appendTime:1000,
removeTime:1000,
multiOpenDelay:500,
icon:'page_white.png'
};
})(jQuery);