jstree.checkbox.js
11.6 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
* ### Checkbox plugin
*
* This plugin renders checkbox icons in front of each node, making multiple selection much easier.
* It also supports tri-state behavior, meaning that if a node has a few of its children checked it will be rendered as undetermined, and state will be propagated up.
*/
/*globals jQuery, define, exports, require, document */
(function (factory) {
"use strict";
if (typeof define === 'function' && define.amd) {
define('jstree.checkbox', ['jquery','jstree'], factory);
}
else if(typeof exports === 'object') {
factory(require('jquery'), require('jstree'));
}
else {
factory(jQuery, jQuery.jstree);
}
}(function ($, jstree, undefined) {
"use strict";
if($.jstree.plugins.checkbox) { return; }
var _i = document.createElement('I');
_i.className = 'jstree-icon jstree-checkbox';
/**
* stores all defaults for the checkbox plugin
* @name $.jstree.defaults.checkbox
* @plugin checkbox
*/
$.jstree.defaults.checkbox = {
/**
* a boolean indicating if checkboxes should be visible (can be changed at a later time using `show_checkboxes()` and `hide_checkboxes`). Defaults to `true`.
* @name $.jstree.defaults.checkbox.visible
* @plugin checkbox
*/
visible : true,
/**
* a boolean indicating if checkboxes should cascade down and have an undetermined state. Defaults to `true`.
* @name $.jstree.defaults.checkbox.three_state
* @plugin checkbox
*/
three_state : true,
/**
* a boolean indicating if clicking anywhere on the node should act as clicking on the checkbox. Defaults to `true`.
* @name $.jstree.defaults.checkbox.whole_node
* @plugin checkbox
*/
whole_node : true,
/**
* a boolean indicating if the selected style of a node should be kept, or removed. Defaults to `true`.
* @name $.jstree.defaults.checkbox.keep_selected_style
* @plugin checkbox
*/
keep_selected_style : true
};
$.jstree.plugins.checkbox = function (options, parent) {
this.bind = function () {
parent.bind.call(this);
this._data.checkbox.uto = false;
this.element
.on("init.jstree", $.proxy(function () {
this._data.checkbox.visible = this.settings.checkbox.visible;
if(!this.settings.checkbox.keep_selected_style) {
this.element.addClass('jstree-checkbox-no-clicked');
}
}, this))
.on("loading.jstree", $.proxy(function () {
this[ this._data.checkbox.visible ? 'show_checkboxes' : 'hide_checkboxes' ]();
}, this));
if(this.settings.checkbox.three_state) {
this.element
.on('changed.jstree move_node.jstree copy_node.jstree redraw.jstree open_node.jstree', $.proxy(function () {
if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); }
this._data.checkbox.uto = setTimeout($.proxy(this._undetermined, this), 50);
}, this))
.on('model.jstree', $.proxy(function (e, data) {
var m = this._model.data,
p = m[data.parent],
dpc = data.nodes,
chd = [],
c, i, j, k, l, tmp;
// apply down
if(p.state.selected) {
for(i = 0, j = dpc.length; i < j; i++) {
m[dpc[i]].state.selected = true;
}
this._data.core.selected = this._data.core.selected.concat(dpc);
}
else {
for(i = 0, j = dpc.length; i < j; i++) {
if(m[dpc[i]].state.selected) {
for(k = 0, l = m[dpc[i]].children_d.length; k < l; k++) {
m[m[dpc[i]].children_d[k]].state.selected = true;
}
this._data.core.selected = this._data.core.selected.concat(m[dpc[i]].children_d);
}
}
}
// apply up
for(i = 0, j = p.children_d.length; i < j; i++) {
if(!m[p.children_d[i]].children.length) {
chd.push(m[p.children_d[i]].parent);
}
}
chd = $.vakata.array_unique(chd);
for(k = 0, l = chd.length; k < l; k++) {
p = m[chd[k]];
while(p && p.id !== '#') {
c = 0;
for(i = 0, j = p.children.length; i < j; i++) {
c += m[p.children[i]].state.selected;
}
if(c === j) {
p.state.selected = true;
this._data.core.selected.push(p.id);
tmp = this.get_node(p, true);
if(tmp && tmp.length) {
tmp.children('.jstree-anchor').addClass('jstree-clicked');
}
}
else {
break;
}
p = this.get_node(p.parent);
}
}
this._data.core.selected = $.vakata.array_unique(this._data.core.selected);
}, this))
.on('select_node.jstree', $.proxy(function (e, data) {
var obj = data.node,
m = this._model.data,
par = this.get_node(obj.parent),
dom = this.get_node(obj, true),
i, j, c, tmp;
this._data.core.selected = $.vakata.array_unique(this._data.core.selected.concat(obj.children_d));
for(i = 0, j = obj.children_d.length; i < j; i++) {
m[obj.children_d[i]].state.selected = true;
}
while(par && par.id !== '#') {
c = 0;
for(i = 0, j = par.children.length; i < j; i++) {
c += m[par.children[i]].state.selected;
}
if(c === j) {
par.state.selected = true;
this._data.core.selected.push(par.id);
tmp = this.get_node(par, true);
if(tmp && tmp.length) {
tmp.children('.jstree-anchor').addClass('jstree-clicked');
}
}
else {
break;
}
par = this.get_node(par.parent);
}
if(dom.length) {
dom.find('.jstree-anchor').addClass('jstree-clicked');
}
}, this))
.on('deselect_node.jstree', $.proxy(function (e, data) {
var obj = data.node,
dom = this.get_node(obj, true),
i, j, tmp;
for(i = 0, j = obj.children_d.length; i < j; i++) {
this._model.data[obj.children_d[i]].state.selected = false;
}
for(i = 0, j = obj.parents.length; i < j; i++) {
this._model.data[obj.parents[i]].state.selected = false;
tmp = this.get_node(obj.parents[i], true);
if(tmp && tmp.length) {
tmp.children('.jstree-anchor').removeClass('jstree-clicked');
}
}
tmp = [];
for(i = 0, j = this._data.core.selected.length; i < j; i++) {
if($.inArray(this._data.core.selected[i], obj.children_d) === -1 && $.inArray(this._data.core.selected[i], obj.parents) === -1) {
tmp.push(this._data.core.selected[i]);
}
}
this._data.core.selected = $.vakata.array_unique(tmp);
if(dom.length) {
dom.find('.jstree-anchor').removeClass('jstree-clicked');
}
}, this))
.on('delete_node.jstree', $.proxy(function (e, data) {
var p = this.get_node(data.parent),
m = this._model.data,
i, j, c, tmp;
while(p && p.id !== '#') {
c = 0;
for(i = 0, j = p.children.length; i < j; i++) {
c += m[p.children[i]].state.selected;
}
if(c === j) {
p.state.selected = true;
this._data.core.selected.push(p.id);
tmp = this.get_node(p, true);
if(tmp && tmp.length) {
tmp.children('.jstree-anchor').addClass('jstree-clicked');
}
}
else {
break;
}
p = this.get_node(p.parent);
}
}, this))
.on('move_node.jstree', $.proxy(function (e, data) {
var is_multi = data.is_multi,
old_par = data.old_parent,
new_par = this.get_node(data.parent),
m = this._model.data,
p, c, i, j, tmp;
if(!is_multi) {
p = this.get_node(old_par);
while(p && p.id !== '#') {
c = 0;
for(i = 0, j = p.children.length; i < j; i++) {
c += m[p.children[i]].state.selected;
}
if(c === j) {
p.state.selected = true;
this._data.core.selected.push(p.id);
tmp = this.get_node(p, true);
if(tmp && tmp.length) {
tmp.children('.jstree-anchor').addClass('jstree-clicked');
}
}
else {
break;
}
p = this.get_node(p.parent);
}
}
p = new_par;
while(p && p.id !== '#') {
c = 0;
for(i = 0, j = p.children.length; i < j; i++) {
c += m[p.children[i]].state.selected;
}
if(c === j) {
if(!p.state.selected) {
p.state.selected = true;
this._data.core.selected.push(p.id);
tmp = this.get_node(p, true);
if(tmp && tmp.length) {
tmp.children('.jstree-anchor').addClass('jstree-clicked');
}
}
}
else {
if(p.state.selected) {
p.state.selected = false;
this._data.core.selected = $.vakata.array_remove_item(this._data.core.selected, p.id);
tmp = this.get_node(p, true);
if(tmp && tmp.length) {
tmp.children('.jstree-anchor').removeClass('jstree-clicked');
}
}
else {
break;
}
}
p = this.get_node(p.parent);
}
}, this));
}
};
/**
* set the undetermined state where and if necessary. Used internally.
* @private
* @name _undetermined()
* @plugin checkbox
*/
this._undetermined = function () {
var i, j, m = this._model.data, s = this._data.core.selected, p = [], t = this;
for(i = 0, j = s.length; i < j; i++) {
if(m[s[i]] && m[s[i]].parents) {
p = p.concat(m[s[i]].parents);
}
}
// attempt for server side undetermined state
this.element.find('.jstree-closed').not(':has(ul)')
.each(function () {
var tmp = t.get_node(this);
if(!tmp.state.loaded && tmp.original && tmp.original.state && tmp.original.state.undetermined && tmp.original.state.undetermined === true) {
p.push(tmp.id);
p = p.concat(tmp.parents);
}
});
p = $.vakata.array_unique(p);
i = $.inArray('#', p);
if(i !== -1) {
p = $.vakata.array_remove(p, i);
}
this.element.find('.jstree-undetermined').removeClass('jstree-undetermined');
for(i = 0, j = p.length; i < j; i++) {
if(!m[p[i]].state.selected) {
s = this.get_node(p[i], true);
if(s && s.length) {
s.children('a').children('.jstree-checkbox').addClass('jstree-undetermined');
}
}
}
};
this.redraw_node = function(obj, deep, is_callback) {
obj = parent.redraw_node.call(this, obj, deep, is_callback);
if(obj) {
var tmp = obj.getElementsByTagName('A')[0];
tmp.insertBefore(_i.cloneNode(), tmp.childNodes[0]);
}
if(!is_callback && this.settings.checkbox.three_state) {
if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); }
this._data.checkbox.uto = setTimeout($.proxy(this._undetermined, this), 50);
}
return obj;
};
this.activate_node = function (obj, e) {
if(this.settings.checkbox.whole_node || $(e.target).hasClass('jstree-checkbox')) {
e.ctrlKey = true;
}
return parent.activate_node.call(this, obj, e);
};
/**
* show the node checkbox icons
* @name show_checkboxes()
* @plugin checkbox
*/
this.show_checkboxes = function () { this._data.core.themes.checkboxes = true; this.element.children("ul").removeClass("jstree-no-checkboxes"); };
/**
* hide the node checkbox icons
* @name hide_checkboxes()
* @plugin checkbox
*/
this.hide_checkboxes = function () { this._data.core.themes.checkboxes = false; this.element.children("ul").addClass("jstree-no-checkboxes"); };
/**
* toggle the node icons
* @name toggle_checkboxes()
* @plugin checkbox
*/
this.toggle_checkboxes = function () { if(this._data.core.themes.checkboxes) { this.hide_checkboxes(); } else { this.show_checkboxes(); } };
};
// include the checkbox plugin by default
// $.jstree.defaults.plugins.push("checkbox");
}));