Skip to content

Commit 489feb2

Browse files
Added the "tree-control" interface, providing an API to control the tree.
1 parent 4d69466 commit 489feb2

10 files changed

+976
-81
lines changed

dist/abn_tree_directive.js

Lines changed: 248 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ module.directive('abnTree', function($timeout) {
99
scope: {
1010
treeData: '=',
1111
onSelect: '&',
12-
initialSelection: '@'
12+
initialSelection: '@',
13+
treeControl: '='
1314
},
1415
link: function(scope, element, attrs) {
15-
var error, expand_all_parents, expand_level, for_all_ancestors, for_each_branch, get_parent, on_treeData_change, select_branch, selected_branch;
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;
1617
error = function(s) {
1718
console.log('ERROR:' + s);
1819
debugger;
@@ -31,7 +32,6 @@ module.directive('abnTree', function($timeout) {
3132
attrs.expandLevel = '3';
3233
}
3334
expand_level = parseInt(attrs.expandLevel, 10);
34-
scope.header = attrs.header;
3535
if (!scope.treeData) {
3636
alert('no treeData defined for the tree!');
3737
return;
@@ -67,10 +67,6 @@ module.directive('abnTree', function($timeout) {
6767
}
6868
return _results;
6969
};
70-
for_each_branch(function(b, level) {
71-
b.level = level;
72-
return b.expanded = b.level < expand_level;
73-
});
7470
selected_branch = null;
7571
select_branch = function(branch) {
7672
if (!branch) {
@@ -214,14 +210,258 @@ module.directive('abnTree', function($timeout) {
214210
};
215211
scope.$watch('treeData', on_treeData_change, true);
216212
if (attrs.initialSelection != null) {
217-
return for_each_branch(function(b) {
213+
for_each_branch(function(b) {
218214
if (b.label === attrs.initialSelection) {
219215
return $timeout(function() {
220216
return select_branch(b);
221217
});
222218
}
223219
});
224220
}
221+
n = scope.treeData.length;
222+
console.log('num root branches = ' + n);
223+
for_each_branch(function(b, level) {
224+
b.level = level;
225+
return b.expanded = b.level < expand_level;
226+
});
227+
if (scope.treeControl != null) {
228+
if (angular.isObject(scope.treeControl)) {
229+
tree = scope.treeControl;
230+
tree.expand_all = function() {
231+
return for_each_branch(function(b, level) {
232+
return b.expanded = true;
233+
});
234+
};
235+
tree.collapse_all = function() {
236+
return for_each_branch(function(b, level) {
237+
return b.expanded = false;
238+
});
239+
};
240+
tree.get_first_branch = function() {
241+
n = scope.treeData.length;
242+
if (n > 0) {
243+
return scope.treeData[0];
244+
}
245+
};
246+
tree.select_first_branch = function() {
247+
var b;
248+
b = tree.get_first_branch();
249+
return tree.select_branch(b);
250+
};
251+
tree.get_selected_branch = function() {
252+
return selected_branch;
253+
};
254+
tree.get_parent_branch = function(b) {
255+
return get_parent(b);
256+
};
257+
tree.select_branch = function(b) {
258+
select_branch(b);
259+
return b;
260+
};
261+
tree.get_children = function(b) {
262+
return b.children;
263+
};
264+
tree.select_parent_branch = function(b) {
265+
var p;
266+
if (b == null) {
267+
b = tree.get_selected_branch();
268+
}
269+
if (b != null) {
270+
p = tree.get_parent_branch(b);
271+
if (p != null) {
272+
tree.select_branch(p);
273+
return p;
274+
}
275+
}
276+
};
277+
tree.add_branch = function(parent, new_branch) {
278+
if (parent != null) {
279+
parent.children.push(new_branch);
280+
parent.expanded = true;
281+
} else {
282+
scope.treeData.push(new_branch);
283+
}
284+
return new_branch;
285+
};
286+
tree.add_root_branch = function(new_branch) {
287+
tree.add_branch(null, new_branch);
288+
return new_branch;
289+
};
290+
tree.expand_branch = function(b) {
291+
if (b == null) {
292+
b = tree.get_selected_branch();
293+
}
294+
if (b != null) {
295+
b.expanded = true;
296+
return b;
297+
}
298+
};
299+
tree.collapse_branch = function(b) {
300+
if (b == null) {
301+
b = selected_branch;
302+
}
303+
if (b != null) {
304+
b.expanded = false;
305+
return b;
306+
}
307+
};
308+
tree.get_siblings = function(b) {
309+
var p, siblings;
310+
if (b == null) {
311+
b = selected_branch;
312+
}
313+
if (b != null) {
314+
p = tree.get_parent_branch(b);
315+
if (p) {
316+
siblings = p.children;
317+
} else {
318+
siblings = scope.treeData;
319+
}
320+
return siblings;
321+
}
322+
};
323+
tree.get_next_sibling = function(b) {
324+
var i, siblings;
325+
if (b == null) {
326+
b = selected_branch;
327+
}
328+
if (b != null) {
329+
siblings = tree.get_siblings(b);
330+
n = siblings.length;
331+
i = siblings.indexOf(b);
332+
if (i < n) {
333+
return siblings[i + 1];
334+
}
335+
}
336+
};
337+
tree.get_prev_sibling = function(b) {
338+
var i, siblings;
339+
if (b == null) {
340+
b = selected_branch;
341+
}
342+
siblings = tree.get_siblings(b);
343+
n = siblings.length;
344+
i = siblings.indexOf(b);
345+
if (i > 0) {
346+
return siblings[i - 1];
347+
}
348+
};
349+
tree.select_next_sibling = function(b) {
350+
var next;
351+
if (b == null) {
352+
b = selected_branch;
353+
}
354+
if (b != null) {
355+
next = tree.get_next_sibling(b);
356+
if (next != null) {
357+
return tree.select_branch(next);
358+
}
359+
}
360+
};
361+
tree.select_prev_sibling = function(b) {
362+
var prev;
363+
if (b == null) {
364+
b = selected_branch;
365+
}
366+
if (b != null) {
367+
prev = tree.get_prev_sibling(b);
368+
if (prev != null) {
369+
return tree.select_branch(prev);
370+
}
371+
}
372+
};
373+
tree.get_first_child = function(b) {
374+
var _ref;
375+
if (b == null) {
376+
b = selected_branch;
377+
}
378+
if (b != null) {
379+
if (((_ref = b.children) != null ? _ref.length : void 0) > 0) {
380+
return b.children[0];
381+
}
382+
}
383+
};
384+
tree.get_closest_ancestor_next_sibling = function(b) {
385+
var next, parent;
386+
next = tree.get_next_sibling(b);
387+
if (next != null) {
388+
return next;
389+
} else {
390+
parent = tree.get_parent_branch(b);
391+
return tree.get_closest_ancestor_next_sibling(parent);
392+
}
393+
};
394+
tree.get_next_branch = function(b) {
395+
var next;
396+
if (b == null) {
397+
b = selected_branch;
398+
}
399+
if (b != null) {
400+
next = tree.get_first_child(b);
401+
if (next != null) {
402+
return next;
403+
} else {
404+
next = tree.get_closest_ancestor_next_sibling(b);
405+
return next;
406+
}
407+
}
408+
};
409+
tree.select_next_branch = function(b) {
410+
var next;
411+
if (b == null) {
412+
b = selected_branch;
413+
}
414+
if (b != null) {
415+
next = tree.get_next_branch(b);
416+
if (next != null) {
417+
tree.select_branch(next);
418+
return next;
419+
}
420+
}
421+
};
422+
tree.last_descendant = function(b) {
423+
var last_child;
424+
if (b == null) {
425+
debugger;
426+
}
427+
n = b.children.length;
428+
if (n === 0) {
429+
return b;
430+
} else {
431+
last_child = b.children[n - 1];
432+
return tree.last_descendant(last_child);
433+
}
434+
};
435+
tree.get_prev_branch = function(b) {
436+
var parent, prev_sibling;
437+
if (b == null) {
438+
b = selected_branch;
439+
}
440+
if (b != null) {
441+
prev_sibling = tree.get_prev_sibling(b);
442+
if (prev_sibling != null) {
443+
return tree.last_descendant(prev_sibling);
444+
} else {
445+
parent = tree.get_parent_branch(b);
446+
return parent;
447+
}
448+
}
449+
};
450+
return tree.select_prev_branch = function(b) {
451+
var prev;
452+
if (b == null) {
453+
b = selected_branch;
454+
}
455+
if (b != null) {
456+
prev = tree.get_prev_branch(b);
457+
if (prev != null) {
458+
tree.select_branch(prev);
459+
return prev;
460+
}
461+
}
462+
};
463+
}
464+
}
225465
}
226466
};
227467
});

0 commit comments

Comments
 (0)