1 define(['app/yangui/yangui.module'], function(yangui) {
3 yangui.register.directive('abnTree', [
4 '$timeout', function($timeout) {
7 templateUrl: 'src/app/yangui/abn-tree.tpl.html',
12 initialSelection: '@',
15 link: function(scope, element, attrs) {
16 var error, expand_all_parents, expand_level, for_all_ancestors, for_each_branch, get_parent, n, on_treeData_change, select_branch, selected_branch, tree;
18 console.log('ERROR:' + s);
22 if (attrs.iconExpand == null) {
23 attrs.iconExpand = 'icon-plus glyphicon glyphicon-plus fa fa-plus';
25 if (attrs.iconCollapse == null) {
26 attrs.iconCollapse = 'icon-minus glyphicon glyphicon-minus fa fa-minus';
28 if (attrs.iconLeaf == null) {
29 attrs.iconLeaf = 'icon-file glyphicon glyphicon-file fa fa-file';
31 if (attrs.expandLevel == null) {
32 attrs.expandLevel = '3';
34 expand_level = parseInt(attrs.expandLevel, 10);
35 if (!scope.treeData) {
36 console.warn('no treeData defined for the tree!');
39 if (scope.treeData.length == null) {
40 if (treeData.label != null) {
41 scope.treeData = [treeData];
43 console.warn('treeData should be an array of root branches');
47 for_each_branch = function(f) {
48 var do_f, root_branch, _i, _len, _ref, _results;
49 do_f = function(branch, level) {
50 var child, _i, _len, _ref, _results;
52 if (branch.children != null) {
53 _ref = branch.children;
55 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
57 _results.push(do_f(child, level + 1));
62 _ref = scope.treeData;
64 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
65 root_branch = _ref[_i];
66 _results.push(do_f(root_branch, 1));
70 selected_branch = null;
71 select_branch = function(branch) {
73 if (selected_branch != null) {
74 selected_branch.selected = false;
76 selected_branch = null;
79 if (branch !== selected_branch) {
80 if (selected_branch != null) {
81 selected_branch.selected = false;
83 branch.selected = true;
84 selected_branch = branch;
85 expand_all_parents(branch);
86 if (branch.onSelect != null) {
87 return $timeout(function() {
88 return branch.onSelect(branch);
91 if (scope.onSelect != null) {
92 return $timeout(function() {
93 return scope.onSelect({
101 scope.user_clicks_branch = function(branch) {
102 if (branch !== selected_branch) {
103 return select_branch(branch);
107 get_parent = function(child) {
110 if (child.parent_uid) {
111 for_each_branch(function(b) {
112 if (b.uid === child.parent_uid) {
119 for_all_ancestors = function(child, fn) {
121 parent = get_parent(child);
122 if (parent != null) {
124 return for_all_ancestors(parent, fn);
127 expand_all_parents = function(child) {
128 return for_all_ancestors(child, function(b) {
129 return b.expanded = true;
133 scope.expandedTree = false;
134 scope.expand_collapse_all_items = function(){
136 var expand = !scope.expandedTree ? true : false;
138 scope.tree_rows.forEach(function(child){
139 child.branch.expanded = expand;
142 scope.expandedTree = !scope.expandedTree;
143 // console.log(scope.tree_rows);
145 scope.collapse_others = function(){
147 expandParent = function(parentId){
149 scope.tree_rows.forEach(function(child){
150 if ( child.branch.uid === parentId ) {
151 child.branch.expanded = true;
152 parentId = child.branch.parent_uid;
155 expandParent(parentId);
162 scope.tree_rows.forEach(function(child){
163 if ( child.branch.selected ) {
164 parentId = child.branch.parent_uid;
166 child.branch.expanded = child.branch.selected ? child.branch.expanded : false;
170 expandParent(parentId);
175 scope.tree_rows = [];
176 on_treeData_change = function() {
177 var add_branch_to_list, root_branch, _i, _len, _ref, _results;
178 for_each_branch(function(b, level) {
180 return b.uid = "" + Math.random();
183 // console.log('UIDs are set.');
184 for_each_branch(function(b) {
185 var child, _i, _len, _ref, _results;
186 if (angular.isArray(b.children)) {
189 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
191 _results.push(child.parent_uid = b.uid);
196 scope.tree_rows = [];
197 for_each_branch(function(branch) {
199 if (branch.children) {
200 if (branch.children.length > 0) {
202 if (typeof e === 'string') {
211 return branch.children = (function() {
212 var _i, _len, _ref, _results;
213 _ref = branch.children;
215 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
217 _results.push(f(child));
223 return branch.children = [];
226 add_branch_to_list = function(level, branch, visible) {
227 var child, child_visible, tree_icon, _i, _len, _ref, _results;
228 if (branch.expanded == null) {
229 branch.expanded = false;
231 if (!branch.children || branch.children.length === 0) {
232 tree_icon = attrs.iconLeaf;
234 if (branch.expanded) {
235 tree_icon = attrs.iconCollapse;
237 tree_icon = attrs.iconExpand;
240 scope.tree_rows.push({
244 tree_icon: tree_icon,
247 if (branch.children != null) {
248 _ref = branch.children;
250 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
252 child_visible = visible && branch.expanded;
253 _results.push(add_branch_to_list(level + 1, child, child_visible));
258 _ref = scope.treeData;
260 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
261 root_branch = _ref[_i];
262 _results.push(add_branch_to_list(1, root_branch, true));
266 scope.$watch('treeData', on_treeData_change, true);
267 if (attrs.initialSelection != null) {
268 for_each_branch(function(b) {
269 if (b.label === attrs.initialSelection) {
270 return $timeout(function() {
271 return select_branch(b);
276 n = scope.treeData.length;
277 // console.log('num root branches = ' + n);
278 for_each_branch(function(b, level) {
280 return b.expanded = b.level < expand_level;
282 if (scope.treeControl != null) {
283 if (angular.isObject(scope.treeControl)) {
284 tree = scope.treeControl;
285 tree.expand_all = function() {
286 return for_each_branch(function(b, level) {
287 return b.expanded = true;
290 tree.collapse_all = function() {
291 return for_each_branch(function(b, level) {
292 return b.expanded = false;
295 tree.get_first_branch = function() {
296 n = scope.treeData.length;
298 return scope.treeData[0];
301 tree.select_first_branch = function() {
303 b = tree.get_first_branch();
304 return tree.select_branch(b);
306 tree.get_selected_branch = function() {
307 return selected_branch;
309 tree.get_parent_branch = function(b) {
310 return get_parent(b);
312 tree.select_branch = function(b) {
316 tree.get_children = function(b) {
319 tree.select_parent_branch = function(b) {
322 b = tree.get_selected_branch();
325 p = tree.get_parent_branch(b);
327 tree.select_branch(p);
332 tree.add_branch = function(parent, new_branch) {
333 if (parent != null) {
334 parent.children.push(new_branch);
335 parent.expanded = true;
337 scope.treeData.push(new_branch);
341 tree.add_root_branch = function(new_branch) {
342 tree.add_branch(null, new_branch);
345 tree.expand_branch = function(b) {
347 b = tree.get_selected_branch();
354 tree.collapse_branch = function(b) {
363 tree.get_siblings = function(b) {
369 p = tree.get_parent_branch(b);
371 siblings = p.children;
373 siblings = scope.treeData;
378 tree.get_next_sibling = function(b) {
384 siblings = tree.get_siblings(b);
386 i = siblings.indexOf(b);
388 return siblings[i + 1];
392 tree.get_prev_sibling = function(b) {
397 siblings = tree.get_siblings(b);
399 i = siblings.indexOf(b);
401 return siblings[i - 1];
404 tree.select_next_sibling = function(b) {
410 next = tree.get_next_sibling(b);
412 return tree.select_branch(next);
416 tree.select_prev_sibling = function(b) {
422 prev = tree.get_prev_sibling(b);
424 return tree.select_branch(prev);
428 tree.get_first_child = function(b) {
434 if (((_ref = b.children) != null ? _ref.length : void 0) > 0) {
435 return b.children[0];
439 tree.get_closest_ancestor_next_sibling = function(b) {
441 next = tree.get_next_sibling(b);
445 parent = tree.get_parent_branch(b);
446 return tree.get_closest_ancestor_next_sibling(parent);
449 tree.get_next_branch = function(b) {
455 next = tree.get_first_child(b);
459 next = tree.get_closest_ancestor_next_sibling(b);
464 tree.select_next_branch = function(b) {
470 next = tree.get_next_branch(b);
472 tree.select_branch(next);
477 tree.last_descendant = function(b) {
482 n = b.children.length;
486 last_child = b.children[n - 1];
487 return tree.last_descendant(last_child);
490 tree.get_prev_branch = function(b) {
491 var parent, prev_sibling;
496 prev_sibling = tree.get_prev_sibling(b);
497 if (prev_sibling != null) {
498 return tree.last_descendant(prev_sibling);
500 parent = tree.get_parent_branch(b);
505 return tree.select_prev_branch = function(b) {
511 prev = tree.get_prev_branch(b);
513 tree.select_branch(prev);