Skip to content

Commit 8e6000f

Browse files
committed
fix: allow multiple events for context-menu-on
Closes #99
1 parent a1cfb28 commit 8e6000f

3 files changed

Lines changed: 48 additions & 43 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ AngularJS UI Bootstrap Module for adding context menus to elements. [Demo](http:
1414
Add a reference to `contextMenu.js`. In your app config add `ui.bootstrap.contextMenu` as a dependency module.
1515

1616
## Context Menu Settings
17+
- `context-menu-on` - (Default: 'contextmenu') A comma-separated string literal containing the events that will trigger the context menu to appear.
1718
- `context-menu-empty-text` - (Default: 'empty') An angular expression containing the string to be used when the context menu is empty
1819
- `context-menu-class` - A string literal containing a custom class to be added to the context menu (The <ul> elements)
1920
- `allow-event-propagation` - (Default: false) A boolean determining whether to allow event propagation. Note that if you set this to true, and don’t catch it with something else the browser’s context menu will be shown on top of this library’s context menu.

contextMenu.js

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -559,59 +559,62 @@
559559
}
560560

561561
return function ($scope, element, attrs) {
562-
var openMenuEvent = 'contextmenu';
562+
var openMenuEvents = ['contextmenu'];
563563
_emptyText = $scope.$eval(attrs.contextMenuEmptyText) || 'empty';
564564

565565
if(attrs.contextMenuOn && typeof(attrs.contextMenuOn) === 'string'){
566-
openMenuEvent = attrs.contextMenuOn;
566+
openMenuEvents = attrs.contextMenuOn.split(',');
567567
}
568-
element.on(openMenuEvent, function (event) {
569-
if(!attrs.allowEventPropagation) {
570-
event.stopPropagation();
571-
event.preventDefault();
572-
}
573568

574-
// Don't show context menu if on touch device and element is draggable
575-
if(isTouchDevice() && element.attr('draggable') === 'true') {
576-
return false;
577-
}
569+
angular.forEach(openMenuEvents, function (openMenuEvent) {
570+
element.on(openMenuEvent.trim(), function (event) {
571+
if(!attrs.allowEventPropagation) {
572+
event.stopPropagation();
573+
event.preventDefault();
574+
}
578575

579-
// Remove if the user clicks outside
580-
$document.find('body').on('mousedown', removeOnOutsideClickEvent);
581-
// Remove the menu when the scroll moves
582-
$document.on('scroll', removeOnScrollEvent);
576+
// Don't show context menu if on touch device and element is draggable
577+
if(isTouchDevice() && element.attr('draggable') === 'true') {
578+
return false;
579+
}
583580

584-
_clickedElement = event.currentTarget;
585-
$(_clickedElement).addClass('context');
581+
// Remove if the user clicks outside
582+
$document.find('body').on('mousedown', removeOnOutsideClickEvent);
583+
// Remove the menu when the scroll moves
584+
$document.on('scroll', removeOnScrollEvent);
586585

587-
$scope.$apply(function () {
588-
var options = $scope.$eval(attrs.contextMenu);
589-
var customClass = attrs.contextMenuClass;
590-
var modelValue = $scope.$eval(attrs.model);
591-
var orientation = attrs.contextMenuOrientation;
586+
_clickedElement = event.currentTarget;
587+
$(_clickedElement).addClass('context');
592588

593-
$q.when(options).then(function(promisedMenu) {
594-
if (angular.isFunction(promisedMenu)) {
595-
// support for dynamic items
596-
promisedMenu = promisedMenu.call($scope, $scope, event, modelValue);
597-
}
598-
var params = {
599-
'$scope' : $scope,
600-
'event' : event,
601-
'options' : promisedMenu,
602-
'modelValue' : modelValue,
603-
'level' : 0,
604-
'customClass' : customClass,
605-
'orientation': orientation
606-
};
607-
$rootScope.$broadcast(ContextMenuEvents.ContextMenuOpening, { context: _clickedElement });
608-
renderContextMenu(params);
589+
$scope.$apply(function () {
590+
var options = $scope.$eval(attrs.contextMenu);
591+
var customClass = attrs.contextMenuClass;
592+
var modelValue = $scope.$eval(attrs.model);
593+
var orientation = attrs.contextMenuOrientation;
594+
595+
$q.when(options).then(function(promisedMenu) {
596+
if (angular.isFunction(promisedMenu)) {
597+
// support for dynamic items
598+
promisedMenu = promisedMenu.call($scope, $scope, event, modelValue);
599+
}
600+
var params = {
601+
'$scope' : $scope,
602+
'event' : event,
603+
'options' : promisedMenu,
604+
'modelValue' : modelValue,
605+
'level' : 0,
606+
'customClass' : customClass,
607+
'orientation': orientation
608+
};
609+
$rootScope.$broadcast(ContextMenuEvents.ContextMenuOpening, { context: _clickedElement });
610+
renderContextMenu(params);
611+
});
609612
});
610-
});
611613

612-
// Remove all context menus if the scope is destroyed
613-
$scope.$on('$destroy', function () {
614-
removeAllContextMenus();
614+
// Remove all context menus if the scope is destroyed
615+
$scope.$on('$destroy', function () {
616+
removeAllContextMenus();
617+
});
615618
});
616619
});
617620
};

demo/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
<br/>
3333
<button class="btn btn-default"
3434
context-menu="otherMenuOptions"
35-
model="'Red'">Right Click</button>
35+
context-menu-on="click, contextmenu"
36+
model="'Red'">Left or Right Click</button>
3637

3738
<br/>
3839
<br/>

0 commit comments

Comments
 (0)