1 define(['angular'], function (angular) {
4 angular.module('app.yangman').directive('abnApiTree', [
5 '$timeout', 'constants', function ($timeout, constants) {
8 templateUrl: 'src/app/yangman/views/directives/abn-tree.tpl.html',
13 initialSelection: '@',
17 link: function (scope, element, attrs) {
29 if (attrs.iconExpand == null) {
30 attrs.iconExpand = constants.ICON_EXPAND_ADD;
32 if (attrs.iconCollapse == null) {
33 attrs.iconCollapse = constants.ICON_COLLAPSE_REMOVE;
35 if (attrs.iconLeaf == null) {
36 attrs.iconLeaf = constants.ICON_KEYBOARD_ARROW_RIGHT;
38 if (attrs.expandLevel == null) {
39 attrs.expandLevel = constants.EXPAND_LEVEL_THREE;
41 expandLevel = parseInt(attrs.expandLevel, 10);
42 if (!scope.treeData) {
45 if (scope.treeData.length == null) {
46 if (scope.treeData.label != null) {
47 scope.treeData = [scope.treeData];
52 forEachBranch = function (f) {
60 do_f = function (branch, level) {
68 if (branch.children != null) {
69 _ref = branch.children;
71 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
73 _results.push(do_f(child, level + 1));
78 _ref = scope.treeData;
80 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
81 root_branch = _ref[_i];
82 _results.push(do_f(root_branch, 1));
86 selectedBranch = null;
87 selectBranch = function (branch) {
89 if (selectedBranch != null) {
90 selectedBranch.selected = false;
92 selectedBranch = null;
96 if (selectedBranch != null) {
97 selectedBranch.selected = false;
99 branch.selected = true;
100 selectedBranch = branch;
101 expandAllParents(branch);
102 if (branch.onSelect != null) {
103 return $timeout(function () {
104 return branch.onSelect(branch);
107 if (scope.onSelect != null) {
108 return $timeout(function () {
109 return scope.onSelect({
117 scope.user_clicks_branch = function (branch) {
119 scope.tree_rows.forEach(function (item){
120 item.branch.selected = false;
123 if (branch === selectedBranch) {
124 branch.selected = true;
127 return selectBranch(branch);
130 getParent = function (child) {
133 if (child.parent_uid) {
134 forEachBranch(function (b) {
135 if (b.uid === child.parent_uid) {
142 forAllAncestors = function (child, fn) {
144 parent = getParent(child);
145 if (parent != null) {
147 return forAllAncestors(parent, fn);
150 expandAllParents = function (child) {
151 return forAllAncestors(child, function (b) {
152 return b.expanded = true;
156 scope.expandedTree = false;
157 scope.expand_collapse_all_items = function (){
159 var expand = !scope.expandedTree ? true : false;
161 scope.tree_rows.forEach(function (child){
162 child.branch.expanded = expand;
165 scope.expandedTree = !scope.expandedTree;
167 scope.collapse_others = function (){
169 expandParent = function (parentId){
171 scope.tree_rows.forEach(function (child){
172 if ( child.branch.uid === parentId ) {
173 child.branch.expanded = true;
174 parentId = child.branch.parent_uid;
177 expandParent(parentId);
184 scope.tree_rows.forEach(function (child){
185 if ( child.branch.selected ) {
186 parentId = child.branch.parent_uid;
188 child.branch.expanded = child.branch.selected ? child.branch.expanded : false;
192 expandParent(parentId);
197 scope.$watch(constants.TREE_ROWS, function () {
198 scope.treeRows = scope.tree_rows;
199 scope.$emit(constants.SET_SCOPE_TREE_ROWS, scope.treeRows);
202 scope.tree_rows = [];
203 onTreeDataChange = function () {
204 var add_branch_to_list,
211 forEachBranch(function (b) {
213 return b.uid = '' + Math.random();
216 forEachBranch(function (b) {
223 if (angular.isArray(b.children)) {
226 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
228 _results.push(child.parent_uid = b.uid);
233 scope.tree_rows = [];
234 forEachBranch(function (branch) {
238 if (branch.children) {
239 if (branch.children.length > 0) {
241 if (typeof e === 'string') {
250 return branch.children = (function () {
256 _ref = branch.children;
258 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
260 _results.push(f(child));
266 return branch.children = [];
269 add_branch_to_list = function (level, branch, visible) {
278 if (branch.expanded == null) {
279 branch.expanded = false;
281 if (!branch.children || branch.children.length === 0) {
282 tree_icon = attrs.iconLeaf;
284 if (branch.expanded) {
285 tree_icon = attrs.iconCollapse;
287 tree_icon = attrs.iconExpand;
290 scope.tree_rows.push({
291 index: scope.tree_rows.length, // in template tracking by branch.uid, integer needed
295 tree_icon: tree_icon,
298 if (branch.children != null) {
299 _ref = branch.children;
301 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
303 child_visible = visible && branch.expanded;
304 _results.push(add_branch_to_list(level + 1, child, child_visible));
309 _ref = scope.treeData;
311 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
312 root_branch = _ref[_i];
313 _results.push(add_branch_to_list(1, root_branch, true));
317 scope.$watch('treeData', onTreeDataChange, true);
318 if (attrs.initialSelection != null) {
319 forEachBranch(function (b) {
320 if (b.label === attrs.initialSelection) {
321 return $timeout(function () {
322 return selectBranch(b);
327 n = scope.treeData.length;
328 forEachBranch(function (b, level) {
330 return b.expanded = b.level < expandLevel;
332 if (scope.treeControl != null) {
333 if (angular.isObject(scope.treeControl)) {
334 tree = scope.treeControl;
335 tree.expand_all = function () {
336 return forEachBranch(function (b) {
337 return b.expanded = true;
340 tree.collapse_all = function () {
341 return forEachBranch(function (b) {
342 return b.expanded = false;
345 tree.get_first_branch = function () {
346 n = scope.treeData.length;
348 return scope.treeData[0];
351 tree.select_first_branch = function () {
353 b = tree.get_first_branch();
354 return tree.selectBranch(b);
356 tree.get_selected_branch = function () {
357 return selectedBranch;
359 tree.get_parent_branch = function (b) {
362 tree.select_branch = function (b) {
366 tree.get_children = function (b) {
369 tree.select_parent_branch = function (b) {
372 b = tree.get_selected_branch();
375 p = tree.get_parent_branch(b);
377 tree.select_branch(p);
382 tree.add_branch = function (parent, new_branch) {
383 if (parent != null) {
384 parent.children.push(new_branch);
385 parent.expanded = true;
387 scope.treeData.push(new_branch);
391 tree.add_root_branch = function (new_branch) {
392 tree.add_branch(null, new_branch);
395 tree.expand_branch = function (b) {
397 b = tree.get_selected_branch();
404 tree.collapse_branch = function (b) {
413 tree.get_siblings = function (b) {
421 p = tree.get_parent_branch(b);
423 siblings = p.children;
425 siblings = scope.treeData;
430 tree.get_next_sibling = function (b) {
438 siblings = tree.get_siblings(b);
440 i = siblings.indexOf(b);
442 return siblings[i + 1];
446 tree.get_prev_sibling = function (b) {
453 siblings = tree.get_siblings(b);
455 i = siblings.indexOf(b);
457 return siblings[i - 1];
460 tree.select_next_sibling = function (b) {
466 next = tree.get_next_sibling(b);
468 return tree.select_branch(next);
472 tree.select_prev_sibling = function (b) {
478 prev = tree.get_prev_sibling(b);
480 return tree.select_branch(prev);
484 tree.get_first_child = function (b) {
490 if (((_ref = b.children) != null ? _ref.length : void 0) > 0) {
491 return b.children[0];
495 tree.get_closest_ancestor_next_sibling = function (b) {
499 next = tree.get_next_sibling(b);
503 parent = tree.get_parent_branch(b);
504 return tree.get_closest_ancestor_next_sibling(parent);
507 tree.get_next_branch = function (b) {
513 next = tree.get_first_child(b);
517 next = tree.get_closest_ancestor_next_sibling(b);
522 tree.select_next_branch = function (b) {
528 next = tree.get_next_branch(b);
530 tree.select_branch(next);
535 tree.last_descendant = function (b) {
540 n = b.children.length;
544 last_child = b.children[n - 1];
545 return tree.last_descendant(last_child);
548 tree.get_prev_branch = function (b) {
556 prev_sibling = tree.get_prev_sibling(b);
557 if (prev_sibling != null) {
558 return tree.last_descendant(prev_sibling);
560 parent = tree.get_parent_branch(b);
565 return tree.select_prev_branch = function (b) {
571 prev = tree.get_prev_branch(b);
573 tree.select_branch(prev);