Skip to content

Added right click event. #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
38 changes: 28 additions & 10 deletions angular-tree-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
selectedNode: "=?",
expandedNodes: "=?",
onSelection: "&",
onRightClick: "&",
onNodeToggle: "&",
options: "=?",
orderBy: "@",
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 = "";
Expand All @@ -149,7 +164,7 @@
'<li ng-repeat="node in node.' + $scope.options.nodeChildren + ' | orderBy:orderBy:reverseOrder" ng-class="headClass(node)" '+classIfDefined($scope.options.injectClasses.li, true)+'>' +
'<i class="tree-branch-head" ng-class="iBranchClass()" ng-click="selectNodeHead(node)"></i>' +
'<i class="tree-leaf-head '+classIfDefined($scope.options.injectClasses.iLeaf, false)+'"></i>' +
'<div class="tree-label '+classIfDefined($scope.options.injectClasses.label, false)+'" ng-class="selectedClass()" ng-click="selectNodeLabel(node)" tree-transclude></div>' +
'<div class="tree-label '+classIfDefined($scope.options.injectClasses.label, false)+'" ng-class="selectedClass()" ng-click="selectNodeLabel(node)" ng-right-click="rightClickNodeLabel(node)" tree-transclude></div>' +
'<treeitem ng-if="nodeExpanded()"></treeitem>' +
'</li>' +
'</ul>';
Expand All @@ -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 {
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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();
});
Expand Down