Skip to content

Commit fba484e

Browse files
committed
Adding context-menu-disabled attribute
1 parent 0952d2d commit fba484e

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ var app = angular.module('menu-demo', ['ngRoute', 'ng-context-menu'])
3030
menu in a container with padding/margin/etc so it's best to place it as a direct child of the body element as is
3131
shown in this example.
3232

33+
#### Disabling the contextmenu
34+
35+
If you need to disable the contextmenu in certain circumstances, you can add an expression to the
36+
```context-menu-disabled``` attribute. If the expression evaluates to true, the contextmenu will be
37+
disabled, for example, ```context-menu-disabled="1 === 1"```
38+
3339
That's it, I hope you find this useful!
3440

3541
«–– [Ian](http://ianvonwalter.com)

dist/ng-context-menu.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ angular
1111
link: function($scope, element, attrs) {
1212
var opened = false,
1313
openTarget,
14+
disabled = $scope.$eval(attrs.contextMenuDisabled),
1415
win = angular.element($window),
1516
menuElement = angular.element(document.getElementById(attrs.target)),
1617
fn = $parse(attrs.contextMenu);
@@ -30,25 +31,27 @@ angular
3031
menuElement.css('position', 'absolute');
3132

3233
element.bind('contextmenu', function(event) {
33-
openTarget = event.target;
34-
event.preventDefault();
35-
event.stopPropagation();
36-
$scope.$apply(function() {
37-
fn($scope, { $event: event });
38-
open(event, menuElement);
39-
});
34+
if (!disabled) {
35+
openTarget = event.target;
36+
event.preventDefault();
37+
event.stopPropagation();
38+
$scope.$apply(function() {
39+
fn($scope, { $event: event });
40+
open(event, menuElement);
41+
});
42+
}
4043
});
4144

4245
win.bind('keyup', function(event) {
43-
if (opened && event.keyCode === 27) {
46+
if (!disabled && opened && event.keyCode === 27) {
4447
$scope.$apply(function() {
4548
close(menuElement);
4649
});
4750
}
4851
});
4952

5053
function handleWindowClickEvent(event) {
51-
if (opened && (event.button !== 2 || event.target !== openTarget)) {
54+
if (!disabled && opened && (event.button !== 2 || event.target !== openTarget)) {
5255
$scope.$apply(function() {
5356
close(menuElement);
5457
});

dist/ng-context-menu.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ng-context-menu.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ angular
1111
link: function($scope, element, attrs) {
1212
var opened = false,
1313
openTarget,
14+
disabled = $scope.$eval(attrs.contextMenuDisabled),
1415
win = angular.element($window),
1516
menuElement = angular.element(document.getElementById(attrs.target)),
1617
fn = $parse(attrs.contextMenu);
@@ -30,25 +31,27 @@ angular
3031
menuElement.css('position', 'absolute');
3132

3233
element.bind('contextmenu', function(event) {
33-
openTarget = event.target;
34-
event.preventDefault();
35-
event.stopPropagation();
36-
$scope.$apply(function() {
37-
fn($scope, { $event: event });
38-
open(event, menuElement);
39-
});
34+
if (!disabled) {
35+
openTarget = event.target;
36+
event.preventDefault();
37+
event.stopPropagation();
38+
$scope.$apply(function() {
39+
fn($scope, { $event: event });
40+
open(event, menuElement);
41+
});
42+
}
4043
});
4144

4245
win.bind('keyup', function(event) {
43-
if (opened && event.keyCode === 27) {
46+
if (!disabled && opened && event.keyCode === 27) {
4447
$scope.$apply(function() {
4548
close(menuElement);
4649
});
4750
}
4851
});
4952

5053
function handleWindowClickEvent(event) {
51-
if (opened && (event.button !== 2 || event.target !== openTarget)) {
54+
if (!disabled && opened && (event.button !== 2 || event.target !== openTarget)) {
5255
$scope.$apply(function() {
5356
close(menuElement);
5457
});

0 commit comments

Comments
 (0)