-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathjquery.checktree.js
221 lines (195 loc) · 7.2 KB
/
jquery.checktree.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
/**
Project: CheckTree jQuery Plugin
Version: 0.4
Legacy Project Website (to v0.3): http://jquery-checktree.googlecode.com/
Author: JJ Geeway <[email protected]>
Project Website (since v0.4): https://github.com/yeurch/checktree
Author: Richard Fawcett
Twitter: @yeurch
License:
The CheckTree jQuery plugin is currently available for use in all personal or
commercial projects under both MIT and GPL licenses. This means that you can choose
the license that best suits your project and use it accordingly.
*/
(function(jQuery) {
jQuery.fn.checkTree = function(settings) {
settings = jQuery.extend({
/* Callbacks
The callbacks should be functions that take one argument. The checkbox tree
will return the jQuery wrapped LI element of the item that was checked/expanded.
*/
onExpand: null,
onCollapse: null,
onCheck: null,
onUnCheck: null,
onHalfCheck: null,
onLabelHoverOver: null,
onLabelHoverOut: null,
/* Valid choices: 'expand', 'check' */
labelAction: "expand",
// Debug (currently does nothing)
debug: false
}, settings);
var $tree = this;
var $lis = $tree.find('li');
var $checkboxes = $lis.find(":checkbox");
//remove all divs to avoid issues if it already has created the elements
$tree.find('div.checktree').remove();
// Hide all checkbox inputs
$checkboxes.css('display', 'none');
$lis.not(':has(.arrow)').each(function() {
// This little piece here is by far the slowest.
jQuery(this).prepend('<div class="checktree arrow"></div><div class="checktree checkbox"></div>');
});
/*
What to do when the arrow is clicked
Tried:
- $lis.filter(':has(li)').find(' > .arrow')
- $lis.filter(':has(li)').find('.arrow')
- $tree.find('li:has(li) .arrow')
- $tree.find('li:has(li) > .arrow') <- This was the fastest.
*/
$tree.find('li:has(li) > .arrow')
.click(function() {
var $this = jQuery(this);
$this
.toggleClass('expanded')
.toggleClass('collapsed')
.siblings("ul:first")
.toggle()
;
// Handle callbacks
if (settings.onExpand && $this.hasClass('expanded')) {
settings.onExpand($this.parent());
}
else if (settings.onCollapse && $this.hasClass('collapsed')){
settings.onCollapse($this.parent());
}
})
.addClass(function(){
return $(this).siblings('ul.expanded').length === 1 ? 'expanded' : 'collapsed';
})
;
// Remove the now redundant 'expanded' class from any sub-lists
$tree.find('ul').removeClass('expanded');
// Hide all collapsed sub-trees
$tree.find('li:has(> .arrow.collapsed) > ul').css('display', 'none');
/*
What to do when the checkbox is clicked
*/
$tree.find('.checkbox').click(function() {
var $this = jQuery(this);
$this
.toggleClass('checked')
.removeClass('half_checked')
.siblings(':checkbox:first').prop('checked', $this.hasClass('checked'))
;
$this.filter('.checked').siblings('ul:first').find('.checkbox:not(.checked)')
.removeClass('half_checked')
.addClass('checked')
.siblings(':checkbox').prop('checked', true)
;
$this.filter(':not(.checked)').siblings('ul:first').find('.checkbox.checked')
.removeClass('checked half_checked')
.siblings(':checkbox').prop('checked', false)
;
// Send a change event to our parent checkbox:
$this.parents("ul:first").siblings(":checkbox:first").trigger('refresh');
// Handle callbacks
if (settings.onCheck && $this.hasClass('checked')) {
settings.onCheck($this.parent());
}
else if (settings.onUnCheck && $this.hasClass('checked') == false) {
settings.onUnCheck($this.parent());
}
});
/*
What to do when a checkbox gets a change event
(Fired when the children of this checkbox have changed)
*/
$checkboxes.on('refresh', function() {
// If all the children are checked, this should be checked:
var $this = jQuery(this);
var $checkbox = $this.siblings('.checkbox:first');
var any_checked = $this.siblings('ul:first').find(':checkbox:checked:first').length == 1;
var any_unchecked = $this.siblings('ul:first').find(':checkbox:not(:checked):first').length == 1;
if (any_checked) {
$this.prop('checked', true);
if (any_unchecked) {
$checkbox
.addClass('half_checked')
.removeClass('checked')
;
if (settings.onHalfCheck) {
settings.onHalfCheck($this.parent());
}
}
else {
$checkbox
.addClass('checked')
.removeClass('half_checked')
;
}
}
else {
$checkbox.removeClass('checked half_checked');
$this.prop('checked', false);
}
// Bubble up to our parent checkbox
$this.parents('ul:first').siblings(':checkbox:first').trigger('refresh');
});
/*
What to do when a label is hovered or clicked
*/
$tree.find('label')
.click(function() {
switch(settings.labelAction) {
case 'expand':
jQuery(this).siblings('.arrow:first').click();
break;
case 'check':
jQuery(this).siblings('.checkbox:first').click();
break;
}
})
.hover(
function() {
jQuery(this).addClass("hover");
if (settings.onLabelHoverOver) {
settings.onLabelHoverOver(jQuery(this).parent());
}
},
function() {
jQuery(this).removeClass("hover");
if (settings.onLabelHoverOut) {
settings.onLabelHoverOut(jQuery(this).parent());
}
}
)
;
/*
Extra convenience methods
*/
$tree.clear = function() {
$tree.find('.checkbox')
.removeClass('checked')
.siblings(':checkbox').prop('checked', false)
;
};
/*
Add checked class to elements with "checktree checkbox" when checkbox is selected
*/
$checkboxes.change(function () {
var $this = jQuery(this);
var $checkbox = $this.siblings('.checkbox:first');
if ($this.prop('checked')) {
$checkbox.addClass('checked');
} else {
$checkbox.removeClass('checked');
}
$checkbox.parents("ul:first").siblings(":checkbox:first").trigger('refresh');
});
// Trigger the change event for any pre-checked checkboxes on initialization
$checkboxes.filter(':checked').trigger('change');
};
})(jQuery);