jstree.checkbox.js
35.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
/**
* ### 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.js'], factory);
}
else if(typeof exports === 'object') {
factory(require('jquery'), require('./jstree.js'));
}
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';
_i.setAttribute('role', 'presentation');
/**
* 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,
/**
* This setting controls how cascading and undetermined nodes are applied.
* If 'up' is in the string - cascading up is enabled, if 'down' is in the string - cascading down is enabled, if 'undetermined' is in the string - undetermined nodes will be used.
* If `three_state` is set to `true` this setting is automatically set to 'up+down+undetermined'. Defaults to ''.
* @name $.jstree.defaults.checkbox.cascade
* @plugin checkbox
*/
cascade : '',
/**
* This setting controls if checkbox are bound to the general tree selection or to an internal array maintained by the checkbox plugin. Defaults to `true`, only set to `false` if you know exactly what you are doing.
* @name $.jstree.defaults.checkbox.tie_selection
* @plugin checkbox
*/
tie_selection : true,
/**
* This setting controls if cascading down affects disabled checkboxes
* @name $.jstree.defaults.checkbox.cascade_to_disabled
* @plugin checkbox
*/
cascade_to_disabled : true,
/**
* This setting controls if cascading down affects hidden checkboxes
* @name $.jstree.defaults.checkbox.cascade_to_hidden
* @plugin checkbox
*/
cascade_to_hidden : true
};
$.jstree.plugins.checkbox = function (options, parent) {
this.bind = function () {
parent.bind.call(this);
this._data.checkbox.uto = false;
this._data.checkbox.selected = [];
if(this.settings.checkbox.three_state) {
this.settings.checkbox.cascade = 'up+down+undetermined';
}
this.element
.on("init.jstree", function () {
this._data.checkbox.visible = this.settings.checkbox.visible;
if(!this.settings.checkbox.keep_selected_style) {
this.element.addClass('jstree-checkbox-no-clicked');
}
if(this.settings.checkbox.tie_selection) {
this.element.addClass('jstree-checkbox-selection');
}
}.bind(this))
.on("loading.jstree", function () {
this[ this._data.checkbox.visible ? 'show_checkboxes' : 'hide_checkboxes' ]();
}.bind(this));
if(this.settings.checkbox.cascade.indexOf('undetermined') !== -1) {
this.element
.on('changed.jstree uncheck_node.jstree check_node.jstree uncheck_all.jstree check_all.jstree move_node.jstree copy_node.jstree redraw.jstree open_node.jstree', function () {
// only if undetermined is in setting
if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); }
this._data.checkbox.uto = setTimeout(this._undetermined.bind(this), 50);
}.bind(this));
}
if(!this.settings.checkbox.tie_selection) {
this.element
.on('model.jstree', function (e, data) {
var m = this._model.data,
p = m[data.parent],
dpc = data.nodes,
i, j;
for(i = 0, j = dpc.length; i < j; i++) {
m[dpc[i]].state.checked = m[dpc[i]].state.checked || (m[dpc[i]].original && m[dpc[i]].original.state && m[dpc[i]].original.state.checked);
if(m[dpc[i]].state.checked) {
this._data.checkbox.selected.push(dpc[i]);
}
}
}.bind(this));
}
if(this.settings.checkbox.cascade.indexOf('up') !== -1 || this.settings.checkbox.cascade.indexOf('down') !== -1) {
this.element
.on('model.jstree', function (e, data) {
var m = this._model.data,
p = m[data.parent],
dpc = data.nodes,
chd = [],
c, i, j, k, l, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection;
if(s.indexOf('down') !== -1) {
// apply down
if(p.state[ t ? 'selected' : 'checked' ]) {
for(i = 0, j = dpc.length; i < j; i++) {
m[dpc[i]].state[ t ? 'selected' : 'checked' ] = true;
}
this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(dpc);
}
else {
for(i = 0, j = dpc.length; i < j; i++) {
if(m[dpc[i]].state[ t ? 'selected' : 'checked' ]) {
for(k = 0, l = m[dpc[i]].children_d.length; k < l; k++) {
m[m[dpc[i]].children_d[k]].state[ t ? 'selected' : 'checked' ] = true;
}
this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(m[dpc[i]].children_d);
}
}
}
}
if(s.indexOf('up') !== -1) {
// 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 !== $.jstree.root) {
c = 0;
for(i = 0, j = p.children.length; i < j; i++) {
c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
}
if(c === j) {
p.state[ t ? 'selected' : 'checked' ] = true;
this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
tmp = this.get_node(p, true);
if(tmp && tmp.length) {
tmp.attr('aria-selected', true).children('.jstree-anchor').addClass( t ? 'jstree-clicked' : 'jstree-checked');
}
}
else {
break;
}
p = this.get_node(p.parent);
}
}
}
this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected);
}.bind(this))
.on(this.settings.checkbox.tie_selection ? 'select_node.jstree' : 'check_node.jstree', function (e, data) {
var self = this,
obj = data.node,
m = this._model.data,
par = this.get_node(obj.parent),
i, j, c, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection,
sel = {}, cur = this._data[ t ? 'core' : 'checkbox' ].selected;
for (i = 0, j = cur.length; i < j; i++) {
sel[cur[i]] = true;
}
// apply down
if(s.indexOf('down') !== -1) {
//this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected.concat(obj.children_d));
var selectedIds = this._cascade_new_checked_state(obj.id, true);
var temp = obj.children_d.concat(obj.id);
for (i = 0, j = temp.length; i < j; i++) {
if (selectedIds.indexOf(temp[i]) > -1) {
sel[temp[i]] = true;
}
else {
delete sel[temp[i]];
}
}
}
// apply up
if(s.indexOf('up') !== -1) {
while(par && par.id !== $.jstree.root) {
c = 0;
for(i = 0, j = par.children.length; i < j; i++) {
c += m[par.children[i]].state[ t ? 'selected' : 'checked' ];
}
if(c === j) {
par.state[ t ? 'selected' : 'checked' ] = true;
sel[par.id] = true;
//this._data[ t ? 'core' : 'checkbox' ].selected.push(par.id);
tmp = this.get_node(par, true);
if(tmp && tmp.length) {
tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
}
}
else {
break;
}
par = this.get_node(par.parent);
}
}
cur = [];
for (i in sel) {
if (sel.hasOwnProperty(i)) {
cur.push(i);
}
}
this._data[ t ? 'core' : 'checkbox' ].selected = cur;
}.bind(this))
.on(this.settings.checkbox.tie_selection ? 'deselect_all.jstree' : 'uncheck_all.jstree', function (e, data) {
var obj = this.get_node($.jstree.root),
m = this._model.data,
i, j, tmp;
for(i = 0, j = obj.children_d.length; i < j; i++) {
tmp = m[obj.children_d[i]];
if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
tmp.original.state.undetermined = false;
}
}
}.bind(this))
.on(this.settings.checkbox.tie_selection ? 'deselect_node.jstree' : 'uncheck_node.jstree', function (e, data) {
var self = this,
obj = data.node,
dom = this.get_node(obj, true),
i, j, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection,
cur = this._data[ t ? 'core' : 'checkbox' ].selected, sel = {},
stillSelectedIds = [],
allIds = obj.children_d.concat(obj.id);
// apply down
if(s.indexOf('down') !== -1) {
var selectedIds = this._cascade_new_checked_state(obj.id, false);
cur = $.vakata.array_filter(cur, function(id) {
return allIds.indexOf(id) === -1 || selectedIds.indexOf(id) > -1;
});
}
// only apply up if cascade up is enabled and if this node is not selected
// (if all child nodes are disabled and cascade_to_disabled === false then this node will till be selected).
if(s.indexOf('up') !== -1 && cur.indexOf(obj.id) === -1) {
for(i = 0, j = obj.parents.length; i < j; i++) {
tmp = this._model.data[obj.parents[i]];
tmp.state[ t ? 'selected' : 'checked' ] = false;
if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
tmp.original.state.undetermined = false;
}
tmp = this.get_node(obj.parents[i], true);
if(tmp && tmp.length) {
tmp.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
}
}
cur = $.vakata.array_filter(cur, function(id) {
return obj.parents.indexOf(id) === -1;
});
}
this._data[ t ? 'core' : 'checkbox' ].selected = cur;
}.bind(this));
}
if(this.settings.checkbox.cascade.indexOf('up') !== -1) {
this.element
.on('delete_node.jstree', function (e, data) {
// apply up (whole handler)
var p = this.get_node(data.parent),
m = this._model.data,
i, j, c, tmp, t = this.settings.checkbox.tie_selection;
while(p && p.id !== $.jstree.root && !p.state[ t ? 'selected' : 'checked' ]) {
c = 0;
for(i = 0, j = p.children.length; i < j; i++) {
c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
}
if(j > 0 && c === j) {
p.state[ t ? 'selected' : 'checked' ] = true;
this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
tmp = this.get_node(p, true);
if(tmp && tmp.length) {
tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
}
}
else {
break;
}
p = this.get_node(p.parent);
}
}.bind(this))
.on('move_node.jstree', function (e, data) {
// apply up (whole handler)
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, t = this.settings.checkbox.tie_selection;
if(!is_multi) {
p = this.get_node(old_par);
while(p && p.id !== $.jstree.root && !p.state[ t ? 'selected' : 'checked' ]) {
c = 0;
for(i = 0, j = p.children.length; i < j; i++) {
c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
}
if(j > 0 && c === j) {
p.state[ t ? 'selected' : 'checked' ] = true;
this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
tmp = this.get_node(p, true);
if(tmp && tmp.length) {
tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
}
}
else {
break;
}
p = this.get_node(p.parent);
}
}
p = new_par;
while(p && p.id !== $.jstree.root) {
c = 0;
for(i = 0, j = p.children.length; i < j; i++) {
c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
}
if(c === j) {
if(!p.state[ t ? 'selected' : 'checked' ]) {
p.state[ t ? 'selected' : 'checked' ] = true;
this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
tmp = this.get_node(p, true);
if(tmp && tmp.length) {
tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
}
}
}
else {
if(p.state[ t ? 'selected' : 'checked' ]) {
p.state[ t ? 'selected' : 'checked' ] = false;
this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_remove_item(this._data[ t ? 'core' : 'checkbox' ].selected, p.id);
tmp = this.get_node(p, true);
if(tmp && tmp.length) {
tmp.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
}
}
else {
break;
}
}
p = this.get_node(p.parent);
}
}.bind(this));
}
};
/**
* get an array of all nodes whose state is "undetermined"
* @name get_undetermined([full])
* @param {boolean} full: if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
* @return {Array}
* @plugin checkbox
*/
this.get_undetermined = function (full) {
if (this.settings.checkbox.cascade.indexOf('undetermined') === -1) {
return [];
}
var i, j, k, l, o = {}, m = this._model.data, t = this.settings.checkbox.tie_selection, s = this._data[ t ? 'core' : 'checkbox' ].selected, p = [], tt = this, r = [];
for(i = 0, j = s.length; i < j; i++) {
if(m[s[i]] && m[s[i]].parents) {
for(k = 0, l = m[s[i]].parents.length; k < l; k++) {
if(o[m[s[i]].parents[k]] !== undefined) {
break;
}
if(m[s[i]].parents[k] !== $.jstree.root) {
o[m[s[i]].parents[k]] = true;
p.push(m[s[i]].parents[k]);
}
}
}
}
// attempt for server side undetermined state
this.element.find('.jstree-closed').not(':has(.jstree-children)')
.each(function () {
var tmp = tt.get_node(this), tmp2;
if(!tmp) { return; }
if(!tmp.state.loaded) {
if(tmp.original && tmp.original.state && tmp.original.state.undetermined && tmp.original.state.undetermined === true) {
if(o[tmp.id] === undefined && tmp.id !== $.jstree.root) {
o[tmp.id] = true;
p.push(tmp.id);
}
for(k = 0, l = tmp.parents.length; k < l; k++) {
if(o[tmp.parents[k]] === undefined && tmp.parents[k] !== $.jstree.root) {
o[tmp.parents[k]] = true;
p.push(tmp.parents[k]);
}
}
}
}
else {
for(i = 0, j = tmp.children_d.length; i < j; i++) {
tmp2 = m[tmp.children_d[i]];
if(!tmp2.state.loaded && tmp2.original && tmp2.original.state && tmp2.original.state.undetermined && tmp2.original.state.undetermined === true) {
if(o[tmp2.id] === undefined && tmp2.id !== $.jstree.root) {
o[tmp2.id] = true;
p.push(tmp2.id);
}
for(k = 0, l = tmp2.parents.length; k < l; k++) {
if(o[tmp2.parents[k]] === undefined && tmp2.parents[k] !== $.jstree.root) {
o[tmp2.parents[k]] = true;
p.push(tmp2.parents[k]);
}
}
}
}
}
});
for (i = 0, j = p.length; i < j; i++) {
if(!m[p[i]].state[ t ? 'selected' : 'checked' ]) {
r.push(full ? m[p[i]] : p[i]);
}
}
return r;
};
/**
* set the undetermined state where and if necessary. Used internally.
* @private
* @name _undetermined()
* @plugin checkbox
*/
this._undetermined = function () {
if(this.element === null) { return; }
var p = this.get_undetermined(false), i, j, s;
this.element.find('.jstree-undetermined').removeClass('jstree-undetermined');
for (i = 0, j = p.length; i < j; i++) {
s = this.get_node(p[i], true);
if(s && s.length) {
s.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-undetermined');
}
}
};
this.redraw_node = function(obj, deep, is_callback, force_render) {
obj = parent.redraw_node.apply(this, arguments);
if(obj) {
var i, j, tmp = null, icon = null;
for(i = 0, j = obj.childNodes.length; i < j; i++) {
if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
tmp = obj.childNodes[i];
break;
}
}
if(tmp) {
if(!this.settings.checkbox.tie_selection && this._model.data[obj.id].state.checked) { tmp.className += ' jstree-checked'; }
icon = _i.cloneNode(false);
if(this._model.data[obj.id].state.checkbox_disabled) { icon.className += ' jstree-checkbox-disabled'; }
tmp.insertBefore(icon, tmp.childNodes[0]);
}
}
if(!is_callback && this.settings.checkbox.cascade.indexOf('undetermined') !== -1) {
if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); }
this._data.checkbox.uto = setTimeout(this._undetermined.bind(this), 50);
}
return obj;
};
/**
* show the node checkbox icons
* @name show_checkboxes()
* @plugin checkbox
*/
this.show_checkboxes = function () { this._data.core.themes.checkboxes = true; this.get_container_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.get_container_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(); } };
/**
* checks if a node is in an undetermined state
* @name is_undetermined(obj)
* @param {mixed} obj
* @return {Boolean}
*/
this.is_undetermined = function (obj) {
obj = this.get_node(obj);
var s = this.settings.checkbox.cascade, i, j, t = this.settings.checkbox.tie_selection, d = this._data[ t ? 'core' : 'checkbox' ].selected, m = this._model.data;
if(!obj || obj.state[ t ? 'selected' : 'checked' ] === true || s.indexOf('undetermined') === -1 || (s.indexOf('down') === -1 && s.indexOf('up') === -1)) {
return false;
}
if(!obj.state.loaded && obj.original.state.undetermined === true) {
return true;
}
for(i = 0, j = obj.children_d.length; i < j; i++) {
if($.inArray(obj.children_d[i], d) !== -1 || (!m[obj.children_d[i]].state.loaded && m[obj.children_d[i]].original.state.undetermined)) {
return true;
}
}
return false;
};
/**
* disable a node's checkbox
* @name disable_checkbox(obj)
* @param {mixed} obj an array can be used too
* @trigger disable_checkbox.jstree
* @plugin checkbox
*/
this.disable_checkbox = function (obj) {
var t1, t2, dom;
if($.vakata.is_array(obj)) {
obj = obj.slice();
for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
this.disable_checkbox(obj[t1]);
}
return true;
}
obj = this.get_node(obj);
if(!obj || obj.id === $.jstree.root) {
return false;
}
dom = this.get_node(obj, true);
if(!obj.state.checkbox_disabled) {
obj.state.checkbox_disabled = true;
if(dom && dom.length) {
dom.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-checkbox-disabled');
}
/**
* triggered when an node's checkbox is disabled
* @event
* @name disable_checkbox.jstree
* @param {Object} node
* @plugin checkbox
*/
this.trigger('disable_checkbox', { 'node' : obj });
}
};
/**
* enable a node's checkbox
* @name enable_checkbox(obj)
* @param {mixed} obj an array can be used too
* @trigger enable_checkbox.jstree
* @plugin checkbox
*/
this.enable_checkbox = function (obj) {
var t1, t2, dom;
if($.vakata.is_array(obj)) {
obj = obj.slice();
for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
this.enable_checkbox(obj[t1]);
}
return true;
}
obj = this.get_node(obj);
if(!obj || obj.id === $.jstree.root) {
return false;
}
dom = this.get_node(obj, true);
if(obj.state.checkbox_disabled) {
obj.state.checkbox_disabled = false;
if(dom && dom.length) {
dom.children('.jstree-anchor').children('.jstree-checkbox').removeClass('jstree-checkbox-disabled');
}
/**
* triggered when an node's checkbox is enabled
* @event
* @name enable_checkbox.jstree
* @param {Object} node
* @plugin checkbox
*/
this.trigger('enable_checkbox', { 'node' : obj });
}
};
this.activate_node = function (obj, e) {
if($(e.target).hasClass('jstree-checkbox-disabled')) {
return false;
}
if(this.settings.checkbox.tie_selection && (this.settings.checkbox.whole_node || $(e.target).hasClass('jstree-checkbox'))) {
e.ctrlKey = true;
}
if(this.settings.checkbox.tie_selection || (!this.settings.checkbox.whole_node && !$(e.target).hasClass('jstree-checkbox'))) {
return parent.activate_node.call(this, obj, e);
}
if(this.is_disabled(obj)) {
return false;
}
if(this.is_checked(obj)) {
this.uncheck_node(obj, e);
}
else {
this.check_node(obj, e);
}
this.trigger('activate_node', { 'node' : this.get_node(obj) });
};
/**
* Cascades checked state to a node and all its descendants. This function does NOT affect hidden and disabled nodes (or their descendants).
* However if these unaffected nodes are already selected their ids will be included in the returned array.
* @private
* @name _cascade_new_checked_state(id, checkedState)
* @param {string} id the node ID
* @param {bool} checkedState should the nodes be checked or not
* @returns {Array} Array of all node id's (in this tree branch) that are checked.
*/
this._cascade_new_checked_state = function (id, checkedState) {
var self = this;
var t = this.settings.checkbox.tie_selection;
var node = this._model.data[id];
var selectedNodeIds = [];
var selectedChildrenIds = [], i, j, selectedChildIds;
if (
(this.settings.checkbox.cascade_to_disabled || !node.state.disabled) &&
(this.settings.checkbox.cascade_to_hidden || !node.state.hidden)
) {
//First try and check/uncheck the children
if (node.children) {
for (i = 0, j = node.children.length; i < j; i++) {
var childId = node.children[i];
selectedChildIds = self._cascade_new_checked_state(childId, checkedState);
selectedNodeIds = selectedNodeIds.concat(selectedChildIds);
if (selectedChildIds.indexOf(childId) > -1) {
selectedChildrenIds.push(childId);
}
}
}
var dom = self.get_node(node, true);
//A node's state is undetermined if some but not all of it's children are checked/selected .
var undetermined = selectedChildrenIds.length > 0 && selectedChildrenIds.length < node.children.length;
if(node.original && node.original.state && node.original.state.undetermined) {
node.original.state.undetermined = undetermined;
}
//If a node is undetermined then remove selected class
if (undetermined) {
node.state[ t ? 'selected' : 'checked' ] = false;
dom.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
}
//Otherwise, if the checkedState === true (i.e. the node is being checked now) and all of the node's children are checked (if it has any children),
//check the node and style it correctly.
else if (checkedState && selectedChildrenIds.length === node.children.length) {
node.state[ t ? 'selected' : 'checked' ] = checkedState;
selectedNodeIds.push(node.id);
dom.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
}
else {
node.state[ t ? 'selected' : 'checked' ] = false;
dom.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
}
}
else {
selectedChildIds = this.get_checked_descendants(id);
if (node.state[ t ? 'selected' : 'checked' ]) {
selectedChildIds.push(node.id);
}
selectedNodeIds = selectedNodeIds.concat(selectedChildIds);
}
return selectedNodeIds;
};
/**
* Gets ids of nodes selected in branch (of tree) specified by id (does not include the node specified by id)
* @name get_checked_descendants(obj)
* @param {string} id the node ID
* @return {Array} array of IDs
* @plugin checkbox
*/
this.get_checked_descendants = function (id) {
var self = this;
var t = self.settings.checkbox.tie_selection;
var node = self._model.data[id];
return $.vakata.array_filter(node.children_d, function(_id) {
return self._model.data[_id].state[ t ? 'selected' : 'checked' ];
});
};
/**
* check a node (only if tie_selection in checkbox settings is false, otherwise select_node will be called internally)
* @name check_node(obj)
* @param {mixed} obj an array can be used to check multiple nodes
* @trigger check_node.jstree
* @plugin checkbox
*/
this.check_node = function (obj, e) {
if(this.settings.checkbox.tie_selection) { return this.select_node(obj, false, true, e); }
var dom, t1, t2, th;
if($.vakata.is_array(obj)) {
obj = obj.slice();
for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
this.check_node(obj[t1], e);
}
return true;
}
obj = this.get_node(obj);
if(!obj || obj.id === $.jstree.root) {
return false;
}
dom = this.get_node(obj, true);
if(!obj.state.checked) {
obj.state.checked = true;
this._data.checkbox.selected.push(obj.id);
if(dom && dom.length) {
dom.children('.jstree-anchor').addClass('jstree-checked');
}
/**
* triggered when an node is checked (only if tie_selection in checkbox settings is false)
* @event
* @name check_node.jstree
* @param {Object} node
* @param {Array} selected the current selection
* @param {Object} event the event (if any) that triggered this check_node
* @plugin checkbox
*/
this.trigger('check_node', { 'node' : obj, 'selected' : this._data.checkbox.selected, 'event' : e });
}
};
/**
* uncheck a node (only if tie_selection in checkbox settings is false, otherwise deselect_node will be called internally)
* @name uncheck_node(obj)
* @param {mixed} obj an array can be used to uncheck multiple nodes
* @trigger uncheck_node.jstree
* @plugin checkbox
*/
this.uncheck_node = function (obj, e) {
if(this.settings.checkbox.tie_selection) { return this.deselect_node(obj, false, e); }
var t1, t2, dom;
if($.vakata.is_array(obj)) {
obj = obj.slice();
for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
this.uncheck_node(obj[t1], e);
}
return true;
}
obj = this.get_node(obj);
if(!obj || obj.id === $.jstree.root) {
return false;
}
dom = this.get_node(obj, true);
if(obj.state.checked) {
obj.state.checked = false;
this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, obj.id);
if(dom.length) {
dom.children('.jstree-anchor').removeClass('jstree-checked');
}
/**
* triggered when an node is unchecked (only if tie_selection in checkbox settings is false)
* @event
* @name uncheck_node.jstree
* @param {Object} node
* @param {Array} selected the current selection
* @param {Object} event the event (if any) that triggered this uncheck_node
* @plugin checkbox
*/
this.trigger('uncheck_node', { 'node' : obj, 'selected' : this._data.checkbox.selected, 'event' : e });
}
};
/**
* checks all nodes in the tree (only if tie_selection in checkbox settings is false, otherwise select_all will be called internally)
* @name check_all()
* @trigger check_all.jstree, changed.jstree
* @plugin checkbox
*/
this.check_all = function () {
if(this.settings.checkbox.tie_selection) { return this.select_all(); }
var tmp = this._data.checkbox.selected.concat([]), i, j;
this._data.checkbox.selected = this._model.data[$.jstree.root].children_d.concat();
for(i = 0, j = this._data.checkbox.selected.length; i < j; i++) {
if(this._model.data[this._data.checkbox.selected[i]]) {
this._model.data[this._data.checkbox.selected[i]].state.checked = true;
}
}
this.redraw(true);
/**
* triggered when all nodes are checked (only if tie_selection in checkbox settings is false)
* @event
* @name check_all.jstree
* @param {Array} selected the current selection
* @plugin checkbox
*/
this.trigger('check_all', { 'selected' : this._data.checkbox.selected });
};
/**
* uncheck all checked nodes (only if tie_selection in checkbox settings is false, otherwise deselect_all will be called internally)
* @name uncheck_all()
* @trigger uncheck_all.jstree
* @plugin checkbox
*/
this.uncheck_all = function () {
if(this.settings.checkbox.tie_selection) { return this.deselect_all(); }
var tmp = this._data.checkbox.selected.concat([]), i, j;
for(i = 0, j = this._data.checkbox.selected.length; i < j; i++) {
if(this._model.data[this._data.checkbox.selected[i]]) {
this._model.data[this._data.checkbox.selected[i]].state.checked = false;
}
}
this._data.checkbox.selected = [];
this.element.find('.jstree-checked').removeClass('jstree-checked');
/**
* triggered when all nodes are unchecked (only if tie_selection in checkbox settings is false)
* @event
* @name uncheck_all.jstree
* @param {Object} node the previous selection
* @param {Array} selected the current selection
* @plugin checkbox
*/
this.trigger('uncheck_all', { 'selected' : this._data.checkbox.selected, 'node' : tmp });
};
/**
* checks if a node is checked (if tie_selection is on in the settings this function will return the same as is_selected)
* @name is_checked(obj)
* @param {mixed} obj
* @return {Boolean}
* @plugin checkbox
*/
this.is_checked = function (obj) {
if(this.settings.checkbox.tie_selection) { return this.is_selected(obj); }
obj = this.get_node(obj);
if(!obj || obj.id === $.jstree.root) { return false; }
return obj.state.checked;
};
/**
* get an array of all checked nodes (if tie_selection is on in the settings this function will return the same as get_selected)
* @name get_checked([full])
* @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
* @return {Array}
* @plugin checkbox
*/
this.get_checked = function (full) {
if(this.settings.checkbox.tie_selection) { return this.get_selected(full); }
return full ? $.map(this._data.checkbox.selected, function (i) { return this.get_node(i); }.bind(this)) : this._data.checkbox.selected.slice();
};
/**
* get an array of all top level checked nodes (ignoring children of checked nodes) (if tie_selection is on in the settings this function will return the same as get_top_selected)
* @name get_top_checked([full])
* @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
* @return {Array}
* @plugin checkbox
*/
this.get_top_checked = function (full) {
if(this.settings.checkbox.tie_selection) { return this.get_top_selected(full); }
var tmp = this.get_checked(true),
obj = {}, i, j, k, l;
for(i = 0, j = tmp.length; i < j; i++) {
obj[tmp[i].id] = tmp[i];
}
for(i = 0, j = tmp.length; i < j; i++) {
for(k = 0, l = tmp[i].children_d.length; k < l; k++) {
if(obj[tmp[i].children_d[k]]) {
delete obj[tmp[i].children_d[k]];
}
}
}
tmp = [];
for(i in obj) {
if(obj.hasOwnProperty(i)) {
tmp.push(i);
}
}
return full ? $.map(tmp, function (i) { return this.get_node(i); }.bind(this)) : tmp;
};
/**
* get an array of all bottom level checked nodes (ignoring selected parents) (if tie_selection is on in the settings this function will return the same as get_bottom_selected)
* @name get_bottom_checked([full])
* @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
* @return {Array}
* @plugin checkbox
*/
this.get_bottom_checked = function (full) {
if(this.settings.checkbox.tie_selection) { return this.get_bottom_selected(full); }
var tmp = this.get_checked(true),
obj = [], i, j;
for(i = 0, j = tmp.length; i < j; i++) {
if(!tmp[i].children.length) {
obj.push(tmp[i].id);
}
}
return full ? $.map(obj, function (i) { return this.get_node(i); }.bind(this)) : obj;
};
this.load_node = function (obj, callback) {
var k, l, i, j, c, tmp;
if(!$.vakata.is_array(obj) && !this.settings.checkbox.tie_selection) {
tmp = this.get_node(obj);
if(tmp && tmp.state.loaded) {
for(k = 0, l = tmp.children_d.length; k < l; k++) {
if(this._model.data[tmp.children_d[k]].state.checked) {
c = true;
this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, tmp.children_d[k]);
}
}
}
}
return parent.load_node.apply(this, arguments);
};
this.get_state = function () {
var state = parent.get_state.apply(this, arguments);
if(this.settings.checkbox.tie_selection) { return state; }
state.checkbox = this._data.checkbox.selected.slice();
return state;
};
this.set_state = function (state, callback) {
var res = parent.set_state.apply(this, arguments);
if(res && state.checkbox) {
if(!this.settings.checkbox.tie_selection) {
this.uncheck_all();
var _this = this;
$.each(state.checkbox, function (i, v) {
_this.check_node(v);
});
}
delete state.checkbox;
this.set_state(state, callback);
return false;
}
return res;
};
this.refresh = function (skip_loading, forget_state) {
if(this.settings.checkbox.tie_selection) {
this._data.checkbox.selected = [];
}
return parent.refresh.apply(this, arguments);
};
};
// include the checkbox plugin by default
// $.jstree.defaults.plugins.push("checkbox");
}));