-
Notifications
You must be signed in to change notification settings - Fork 0
/
showByTree.js
58 lines (56 loc) · 2.53 KB
/
showByTree.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
$.fn.extend({
showByTree: function(setting, onFinished){
if( !$(this).length ) return;
var opts = $.extend({
itemTag: 'li',
groupTag: 'ul',
groupIdPrefix: 'childrenOf_',
itemBodyTag: 'div',
itemIdPrefix: 'item_',
showButtonInsert: 'after'
}, setting || {});
return $(this).each(function(){
$(this).children(opts.itemTag).each(function(){
var pId = $(this).data('pid');
if( pId > 0 ) {
var parent = $('#' + opts.itemIdPrefix + pId);
if( ! parent.children(opts.groupTag).length ) {
var toggleButton = $('<i class="showChildren">+</i>');
var childrenBox = $('<'+ opts.groupTag +'>').attr('id', opts.groupIdPrefix + pId);
if( opts.showButtonInsert == 'after' ){
parent.append(toggleButton);
} else if( opts.showButtonInsert == 'inbody' ){
parent.children(opts.itemBodyTag).prepend(toggleButton);
} else {
parent.prepend(toggleButton);
}
parent.addClass('hasChild').append(childrenBox);
toggleButton.on('click', function(){
$(this).toggleClass('isOn');
childrenBox.toggleClass('on');
if($(this).is('.isOn')) $(this).text('-');
else $(this).text('+');
});
} else {
parent.addClass('hasChildren');
}
$(this).appendTo( parent.children(opts.groupTag) );
}
});
$(this).children(opts.itemTag).each(function(){
setLevel( $(this), 0 );
});
if( onFinished )
onFinished( $(this) );
});
function setLevel( item, i ){
var subItem = item.children(opts.groupTag);
if( subItem.length ){
subItem.addClass('deepth_' + (++i));
subItem.children(opts.itemTag).each(function(){
setLevel($(this), i);
});
}
}
}
})