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({
294 tree_icon: tree_icon,
297 if (branch.children != null) {
298 _ref = branch.children;
300 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
302 child_visible = visible && branch.expanded;
303 _results.push(add_branch_to_list(level + 1, child, child_visible));
308 _ref = scope.treeData;
310 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
311 root_branch = _ref[_i];
312 _results.push(add_branch_to_list(1, root_branch, true));
316 scope.$watch('treeData', onTreeDataChange, true);
317 if (attrs.initialSelection != null) {
318 forEachBranch(function (b) {
319 if (b.label === attrs.initialSelection) {
320 return $timeout(function () {
321 return selectBranch(b);
326 n = scope.treeData.length;
327 forEachBranch(function (b, level) {
329 return b.expanded = b.level < expandLevel;
331 if (scope.treeControl != null) {
332 if (angular.isObject(scope.treeControl)) {
333 tree = scope.treeControl;
334 tree.expand_all = function () {
335 return forEachBranch(function (b) {
336 return b.expanded = true;
339 tree.collapse_all = function () {
340 return forEachBranch(function (b) {
341 return b.expanded = false;
344 tree.get_first_branch = function () {
345 n = scope.treeData.length;
347 return scope.treeData[0];
350 tree.select_first_branch = function () {
352 b = tree.get_first_branch();
353 return tree.selectBranch(b);
355 tree.get_selected_branch = function () {
356 return selectedBranch;
358 tree.get_parent_branch = function (b) {
361 tree.select_branch = function (b) {
365 tree.get_children = function (b) {
368 tree.select_parent_branch = function (b) {
371 b = tree.get_selected_branch();
374 p = tree.get_parent_branch(b);
376 tree.select_branch(p);
381 tree.add_branch = function (parent, new_branch) {
382 if (parent != null) {
383 parent.children.push(new_branch);
384 parent.expanded = true;
386 scope.treeData.push(new_branch);
390 tree.add_root_branch = function (new_branch) {
391 tree.add_branch(null, new_branch);
394 tree.expand_branch = function (b) {
396 b = tree.get_selected_branch();
403 tree.collapse_branch = function (b) {
412 tree.get_siblings = function (b) {
420 p = tree.get_parent_branch(b);
422 siblings = p.children;
424 siblings = scope.treeData;
429 tree.get_next_sibling = function (b) {
437 siblings = tree.get_siblings(b);
439 i = siblings.indexOf(b);
441 return siblings[i + 1];
445 tree.get_prev_sibling = function (b) {
452 siblings = tree.get_siblings(b);
454 i = siblings.indexOf(b);
456 return siblings[i - 1];
459 tree.select_next_sibling = function (b) {
465 next = tree.get_next_sibling(b);
467 return tree.select_branch(next);
471 tree.select_prev_sibling = function (b) {
477 prev = tree.get_prev_sibling(b);
479 return tree.select_branch(prev);
483 tree.get_first_child = function (b) {
489 if (((_ref = b.children) != null ? _ref.length : void 0) > 0) {
490 return b.children[0];
494 tree.get_closest_ancestor_next_sibling = function (b) {
498 next = tree.get_next_sibling(b);
502 parent = tree.get_parent_branch(b);
503 return tree.get_closest_ancestor_next_sibling(parent);
506 tree.get_next_branch = function (b) {
512 next = tree.get_first_child(b);
516 next = tree.get_closest_ancestor_next_sibling(b);
521 tree.select_next_branch = function (b) {
527 next = tree.get_next_branch(b);
529 tree.select_branch(next);
534 tree.last_descendant = function (b) {
539 n = b.children.length;
543 last_child = b.children[n - 1];
544 return tree.last_descendant(last_child);
547 tree.get_prev_branch = function (b) {
555 prev_sibling = tree.get_prev_sibling(b);
556 if (prev_sibling != null) {
557 return tree.last_descendant(prev_sibling);
559 parent = tree.get_parent_branch(b);
564 return tree.select_prev_branch = function (b) {
570 prev = tree.get_prev_branch(b);
572 tree.select_branch(prev);