Skip to content
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

add controllerAs option, to make the context menu work with 1.5 component #95

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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ Add your custom class to top element of the context menu
<div context-menu="menuOptions" context-menu-class="custom_class"></div>
```

## controllerAs

If you are using controllerAs syntax in your controllers or if you are using Angular 1.5's component method, pass the controllerAs value to `cm-controller-as`

```
<my-component context-menu="$ctrl.menuOptions" cm-controller-as="$ctrl"></my-component>
```

## Model Attribute (optional)

In instances where a reference is not passed through the `$itemScope` (i.e. not using `ngRepeat`), there is a `model` attribute that can pass a value.
Expand Down
43 changes: 26 additions & 17 deletions contextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,23 +182,27 @@ angular.module('ui.bootstrap.contextMenu', [])
});
};

$li.on('click', function ($event) {
if($event.which == 1) {
$event.preventDefault();
$scope.$apply(function () {
if (nestedMenu) {
openNestedMenu($event);
} else {
$(event.currentTarget).removeClass('context');
removeContextMenus();

if (angular.isFunction(item[1])) {
item[1].call($scope, $scope, event, modelValue, text, $li);
} else {
item.click.call($scope, $scope, event, modelValue, text, $li);
}
}
});
$li.on('click', function($event) {
if ($event.which == 1) {
$event.preventDefault();
$scope.$apply(function() {
if (nestedMenu) {
openNestedMenu($event);
} else {
$(event.currentTarget).removeClass('context');
removeContextMenus();

if (angular.isFunction(item[1])) {
if (typeof($scope.cmControllerAs) === "string") {
item[1].call($scope[$scope.cmControllerAs], $scope[$scope.cmControllerAs], event, modelValue, text, $li);
} else {
item[1].call($scope, $scope, event, modelValue, text, $li);
}
} else {
item.click.call($scope, $scope, event, modelValue, text, $li);
}
}
});
}
});

Expand Down Expand Up @@ -301,6 +305,11 @@ angular.module('ui.bootstrap.contextMenu', [])

return function ($scope, element, attrs) {
var openMenuEvent = "contextmenu";

if (attrs.cmControllerAs && typeof(attrs.cmControllerAs) === "string") {
$scope.cmControllerAs = attrs.cmControllerAs;
}

if(attrs.contextMenuOn && typeof(attrs.contextMenuOn) === "string"){
openMenuEvent = attrs.contextMenuOn;
}
Expand Down
19 changes: 19 additions & 0 deletions demo/demo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
// Demo App
angular.module('app', ['ui.bootstrap.contextMenu'])

.component('demoComponent', {
template: `<button class="btn btn-default"
context-menu="$ctrl.otherMenuOptions"
cm-controller-as="$ctrl"
model="'Red'">Component Right Click</button>`,
controller: function() {
this.otherMenuOptions = [
['Favorite Color', function ($itemScope, event, modelValue, text, $li) {
alert(modelValue);
console.info($itemScope.otherMenuOptions[0]);
console.info(event);
console.info(modelValue);
console.info(text);
console.info($li);
}]
];
}
})

.controller('DemoController', [
'$scope',
function ($scope) {
Expand Down
3 changes: 3 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
context-menu="otherMenuOptions"
model="'Red'">Right Click</button>

<br/>
<br/>
<demo-component></demo-component>
<br/>
<br/>
<button class="btn btn-default"
Expand Down