|
| 1 | +var module; |
| 2 | + |
| 3 | +module = angular.module('angularBootstrapNavTree', []); |
| 4 | + |
| 5 | +module.directive('abnTree', function($timeout) { |
| 6 | + return { |
| 7 | + restrict: 'E', |
| 8 | + templateUrl: '../dist/abn_tree_template.html', |
| 9 | + scope: { |
| 10 | + treeData: '=', |
| 11 | + onSelect: '&', |
| 12 | + initialSelection: '=' |
| 13 | + }, |
| 14 | + link: function(scope, element, attrs) { |
| 15 | + var expand_all, expand_level, for_each_branch, on_treeData_change, select_branch, selected_branch, unselect_all; |
| 16 | + if (attrs.iconExpand == null) { |
| 17 | + attrs.iconExpand = 'icon-plus'; |
| 18 | + } |
| 19 | + if (attrs.iconCollapse == null) { |
| 20 | + attrs.iconCollapse = 'icon-minus'; |
| 21 | + } |
| 22 | + if (attrs.iconLeaf == null) { |
| 23 | + attrs.iconLeaf = 'icon-chevron-right'; |
| 24 | + } |
| 25 | + if (attrs.expandLevel == null) { |
| 26 | + attrs.expandLevel = '3'; |
| 27 | + } |
| 28 | + expand_level = parseInt(attrs.expandLevel, 10); |
| 29 | + scope.header = attrs.header; |
| 30 | + if (!scope.treeData) { |
| 31 | + alert('no treeData defined for the tree!'); |
| 32 | + } |
| 33 | + if (scope.treeData.length == null) { |
| 34 | + if (treeData.label != null) { |
| 35 | + scope.treeData = [treeData]; |
| 36 | + } else { |
| 37 | + alert('treeData should be an array of root branches'); |
| 38 | + } |
| 39 | + } |
| 40 | + for_each_branch = function(f) { |
| 41 | + var do_f, root_branch, _i, _len, _ref, _results; |
| 42 | + do_f = function(branch, level) { |
| 43 | + var child, _i, _len, _ref, _results; |
| 44 | + f(branch, level); |
| 45 | + if (branch.children != null) { |
| 46 | + _ref = branch.children; |
| 47 | + _results = []; |
| 48 | + for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
| 49 | + child = _ref[_i]; |
| 50 | + _results.push(do_f(child, level + 1)); |
| 51 | + } |
| 52 | + return _results; |
| 53 | + } |
| 54 | + }; |
| 55 | + _ref = scope.treeData; |
| 56 | + _results = []; |
| 57 | + for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
| 58 | + root_branch = _ref[_i]; |
| 59 | + _results.push(do_f(root_branch, 1)); |
| 60 | + } |
| 61 | + return _results; |
| 62 | + }; |
| 63 | + for_each_branch(function(branch) { |
| 64 | + if (branch.children) { |
| 65 | + if (branch.children.length > 0) { |
| 66 | + return branch.children = branch.children.map(function(e) { |
| 67 | + if (typeof e === 'string') { |
| 68 | + return { |
| 69 | + label: e, |
| 70 | + children: [] |
| 71 | + }; |
| 72 | + } else { |
| 73 | + return e; |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + } else { |
| 78 | + return branch.children = []; |
| 79 | + } |
| 80 | + }); |
| 81 | + for_each_branch(function(b, level) { |
| 82 | + b.level = level; |
| 83 | + b.expanded = b.level < expand_level; |
| 84 | + return b.uid = "" + Math.random(); |
| 85 | + }); |
| 86 | + expand_all = function() { |
| 87 | + return for_each_branch(function(branch) { |
| 88 | + return branch.expanded = true; |
| 89 | + }); |
| 90 | + }; |
| 91 | + unselect_all = function() { |
| 92 | + return for_each_branch(function(branch) { |
| 93 | + return branch.selected = false; |
| 94 | + }); |
| 95 | + }; |
| 96 | + selected_branch = null; |
| 97 | + select_branch = function(branch) { |
| 98 | + if (branch !== selected_branch) { |
| 99 | + unselect_all(); |
| 100 | + branch.selected = true; |
| 101 | + selected_branch = 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 scope.onSelect({ |
| 109 | + branch: branch |
| 110 | + }); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + }; |
| 115 | + scope.user_clicks_branch = function(branch) { |
| 116 | + if (branch !== selected_branch) { |
| 117 | + return select_branch(branch); |
| 118 | + } |
| 119 | + }; |
| 120 | + scope.tree_rows = []; |
| 121 | + on_treeData_change = function() { |
| 122 | + var add_all, root_branch, _i, _len, _ref, _results; |
| 123 | + scope.tree_rows = []; |
| 124 | + add_all = function(level, branch, visible) { |
| 125 | + var child, child_visible, tree_icon, _i, _len, _ref, _results; |
| 126 | + if (branch.expanded == null) { |
| 127 | + branch.expanded = false; |
| 128 | + } |
| 129 | + if (branch.children.length === 0) { |
| 130 | + tree_icon = attrs.iconLeaf; |
| 131 | + } else { |
| 132 | + if (branch.expanded) { |
| 133 | + tree_icon = attrs.iconCollapse; |
| 134 | + } else { |
| 135 | + tree_icon = attrs.iconExpand; |
| 136 | + } |
| 137 | + } |
| 138 | + scope.tree_rows.push({ |
| 139 | + level: level, |
| 140 | + branch: branch, |
| 141 | + label: branch.label, |
| 142 | + tree_icon: tree_icon, |
| 143 | + visible: visible |
| 144 | + }); |
| 145 | + if (branch.children != null) { |
| 146 | + _ref = branch.children; |
| 147 | + _results = []; |
| 148 | + for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
| 149 | + child = _ref[_i]; |
| 150 | + child_visible = visible && branch.expanded; |
| 151 | + _results.push(add_all(level + 1, child, child_visible)); |
| 152 | + } |
| 153 | + return _results; |
| 154 | + } |
| 155 | + }; |
| 156 | + _ref = scope.treeData; |
| 157 | + _results = []; |
| 158 | + for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
| 159 | + root_branch = _ref[_i]; |
| 160 | + _results.push(add_all(1, root_branch, true)); |
| 161 | + } |
| 162 | + return _results; |
| 163 | + }; |
| 164 | + if (attrs.initialSelection != null) { |
| 165 | + for_each_branch(function(b) { |
| 166 | + if (b.label === attrs.initialSelection) { |
| 167 | + return select_branch(b); |
| 168 | + } |
| 169 | + }); |
| 170 | + } |
| 171 | + return scope.$watch('treeData', on_treeData_change, true); |
| 172 | + } |
| 173 | + }; |
| 174 | +}); |
0 commit comments