From f9532326bf588327289e30b89a294fe8cc2cba12 Mon Sep 17 00:00:00 2001 From: tkaplan Date: Tue, 26 Aug 2014 18:04:03 -0700 Subject: [PATCH] Added right click event. --- README.md | 1 + angular-tree-control.js | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 7a280a3..ce97dd4 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ Attributes of angular treecontrol - `tree-model` : [Node|Array[Node]] the tree data on the `$scope`. This can be an array of nodes or a single node. - `selected-node` : [Node] binding for the selected node in the tree. Updating this value updates the selection displayed in the tree. Selecting a node in the tree will update this value. - `expanded-nodes` : [Array[Node]] binding for the expanded nodes in the tree. Updating this value updates the nodes that are expanded in the tree. +- `on-right-click` : [Array[Node]] binding for the expanded nodes in the tree. Updating this value updates the nodes that are expanded in the tree. - `on-selection` : callback called whenever selecting a node in the tree. The callback argument is the selected node. - `on-node-toggle` : callback called whenever a node expands or collapses in the tree. The function arguments are the toggled node and a boolean which is true for expansion, false for collapse. - `options` : different options to customize the tree control. diff --git a/angular-tree-control.js b/angular-tree-control.js index dc004a8..c98834c 100644 --- a/angular-tree-control.js +++ b/angular-tree-control.js @@ -32,6 +32,7 @@ selectedNode: "=?", expandedNodes: "=?", onSelection: "&", + onRightClick: "&", onNodeToggle: "&", options: "=?", orderBy: "@", @@ -79,7 +80,7 @@ $scope.headClass = function(node) { var liSelectionClass = classIfDefined($scope.options.injectClasses.liSelected, false); var injectSelectionClass = ""; - if (liSelectionClass && ($scope.options.equality(this.node, $scope.selectedNode))) + if (liSelectionClass && (this.node == $scope.selectedNode)) injectSelectionClass = " " + liSelectionClass; if ($scope.options.isLeaf(node)) return "tree-leaf" + injectSelectionClass; @@ -134,6 +135,20 @@ } }; + $scope.rightClickNodeLabel = function( selectedNode ){ + if (selectedNode[$scope.options.nodeChildren] && selectedNode[$scope.options.nodeChildren].length > 0 && + !$scope.options.dirSelectable) { + this.selectNodeHead(); + } + else { + if ($scope.selectedNode != selectedNode) { + $scope.selectedNode = selectedNode; + if ($scope.onRightClick) + $scope.onRightClick({node: selectedNode}); + } + } + }; + $scope.selectedClass = function() { var labelSelectionClass = classIfDefined($scope.options.injectClasses.labelSelected, false); var injectSelectionClass = ""; @@ -149,7 +164,7 @@ '
  • ' + '' + '' + - '
    ' + + '
    ' + '' + '
  • ' + ''; @@ -166,7 +181,6 @@ 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 { @@ -221,6 +235,17 @@ } }; }]) + .directive('ngRightClick', function($parse) { + return function(scope, element, attrs) { + var fn = $parse(attrs.ngRightClick); + element.bind('contextmenu', function(event) { + scope.$apply(function() { + event.preventDefault(); + fn(scope, {$event:event}); + }); + }); + }; + }) .directive("treeitem", function() { return { restrict: 'E', @@ -251,13 +276,6 @@ // 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.$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(); });