| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | 1 1 1 1454 53 53 53 53 97 75 97 97 53 1 23616 1166 1 4538 1 244780 244780 244780 244780 663814 489550 244780 1 133128 10738 122390 122390 122390 122390 122390 1 4508 1 1476 1476 1476 1476 1476 1476 1476 1476 1476 1476 1476 1476 1476 1476 1476 1476 1 1 17039 390 22 368 16649 80 80 80 80 80 80 80 10 80 1 4636 38 4598 129 90 33 96 80 3141 3141 3141 4 3141 2296 845 329 516 80 4526 450 4076 80 3106 80 33 33 33 33 28 5 5 9 5 5 5 33 33 33 33 80 29 29 3 26 1 25 25 9 9 4 2 2 9 7 7 2 16 13 13 3 2 1 1 25 25 25 25 80 4526 4526 4526 4526 4 4526 80 4526 4526 4526 80 14 80 14 80 80 80 80 1 80 79 80 80 80 84 84 3 81 81 81 80 115 115 115 115 115 122 122 122 115 1163 1163 11139 11139 53 53 1163 1110 115 80 80 80 80 1396 1396 80 150 150 80 1396 1396 279 116023 111 111 1396 2 1394 22 22 6 2 22 1396 1396 1396 1396 1396 1396 1396 1396 1396 1396 1396 18 1396 1396 1396 | /* commonjs package manager support (eg componentjs) */ Iif (typeof module !== "undefined" && typeof exports !== "undefined" && module.exports === exports){ module.exports = 'treeControl'; } (function ( angular ) { 'use strict'; function createPath(startScope) { return function path() { var _path = []; var scope = startScope; var prevNode; while (scope && scope.node !== startScope.synteticRoot) { if (prevNode !== scope.node) _path.push(scope.node); prevNode = scope.node; scope = scope.$parent; } return _path; } } function ensureDefault(obj, prop, value) { if (!obj.hasOwnProperty(prop)) obj[prop] = value; } function defaultIsLeaf(node, $scope) { return !node[$scope.options.nodeChildren] || node[$scope.options.nodeChildren].length === 0; } function shallowCopy(src, dst) { Iif (angular.isArray(src)) { dst = dst || []; for (var i = 0; i < src.length; i++) { dst[i] = src[i]; } } else Eif (angular.isObject(src)) { dst = dst || {}; for (var key in src) { if (hasOwnProperty.call(src, key) && !(key.charAt(0) === '$' && key.charAt(1) === '$')) { dst[key] = src[key]; } } } return dst || src; } function defaultEquality(a, b,$scope) { if (!a || !b) return false; a = shallowCopy(a); a[$scope.options.nodeChildren] = []; b = shallowCopy(b); b[$scope.options.nodeChildren] = []; return angular.equals(a, b); } function defaultIsSelectable() { return true; } function ensureAllDefaultOptions($scope) { ensureDefault($scope.options, "multiSelection", false); ensureDefault($scope.options, "nodeChildren", "children"); ensureDefault($scope.options, "dirSelectable", "true"); ensureDefault($scope.options, "injectClasses", {}); ensureDefault($scope.options.injectClasses, "ul", ""); ensureDefault($scope.options.injectClasses, "li", ""); ensureDefault($scope.options.injectClasses, "liSelected", ""); ensureDefault($scope.options.injectClasses, "iExpanded", ""); ensureDefault($scope.options.injectClasses, "iCollapsed", ""); ensureDefault($scope.options.injectClasses, "iLeaf", ""); ensureDefault($scope.options.injectClasses, "label", ""); ensureDefault($scope.options.injectClasses, "labelSelected", ""); ensureDefault($scope.options, "equality", defaultEquality); ensureDefault($scope.options, "isLeaf", defaultIsLeaf); ensureDefault($scope.options, "allowDeselect", true); ensureDefault($scope.options, "isSelectable", defaultIsSelectable); } angular.module( 'treeControl', [] ) .constant('treeConfig', { templateUrl: null }) .directive( 'treecontrol', ['$compile', function( $compile ) { /** * @param cssClass - the css class * @param addClassProperty - should we wrap the class name with class="" */ function classIfDefined(cssClass, addClassProperty) { if (cssClass) { if (addClassProperty) return 'class="' + cssClass + '"'; else return cssClass; } else return ""; } return { restrict: 'EA', require: "treecontrol", transclude: true, scope: { treeModel: "=", selectedNode: "=?", selectedNodes: "=?", expandedNodes: "=?", onSelection: "&", onNodeToggle: "&", options: "=?", orderBy: "=?", reverseOrder: "@", filterExpression: "=?", filterComparator: "=?" }, controller: ['$scope', '$templateCache', '$interpolate', 'treeConfig', function ($scope, $templateCache, $interpolate, treeConfig) { $scope.options = $scope.options || {}; ensureAllDefaultOptions($scope); $scope.selectedNodes = $scope.selectedNodes || []; $scope.expandedNodes = $scope.expandedNodes || []; $scope.expandedNodesMap = {}; for (var i=0; i < $scope.expandedNodes.length; i++) { $scope.expandedNodesMap["a"+i] = $scope.expandedNodes[i]; } $scope.parentScopeOfTree = $scope.$parent; function isSelectedNode(node) { if (!$scope.options.multiSelection && ($scope.options.equality(node, $scope.selectedNode , $scope))) return true; else if ($scope.options.multiSelection && $scope.selectedNodes) { for (var i = 0; (i < $scope.selectedNodes.length); i++) { if ($scope.options.equality(node, $scope.selectedNodes[i] , $scope)) { return true; } } return false; } } $scope.headClass = function(node) { var liSelectionClass = classIfDefined($scope.options.injectClasses.liSelected, false); var injectSelectionClass = ""; if (liSelectionClass && isSelectedNode(node)) injectSelectionClass = " " + liSelectionClass; if ($scope.options.isLeaf(node, $scope)) return "tree-leaf" + injectSelectionClass; if ($scope.expandedNodesMap[this.$id]) return "tree-expanded" + injectSelectionClass; else return "tree-collapsed" + injectSelectionClass; }; $scope.iBranchClass = function() { if ($scope.expandedNodesMap[this.$id]) return classIfDefined($scope.options.injectClasses.iExpanded); else return classIfDefined($scope.options.injectClasses.iCollapsed); }; $scope.nodeExpanded = function() { return !!$scope.expandedNodesMap[this.$id]; }; $scope.selectNodeHead = function() { var transcludedScope = this; var expanding = $scope.expandedNodesMap[transcludedScope.$id] === undefined; $scope.expandedNodesMap[transcludedScope.$id] = (expanding ? transcludedScope.node : undefined); if (expanding) { $scope.expandedNodes.push(transcludedScope.node); } else { var index; for (var i=0; (i < $scope.expandedNodes.length) && !index; i++) { if ($scope.options.equality($scope.expandedNodes[i], transcludedScope.node , $scope)) { index = i; } } Eif (index !== undefined) $scope.expandedNodes.splice(index, 1); } Eif ($scope.onNodeToggle) { var parentNode = (transcludedScope.$parent.node === transcludedScope.synteticRoot)?null:transcludedScope.$parent.node; var path = createPath(transcludedScope); $scope.onNodeToggle({node: transcludedScope.node, $parentNode: parentNode, $path: path, $index: transcludedScope.$index, $first: transcludedScope.$first, $middle: transcludedScope.$middle, $last: transcludedScope.$last, $odd: transcludedScope.$odd, $even: transcludedScope.$even, expanded: expanding}); } }; $scope.selectNodeLabel = function( selectedNode){ var transcludedScope = this; if(!$scope.options.isLeaf(selectedNode, $scope) && (!$scope.options.dirSelectable || !$scope.options.isSelectable(selectedNode))) { // Branch node is not selectable, expand this.selectNodeHead(); } else if($scope.options.isLeaf(selectedNode, $scope) && (!$scope.options.isSelectable(selectedNode))) { // Leaf node is not selectable return; } else { var selected = false; if ($scope.options.multiSelection) { var pos = -1; for (var i=0; i < $scope.selectedNodes.length; i++) { if($scope.options.equality(selectedNode, $scope.selectedNodes[i] , $scope)) { pos = i; break; } } if (pos === -1) { $scope.selectedNodes.push(selectedNode); selected = true; } else { $scope.selectedNodes.splice(pos, 1); } } else { if (!$scope.options.equality(selectedNode, $scope.selectedNode , $scope)) { $scope.selectedNode = selectedNode; selected = true; } else { if ($scope.options.allowDeselect) { $scope.selectedNode = undefined; } else { $scope.selectedNode = selectedNode; selected = true; } } } Eif ($scope.onSelection) { var parentNode = (transcludedScope.$parent.node === transcludedScope.synteticRoot)?null:transcludedScope.$parent.node; var path = createPath(transcludedScope) $scope.onSelection({node: selectedNode, selected: selected, $parentNode: parentNode, $path: path, $index: transcludedScope.$index, $first: transcludedScope.$first, $middle: transcludedScope.$middle, $last: transcludedScope.$last, $odd: transcludedScope.$odd, $even: transcludedScope.$even}); } } }; $scope.selectedClass = function() { var isThisNodeSelected = isSelectedNode(this.node); var labelSelectionClass = classIfDefined($scope.options.injectClasses.labelSelected, false); var injectSelectionClass = ""; if (labelSelectionClass && isThisNodeSelected) injectSelectionClass = " " + labelSelectionClass; return isThisNodeSelected ? "tree-selected" + injectSelectionClass : ""; }; $scope.unselectableClass = function() { var isThisNodeUnselectable = !$scope.options.isSelectable(this.node); var labelUnselectableClass = classIfDefined($scope.options.injectClasses.labelUnselectable, false); return isThisNodeUnselectable ? "tree-unselectable " + labelUnselectableClass : ""; }; //tree template $scope.isReverse = function() { return !($scope.reverseOrder === 'false' || $scope.reverseOrder === 'False' || $scope.reverseOrder === '' || $scope.reverseOrder === false); }; $scope.orderByFunc = function() { return $scope.orderBy; }; // return "" + $scope.orderBy; var templateOptions = { orderBy: $scope.orderBy ? " | orderBy:orderByFunc():isReverse()" : '', ulClass: classIfDefined($scope.options.injectClasses.ul, true), nodeChildren: $scope.options.nodeChildren, liClass: classIfDefined($scope.options.injectClasses.li, true), iLeafClass: classIfDefined($scope.options.injectClasses.iLeaf, false), labelClass: classIfDefined($scope.options.injectClasses.label, false) }; var template; var templateUrl = $scope.options.templateUrl || treeConfig.templateUrl; if(templateUrl) { template = $templateCache.get(templateUrl); } if(!template) { template = '<ul {{options.ulClass}} >' + '<li ng-repeat="node in node.{{options.nodeChildren}} | filter:filterExpression:filterComparator {{options.orderBy}}" ng-class="headClass(node)" {{options.liClass}}' + 'set-node-to-data>' + '<i class="tree-branch-head" ng-class="iBranchClass()" ng-click="selectNodeHead(node)"></i>' + '<i class="tree-leaf-head {{options.iLeafClass}}"></i>' + '<div class="tree-label {{options.labelClass}}" ng-class="[selectedClass(), unselectableClass()]" ng-click="selectNodeLabel(node)" tree-transclude></div>' + '<treeitem ng-if="nodeExpanded()"></treeitem>' + '</li>' + '</ul>'; } this.template = $compile($interpolate(template)({options: templateOptions})); }], compile: function(element, attrs, childTranscludeFn) { return function ( scope, element, attrs, treemodelCntr ) { scope.$watch("treeModel", function updateNodeOnRootScope(newValue) { Eif (angular.isArray(newValue)) { if (angular.isDefined(scope.node) && angular.equals(scope.node[scope.options.nodeChildren], newValue)) return; scope.node = {}; scope.synteticRoot = scope.node; scope.node[scope.options.nodeChildren] = newValue; } else { if (angular.equals(scope.node, newValue)) return; scope.node = newValue; } }); scope.$watchCollection('expandedNodes', function(newValue, oldValue) { var notFoundIds = 0; var newExpandedNodesMap = {}; var $liElements = element.find('li'); var existingScopes = []; // find all nodes visible on the tree and the scope $id of the scopes including them angular.forEach($liElements, function(liElement) { var $liElement = angular.element(liElement); var liScope = { $id: $liElement.data('scope-id'), node: $liElement.data('node') }; existingScopes.push(liScope); }); // iterate over the newValue, the new expanded nodes, and for each find it in the existingNodesAndScopes // if found, add the mapping $id -> node into newExpandedNodesMap // if not found, add the mapping num -> node into newExpandedNodesMap angular.forEach(newValue, function(newExNode) { var found = false; for (var i=0; (i < existingScopes.length) && !found; i++) { var existingScope = existingScopes[i]; if (scope.options.equality(newExNode, existingScope.node , scope)) { newExpandedNodesMap[existingScope.$id] = existingScope.node; found = true; } } if (!found) newExpandedNodesMap['a' + notFoundIds++] = newExNode; }); scope.expandedNodesMap = newExpandedNodesMap; }); // scope.$watch('expandedNodesMap', function(newValue) { // // }); //Rendering template for a root node treemodelCntr.template( scope, function(clone) { element.html('').append( clone ); }); // save the transclude function from compile (which is not bound to a scope as apposed to the one from link) // we can fix this to work with the link transclude function with angular 1.2.6. as for angular 1.2.0 we need // to keep using the compile function scope.$treeTransclude = childTranscludeFn; }; } }; }]) .directive("setNodeToData", ['$parse', function($parse) { return { restrict: 'A', link: function($scope, $element, $attrs) { $element.data('node', $scope.node); $element.data('scope-id', $scope.$id); } }; }]) .directive("treeitem", function() { return { restrict: 'E', require: "^treecontrol", link: function( scope, element, attrs, treemodelCntr) { // Rendering template for the current node treemodelCntr.template(scope, function(clone) { element.html('').append(clone); }); } }; }) .directive("treeTransclude", function () { return { controller: function ($scope) { ensureAllDefaultOptions($scope); }, link: function(scope, element, attrs, controller) { if (!scope.options.isLeaf(scope.node, scope)) { angular.forEach(scope.expandedNodesMap, function (node, id) { if (scope.options.equality(node, scope.node , scope)) { scope.expandedNodesMap[scope.$id] = scope.node; scope.expandedNodesMap[id] = undefined; } }); } if (!scope.options.multiSelection && scope.options.equality(scope.node, scope.selectedNode , scope)) { scope.selectedNode = scope.node; } else if (scope.options.multiSelection) { var newSelectedNodes = []; for (var i = 0; (i < scope.selectedNodes.length); i++) { if (scope.options.equality(scope.node, scope.selectedNodes[i] , scope)) { newSelectedNodes.push(scope.node); } } scope.selectedNodes = newSelectedNodes; } // create a scope for the transclusion, whos parent is the parent of the tree control scope.transcludeScope = scope.parentScopeOfTree.$new(); scope.transcludeScope.node = scope.node; scope.transcludeScope.$path = createPath(scope); scope.transcludeScope.$parentNode = (scope.$parent.node === scope.synteticRoot)?null:scope.$parent.node; scope.transcludeScope.$index = scope.$index; scope.transcludeScope.$first = scope.$first; scope.transcludeScope.$middle = scope.$middle; scope.transcludeScope.$last = scope.$last; scope.transcludeScope.$odd = scope.$odd; scope.transcludeScope.$even = scope.$even; scope.$on('$destroy', function() { scope.transcludeScope.$destroy(); }); scope.$treeTransclude(scope.transcludeScope, function(clone) { element.empty(); element.append(clone); }); } }; }); })( angular ); |