Skip to content

Commit 2fc503e

Browse files
authored
Merge pull request #55 from msarsha/new-api
closes #43 #36 #7 #4
2 parents 22fb647 + f7dcc86 commit 2fc503e

33 files changed

+771
-918
lines changed

.angular-cli.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"environments": {
2727
"dev": "environments/environment.ts",
2828
"prod": "environments/environment.prod.ts"
29-
}
29+
},
30+
"showCircularDependencies": false
3031
}
3132
],
3233
"e2e": {
@@ -55,6 +56,9 @@
5556
},
5657
"defaults": {
5758
"styleExt": "css",
58-
"component": {}
59+
"component": {},
60+
"build": {
61+
"showCircularDependencies": false
62+
}
5963
}
6064
}

README.md

Lines changed: 65 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,185 +1,105 @@
1-
### WIP !
2-
3-
# ng2-right-click-menu
1+
## ng2-right-click-menu
42
_Right click context menu for Angular 2+_
53

64
__DEMO__ https://msarsha.github.io/ng2-right-click-menu/
75

8-
## How to use
6+
### Dependencies
97

10-
- `npm install --save ng2-right-click-menu`
11-
- import `ShContextMenuModule` into your app module
8+
`@angular/cdk`
129

13-
Add the `[sh-context]` directive to the desired element and bind an `IShContextMenuItem` array.
10+
`@angular/cdk/overlay-prebuilt.css"`
1411

15-
Use the `[sh-data-context]` property to inject a context object of type `any`.
12+
### Setup
1613

17-
````html
18-
<div class="box" [sh-context]="menuItems" [sh-data-context]="dataObject">
19-
// content
20-
</div>
21-
````
14+
`npm install --save ng2-right-click-menu @angular/cdk`
2215

23-
### `[sh-context]` (`IShContextMenuItem[]`)
16+
import `ShContextMenuModule`
2417

2518
````typescript
26-
interface IShContextMenuItem {
27-
label?: ((context: any) => string) | string; // as of version 0.0.11 this property is rendered as HTML
28-
divider?: boolean;
29-
onClick?($event: any): void;
30-
visible?(context: any): boolean;
31-
disabled?(context: any): boolean;
32-
subMenu?: boolean;
33-
subMenuItems?: IShContextMenuItem[];
34-
}
35-
````
36-
37-
Example:
19+
import {ShContextMenuModule} from 'ng2-right-click-menu'
3820

39-
````typescript
40-
items: IShContextMenuItem[];
41-
42-
this.items = [
43-
{
44-
label: '<span class="menu-icon">Save</span>',
45-
onClick: this.clickEvent
46-
},
47-
{
48-
label: (context) => `Edit ${context.someVariable}`,
49-
onClick: this.clickEvent
50-
},
51-
{
52-
label: '<b>Sub</b> Menu',
53-
subMenu: true,
54-
subMenuItems: [
55-
{
56-
label: 'Save',
57-
onClick: this.clickEvent
58-
},
59-
{
60-
label: 'Edit',
61-
onClick: this.clickEvent
62-
}]
63-
}
64-
{
65-
divider: true
66-
},
67-
{
68-
label: 'Remove',
69-
disabled: ctx => {
70-
return ctx.Two === 'Two';
71-
},
72-
onClick: this.clickEvent
73-
},
74-
{
75-
label: (context) => `Hide ${context.name}`,
76-
onClick: this.clickEvent,
77-
visible: ctx => {
78-
return ctx.One === 'One';
79-
}
80-
}
81-
];
82-
83-
clickEvent($event: any){
84-
console.log('clicked ', $event);
85-
};
21+
@NgModule({
22+
//...
23+
imports: [ShContextMenuModule]
24+
//...
25+
})
8626
````
8727

88-
### Passing a function to the label option
28+
import css file in your `styles.css`:
8929

90-
You can pass either a string or a function that returns a string (using the data context as a parameter) to the `label` option of menu items. Passing a function allows the label to contain dynamic content.
91-
92-
### onBeforeMenuOpen (v0.0.14)
93-
94-
The `onBeforeMenuOpen` event can be used to cancel the menu from opening and allow to modify the menu items that will be display by the current event.
95-
96-
The `open()` callback is used to continue the context menu event and can be injected with the new modified `IShContextMenuItem` items array. (optional. if items array is not provided the original array defined by `[sh-context]` will be used.)
97-
98-
````html
99-
<div (onBeforeMenuOpen)="onBefore($event)" [sh-context]="items" [sh-data-context]="dataCtxOne">
100-
Click Me !
101-
</div>
30+
````css
31+
@import "~@angular/cdk/overlay-prebuilt.css";
10232
````
10333

104-
component:
34+
## Usage
10535

106-
````typescript
107-
onBefore = (event: BeforeMenuEvent) => {
108-
event.open([new items]);
109-
};
110-
````
36+
#### Defining a Basic Menu Template
11137

112-
`BeforeMenuEvent` interface:
113-
````typescript
114-
interface BeforeMenuEvent {
115-
event: MouseEvent;
116-
items: IShContextMenuItem[];
117-
open(items?: IShContextMenuItem[]): void;
118-
}
119-
````
38+
The menu template is built using the `sh-context-menu` component as the menu wrapper,
39+
and nested `ng-template` with the `shContextMenuItem` directive for every menu item:
12040

121-
### Options Object (v0.0.10)
41+
The `shContextMenuItem` directive provide a template variable (`let-data`) that gives you access to the data object attached to the menu.
12242

12343
````html
124-
<div [sh-options]="options"></div>
125-
````
126-
127-
````typescript
128-
options: IShContextOptions = {
129-
// set options
130-
}
44+
<sh-context-menu #menu>
45+
<ng-template shContextMenuItem let-data (click)="onClick($event)">
46+
<div>
47+
Menu Item - {{data.label}}
48+
</div>
49+
</ng-template>
50+
</sh-context-menu>
13151
````
13252

133-
The options object is of type `IShContextOptions` and currently support the following options:
53+
#### Attaching Menu To An Element
13454

135-
Options | Type | Default | Description
136-
:---:|:---:|:---:|:---|
137-
rtl|boolean|false|right to left support
138-
theme|string|light|menu color theme
55+
Attaching works by using the `shAttachMenu` directive and providing the `#menu` (from the above example) template variable:
13956

140-
### Sub Menus (v0.0.9)
57+
The object provided to the `[shMenuData]` input will be available as a template variable inside `ng-template`s with `shContextMenuItem`
14158

142-
Setting the `subMenu` property to `true` and the `subMenuItems` property to a `IShContextMenuItem[]` will render a sub menu.
59+
```html
60+
<div [shAttachMenu]="menu" [shMenuData]="data">Right Click Me</div>
61+
```
14362

144-
````typescript
145-
{
146-
label: 'Sub Menu',
147-
subMenu: true,
148-
subMenuItems: [
149-
{
150-
label: 'Save',
151-
onClick: this.clickEvent
152-
},
153-
{
154-
label: 'Edit',
155-
onClick: this.clickEvent
156-
}]
157-
}
158-
````
159-
160-
#### The `onClick` handler
63+
## Sub Menus
16164

162-
The `onClick` handler is a function that is being injected with `$event` parameter.
65+
Sub menu is attached to the `shContextMenuItem` directive using the `[subMenu]` input.
16366

164-
The `$event` structure is:
67+
The `[subMenu]` input is provided with a `sh-context-menu`'s template variable (just like attaching a menu to an element).
16568

166-
````typescript
167-
{
168-
menuItem: item,
169-
dataContext: this.dataContext
170-
}
69+
````html
70+
<sh-context-menu #menu>
71+
<ng-template shContextMenuItem [subMenu]="#nestedMenu">
72+
<div>
73+
Menu Item - {{data.label}}
74+
</div>
75+
</ng-template>
76+
<sh-context-menu #nestedMenu>
77+
<ng-template shContextMenuItem let-data>
78+
<div>
79+
Menu Item - {{data.label}}
80+
</div>
81+
</ng-template>
82+
</sh-context-menu>
83+
</sh-context-menu>
17184
````
17285

173-
Where the `menuItem` property is of type `IShContextMenuItem` and is the clicked menu item.
174-
175-
And the `dataContext` is the object used on the `[sh-data-context]` binding.
86+
## API
17687

88+
#### sh-context-menu
17789

178-
#### The `disabled` and `visible` handlers
90+
Name | Type | Default | Description
91+
:---:|:---:|:---:|:---:
92+
[this]|any|null|the `this` context for input callbacks (visible) - typically the menu's host component
17993

180-
Both get injected with the object used on the `[sh-data-context]` binding
94+
#### shContextMenuItem
18195

182-
And should return a `boolean` to indicate if the current `IShContextMenuItem` is disabled or visible.
96+
Name | Type | Default | Description
97+
:---:|:---:|:---:|:---:
98+
[subMenu]|ShContextMenuComponent|null|sub menu
99+
[divider]|boolean|false|render a divider
100+
[closeOnClick]|boolean|true|should the menu close on click
101+
[visible]|(event: ShContextMenuClickEvent) => boolean|null|function to determine if a item is visible
102+
(click)|ShContextMenuClickEvent|null|click handler
183103

184104

185105
### Setting up development env

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 1.0.0 - BREAKING CHANGES
2+
3+
new api using ng-template [#fc09e66](https://github.com/msarsha/ng2-right-click-menu/commit/fc09e6687ce7ca376708386b8841d5336b3ac82a)
4+
15
### 0.0.16
26

37
z-index increased to bring menu to foreground [#fc09e66](https://github.com/msarsha/ng2-right-click-menu/commit/fc09e6687ce7ca376708386b8841d5336b3ac82a)

lib/src/html.directive.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

lib/src/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)