-
Notifications
You must be signed in to change notification settings - Fork 3
/
jquery.tbTree.js
166 lines (144 loc) · 5.5 KB
/
jquery.tbTree.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
(function($){
/* private fields */
var _depth = 0;
var _options = null;
var _isFirstRun = true;
var _self = null;
/* public functions */
getOptions = function(){
return _options;
};
getDepth = function(){
return _depth;
};
$.fn.extend({
tbTree: function(options) {
var defaults = {
truncate: 15,
treeLayout: {},
preToggle: function(e){},
postToggle: function(e){},
sortable: true,
};
_options = $.extend(defaults, options);
_self = this;
_self.getOptions = getOptions;
_self.getDepth = getDepth;
_init();
return this.each(function() {
var o = _options;
});
}
});
/* private functions */
function _init(){
_isFirstRun = true;
var $ul = $("<ul class='tbTree-connectable nav nav-list'></ul>");
var level = 0;//first level
_parseJson(_options.treeLayout, $ul, 0);
$(_self).append($ul);
_calculateDepth();
_bindEvents();
$(_self).find(".tbTree-toggle").click();
$(_self).find("li > .nav").hide();
_isFirstRun = false;
if(_options.sortable){
//apply drag and drop sorting
$( ".tbTree-connectable, .tbTree-connectable" ).sortable({
connectWith: ".tbTree-connectable"
}).disableSelection();
}
}
function _bindEvents(){
//toggle click event
$(_self).find(".tbTree-toggle").click(function(e){
_handleToggle(e, this);
});
}
function _handleToggle(e, elem){
var $elem = $(elem);
var level = $elem.data("level");
var label = $elem.data("label");
var childrenCount = $elem.data("childrenCount");
//handle pre toggle event
if (_options.preToggle !== undefined && !_isFirstRun) {
_options.preToggle(e, $elem, label, level, childrenCount);
}
//handle toggle
$elem.closest("li").children(".nav").toggle("fast");
if( $elem.find(".icon-plus").length){
$elem.find(".icon-plus").toggleClass("icon-plus icon-minus ");
}else{
$elem.find(".icon-minus").toggleClass("icon-minus icon-plus ");
}
//handle post toggle event
if (_options.postToggle !== undefined && !_isFirstRun) {
_options.postToggle(e, $elem, label, level, childrenCount);
}
}
function _parseJson(treeLayout, $ul, length){
$.each(treeLayout, function(i,d){
var $li = $("<li ></li>");
length = this.children.length;
if(this.children.length > 0){
$li.append("<a class='tbTree-toggle'"
+
"title='" + this.label + "'href='#'><i class='icon-minus '></i>"
+
trunc(this.label, _options.truncate, false)
+
" (" + length + ")</a>");
$li.children(".tbTree-toggle").attr("data-children-count", length);
$li.children(".tbTree-toggle").attr("data-label", this.label );
$li.children(".tbTree-toggle").attr("data-label-shortened", trunc(this.label, _options.truncate, false) );
var $subUl = $("<ul class='tbTree-connectable nav nav-list'></ul>");
$li.append($subUl);
_parseJson(this.children, $subUl, length);
}else{
if(d.count !== undefined){
$li.append("<a class='tbTree-toggle'"
+
"title='" + this.label + "'href='#'><i class='icon-blank '></i>"
+
trunc(this.label, _options.truncate, false)
+
" (" + d.count + ")</a>");
$li.children(".tbTree-toggle").attr("data-children-count", d.count);
$li.children(".tbTree-toggle").attr("data-label", this.label );
$li.children(".tbTree-toggle").attr("data-label-shortened", trunc(this.label, _options.truncate, false) );
}else{
$li.append("<a class='tbTree-toggle'"
+
"title='" + this.label + "'href='#'><i class='icon-blank '></i>"
+
trunc(this.label, _options.truncate, false)
+
" </a>");
$li.children(".tbTree-toggle").attr("data-children-count", 0);
$li.children(".tbTree-toggle").attr("data-label", this.label );
$li.children(".tbTree-toggle").attr("data-label-shortened", trunc(this.label, _options.truncate, false) );
}
}
$ul.append($li);
});
}
function _calculateDepth(){
var level = 0;
$(_self).find("ul").each(function() {
var depth = $(this).parents('ul').length;
$(this).children()
.children("a")
.addClass("tbTree-level-" + depth)
.attr("data-level", depth);
if(_depth < depth){
_depth = depth;
}
});
}
function trunc(str, n, useWordBoundary){
var toLong = str.length>n,
s_ = toLong ? str.substr(0,n-1) : str;
s_ = useWordBoundary && toLong ? s_.substr(0,s_.lastIndexOf(' ')) : s_;
return toLong ? s_ + '…' : s_;
}
})(jQuery);