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