Skip to content

Commit fc37c23

Browse files
Merge branch 'master' into gh-pages
2 parents e49237f + d4615ff commit fc37c23

18 files changed

+1852
-267
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
21
angular-bootstrap-nav-tree
32
==========================
43

54
This is a Tree directive for Angular JS apps that use Bootstrap CSS.
65

7-
example: http://nickperkinslondon.github.io/angular-bootstrap-nav-tree/test/bs2_test_page.html
6+
example: http://nickperkinslondon.github.io/angular-bootstrap-nav-tree/test/bs2_ng115_test_page.html
87

98
The style is completely Bootstrap because the tree is actually just a Bootstrap "nav" list, with a few changes: Indentation is added, expand/collapse icons are added, and Angular CSS animations are used during expand/collapse.
109

@@ -40,7 +39,7 @@ But there are other attributes to customize the tree:
4039
tree-data = "example_treedata"
4140
icon-leaf = "icon-file"
4241
icon-expand = "icon-plus-sign"
43-
icon-contract = "icon-minus-sign"
42+
icon-collapse = "icon-minus-sign"
4443
on-select = "my_tree_handler(branch)"
4544
expand-level = "2"
4645
initial-selection = "Vegetable">

README.md~

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
angular-bootstrap-nav-tree
3+
==========================
4+
5+
This is a Tree directive for Angular JS apps that use Bootstrap CSS.
6+
7+
example: http://nickperkinslondon.github.io/angular-bootstrap-nav-tree/test/bs2_test_page.html
8+
9+
The style is completely Bootstrap because the tree is actually just a Bootstrap "nav" list, with a few changes: Indentation is added, expand/collapse icons are added, and Angular CSS animations are used during expand/collapse.
10+
11+
The abn-tree now works Bootsrap 2, or Bootstrap 3, and with Angular 1.1.5 or 1.2.0
12+
13+
The normal Glyphicons work well, but they appear black instead of blue. Alternatively, you can use the Font Awesome icons, which look even better, and match the blue color of the text.
14+
15+
You can change the icons used by specifying them in html attributes.
16+
17+
This tree is developed using CoffeeScript and Jade, but you don't need to be using either of those to use this tree -- you just have to be using Angular and Bootsrap.
18+
19+
20+
How to use it:
21+
Just include the 3 files from "dist":
22+
23+
abn_tree_directive.js
24+
abn_tree_template.html
25+
abn_tree.css
26+
27+
NOTE: you will have to adjust the "templateUrl" in the directive js, to adjust for your directory structure.
28+
29+
30+
Then put an `<abn-tree>` directive in your HTML.
31+
( see the example in "test" )
32+
33+
At a miniumum, you must supply `tree-data` :
34+
35+
<abn-tree tree-data="example_treedata"></abn-tree>
36+
37+
But there are other attributes to customize the tree:
38+
39+
<abn-tree
40+
tree-data = "example_treedata"
41+
icon-leaf = "icon-file"
42+
icon-expand = "icon-plus-sign"
43+
icon-contract = "icon-minus-sign"
44+
on-select = "my_tree_handler(branch)"
45+
expand-level = "2"
46+
initial-selection = "Vegetable">
47+
></abn-tree>
48+
49+
50+
The data to create the tree is defined in your controller, and could be as simple as this:
51+
52+
$scope.my_data = [{
53+
label: 'Languages',
54+
children: ['Jade','Less','Coffeescript']
55+
}]
56+
57+
There is a long-form for elements, in which each node is an object with a "label", and optionally other stuff like "data", and "children".
58+
There is a short-form for listing nodes children (as used for "children" above), where the "children" is just a list of strings.
59+
If you use the short-form for listing elements, then your "on-select" function will have to act based only upon the "branch.label". If you use the
60+
long-form, where is branch is an object, then you can also attach "data" to a branch.
61+
62+
63+
64+
You can supply a single default "on-select" function for the whole tree -- it will be called whenever a branch is selected:
65+
66+
$scope.my_tree_hander = function(branch){...}
67+
68+
69+
Or, you can put a custom "on-select" function on an individual branch:
70+
71+
$scope.my_data = [{
72+
label: 'Languages',
73+
onSelect: function(branch){...},
74+
children: ['Jade','Less','Coffeescript']
75+
}]
76+
77+
Each branch can have a "data" element which you can use to hold whatever data you want. It is not touched by the tree, and it is available to your "on-select" function as "branch.data". In the example, in the "test" folder, this technique is used in "my_tree_handler" to add extra info to "Dog","Cat", and "Hippo".
78+
79+
Warning: If you attach extra attributes directly to a branch (instead of to "branch.data"), they could conflict with the internal workings of the tree, which adds branch attributes at runtime, like "expanded" and "selected".
80+

dist/abn_tree.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,17 @@ ul.nav.abn-tree .level-5 .indented {
105105
}
106106
ul.nav.abn-tree .level-6 .indented {
107107
position: relative;
108-
left: 80px;
108+
left: 100px;
109109
}
110110
ul.nav.nav-list.abn-tree .level-7 .indented {
111111
position: relative;
112-
left: 100px;
112+
left: 120px;
113113
}
114114
ul.nav.nav-list.abn-tree .level-8 .indented {
115115
position: relative;
116-
left: 120px;
116+
left: 140px;
117117
}
118118
ul.nav.nav-list.abn-tree .level-9 .indented {
119119
position: relative;
120-
left: 140px;
120+
left: 160px;
121121
}

dist/abn_tree_directive.js

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ module.directive('abnTree', function($timeout) {
99
scope: {
1010
treeData: '=',
1111
onSelect: '&',
12-
initialSelection: '='
12+
initialSelection: '@'
1313
},
1414
link: function(scope, element, attrs) {
15-
var expand_level, for_each_branch, on_treeData_change, select_branch, selected_branch;
15+
var error, expand_all_parents, expand_level, for_all_ancestors, for_each_branch, get_parent, on_treeData_change, select_branch, selected_branch;
16+
error = function(s) {
17+
console.log('ERROR:' + s);
18+
debugger;
19+
return void 0;
20+
};
1621
if (attrs.iconExpand == null) {
1722
attrs.iconExpand = 'icon-plus';
1823
}
@@ -29,15 +34,13 @@ module.directive('abnTree', function($timeout) {
2934
scope.header = attrs.header;
3035
if (!scope.treeData) {
3136
alert('no treeData defined for the tree!');
32-
debugger;
3337
return;
3438
}
3539
if (scope.treeData.length == null) {
3640
if (treeData.label != null) {
3741
scope.treeData = [treeData];
3842
} else {
3943
alert('treeData should be an array of root branches');
40-
debugger;
4144
return;
4245
}
4346
}
@@ -70,12 +73,20 @@ module.directive('abnTree', function($timeout) {
7073
});
7174
selected_branch = null;
7275
select_branch = function(branch) {
76+
if (!branch) {
77+
if (selected_branch != null) {
78+
selected_branch.selected = false;
79+
}
80+
selected_branch = null;
81+
return;
82+
}
7383
if (branch !== selected_branch) {
7484
if (selected_branch != null) {
7585
selected_branch.selected = false;
7686
}
7787
branch.selected = true;
7888
selected_branch = branch;
89+
expand_all_parents(branch);
7990
if (branch.onSelect != null) {
8091
return $timeout(function() {
8192
return branch.onSelect(branch);
@@ -96,9 +107,52 @@ module.directive('abnTree', function($timeout) {
96107
return select_branch(branch);
97108
}
98109
};
110+
get_parent = function(child) {
111+
var parent;
112+
parent = void 0;
113+
if (child.parent_uid) {
114+
for_each_branch(function(b) {
115+
if (b.uid === child.parent_uid) {
116+
return parent = b;
117+
}
118+
});
119+
}
120+
return parent;
121+
};
122+
for_all_ancestors = function(child, fn) {
123+
var parent;
124+
parent = get_parent(child);
125+
if (parent != null) {
126+
fn(parent);
127+
return for_all_ancestors(parent, fn);
128+
}
129+
};
130+
expand_all_parents = function(child) {
131+
return for_all_ancestors(child, function(b) {
132+
return b.expanded = true;
133+
});
134+
};
99135
scope.tree_rows = [];
100136
on_treeData_change = function() {
101137
var add_branch_to_list, root_branch, _i, _len, _ref, _results;
138+
for_each_branch(function(b, level) {
139+
if (!b.uid) {
140+
return b.uid = "" + Math.random();
141+
}
142+
});
143+
console.log('UIDs are set.');
144+
for_each_branch(function(b) {
145+
var child, _i, _len, _ref, _results;
146+
if (angular.isArray(b.children)) {
147+
_ref = b.children;
148+
_results = [];
149+
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
150+
child = _ref[_i];
151+
_results.push(child.parent_uid = b.uid);
152+
}
153+
return _results;
154+
}
155+
});
102156
scope.tree_rows = [];
103157
for_each_branch(function(branch) {
104158
if (branch.children) {
@@ -118,11 +172,6 @@ module.directive('abnTree', function($timeout) {
118172
return branch.children = [];
119173
}
120174
});
121-
for_each_branch(function(b, level) {
122-
if (!b.uid) {
123-
return b.uid = "" + Math.random();
124-
}
125-
});
126175
add_branch_to_list = function(level, branch, visible) {
127176
var child, child_visible, tree_icon, _i, _len, _ref, _results;
128177
if (branch.expanded == null) {
@@ -163,14 +212,16 @@ module.directive('abnTree', function($timeout) {
163212
}
164213
return _results;
165214
};
215+
scope.$watch('treeData', on_treeData_change, true);
166216
if (attrs.initialSelection != null) {
167-
for_each_branch(function(b) {
217+
return for_each_branch(function(b) {
168218
if (b.label === attrs.initialSelection) {
169-
return select_branch(b);
219+
return $timeout(function() {
220+
return select_branch(b);
221+
});
170222
}
171223
});
172224
}
173-
return scope.$watch('treeData', on_treeData_change, true);
174225
}
175226
};
176227
});

dist/abn_tree_template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<li ng-repeat="row in tree_rows | filter:{visible:true} track by row.branch.uid" ng-animate="'abn-tree-animate'" ng-class="'level-' + {{ row.level }} + (row.branch.selected ? ' active':'')" class="abn-tree-row">
44
<a ng-click="user_clicks_branch(row.branch)">
55
<i ng-class="row.tree_icon" ng-click="row.branch.expanded = !row.branch.expanded" class="indented tree-icon"> </i>
6-
<span class="indented tree-label">{{ row.label }}</span>
6+
<span class="indented tree-label">{{ row.label }} </span>
77
</a>
88
</li>
99
</ul>

0 commit comments

Comments
 (0)