Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/isaacplmann/angular2-contextmenu
-Deprecated in favor of ngx-contextmenu- A context menu built with Angular 2 inspired by ui.bootstrap.contextMenu.
https://github.com/isaacplmann/angular2-contextmenu
angular contextmenu ngx
Last synced: 30 days ago
JSON representation
-Deprecated in favor of ngx-contextmenu- A context menu built with Angular 2 inspired by ui.bootstrap.contextMenu.
- Host: GitHub
- URL: https://github.com/isaacplmann/angular2-contextmenu
- Owner: isaacplmann
- License: mit
- Created: 2016-05-24T18:19:08.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-09-11T21:34:32.000Z (over 7 years ago)
- Last Synced: 2024-10-29T23:13:35.631Z (about 1 month ago)
- Topics: angular, contextmenu, ngx
- Language: TypeScript
- Homepage:
- Size: 777 KB
- Stars: 66
- Watchers: 9
- Forks: 25
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-angular-components - angular2-contextmenu - A context menu built with Angular 2 inspired by ui.bootstrap.contextMenu. (Uncategorized / Uncategorized)
- awesome-angular - angular2-contextmenu - A context menu built with Angular 2 inspired by ui.bootstrap.contextMenu. (Uncategorized / Uncategorized)
- awesome-angular-components - angular2-contextmenu - A context menu built with Angular 2 inspired by ui.bootstrap.contextMenu. (Uncategorized / Uncategorized)
README
# Deprecated - use [ngx-contextmenu](https://github.com/isaacplmann/ngx-contextmenu) instead
# angular2-contextmenu
**This library is being moved to [ngx-contextmenu](https://github.com/isaacplmann/ngx-contextmenu). With the name change comes support for Angular 4 and removal of the old imperative syntax.**
A context menu built with Angular 2 inspired by [ui.bootstrap.contextMenu](https://github.com/Templarian/ui.bootstrap.contextMenu). Bootstrap classes are included in the markup, but there is no explicit dependency on Bootstrap. [Demo](http://plnkr.co/edit/wpJXpEh4zNZ4uCxTURx2?p=preview)
## Installation
- `npm install angular2-contextmenu`
- import ContextMenuModule into your app module## Usage
### Declarative vs. Imperative
With version 0.2.0, there is a new declarative syntax that allows for quite a bit more flexibility and keeps html out of configuration objects.
The older syntax is deprecated and will be removed in version 1.x. (I have no timeline on when I'll release 1.x, but wanted to give everyone advance warning.)### Template
```html
- Right Click: {{item?.name}}
Say hi!
Bye, {{item?.name}}
Input something:
```
### Component Code
```js
@Component({
...
})
export class MyContextMenuClass {
public items = [
{ name: 'John', otherProperty: 'Foo' },
{ name: 'Joe', otherProperty: 'Bar' }
];
@ViewChild(ContextMenuComponent) public basicMenu: ContextMenuComponent;
}
```
## Context Menu Items
- Each context menu item is a `` element with the `contextMenuItem` attribute directive applied.
- If the `item` object is used in the context menu item template, the `let-item` attribute must be applied to the `` element.
** Note: ** Make sure to use the `item?.property` syntax in the template rather than `item.property` as the item will be initially `undefined`.
- Every context menu item emits `execute` events. The `$event` object is of the form `{ event: MouseEvent, item: any }` where `event` is the mouse click event
that triggered the execution and `item` is the current item.
- The `divider` input parameter is optional. Items default to normal menu items. If `divider` is `true`, all the other inputs are ignored.
- The `passive` input parameter is optional. If `passive` is `true`, the menu item will not emit execute events or close
the context menu when clicked.
- The `enabled` input parameter is optional. Items are enabled by default.
This can be a boolean value or a function definition that takes an item and returns a boolean.
- The `visible` input parameter is optional. Items are visible by default. This property enables you to show certain context menu items based on what the data item is.
This can be a boolean value or a function definition that takes an item and returns a boolean.
- Within the template, you have access to any components and variables available in the outer context.
```html
Say hi, {{item?.name}}!
With access to the outside context: {{ outsideValue }}
```
```js
public outsideValue = "something";
public isMenuItemType1(item: any): boolean {
return item.type === 'type1';
}
```
## Binding `this` for `visible` and `enabled` functions
If you need access to properties in your component from within the `enabled` or `visible` functions, you'll need to pass in a version of the function with `this` bound to your component.
```html
```
```js
public outsideValue = "something";
public isMenuItemOutsideValueBound = this.isMenuItemOutsideValue.bind(this);
public isMenuItemOutsideValue(item: any): boolean {
return item.type === this.outsideValue;
}
```
## Multiple Context Menus
You can use multiple context menus in the same component if you would like.
```html
- {{item?.name}}
...
- {{item?.name}}
...
```
```js
@ViewChild('basicMenu') public basicMenu: ContextMenuComponent;
@ViewChild('otherMenu') public otherMenu: ContextMenuComponent;
```
## Context Menu In a Different Component
If your `` component is in a different component from your list, you'll need to wire up the context menu event yourself.
```html
- Right Click: {{item.name}}
```
```js
import { ContextMenuService } from 'angular2-contextmenu';
@Component({
...
})
export class MyContextMenuClass {
public items = [
{ name: 'John', otherProperty: 'Foo' },
{ name: 'Joe', otherProperty: 'Bar' }
];
// Optional
@Input() contextMenu: ContextMenuComponent;
constructor(private contextMenuService: ContextMenuService) {}
public onContextMenu($event: MouseEvent, item: any): void {
this.contextMenuService.show.next({
// Optional - if unspecified, all context menu components will open
contextMenu: this.contextMenu,
event: $event,
item: item,
});
$event.preventDefault();
$event.stopPropagation();
}
}
```
## Triggering the Context Menu with a Different Event
The context menu can be triggered at any point using the method above. For instance, to trigger the context menu
with a left click instead of a right click, use this html:
```html
- Left Click: {{item.name}}
```
This could be `(keydown)`, `(mouseover)`, or `(myCustomEvent)` as well.
## Custom Styles
The html that is generated for the context menu looks like this:
```html
```
You can key off of the `angular2-contextmenu` class to create your own styles. Note that the `ul.dropdown-menu` will have inline styles applied for `position`, `display`, `left` and `top` so that it will be positioned at the cursor when you right-click.
```css
.angular2-contextmenu .dropdown-menu {
border: solid 1px chartreuse;
background-color: darkgreen;
padding: 0;
}
.angular2-contextmenu li {
display: block;
border-top: solid 1px chartreuse;
text-transform: uppercase;
text-align: center;
}
.angular2-contextmenu li:first-child {
border-top:none;
}
.angular2-contextmenu a {
color:chartreuse;
display: block;
padding: 0.5em 1em;
}
.angular2-contextmenu a:hover {
color:darkgreen;
background-color:chartreuse;
}
```
## Bootstrap 4
If you're using Bootstrap 4, you can specify a `useBootstrap4` property in the `forRoot` function of the `ContextMenuModule` in order to get the appropriate class names. Like this:
```js
@NgModule({
import: [
ContextMenuModule.forRoot({
useBootstrap4: true,
}),
],
})
export class AppModule {}
```
**Or, if you want to repeat yourself,** you can add a `useBootstrap4` attribute to each `context-menu` component. Like this:
```html
```
## Close event emitter
There is a `(close)` output EventEmitter that you can subscribe to for notifications when the context menu closes (either by clicking outside or choosing a menu item).
```html
```
## CSS Transforms
The context menu will correctly position itself as long as the `` element does not have a parent element that has a complex transform applied to it. Complex in this case means anything besides a simple 2d translation. So rotate, skew, stretch, scale, z-axis translation will all cause the context menu to appear in unexpected places. The common scenario of rendering an element with `transform: translate3d(0px 0px 0px)` in order to trigger the browser's GPU works just fine.
## Deprecated syntax
This alternate, deprecated syntax will continue working until version 1.x.
### Template
```html
- Right Click: {{item.name}}
```
### Component Code
```js
import { ContextMenuService } from 'angular2-contextmenu';
@Component({
...
})
export class MyContextMenuClass {
public items = [
{ name: 'John', otherProperty: 'Foo' },
{ name: 'Joe', otherProperty: 'Bar' }
];
constructor(private contextMenuService: ContextMenuService) {}
public onContextMenu($event: MouseEvent, item: any): void {
this.contextMenuService.show.next({
actions: [
{
html: (item) => `Say hi!`,
click: (item) => alert('Hi, ' + item.name)
},
{
html: (item) => `Something else`,
click: (item) => alert('Or not...')
},
],
event: $event,
item: item,
});
$event.preventDefault();
$event.stopPropagation();
}
}
```