Yangman - changed event strings to constants
[dlux.git] / modules / yangman-resources / src / main / resources / yangman / directives / abn-tree.directive.js
1 define(['angular'], function (angular) {
2     'use strict';
3
4     angular.module('app.yangman').directive('abnApiTree', [
5         '$timeout', 'constants', function ($timeout, constants) {
6             return {
7                 restrict: 'E',
8                 templateUrl: 'src/app/yangman/views/directives/abn-tree.tpl.html',
9                 replace: true,
10                 scope: {
11                     treeData: '=',
12                     onSelect: '&',
13                     initialSelection: '@',
14                     treeControl: '=',
15                     treeRows: '=',
16                 },
17                 link: function (scope, element, attrs) {
18                     var expandAllParents,
19                         expandLevel,
20                         forAllAncestors,
21                         forEachBranch,
22                         getParent,
23                         n,
24                         onTreeDataChange,
25                         selectBranch,
26                         selectedBranch,
27                         tree;
28
29                     if (attrs.iconExpand == null) {
30                         attrs.iconExpand = constants.ICON_EXPAND_ADD;
31                     }
32                     if (attrs.iconCollapse == null) {
33                         attrs.iconCollapse = constants.ICON_COLLAPSE_REMOVE;
34                     }
35                     if (attrs.iconLeaf == null) {
36                         attrs.iconLeaf = constants.ICON_KEYBOARD_ARROW_RIGHT;
37                     }
38                     if (attrs.expandLevel == null) {
39                         attrs.expandLevel = constants.EXPAND_LEVEL_THREE;
40                     }
41                     expandLevel = parseInt(attrs.expandLevel, 10);
42                     if (!scope.treeData) {
43                         return;
44                     }
45                     if (scope.treeData.length == null) {
46                         if (scope.treeData.label != null) {
47                             scope.treeData = [scope.treeData];
48                         } else {
49                             return;
50                         }
51                     }
52                     forEachBranch = function (f) {
53                         var do_f,
54                             root_branch,
55                             _i,
56                             _len,
57                             _ref,
58                             _results;
59
60                         do_f = function (branch, level) {
61                             var child,
62                                 _i,
63                                 _len,
64                                 _ref,
65                                 _results;
66
67                             f(branch, level);
68                             if (branch.children != null) {
69                                 _ref = branch.children;
70                                 _results = [];
71                                 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
72                                     child = _ref[_i];
73                                     _results.push(do_f(child, level + 1));
74                                 }
75                                 return _results;
76                             }
77                         };
78                         _ref = scope.treeData;
79                         _results = [];
80                         for (_i = 0, _len = _ref.length; _i < _len; _i++) {
81                             root_branch = _ref[_i];
82                             _results.push(do_f(root_branch, 1));
83                         }
84                         return _results;
85                     };
86                     selectedBranch = null;
87                     selectBranch = function (branch) {
88                         if (!branch) {
89                             if (selectedBranch != null) {
90                                 selectedBranch.selected = false;
91                             }
92                             selectedBranch = null;
93                             return;
94                         }
95
96                         if (selectedBranch != null) {
97                             selectedBranch.selected = false;
98                         }
99                         branch.selected = true;
100                         selectedBranch = branch;
101                         expandAllParents(branch);
102                         if (branch.onSelect != null) {
103                             return $timeout(function () {
104                                 return branch.onSelect(branch);
105                             });
106                         } else {
107                             if (scope.onSelect != null) {
108                                 return $timeout(function () {
109                                     return scope.onSelect({
110                                         branch: branch,
111                                     });
112                                 });
113                             }
114                         }
115
116                     };
117                     scope.user_clicks_branch = function (branch) {
118
119                         scope.tree_rows.forEach(function (item){
120                             item.branch.selected = false;
121                         });
122
123                         if (branch === selectedBranch) {
124                             branch.selected = true;
125                         }
126
127                         return selectBranch(branch);
128                     };
129
130                     getParent = function (child) {
131                         var parent;
132                         parent = void 0;
133                         if (child.parent_uid) {
134                             forEachBranch(function (b) {
135                                 if (b.uid === child.parent_uid) {
136                                     return parent = b;
137                                 }
138                             });
139                         }
140                         return parent;
141                     };
142                     forAllAncestors = function (child, fn) {
143                         var parent;
144                         parent = getParent(child);
145                         if (parent != null) {
146                             fn(parent);
147                             return forAllAncestors(parent, fn);
148                         }
149                     };
150                     expandAllParents = function (child) {
151                         return forAllAncestors(child, function (b) {
152                             return b.expanded = true;
153                         });
154                     };
155
156                     scope.expandedTree = false;
157                     scope.expand_collapse_all_items = function (){
158
159                         var expand = !scope.expandedTree ? true : false;
160
161                         scope.tree_rows.forEach(function (child){
162                             child.branch.expanded = expand;
163                         });
164
165                         scope.expandedTree = !scope.expandedTree;
166                     };
167                     scope.collapse_others = function (){
168                         var parentId = null,
169                             expandParent = function (parentId){
170                                 if ( 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;
175
176                                             if ( parentId ) {
177                                                 expandParent(parentId);
178                                             }
179                                         }
180                                     });
181                                 }
182                             };
183
184                         scope.tree_rows.forEach(function (child){
185                             if ( child.branch.selected ) {
186                                 parentId = child.branch.parent_uid;
187                             }
188                             child.branch.expanded = child.branch.selected ? child.branch.expanded : false;
189                         });
190
191                         if ( parentId ) {
192                             expandParent(parentId);
193                         }
194
195                     };
196
197                     scope.$watch(constants.TREE_ROWS, function () {
198                         scope.treeRows = scope.tree_rows;
199                         scope.$emit(constants.SET_SCOPE_TREE_ROWS, scope.treeRows);
200                     });
201
202                     scope.tree_rows = [];
203                     onTreeDataChange = function () {
204                         var add_branch_to_list,
205                             root_branch,
206                             _i,
207                             _len,
208                             _ref,
209                             _results;
210
211                         forEachBranch(function (b) {
212                             if (!b.uid) {
213                                 return b.uid = '' + Math.random();
214                             }
215                         });
216                         forEachBranch(function (b) {
217                             var child,
218                                 _i,
219                                 _len,
220                                 _ref,
221                                 _results;
222
223                             if (angular.isArray(b.children)) {
224                                 _ref = b.children;
225                                 _results = [];
226                                 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
227                                     child = _ref[_i];
228                                     _results.push(child.parent_uid = b.uid);
229                                 }
230                                 return _results;
231                             }
232                         });
233                         scope.tree_rows = [];
234                         forEachBranch(function (branch) {
235                             var child,
236                                 f;
237
238                             if (branch.children) {
239                                 if (branch.children.length > 0) {
240                                     f = function (e) {
241                                         if (typeof e === 'string') {
242                                             return {
243                                                 label: e,
244                                                 children: [],
245                                             };
246                                         } else {
247                                             return e;
248                                         }
249                                     };
250                                     return branch.children = (function () {
251                                         var _i,
252                                             _len,
253                                             _ref,
254                                             _results;
255
256                                         _ref = branch.children;
257                                         _results = [];
258                                         for (_i = 0, _len = _ref.length; _i < _len; _i++) {
259                                             child = _ref[_i];
260                                             _results.push(f(child));
261                                         }
262                                         return _results;
263                                     })();
264                                 }
265                             } else {
266                                 return branch.children = [];
267                             }
268                         });
269                         add_branch_to_list = function (level, branch, visible) {
270                             var child,
271                                 child_visible,
272                                 tree_icon,
273                                 _i,
274                                 _len,
275                                 _ref,
276                                 _results;
277
278                             if (branch.expanded == null) {
279                                 branch.expanded = false;
280                             }
281                             if (!branch.children || branch.children.length === 0) {
282                                 tree_icon = attrs.iconLeaf;
283                             } else {
284                                 if (branch.expanded) {
285                                     tree_icon = attrs.iconCollapse;
286                                 } else {
287                                     tree_icon = attrs.iconExpand;
288                                 }
289                             }
290                             scope.tree_rows.push({
291                                 level: level,
292                                 branch: branch,
293                                 label: branch.label,
294                                 tree_icon: tree_icon,
295                                 visible: visible,
296                             });
297                             if (branch.children != null) {
298                                 _ref = branch.children;
299                                 _results = [];
300                                 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
301                                     child = _ref[_i];
302                                     child_visible = visible && branch.expanded;
303                                     _results.push(add_branch_to_list(level + 1, child, child_visible));
304                                 }
305                                 return _results;
306                             }
307                         };
308                         _ref = scope.treeData;
309                         _results = [];
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));
313                         }
314                         return _results;
315                     };
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);
322                                 });
323                             }
324                         });
325                     }
326                     n = scope.treeData.length;
327                     forEachBranch(function (b, level) {
328                         b.level = level;
329                         return b.expanded = b.level < expandLevel;
330                     });
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;
337                                 });
338                             };
339                             tree.collapse_all = function () {
340                                 return forEachBranch(function (b) {
341                                     return b.expanded = false;
342                                 });
343                             };
344                             tree.get_first_branch = function () {
345                                 n = scope.treeData.length;
346                                 if (n > 0) {
347                                     return scope.treeData[0];
348                                 }
349                             };
350                             tree.select_first_branch = function () {
351                                 var b;
352                                 b = tree.get_first_branch();
353                                 return tree.selectBranch(b);
354                             };
355                             tree.get_selected_branch = function () {
356                                 return selectedBranch;
357                             };
358                             tree.get_parent_branch = function (b) {
359                                 return getParent(b);
360                             };
361                             tree.select_branch = function (b) {
362                                 selectBranch(b);
363                                 return b;
364                             };
365                             tree.get_children = function (b) {
366                                 return b.children;
367                             };
368                             tree.select_parent_branch = function (b) {
369                                 var p;
370                                 if (b == null) {
371                                     b = tree.get_selected_branch();
372                                 }
373                                 if (b != null) {
374                                     p = tree.get_parent_branch(b);
375                                     if (p != null) {
376                                         tree.select_branch(p);
377                                         return p;
378                                     }
379                                 }
380                             };
381                             tree.add_branch = function (parent, new_branch) {
382                                 if (parent != null) {
383                                     parent.children.push(new_branch);
384                                     parent.expanded = true;
385                                 } else {
386                                     scope.treeData.push(new_branch);
387                                 }
388                                 return new_branch;
389                             };
390                             tree.add_root_branch = function (new_branch) {
391                                 tree.add_branch(null, new_branch);
392                                 return new_branch;
393                             };
394                             tree.expand_branch = function (b) {
395                                 if (b == null) {
396                                     b = tree.get_selected_branch();
397                                 }
398                                 if (b != null) {
399                                     b.expanded = true;
400                                     return b;
401                                 }
402                             };
403                             tree.collapse_branch = function (b) {
404                                 if (b == null) {
405                                     b = selectedBranch;
406                                 }
407                                 if (b != null) {
408                                     b.expanded = false;
409                                     return b;
410                                 }
411                             };
412                             tree.get_siblings = function (b) {
413                                 var p,
414                                     siblings;
415
416                                 if (b == null) {
417                                     b = selectedBranch;
418                                 }
419                                 if (b != null) {
420                                     p = tree.get_parent_branch(b);
421                                     if (p) {
422                                         siblings = p.children;
423                                     } else {
424                                         siblings = scope.treeData;
425                                     }
426                                     return siblings;
427                                 }
428                             };
429                             tree.get_next_sibling = function (b) {
430                                 var i,
431                                     siblings;
432
433                                 if (b == null) {
434                                     b = selectedBranch;
435                                 }
436                                 if (b != null) {
437                                     siblings = tree.get_siblings(b);
438                                     n = siblings.length;
439                                     i = siblings.indexOf(b);
440                                     if (i < n) {
441                                         return siblings[i + 1];
442                                     }
443                                 }
444                             };
445                             tree.get_prev_sibling = function (b) {
446                                 var i,
447                                     siblings;
448
449                                 if (b == null) {
450                                     b = selectedBranch;
451                                 }
452                                 siblings = tree.get_siblings(b);
453                                 n = siblings.length;
454                                 i = siblings.indexOf(b);
455                                 if (i > 0) {
456                                     return siblings[i - 1];
457                                 }
458                             };
459                             tree.select_next_sibling = function (b) {
460                                 var next;
461                                 if (b == null) {
462                                     b = selectedBranch;
463                                 }
464                                 if (b != null) {
465                                     next = tree.get_next_sibling(b);
466                                     if (next != null) {
467                                         return tree.select_branch(next);
468                                     }
469                                 }
470                             };
471                             tree.select_prev_sibling = function (b) {
472                                 var prev;
473                                 if (b == null) {
474                                     b = selectedBranch;
475                                 }
476                                 if (b != null) {
477                                     prev = tree.get_prev_sibling(b);
478                                     if (prev != null) {
479                                         return tree.select_branch(prev);
480                                     }
481                                 }
482                             };
483                             tree.get_first_child = function (b) {
484                                 var _ref;
485                                 if (b == null) {
486                                     b = selectedBranch;
487                                 }
488                                 if (b != null) {
489                                     if (((_ref = b.children) != null ? _ref.length : void 0) > 0) {
490                                         return b.children[0];
491                                     }
492                                 }
493                             };
494                             tree.get_closest_ancestor_next_sibling = function (b) {
495                                 var next,
496                                     parent;
497
498                                 next = tree.get_next_sibling(b);
499                                 if (next != null) {
500                                     return next;
501                                 } else {
502                                     parent = tree.get_parent_branch(b);
503                                     return tree.get_closest_ancestor_next_sibling(parent);
504                                 }
505                             };
506                             tree.get_next_branch = function (b) {
507                                 var next;
508                                 if (b == null) {
509                                     b = selectedBranch;
510                                 }
511                                 if (b != null) {
512                                     next = tree.get_first_child(b);
513                                     if (next != null) {
514                                         return next;
515                                     } else {
516                                         next = tree.get_closest_ancestor_next_sibling(b);
517                                         return next;
518                                     }
519                                 }
520                             };
521                             tree.select_next_branch = function (b) {
522                                 var next;
523                                 if (b == null) {
524                                     b = selectedBranch;
525                                 }
526                                 if (b != null) {
527                                     next = tree.get_next_branch(b);
528                                     if (next != null) {
529                                         tree.select_branch(next);
530                                         return next;
531                                     }
532                                 }
533                             };
534                             tree.last_descendant = function (b) {
535                                 var last_child;
536                                 if (b == null) {
537                                     // debugger;
538                                 }
539                                 n = b.children.length;
540                                 if (n === 0) {
541                                     return b;
542                                 } else {
543                                     last_child = b.children[n - 1];
544                                     return tree.last_descendant(last_child);
545                                 }
546                             };
547                             tree.get_prev_branch = function (b) {
548                                 var parent,
549                                     prev_sibling;
550
551                                 if (b == null) {
552                                     b = selectedBranch;
553                                 }
554                                 if (b != null) {
555                                     prev_sibling = tree.get_prev_sibling(b);
556                                     if (prev_sibling != null) {
557                                         return tree.last_descendant(prev_sibling);
558                                     } else {
559                                         parent = tree.get_parent_branch(b);
560                                         return parent;
561                                     }
562                                 }
563                             };
564                             return tree.select_prev_branch = function (b) {
565                                 var prev;
566                                 if (b == null) {
567                                     b = selectedBranch;
568                                 }
569                                 if (b != null) {
570                                     prev = tree.get_prev_branch(b);
571                                     if (prev != null) {
572                                         tree.select_branch(prev);
573                                         return prev;
574                                     }
575                                 }
576                             };
577                         }
578                     }
579                 },
580             };
581         },
582     ]);
583
584
585 });