Yangman - make elements accessible via ids - part1
[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                                 index: scope.tree_rows.length, // in template tracking by branch.uid, integer needed
292                                 level: level,
293                                 branch: branch,
294                                 label: branch.label,
295                                 tree_icon: tree_icon,
296                                 visible: visible,
297                             });
298                             if (branch.children != null) {
299                                 _ref = branch.children;
300                                 _results = [];
301                                 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
302                                     child = _ref[_i];
303                                     child_visible = visible && branch.expanded;
304                                     _results.push(add_branch_to_list(level + 1, child, child_visible));
305                                 }
306                                 return _results;
307                             }
308                         };
309                         _ref = scope.treeData;
310                         _results = [];
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));
314                         }
315                         return _results;
316                     };
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);
323                                 });
324                             }
325                         });
326                     }
327                     n = scope.treeData.length;
328                     forEachBranch(function (b, level) {
329                         b.level = level;
330                         return b.expanded = b.level < expandLevel;
331                     });
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;
338                                 });
339                             };
340                             tree.collapse_all = function () {
341                                 return forEachBranch(function (b) {
342                                     return b.expanded = false;
343                                 });
344                             };
345                             tree.get_first_branch = function () {
346                                 n = scope.treeData.length;
347                                 if (n > 0) {
348                                     return scope.treeData[0];
349                                 }
350                             };
351                             tree.select_first_branch = function () {
352                                 var b;
353                                 b = tree.get_first_branch();
354                                 return tree.selectBranch(b);
355                             };
356                             tree.get_selected_branch = function () {
357                                 return selectedBranch;
358                             };
359                             tree.get_parent_branch = function (b) {
360                                 return getParent(b);
361                             };
362                             tree.select_branch = function (b) {
363                                 selectBranch(b);
364                                 return b;
365                             };
366                             tree.get_children = function (b) {
367                                 return b.children;
368                             };
369                             tree.select_parent_branch = function (b) {
370                                 var p;
371                                 if (b == null) {
372                                     b = tree.get_selected_branch();
373                                 }
374                                 if (b != null) {
375                                     p = tree.get_parent_branch(b);
376                                     if (p != null) {
377                                         tree.select_branch(p);
378                                         return p;
379                                     }
380                                 }
381                             };
382                             tree.add_branch = function (parent, new_branch) {
383                                 if (parent != null) {
384                                     parent.children.push(new_branch);
385                                     parent.expanded = true;
386                                 } else {
387                                     scope.treeData.push(new_branch);
388                                 }
389                                 return new_branch;
390                             };
391                             tree.add_root_branch = function (new_branch) {
392                                 tree.add_branch(null, new_branch);
393                                 return new_branch;
394                             };
395                             tree.expand_branch = function (b) {
396                                 if (b == null) {
397                                     b = tree.get_selected_branch();
398                                 }
399                                 if (b != null) {
400                                     b.expanded = true;
401                                     return b;
402                                 }
403                             };
404                             tree.collapse_branch = function (b) {
405                                 if (b == null) {
406                                     b = selectedBranch;
407                                 }
408                                 if (b != null) {
409                                     b.expanded = false;
410                                     return b;
411                                 }
412                             };
413                             tree.get_siblings = function (b) {
414                                 var p,
415                                     siblings;
416
417                                 if (b == null) {
418                                     b = selectedBranch;
419                                 }
420                                 if (b != null) {
421                                     p = tree.get_parent_branch(b);
422                                     if (p) {
423                                         siblings = p.children;
424                                     } else {
425                                         siblings = scope.treeData;
426                                     }
427                                     return siblings;
428                                 }
429                             };
430                             tree.get_next_sibling = function (b) {
431                                 var i,
432                                     siblings;
433
434                                 if (b == null) {
435                                     b = selectedBranch;
436                                 }
437                                 if (b != null) {
438                                     siblings = tree.get_siblings(b);
439                                     n = siblings.length;
440                                     i = siblings.indexOf(b);
441                                     if (i < n) {
442                                         return siblings[i + 1];
443                                     }
444                                 }
445                             };
446                             tree.get_prev_sibling = function (b) {
447                                 var i,
448                                     siblings;
449
450                                 if (b == null) {
451                                     b = selectedBranch;
452                                 }
453                                 siblings = tree.get_siblings(b);
454                                 n = siblings.length;
455                                 i = siblings.indexOf(b);
456                                 if (i > 0) {
457                                     return siblings[i - 1];
458                                 }
459                             };
460                             tree.select_next_sibling = function (b) {
461                                 var next;
462                                 if (b == null) {
463                                     b = selectedBranch;
464                                 }
465                                 if (b != null) {
466                                     next = tree.get_next_sibling(b);
467                                     if (next != null) {
468                                         return tree.select_branch(next);
469                                     }
470                                 }
471                             };
472                             tree.select_prev_sibling = function (b) {
473                                 var prev;
474                                 if (b == null) {
475                                     b = selectedBranch;
476                                 }
477                                 if (b != null) {
478                                     prev = tree.get_prev_sibling(b);
479                                     if (prev != null) {
480                                         return tree.select_branch(prev);
481                                     }
482                                 }
483                             };
484                             tree.get_first_child = function (b) {
485                                 var _ref;
486                                 if (b == null) {
487                                     b = selectedBranch;
488                                 }
489                                 if (b != null) {
490                                     if (((_ref = b.children) != null ? _ref.length : void 0) > 0) {
491                                         return b.children[0];
492                                     }
493                                 }
494                             };
495                             tree.get_closest_ancestor_next_sibling = function (b) {
496                                 var next,
497                                     parent;
498
499                                 next = tree.get_next_sibling(b);
500                                 if (next != null) {
501                                     return next;
502                                 } else {
503                                     parent = tree.get_parent_branch(b);
504                                     return tree.get_closest_ancestor_next_sibling(parent);
505                                 }
506                             };
507                             tree.get_next_branch = function (b) {
508                                 var next;
509                                 if (b == null) {
510                                     b = selectedBranch;
511                                 }
512                                 if (b != null) {
513                                     next = tree.get_first_child(b);
514                                     if (next != null) {
515                                         return next;
516                                     } else {
517                                         next = tree.get_closest_ancestor_next_sibling(b);
518                                         return next;
519                                     }
520                                 }
521                             };
522                             tree.select_next_branch = function (b) {
523                                 var next;
524                                 if (b == null) {
525                                     b = selectedBranch;
526                                 }
527                                 if (b != null) {
528                                     next = tree.get_next_branch(b);
529                                     if (next != null) {
530                                         tree.select_branch(next);
531                                         return next;
532                                     }
533                                 }
534                             };
535                             tree.last_descendant = function (b) {
536                                 var last_child;
537                                 if (b == null) {
538                                     // debugger;
539                                 }
540                                 n = b.children.length;
541                                 if (n === 0) {
542                                     return b;
543                                 } else {
544                                     last_child = b.children[n - 1];
545                                     return tree.last_descendant(last_child);
546                                 }
547                             };
548                             tree.get_prev_branch = function (b) {
549                                 var parent,
550                                     prev_sibling;
551
552                                 if (b == null) {
553                                     b = selectedBranch;
554                                 }
555                                 if (b != null) {
556                                     prev_sibling = tree.get_prev_sibling(b);
557                                     if (prev_sibling != null) {
558                                         return tree.last_descendant(prev_sibling);
559                                     } else {
560                                         parent = tree.get_parent_branch(b);
561                                         return parent;
562                                     }
563                                 }
564                             };
565                             return tree.select_prev_branch = function (b) {
566                                 var prev;
567                                 if (b == null) {
568                                     b = selectedBranch;
569                                 }
570                                 if (b != null) {
571                                     prev = tree.get_prev_branch(b);
572                                     if (prev != null) {
573                                         tree.select_branch(prev);
574                                         return prev;
575                                     }
576                                 }
577                             };
578                         }
579                     }
580                 },
581             };
582         },
583     ]);
584
585
586 });