-
Notifications
You must be signed in to change notification settings - Fork 0
/
hierarchical-checkboxes.js
184 lines (157 loc) · 6.04 KB
/
hierarchical-checkboxes.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
/**
Hierarchical Checkboxes
author: Anil Maharjan
USAGE:
Template Construction:
User ROOT template and Nest as many NODE templates
ROOT:
<div class="hierarchy-checkboxes" rel="test">
<input class="hierarchy-root-checkbox" type="checkbox">
<label class="hierarchy-root-label">Root</label>
<div class="hierarchy-root-child hierarchy-node">
{{ NODE TEMPLATE HERE }}
</div>
</div>
NODE:
<div class="hierarchy-node [leaf]">
<input class="hierarchy-checkbox" type="checkbox">
<label class="hierarchy-label">[Title]</label>
{{ NODE TEMPLATE HERE }}
</div>
// Basic Example Template
<div class="hierarchy-checkboxes" rel="test">
<input class="hierarchy-root-checkbox" type="checkbox">
<label class="hierarchy-root-label">Root</label>
<div class="hierarchy-root-child hierarchy-node">
<div class="hierarchy-node leaf">
<input class="hierarchy-checkbox" type="checkbox">
<label class="hierarchy-label">Markets</label>
<div class="hierarchy-node leaf">
<input class="hierarchy-checkbox" type="checkbox">
<label class="hierarchy-label">Markets</label>
</div>
</div>
<div class="hierarchy-node leaf">
<input class="hierarchy-checkbox" type="checkbox">
<label class="hierarchy-label">Markets</label>
</div>
</div>
</div>
API:
EVENTS:
1. checkboxesUpdate:
Triggers whenever the check/uncheck tasks complete withing the hierarchical checkboxes
Example:
jQuery('.hierarchy-checkboxes[rel=IDENTIFIER]').on('checkboxesUpdate',function(){
console.log("Changed!");
});
**/
jQuery(document).ready(function () {
jQuery(".hierarchy-checkboxes .hierarchy-root-child div div").hide();
jQuery(".hierarchy-checkboxes .hierarchy-root-child")
.attr("rel", function () {
const $this = jQuery(this);
$this.attr("rel", $this.closest(".hierarchy-checkboxes").attr("rel"));
})
.appendTo("body")
.hide();
jQuery(
".hierarchy-checkboxes, .hierarchy-root-child .hierarchy-node"
).prepend('<div class="expand-collapse-button"></div>');
// Root label toggles root-child / Popup layer as a whole
jQuery(".hierarchy-root-label").click(function () {
const $this = jQuery(this);
const $thisNode = $this.parent();
const rel = $thisNode.attr("rel");
const $rootChild = jQuery(".hierarchy-root-child[rel=" + rel + "]");
if (!$thisNode.hasClass("child-expanded")) {
$thisNode.addClass("child-expanded");
const thisPos = $thisNode.offset();
$rootChild
.css({ left: thisPos.left, top: thisPos.top + $thisNode.height() - 1 })
.slideDown("fast");
} else {
$rootChild.slideUp("fast", function () {
$thisNode.removeClass("child-expanded");
});
}
});
jQuery(".expand-collapse-button").click(function () {
jQuery(this).siblings(".hierarchy-label, .hierarchy-root-label").click();
});
jQuery(".hierarchy-root-checkbox").change(function () {
const $this = jQuery(this);
//$thisNode is parent to current checkbox so it would represent current level node
const $thisNode = $this.parent();
const rel = $thisNode.attr("rel"); // Identifier (rel attribute of current hierarchy root)
const $rootChild = jQuery(".hierarchy-root-child[rel=" + rel + "]"); // The node that contains all the elements of hierarchy;
$rootChild
.find("input.hierarchy-checkbox")
.prop("checked", $this.prop("checked"));
$thisNode.trigger("checkboxesUpdate");
});
// Each node's label toggles the node's child / label's sibling
jQuery(".hierarchy-node .hierarchy-label").click(function () {
const $this = jQuery(this);
const $thisNode = $this.parent();
if (!$thisNode.hasClass("child-expanded")) {
$thisNode.addClass("child-expanded");
$this.siblings(".hierarchy-node").slideDown("fast");
} else {
$this.siblings(".hierarchy-node").slideUp("fast", function () {
$thisNode.removeClass("child-expanded");
});
}
});
// Each node's checkbox toggles the node's child / checkbox's sibling
jQuery(".hierarchy-node .hierarchy-checkbox").change(function () {
const $this = jQuery(this);
//$thisNode is parent to current checkbox so it would represent current level node
const $thisNode = $this.parent();
const $parentNode = $thisNode.parent(".hierarchy-node");
const $parentNodeCheckbox = $parentNode.children(
"input.hierarchy-checkbox"
);
const $rootChild = $this.parents(".hierarchy-root-child"); // The node that contains all the elements of hierarchy;
const rel = $rootChild.attr("rel"); // Identifier (rel attribute of current hierarchy root)
const $root = jQuery(".hierarchy-checkboxes[rel=" + rel + "]");
const $rootCheckbox = jQuery(
".hierarchy-checkboxes[rel=" + rel + "] .hierarchy-root-checkbox"
);
// take care of children | Easy one
$this
.siblings(".hierarchy-node")
.find("input.hierarchy-checkbox")
.prop("checked", $this.prop("checked"));
//take care of parents | tough one
if (!$this.prop("checked")) {
// If unchecked uncheck all the ancestors
$thisNode
.parents(".hierarchy-node")
.children("input.hierarchy-checkbox")
.prop("checked", $this.prop("checked"));
// also uncheck the root
$rootCheckbox.prop("checked", false);
} else {
// If checked check for the siblings state and check the parent if all siblings are checked too
const allCheckboxesInCurrentDepth = $parentNode.find(
".hierarchy-node .hierarchy-checkbox"
).length;
const allCheckedCheckboxesInCurrentDepth = $parentNode.find(
".hierarchy-node .hierarchy-checkbox:checked"
).length;
// all nodes in and below siblings are checked
if (allCheckboxesInCurrentDepth === allCheckedCheckboxesInCurrentDepth) {
// check the parent
if ($parentNodeCheckbox.length) {
$parentNodeCheckbox.prop("checked", true);
} else {
$rootCheckbox.prop("checked", true);
}
}
}
$root.trigger("checkboxesUpdate", [
$rootChild.find(".hierarchy-checkbox:checked")
]);
});
});