forked from killme2008/node-zk-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.tree.js
2064 lines (1927 loc) · 86.4 KB
/
jquery.tree.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
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
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* jsTree 0.9.9a
* http://jstree.com/
*
* Copyright (c) 2009 Ivan Bozhanov (vakata.com)
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* Date: 2009-10-06
*
*/
(function($) {
// jQuery plugin
$.tree = {
datastores : { },
plugins : { },
defaults : {
data : {
async : false, // Are async requests used to load open_branch contents
type : "html", // One of included datastores
opts : { method: "GET", url: false } // Options passed to datastore
},
selected : false, // FALSE or STRING or ARRAY
opened : [], // ARRAY OF INITIALLY OPENED NODES
languages : [], // ARRAY of string values (which will be used as CSS classes - so they must be valid)
ui : {
dots : true, // BOOL - dots or no dots
animation : 0, // INT - duration of open/close animations in miliseconds
scroll_spd : 4,
theme_path : false, // Path to the theme CSS file - if set to false and theme_name is not false - will lookup jstree-path-here/themes/theme-name-here/style.css
theme_name : "default",// if set to false no theme will be loaded
selected_parent_close : "select_parent", // false, "deselect", "select_parent"
selected_delete : "select_previous" // false, "select_previous"
},
types : {
"default" : {
clickable : true, // can be function
renameable : true, // can be function
deletable : true, // can be function
creatable : true, // can be function
draggable : true, // can be function
max_children : -1, // -1 - not set, 0 - no children, 1 - one child, etc // can be function
max_depth : -1, // -1 - not set, 0 - no children, 1 - one level of children, etc // can be function
valid_children : "all", // all, none, array of values // can be function
icon : {
image : false,
position : false
}
}
},
rules : {
multiple : false, // FALSE | CTRL | ON - multiple selection off/ with or without holding Ctrl
multitree : "none", // all, none, array of tree IDs to accept from
type_attr : "rel", // STRING attribute name (where is the type stored as string)
createat : "bottom", // STRING (top or bottom) new nodes get inserted at top or bottom
drag_copy : "ctrl", // FALSE | CTRL | ON - drag to copy off/ with or without holding Ctrl
drag_button : "left", // left, right or both
use_max_children : true,
use_max_depth : true,
max_children: -1,
max_depth : -1,
valid_children : "all"
},
lang : {
new_node : "New folder",
loading : "Loading ..."
},
callback : {
beforechange: function(NODE,TREE_OBJ) { return true },
beforeopen : function(NODE,TREE_OBJ) { return true },
beforeclose : function(NODE,TREE_OBJ) { return true },
beforemove : function(NODE,REF_NODE,TYPE,TREE_OBJ) { return true },
beforecreate: function(NODE,REF_NODE,TYPE,TREE_OBJ) { return true },
beforerename: function(NODE,LANG,TREE_OBJ) { return true },
beforedelete: function(NODE,TREE_OBJ) { return true },
beforedata : function(NODE,TREE_OBJ) { return { id : $(NODE).attr("id") || 0 } }, // PARAMETERS PASSED TO SERVER
ondata : function(DATA,TREE_OBJ) { return DATA; }, // modify data before parsing it
onparse : function(STR,TREE_OBJ) { return STR; }, // modify string before visualizing it
onhover : function(NODE,TREE_OBJ) { }, // node hovered
onselect : function(NODE,TREE_OBJ) { }, // node selected
ondeselect : function(NODE,TREE_OBJ) { }, // node deselected
onchange : function(NODE,TREE_OBJ) { }, // focus changed
onrename : function(NODE,TREE_OBJ,RB) { }, // node renamed
onmove : function(NODE,REF_NODE,TYPE,TREE_OBJ,RB) { }, // move completed
oncopy : function(NODE,REF_NODE,TYPE,TREE_OBJ,RB) { }, // copy completed
oncreate : function(NODE,REF_NODE,TYPE,TREE_OBJ,RB) { }, // node created
ondelete : function(NODE,TREE_OBJ,RB) { }, // node deleted
onopen : function(NODE,TREE_OBJ) { }, // node opened
onopen_all : function(TREE_OBJ) { }, // all nodes opened
onclose_all : function(TREE_OBJ) { }, // all nodes closed
onclose : function(NODE,TREE_OBJ) { }, // node closed
error : function(TEXT,TREE_OBJ) { }, // error occured
ondblclk : function(NODE,TREE_OBJ) { TREE_OBJ.toggle_branch.call(TREE_OBJ, NODE); TREE_OBJ.select_branch.call(TREE_OBJ, NODE); },
onrgtclk : function(NODE,TREE_OBJ,EV) { }, // right click - to prevent use: EV.preventDefault(); EV.stopPropagation(); return false
onload : function(TREE_OBJ) { },
oninit : function(TREE_OBJ) { },
onfocus : function(TREE_OBJ) { },
ondestroy : function(TREE_OBJ) { },
onsearch : function(NODES, TREE_OBJ) { NODES.addClass("search"); },
ondrop : function(NODE,REF_NODE,TYPE,TREE_OBJ) { },
check : function(RULE,NODE,VALUE,TREE_OBJ) { return VALUE; },
check_move : function(NODE,REF_NODE,TYPE,TREE_OBJ) { return true; }
},
plugins : { }
},
create : function () { return new tree_component(); },
focused : function () { return tree_component.inst[tree_component.focused]; },
reference : function (obj) {
var o = $(obj);
if(!o.size()) o = $("#" + obj);
if(!o.size()) return null;
o = (o.is(".tree")) ? o.attr("id") : o.parents(".tree:eq(0)").attr("id");
return tree_component.inst[o] || null;
},
rollback : function (data) {
for(var i in data) {
if(!data.hasOwnProperty(i)) continue;
var tmp = tree_component.inst[i];
var lock = !tmp.locked;
// if not locked - lock the tree
if(lock) tmp.lock(true);
// Cancel ongoing rename
tmp.inp = false;
tmp.container.html(data[i].html).find(".dragged").removeClass("dragged").end().find(".hover").removeClass("hover");
if(data[i].selected) {
tmp.selected = $("#" + data[i].selected);
tmp.selected_arr = [];
tmp.container
.find("a.clicked").each( function () {
tmp.selected_arr.push(tmp.get_node(this));
});
}
// if this function set the lock - unlock
if(lock) tmp.lock(false);
delete lock;
delete tmp;
}
},
drop_mode : function (opts) {
opts = $.extend(opts, { show : false, type : "default", str : "Foreign node" });
tree_component.drag_drop.foreign = true;
tree_component.drag_drop.isdown = true;
tree_component.drag_drop.moving = true;
tree_component.drag_drop.appended = false;
tree_component.drag_drop.f_type = opts.type;
tree_component.drag_drop.f_data = opts;
if(!opts.show) {
tree_component.drag_drop.drag_help = false;
tree_component.drag_drop.drag_node = false;
}
else {
tree_component.drag_drop.drag_help = $("<div id='jstree-dragged' class='tree tree-default'><ul><li class='last dragged foreign'><a href='#'><ins> </ins>" + opts.str + "</a></li></ul></div>");
tree_component.drag_drop.drag_node = tree_component.drag_drop.drag_help.find("li:eq(0)");
}
if($.tree.drag_start !== false) $.tree.drag_start.call(null, false);
},
drag_start : false,
drag : false,
drag_end : false
};
$.fn.tree = function (opts) {
return this.each(function() {
var conf = $.extend({},opts);
if(tree_component.inst && tree_component.inst[$(this).attr('id')]) tree_component.inst[$(this).attr('id')].destroy();
if(conf !== false) new tree_component().init(this, conf);
});
};
// core
function tree_component () {
return {
cntr : ++tree_component.cntr,
settings : $.extend({},$.tree.defaults),
init : function(elem, conf) {
var _this = this;
this.container = $(elem);
if(this.container.size == 0) return false;
tree_component.inst[this.cntr] = this;
if(!this.container.attr("id")) this.container.attr("id","jstree_" + this.cntr);
tree_component.inst[this.container.attr("id")] = tree_component.inst[this.cntr];
tree_component.focused = this.cntr;
this.settings = $.extend(true, {}, this.settings, conf);
// DEAL WITH LANGUAGE VERSIONS
if(this.settings.languages && this.settings.languages.length) {
this.current_lang = this.settings.languages[0];
var st = false;
var id = "#" + this.container.attr("id");
for(var ln = 0; ln < this.settings.languages.length; ln++) {
st = tree_component.add_css(id + " ." + this.settings.languages[ln]);
if(st !== false) st.style.display = (this.settings.languages[ln] == this.current_lang) ? "" : "none";
}
}
else this.current_lang = false;
// THEMES
this.container.addClass("tree");
if(this.settings.ui.theme_name !== false) {
if(this.settings.ui.theme_path === false) {
$("script").each(function () {
if(this.src.toString().match(/jquery\.tree.*?js$/)) { _this.settings.ui.theme_path = this.src.toString().replace(/jquery\.tree.*?js$/, "") + "themes/" + _this.settings.ui.theme_name + "/style.css"; return false; }
});
}
if(this.settings.ui.theme_path != "" && $.inArray(this.settings.ui.theme_path, tree_component.themes) == -1) {
tree_component.add_sheet({ url : this.settings.ui.theme_path });
tree_component.themes.push(this.settings.ui.theme_path);
}
this.container.addClass("tree-" + this.settings.ui.theme_name);
}
// TYPE ICONS
var type_icons = "";
for(var t in this.settings.types) {
if(!this.settings.types.hasOwnProperty(t)) continue;
if(!this.settings.types[t].icon) continue;
if( this.settings.types[t].icon.image || this.settings.types[t].icon.position) {
if(t == "default") type_icons += "#" + this.container.attr("id") + " li > a ins { ";
else type_icons += "#" + this.container.attr("id") + " li[rel=" + t + "] > a ins { ";
if(this.settings.types[t].icon.image) type_icons += " background-image:url(" + this.settings.types[t].icon.image + "); ";
if(this.settings.types[t].icon.position) type_icons += " background-position:" + this.settings.types[t].icon.position + "; ";
type_icons += "} ";
}
}
if(type_icons != "") tree_component.add_sheet({ str : type_icons });
if(this.settings.rules.multiple) this.selected_arr = [];
this.offset = false;
this.hovered = false;
this.locked = false;
if(tree_component.drag_drop.marker === false) tree_component.drag_drop.marker = $("<div>").attr({ id : "jstree-marker" }).hide().appendTo("body");
this.callback("oninit", [this]);
this.refresh();
this.attach_events();
this.focus();
},
refresh : function (obj) {
if(this.locked) return this.error("LOCKED");
var _this = this;
if(obj && !this.settings.data.async) obj = false;
this.is_partial_refresh = obj ? true : false;
// SAVE OPENED
this.opened = Array();
if(this.settings.opened != false) {
$.each(this.settings.opened, function (i, item) {
if(this.replace(/^#/,"").length > 0) { _this.opened.push("#" + this.replace(/^#/,"")); }
});
this.settings.opened = false;
}
else {
this.container.find("li.open").each(function (i) { if(this.id) { _this.opened.push("#" + this.id); } });
}
// SAVE SELECTED
if(this.selected) {
this.settings.selected = Array();
if(obj) {
$(obj).find("li:has(a.clicked)").each(function () {
if(this.id) _this.settings.selected.push("#" + this.id);
});
}
else {
if(this.selected_arr) {
$.each(this.selected_arr, function () {
if(this.attr("id")) _this.settings.selected.push("#" + this.attr("id"));
});
}
else {
if(this.selected.attr("id")) this.settings.selected.push("#" + this.selected.attr("id"));
}
}
}
else if(this.settings.selected !== false) {
var tmp = Array();
if((typeof this.settings.selected).toLowerCase() == "object") {
$.each(this.settings.selected, function () {
if(this.replace(/^#/,"").length > 0) tmp.push("#" + this.replace(/^#/,""));
});
}
else {
if(this.settings.selected.replace(/^#/,"").length > 0) tmp.push("#" + this.settings.selected.replace(/^#/,""));
}
this.settings.selected = tmp;
}
if(obj && this.settings.data.async) {
this.opened = Array();
obj = this.get_node(obj);
obj.find("li.open").each(function (i) { _this.opened.push("#" + this.id); });
if(obj.hasClass("open")) obj.removeClass("open").addClass("closed");
if(obj.hasClass("leaf")) obj.removeClass("leaf");
obj.children("ul:eq(0)").html("");
return this.open_branch(obj, true, function () { _this.reselect.apply(_this); });
}
var _this = this;
var _datastore = new $.tree.datastores[this.settings.data.type]();
if(this.container.children("ul").size() == 0) {
this.container.html("<ul class='ltr' style='direction:ltr;'><li class='last'><a class='loading' href='#'><ins> </ins>" + (this.settings.lang.loading || "Loading ...") + "</a></li></ul>");
}
_datastore.load(this.callback("beforedata",[false,this]),this,this.settings.data.opts,function(data) {
data = _this.callback("ondata",[data, _this]);
_datastore.parse(data,_this,_this.settings.data.opts,function(str) {
str = _this.callback("onparse", [str, _this]);
_this.container.empty().append($("<ul class='ltr'>").html(str));
_this.container.find("li:last-child").addClass("last").end().find("li:has(ul)").not(".open").addClass("closed");
_this.container.find("li").not(".open").not(".closed").addClass("leaf");
_this.reselect();
});
});
},
reselect : function (is_callback) {
var _this = this;
if(!is_callback) this.cl_count = 0;
else this.cl_count --;
// REOPEN BRANCHES
if(this.opened && this.opened.length) {
var opn = false;
for(var j = 0; this.opened && j < this.opened.length; j++) {
if(this.settings.data.async) {
var tmp = this.get_node(this.opened[j]);
if(tmp.size() && tmp.hasClass("closed") > 0) {
opn = true;
var tmp = this.opened[j].toString().replace('/','\\/');
delete this.opened[j];
this.open_branch(tmp, true, function () { _this.reselect.apply(_this, [true]); } );
this.cl_count ++;
}
}
else this.open_branch(this.opened[j], true);
}
if(this.settings.data.async && opn) return;
if(this.cl_count > 0) return;
delete this.opened;
}
if(this.cl_count > 0) return;
// DOTS and RIGHT TO LEFT
this.container.css("direction","ltr").children("ul:eq(0)").addClass("ltr");
if(this.settings.ui.dots == false) this.container.children("ul:eq(0)").addClass("no_dots");
// REPOSITION SCROLL
if(this.scrtop) {
this.container.scrollTop(_this.scrtop);
delete this.scrtop;
}
// RESELECT PREVIOUSLY SELECTED
if(this.settings.selected !== false) {
$.each(this.settings.selected, function (i) {
if(_this.is_partial_refresh) _this.select_branch($(_this.settings.selected[i].toString().replace('/','\\/'), _this.container), (_this.settings.rules.multiple !== false) );
else _this.select_branch($(_this.settings.selected[i].toString().replace('/','\\/'), _this.container), (_this.settings.rules.multiple !== false && i > 0) );
});
this.settings.selected = false;
}
this.callback("onload", [_this]);
},
get : function (obj, format, opts) {
if(!format) format = this.settings.data.type;
if(!opts) opts = this.settings.data.opts;
return new $.tree.datastores[format]().get(obj, this, opts);
},
attach_events : function () {
var _this = this;
this.container
.bind("mousedown.jstree", function (event) {
if(tree_component.drag_drop.isdown) {
tree_component.drag_drop.move_type = false;
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
return false;
}
})
.bind("mouseup.jstree", function (event) {
setTimeout( function() { _this.focus.apply(_this); }, 5);
})
.bind("click.jstree", function (event) {
//event.stopPropagation();
return true;
});
$("li", this.container.get(0))
.live("click", function(event) { // WHEN CLICK IS ON THE ARROW
if(event.target.tagName != "LI") return true;
_this.off_height();
if(event.pageY - $(event.target).offset().top > _this.li_height) return true;
_this.toggle_branch.apply(_this, [event.target]);
event.stopPropagation();
return false;
});
$("a", this.container.get(0))
.live("click", function (event) { // WHEN CLICK IS ON THE TEXT OR ICON
if(event.which && event.which == 3) return true;
if(_this.locked) {
event.preventDefault();
event.target.blur();
return _this.error("LOCKED");
}
_this.select_branch.apply(_this, [event.target, event.ctrlKey || _this.settings.rules.multiple == "on"]);
if(_this.inp) { _this.inp.blur(); }
event.preventDefault();
event.target.blur();
return false;
})
.live("dblclick", function (event) { // WHEN DOUBLECLICK ON TEXT OR ICON
if(_this.locked) {
event.preventDefault();
event.stopPropagation();
event.target.blur();
return _this.error("LOCKED");
}
_this.callback("ondblclk", [_this.get_node(event.target).get(0), _this]);
event.preventDefault();
event.stopPropagation();
event.target.blur();
})
.live("contextmenu", function (event) {
if(_this.locked) {
event.target.blur();
return _this.error("LOCKED");
}
return _this.callback("onrgtclk", [_this.get_node(event.target).get(0), _this, event]);
})
.live("mouseover", function (event) {
if(_this.locked) {
event.preventDefault();
event.stopPropagation();
return _this.error("LOCKED");
}
if(_this.hovered !== false && (event.target.tagName == "A" || event.target.tagName == "INS")) {
_this.hovered.children("a").removeClass("hover");
_this.hovered = false;
}
_this.callback("onhover",[_this.get_node(event.target).get(0), _this]);
})
.live("mousedown", function (event) {
if(_this.settings.rules.drag_button == "left" && event.which && event.which != 1) return true;
if(_this.settings.rules.drag_button == "right" && event.which && event.which != 3) return true;
_this.focus.apply(_this);
if(_this.locked) return _this.error("LOCKED");
// SELECT LIST ITEM NODE
var obj = _this.get_node(event.target);
// IF ITEM IS DRAGGABLE
if(_this.settings.rules.multiple != false && _this.selected_arr.length > 1 && obj.children("a:eq(0)").hasClass("clicked")) {
var counter = 0;
for(var i in _this.selected_arr) {
if(!_this.selected_arr.hasOwnProperty(i)) continue;
if(_this.check("draggable", _this.selected_arr[i])) {
_this.selected_arr[i].addClass("dragged");
tree_component.drag_drop.origin_tree = _this;
counter ++;
}
}
if(counter > 0) {
if(_this.check("draggable", obj)) tree_component.drag_drop.drag_node = obj;
else tree_component.drag_drop.drag_node = _this.container.find("li.dragged:eq(0)");
tree_component.drag_drop.isdown = true;
tree_component.drag_drop.drag_help = $("<div id='jstree-dragged' class='tree " + ( _this.settings.ui.theme_name != "" ? " tree-" + _this.settings.ui.theme_name : "" ) + "' />").append("<ul class='" + _this.container.children("ul:eq(0)").get(0).className + "' />");
var tmp = tree_component.drag_drop.drag_node.clone();
if(_this.settings.languages.length > 0) tmp.find("a").not("." + _this.current_lang).hide();
tree_component.drag_drop.drag_help.children("ul:eq(0)").append(tmp);
tree_component.drag_drop.drag_help.find("li:eq(0)").removeClass("last").addClass("last").children("a").html("<ins> </ins>Multiple selection").end().children("ul").remove();
tree_component.drag_drop.dragged = _this.container.find("li.dragged");
}
}
else {
if(_this.check("draggable", obj)) {
tree_component.drag_drop.drag_node = obj;
tree_component.drag_drop.drag_help = $("<div id='jstree-dragged' class='tree " + ( _this.settings.ui.theme_name != "" ? " tree-" + _this.settings.ui.theme_name : "" ) + "' />").append("<ul class='" + _this.container.children("ul:eq(0)").get(0).className + "' />");
var tmp = obj.clone();
if(_this.settings.languages.length > 0) tmp.find("a").not("." + _this.current_lang).hide();
tree_component.drag_drop.drag_help.children("ul:eq(0)").append(tmp);
tree_component.drag_drop.drag_help.find("li:eq(0)").removeClass("last").addClass("last");
tree_component.drag_drop.isdown = true;
tree_component.drag_drop.foreign = false;
tree_component.drag_drop.origin_tree = _this;
obj.addClass("dragged");
tree_component.drag_drop.dragged = _this.container.find("li.dragged");
}
}
tree_component.drag_drop.init_x = event.pageX;
tree_component.drag_drop.init_y = event.pageY;
obj.blur();
event.preventDefault();
event.stopPropagation();
return false;
});
},
focus : function () {
if(this.locked) return false;
if(tree_component.focused != this.cntr) {
tree_component.focused = this.cntr;
this.callback("onfocus",[this]);
}
},
off_height : function () {
if(this.offset === false) {
this.container.css({ position : "relative" });
this.offset = this.container.offset();
var tmp = 0;
tmp = parseInt($.curCSS(this.container.get(0), "paddingTop", true),10);
if(tmp) this.offset.top += tmp;
tmp = parseInt($.curCSS(this.container.get(0), "borderTopWidth", true),10);
if(tmp) this.offset.top += tmp;
this.container.css({ position : "" });
}
if(!this.li_height) {
var tmp = this.container.find("ul li.closed, ul li.leaf").eq(0);
this.li_height = tmp.height();
if(tmp.children("ul:eq(0)").size()) this.li_height -= tmp.children("ul:eq(0)").height();
if(!this.li_height) this.li_height = 18;
}
},
scroll_check : function (x,y) {
var _this = this;
var cnt = _this.container;
var off = _this.container.offset();
var st = cnt.scrollTop();
var sl = cnt.scrollLeft();
// DETECT HORIZONTAL SCROLL
var h_cor = (cnt.get(0).scrollWidth > cnt.width()) ? 40 : 20;
if(y - off.top < 20) cnt.scrollTop(Math.max( (st - _this.settings.ui.scroll_spd) ,0)); // NEAR TOP
if(cnt.height() - (y - off.top) < h_cor) cnt.scrollTop(st + _this.settings.ui.scroll_spd); // NEAR BOTTOM
if(x - off.left < 20) cnt.scrollLeft(Math.max( (sl - _this.settings.ui.scroll_spd),0)); // NEAR LEFT
if(cnt.width() - (x - off.left) < 40) cnt.scrollLeft(sl + _this.settings.ui.scroll_spd); // NEAR RIGHT
if(cnt.scrollLeft() != sl || cnt.scrollTop() != st) {
tree_component.drag_drop.move_type = false;
tree_component.drag_drop.ref_node = false;
tree_component.drag_drop.marker.hide();
}
tree_component.drag_drop.scroll_time = setTimeout( function() { _this.scroll_check(x,y); }, 50);
},
scroll_into_view : function (obj) {
obj = obj ? this.get_node(obj) : this.selected;
if(!obj) return false;
var off_t = obj.offset().top;
var beg_t = this.container.offset().top;
var end_t = beg_t + this.container.height();
var h_cor = (this.container.get(0).scrollWidth > this.container.width()) ? 40 : 20;
if(off_t + 5 < beg_t) this.container.scrollTop(this.container.scrollTop() - (beg_t - off_t + 5) );
if(off_t + h_cor > end_t) this.container.scrollTop(this.container.scrollTop() + (off_t + h_cor - end_t) );
},
get_node : function (obj) {
return $(obj).closest("li");
},
get_type : function (obj) {
obj = !obj ? this.selected : this.get_node(obj);
if(!obj) return;
var tmp = obj.attr(this.settings.rules.type_attr);
return tmp || "default";
},
set_type : function (str, obj) {
obj = !obj ? this.selected : this.get_node(obj);
if(!obj || !str) return;
obj.attr(this.settings.rules.type_attr, str);
},
get_text : function (obj, lang) {
obj = this.get_node(obj);
if(!obj || obj.size() == 0) return "";
if(this.settings.languages && this.settings.languages.length) {
lang = lang ? lang : this.current_lang;
obj = obj.children("a." + lang);
}
else obj = obj.children("a:visible");
var val = "";
obj.contents().each(function () {
if(this.nodeType == 3) { val = this.data; return false; }
});
return val;
},
check : function (rule, obj) {
if(this.locked) return false;
var v = false;
// if root node
if(obj === -1) { if(typeof this.settings.rules[rule] != "undefined") v = this.settings.rules[rule]; }
else {
obj = !obj ? this.selected : this.get_node(obj);
if(!obj) return;
var t = this.get_type(obj);
if(typeof this.settings.types[t] != "undefined" && typeof this.settings.types[t][rule] != "undefined") v = this.settings.types[t][rule];
else if(typeof this.settings.types["default"] != "undefined" && typeof this.settings.types["default"][rule] != "undefined") v = this.settings.types["default"][rule];
}
if(typeof v == "function") v = v.call(null, obj, this);
v = this.callback("check", [rule, obj, v, this]);
return v;
},
check_move : function (nod, ref_node, how) {
if(this.locked) return false;
if($(ref_node).closest("li.dragged").size()) return false;
var tree1 = nod.parents(".tree:eq(0)").get(0);
var tree2 = ref_node.parents(".tree:eq(0)").get(0);
// if different trees
if(tree1 && tree1 != tree2) {
var m = $.tree.reference(tree2.id).settings.rules.multitree;
if(m == "none" || ($.isArray(m) && $.inArray(tree1.id, m) == -1)) return false;
}
var p = (how != "inside") ? this.parent(ref_node) : this.get_node(ref_node);
nod = this.get_node(nod);
if(p == false) return false;
var r = {
max_depth : this.settings.rules.use_max_depth ? this.check("max_depth", p) : -1,
max_children : this.settings.rules.use_max_children ? this.check("max_children", p) : -1,
valid_children : this.check("valid_children", p)
};
var nod_type = (typeof nod == "string") ? nod : this.get_type(nod);
if(typeof r.valid_children != "undefined" && (r.valid_children == "none" || (typeof r.valid_children == "object" && $.inArray(nod_type, $.makeArray(r.valid_children)) == -1))) return false;
if(this.settings.rules.use_max_children) {
if(typeof r.max_children != "undefined" && r.max_children != -1) {
if(r.max_children == 0) return false;
var c_count = 1;
if(tree_component.drag_drop.moving == true && tree_component.drag_drop.foreign == false) {
c_count = tree_component.drag_drop.dragged.size();
c_count = c_count - p.find('> ul > li.dragged').size();
}
if(r.max_children < p.find('> ul > li').size() + c_count) return false;
}
}
if(this.settings.rules.use_max_depth) {
if(typeof r.max_depth != "undefined" && r.max_depth === 0) return this.error("MOVE: MAX-DEPTH REACHED");
// check for max_depth up the chain
var mx = (r.max_depth > 0) ? r.max_depth : false;
var i = 0;
var t = p;
while(t !== -1) {
t = this.parent(t);
i ++;
var m = this.check("max_depth",t);
if(m >= 0) {
mx = (mx === false) ? (m - i) : Math.min(mx, m - i);
}
if(mx !== false && mx <= 0) return this.error("MOVE: MAX-DEPTH REACHED");
}
if(mx !== false && mx <= 0) return this.error("MOVE: MAX-DEPTH REACHED");
if(mx !== false) {
var incr = 1;
if(typeof nod != "string") {
var t = nod;
// possible async problem - when nodes are not all loaded down the chain
while(t.size() > 0) {
if(mx - incr < 0) return this.error("MOVE: MAX-DEPTH REACHED");
t = t.children("ul").children("li");
incr ++;
}
}
}
}
if(this.callback("check_move", [nod, ref_node, how, this]) == false) return false;
return true;
},
hover_branch : function (obj) {
if(this.locked) return this.error("LOCKED");
var _this = this;
var obj = _this.get_node(obj);
if(!obj.size()) return this.error("HOVER: NOT A VALID NODE");
if(!_this.check("clickable", obj)) return this.error("SELECT: NODE NOT SELECTABLE");
if(this.hovered) this.hovered.children("A").removeClass("hover");
this.hovered = obj;
this.hovered.children("a").addClass("hover");
this.scroll_into_view(this.hovered);
},
select_branch : function (obj, multiple) {
if(this.locked) return this.error("LOCKED");
if(!obj && this.hovered !== false) obj = this.hovered;
var _this = this;
obj = _this.get_node(obj);
if(!obj.size()) return this.error("SELECT: NOT A VALID NODE");
obj.children("a").removeClass("hover");
// CHECK AGAINST RULES FOR SELECTABLE NODES
if(!_this.check("clickable", obj)) return this.error("SELECT: NODE NOT SELECTABLE");
if(_this.callback("beforechange",[obj.get(0),_this]) === false) return this.error("SELECT: STOPPED BY USER");
// IF multiple AND obj IS ALREADY SELECTED - DESELECT IT
if(this.settings.rules.multiple != false && multiple && obj.children("a.clicked").size() > 0) {
return this.deselect_branch(obj);
}
if(this.settings.rules.multiple != false && multiple) {
this.selected_arr.push(obj);
}
if(this.settings.rules.multiple != false && !multiple) {
for(var i in this.selected_arr) {
if(!this.selected_arr.hasOwnProperty(i)) continue;
this.selected_arr[i].children("A").removeClass("clicked");
this.callback("ondeselect", [this.selected_arr[i].get(0), _this]);
}
this.selected_arr = [];
this.selected_arr.push(obj);
if(this.selected && this.selected.children("A").hasClass("clicked")) {
this.selected.children("A").removeClass("clicked");
this.callback("ondeselect", [this.selected.get(0), _this]);
}
}
if(!this.settings.rules.multiple) {
if(this.selected) {
this.selected.children("A").removeClass("clicked");
this.callback("ondeselect", [this.selected.get(0), _this]);
}
}
// SAVE NEWLY SELECTED
this.selected = obj;
if(this.hovered !== false) {
this.hovered.children("A").removeClass("hover");
this.hovered = obj;
}
// FOCUS NEW NODE AND OPEN ALL PARENT NODES IF CLOSED
this.selected.children("a").addClass("clicked").end().parents("li.closed").each( function () { _this.open_branch(this, true); });
// SCROLL SELECTED NODE INTO VIEW
this.scroll_into_view(this.selected);
this.callback("onselect", [this.selected.get(0), _this]);
this.callback("onchange", [this.selected.get(0), _this]);
},
deselect_branch : function (obj) {
if(this.locked) return this.error("LOCKED");
var _this = this;
var obj = this.get_node(obj);
if(obj.children("a.clicked").size() == 0) return this.error("DESELECT: NODE NOT SELECTED");
obj.children("a").removeClass("clicked");
this.callback("ondeselect", [obj.get(0), _this]);
if(this.settings.rules.multiple != false && this.selected_arr.length > 1) {
this.selected_arr = [];
this.container.find("a.clicked").filter(":first-child").parent().each(function () {
_this.selected_arr.push($(this));
});
if(obj.get(0) == this.selected.get(0)) {
this.selected = this.selected_arr[0];
}
}
else {
if(this.settings.rules.multiple != false) this.selected_arr = [];
this.selected = false;
}
this.callback("onchange", [obj.get(0), _this]);
},
toggle_branch : function (obj) {
if(this.locked) return this.error("LOCKED");
var obj = this.get_node(obj);
if(obj.hasClass("closed")) return this.open_branch(obj);
if(obj.hasClass("open")) return this.close_branch(obj);
},
open_branch : function (obj, disable_animation, callback) {
var _this = this;
if(this.locked) return this.error("LOCKED");
var obj = this.get_node(obj);
if(!obj.size()) return this.error("OPEN: NO SUCH NODE");
if(obj.hasClass("leaf")) return this.error("OPEN: OPENING LEAF NODE");
if(this.settings.data.async && obj.find("li").size() == 0) {
if(this.callback("beforeopen",[obj.get(0),this]) === false) return this.error("OPEN: STOPPED BY USER");
obj.children("ul:eq(0)").remove().end().append("<ul><li class='last'><a class='loading' href='#'><ins> </ins>" + (_this.settings.lang.loading || "Loading ...") + "</a></li></ul>");
obj.removeClass("closed").addClass("open");
var _datastore = new $.tree.datastores[this.settings.data.type]();
_datastore.load(this.callback("beforedata",[obj,this]),this,this.settings.data.opts,function(data){
data = _this.callback("ondata", [data, _this]);
if(!data || data.length == 0) {
obj.removeClass("closed").removeClass("open").addClass("leaf").children("ul").remove();
if(callback) callback.call();
return;
}
_datastore.parse(data,_this,_this.settings.data.opts,function(str){
str = _this.callback("onparse", [str, _this]);
// if(obj.children('ul:eq(0)').children('li').size() > 1) obj.children("ul").find('.loaading').parent().replaceWith(str); else
obj.children("ul:eq(0)").replaceWith($("<ul>").html(str));
obj.find("li:last-child").addClass("last").end().find("li:has(ul)").not(".open").addClass("closed");
obj.find("li").not(".open").not(".closed").addClass("leaf");
_this.open_branch.apply(_this, [obj]);
if(callback) callback.call();
});
});
return true;
}
else {
if(!this.settings.data.async) {
if(this.callback("beforeopen",[obj.get(0),this]) === false) return this.error("OPEN: STOPPED BY USER");
}
if(parseInt(this.settings.ui.animation) > 0 && !disable_animation ) {
obj.children("ul:eq(0)").css("display","none");
obj.removeClass("closed").addClass("open");
obj.children("ul:eq(0)").slideDown(parseInt(this.settings.ui.animation), function() {
$(this).css("display","");
if(callback) callback.call();
});
} else {
obj.removeClass("closed").addClass("open");
if(callback) callback.call();
}
this.callback("onopen", [obj.get(0), this]);
return true;
}
},
close_branch : function (obj, disable_animation) {
if(this.locked) return this.error("LOCKED");
var _this = this;
var obj = this.get_node(obj);
if(!obj.size()) return this.error("CLOSE: NO SUCH NODE");
if(_this.callback("beforeclose",[obj.get(0),_this]) === false) return this.error("CLOSE: STOPPED BY USER");
if(parseInt(this.settings.ui.animation) > 0 && !disable_animation && obj.children("ul:eq(0)").size() == 1) {
obj.children("ul:eq(0)").slideUp(parseInt(this.settings.ui.animation), function() {
if(obj.hasClass("open")) obj.removeClass("open").addClass("closed");
$(this).css("display","");
});
}
else {
if(obj.hasClass("open")) obj.removeClass("open").addClass("closed");
}
if(this.selected && this.settings.ui.selected_parent_close !== false && obj.children("ul:eq(0)").find("a.clicked").size() > 0) {
obj.find("li:has(a.clicked)").each(function() {
_this.deselect_branch(this);
});
if(this.settings.ui.selected_parent_close == "select_parent" && obj.children("a.clicked").size() == 0) this.select_branch(obj, (this.settings.rules.multiple != false && this.selected_arr.length > 0) );
}
this.callback("onclose", [obj.get(0), this]);
},
open_all : function (obj, callback) {
if(this.locked) return this.error("LOCKED");
var _this = this;
obj = obj ? this.get_node(obj) : this.container;
var s = obj.find("li.closed").size();
if(!callback) this.cl_count = 0;
else this.cl_count --;
if(s > 0) {
this.cl_count += s;
// maybe add .andSelf()
obj.find("li.closed").each( function () { var __this = this; _this.open_branch.apply(_this, [this, true, function() { _this.open_all.apply(_this, [__this, true]); } ]); });
}
else if(this.cl_count == 0) this.callback("onopen_all",[this]);
},
close_all : function (obj) {
if(this.locked) return this.error("LOCKED");
var _this = this;
obj = obj ? this.get_node(obj) : this.container;
// maybe add .andSelf()
obj.find("li.open").each( function () { _this.close_branch(this, true); });
this.callback("onclose_all",[this]);
},
set_lang : function (i) {
if(!$.isArray(this.settings.languages) || this.settings.languages.length == 0) return false;
if(this.locked) return this.error("LOCKED");
if(!$.inArray(i,this.settings.languages) && typeof this.settings.languages[i] != "undefined") i = this.settings.languages[i];
if(typeof i == "undefined") return false;
if(i == this.current_lang) return true;
var st = false;
var id = "#" + this.container.attr("id");
st = tree_component.get_css(id + " ." + this.current_lang);
if(st !== false) st.style.display = "none";
st = tree_component.get_css(id + " ." + i);
if(st !== false) st.style.display = "";
this.current_lang = i;
return true;
},
get_lang : function () {
if(!$.isArray(this.settings.languages) || this.settings.languages.length == 0) return false;
return this.current_lang;
},
create : function (obj, ref_node, position) {
if(this.locked) return this.error("LOCKED");
var root = false;
if(ref_node == -1) { root = true; ref_node = this.container; }
else ref_node = ref_node ? this.get_node(ref_node) : this.selected;
if(!root && (!ref_node || !ref_node.size())) return this.error("CREATE: NO NODE SELECTED");
var pos = position;
var tmp = ref_node; // for type calculation
if(position == "before") {
position = ref_node.parent().children().index(ref_node);
ref_node = ref_node.parents("li:eq(0)");
}
if(position == "after") {
position = ref_node.parent().children().index(ref_node) + 1;
ref_node = ref_node.parents("li:eq(0)");
}
if(!root && ref_node.size() == 0) { root = true; ref_node = this.container; }
if(!root) {
if(!this.check("creatable", ref_node)) return this.error("CREATE: CANNOT CREATE IN NODE");
if(ref_node.hasClass("closed")) {
if(this.settings.data.async && ref_node.children("ul").size() == 0) {
var _this = this;
return this.open_branch(ref_node, true, function () { _this.create.apply(_this, [obj, ref_node, position]); } );
}
else this.open_branch(ref_node, true);
}
}
// creating new object to pass to parseJSON
var torename = false;
if(!obj) obj = {};
else obj = $.extend(true, {}, obj);
if(!obj.attributes) obj.attributes = {};
if(!obj.attributes[this.settings.rules.type_attr]) obj.attributes[this.settings.rules.type_attr] = this.get_type(tmp) || "default";
if(this.settings.languages.length) {
if(!obj.data) { obj.data = {}; torename = true; }
for(var i = 0; i < this.settings.languages.length; i++) {
if(!obj.data[this.settings.languages[i]]) obj.data[this.settings.languages[i]] = ((typeof this.settings.lang.new_node).toLowerCase() != "string" && this.settings.lang.new_node[i]) ? this.settings.lang.new_node[i] : this.settings.lang.new_node;
}
}
else {
if(!obj.data) { obj.data = this.settings.lang.new_node; torename = true; }
}
obj = this.callback("ondata",[obj, this]);
var obj_s = $.tree.datastores.json().parse(obj,this);
obj_s = this.callback("onparse", [obj_s, this]);
var $li = $(obj_s);
if($li.children("ul").size()) {
if(!$li.is(".open")) $li.addClass("closed");
}
else $li.addClass("leaf");
$li.find("li:last-child").addClass("last").end().find("li:has(ul)").not(".open").addClass("closed");
$li.find("li").not(".open").not(".closed").addClass("leaf");
var r = {
max_depth : this.settings.rules.use_max_depth ? this.check("max_depth", (root ? -1 : ref_node) ) : -1,
max_children : this.settings.rules.use_max_children ? this.check("max_children", (root ? -1 : ref_node) ) : -1,
valid_children : this.check("valid_children", (root ? -1 : ref_node) )
};
var nod_type = this.get_type($li);
if(typeof r.valid_children != "undefined" && (r.valid_children == "none" || ($.isArray(r.valid_children) && $.inArray(nod_type, r.valid_children) == -1))) return this.error("CREATE: NODE NOT A VALID CHILD");
if(this.settings.rules.use_max_children) {
if(typeof r.max_children != "undefined" && r.max_children != -1 && r.max_children >= this.children(ref_node).size()) return this.error("CREATE: MAX_CHILDREN REACHED");
}
if(this.settings.rules.use_max_depth) {
if(typeof r.max_depth != "undefined" && r.max_depth === 0) return this.error("CREATE: MAX-DEPTH REACHED");
// check for max_depth up the chain
var mx = (r.max_depth > 0) ? r.max_depth : false;
var i = 0;
var t = ref_node;
while(t !== -1 && !root) {
t = this.parent(t);
i ++;
var m = this.check("max_depth",t);
if(m >= 0) {
mx = (mx === false) ? (m - i) : Math.min(mx, m - i);
}
if(mx !== false && mx <= 0) return this.error("CREATE: MAX-DEPTH REACHED");
}
if(mx !== false && mx <= 0) return this.error("CREATE: MAX-DEPTH REACHED");
if(mx !== false) {
var incr = 1;
var t = $li;
while(t.size() > 0) {
if(mx - incr < 0) return this.error("CREATE: MAX-DEPTH REACHED");
t = t.children("ul").children("li");
incr ++;
}
}
}
if((typeof position).toLowerCase() == "undefined" || position == "inside")
position = (this.settings.rules.createat == "top") ? 0 : ref_node.children("ul:eq(0)").children("li").size();
if(ref_node.children("ul").size() == 0 || (root == true && ref_node.children("ul").children("li").size() == 0) ) {
if(!root) var a = this.moved($li,ref_node.children("a:eq(0)"),"inside", true);
else var a = this.moved($li,this.container.children("ul:eq(0)"),"inside", true);
}
else if(pos == "before" && ref_node.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").size())
var a = this.moved($li,ref_node.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").children("a:eq(0)"),"before", true);
else if(pos == "after" && ref_node.children("ul:eq(0)").children("li:nth-child(" + (position) + ")").size())
var a = this.moved($li,ref_node.children("ul:eq(0)").children("li:nth-child(" + (position) + ")").children("a:eq(0)"),"after", true);
else if(ref_node.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").size())
var a = this.moved($li,ref_node.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").children("a:eq(0)"),"before", true);