Skip to content

Commit c2b96e0

Browse files
committed
readme
1 parent e989950 commit c2b96e0

File tree

5 files changed

+40
-209
lines changed

5 files changed

+40
-209
lines changed

README.md

Lines changed: 38 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ng2-right-click-menu
1+
## ng2-right-click-menu
22
_Right click context menu for Angular 2+_
33

44
__DEMO__ https://msarsha.github.io/ng2-right-click-menu/
@@ -36,189 +36,70 @@ import css file in your `styles.css`:
3636
#### Defining a Basic Menu Template
3737

3838
The menu template is built using the `sh-context-menu` component as the menu wrapper,
39-
and nesting `ng-template` with the `shContextMenuItem` directive for every menu item:
39+
and nested `ng-template` with the `shContextMenuItem` directive for every menu item:
4040

4141
The `shContextMenuItem` directive provide a template variable (`let-data`) that gives you access to the data object attached to the menu.
4242

4343
````html
4444
<sh-context-menu #menu>
45-
<ng-template shContextMenuItem let-data>
45+
<ng-template shContextMenuItem let-data (click)="onClick($event)">
4646
<div>
4747
Menu Item - {{data.label}}
4848
</div>
4949
</ng-template>
5050
</sh-context-menu>
5151
````
5252

53-
#### Attaching The Menu To An Element
53+
#### Attaching Menu To An Element
5454

55-
Attaching works by using the `shMenu` directive and providing the `#menu` template variable:
55+
Attaching works by using the `shAttachMenu` directive and providing the `#menu` (from the above example) template variable:
56+
57+
The object provided to the `[shMenuData]` input will be available as a template variable inside `ng-template`s with `shContextMenuItem`
5658

5759
```html
58-
<div [shAnchorFor]="menu" [shMenuData]="data">Right Click Me</div>
60+
<div [shAttachMenu]="menu" [shMenuData]="data">Right Click Me</div>
5961
```
6062

63+
## Sub Menus
6164

62-
### `[sh-context]` (`IShContextMenuItem[]`)
63-
64-
````typescript
65-
interface IShContextMenuItem {
66-
label?: ((context: any) => string) | string; // as of version 0.0.11 this property is rendered as HTML
67-
divider?: boolean;
68-
onClick?($event: any): void;
69-
visible?(context: any): boolean;
70-
disabled?(context: any): boolean;
71-
subMenu?: boolean;
72-
subMenuItems?: IShContextMenuItem[];
73-
}
74-
````
75-
76-
Example:
77-
78-
````typescript
79-
items: IShContextMenuItem[];
80-
81-
this.items = [
82-
{
83-
label: '<span class="menu-icon">Save</span>',
84-
onClick: this.clickEvent
85-
},
86-
{
87-
label: (context) => `Edit ${context.someVariable}`,
88-
onClick: this.clickEvent
89-
},
90-
{
91-
label: '<b>Sub</b> Menu',
92-
subMenu: true,
93-
subMenuItems: [
94-
{
95-
label: 'Save',
96-
onClick: this.clickEvent
97-
},
98-
{
99-
label: 'Edit',
100-
onClick: this.clickEvent
101-
}]
102-
}
103-
{
104-
divider: true
105-
},
106-
{
107-
label: 'Remove',
108-
disabled: ctx => {
109-
return ctx.Two === 'Two';
110-
},
111-
onClick: this.clickEvent
112-
},
113-
{
114-
label: (context) => `Hide ${context.name}`,
115-
onClick: this.clickEvent,
116-
visible: ctx => {
117-
return ctx.One === 'One';
118-
}
119-
}
120-
];
121-
122-
clickEvent($event: any){
123-
console.log('clicked ', $event);
124-
};
125-
````
126-
127-
### Passing a function to the label option
128-
129-
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.
130-
131-
### onBeforeMenuOpen (v0.0.14)
132-
133-
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.
134-
135-
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.)
136-
137-
````html
138-
<div (onBeforeMenuOpen)="onBefore($event)" [sh-context]="items" [sh-data-context]="dataCtxOne">
139-
Click Me !
140-
</div>
141-
````
142-
143-
component:
144-
145-
````typescript
146-
onBefore = (event: BeforeMenuEvent) => {
147-
event.open([new items]);
148-
};
149-
````
150-
151-
`BeforeMenuEvent` interface:
152-
````typescript
153-
interface BeforeMenuEvent {
154-
event: MouseEvent;
155-
items: IShContextMenuItem[];
156-
open(items?: IShContextMenuItem[]): void;
157-
}
158-
````
65+
Sub menu is attached to the `shContextMenuItem` directive using the `[subMenu]` input.
15966

160-
### Options Object (v0.0.10)
67+
The `[subMenu]` input is provided with a `sh-context-menu`'s template variable (just like attaching a menu to an element).
16168

16269
````html
163-
<div [sh-options]="options"></div>
164-
````
165-
166-
````typescript
167-
options: IShContextOptions = {
168-
// set options
169-
}
170-
````
171-
172-
The options object is of type `IShContextOptions` and currently support the following options:
173-
174-
Options | Type | Default | Description
175-
:---:|:---:|:---:|:---|
176-
rtl|boolean|false|right to left support
177-
theme|string|light|menu color theme
178-
179-
### Sub Menus (v0.0.9)
180-
181-
Setting the `subMenu` property to `true` and the `subMenuItems` property to a `IShContextMenuItem[]` will render a sub menu.
182-
183-
````typescript
184-
{
185-
label: 'Sub Menu',
186-
subMenu: true,
187-
subMenuItems: [
188-
{
189-
label: 'Save',
190-
onClick: this.clickEvent
191-
},
192-
{
193-
label: 'Edit',
194-
onClick: this.clickEvent
195-
}]
196-
}
197-
````
198-
199-
#### The `onClick` handler
200-
201-
The `onClick` handler is a function that is being injected with `$event` parameter.
202-
203-
The `$event` structure is:
204-
205-
````typescript
206-
{
207-
menuItem: item,
208-
dataContext: this.dataContext
209-
}
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>
21084
````
21185

212-
Where the `menuItem` property is of type `IShContextMenuItem` and is the clicked menu item.
213-
214-
And the `dataContext` is the object used on the `[sh-data-context]` binding.
86+
## API
21587

88+
#### sh-context-menu
21689

217-
#### 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
21893

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

221-
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
222103

223104

224105
### Setting up development env

src/lib/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export {ShContextMenuModule} from './sh-context-menu.module';
22
export {ShContextMenuComponent} from './sh-context-menu.component';
3-
export {ShAttachMenuDirective} from './sh-anchor-for.directive';
3+
export {ShAttachMenuDirective} from './sh-attach-menu.directive';
44
export {ShContextMenuItemDirective} from './sh-context-menu-item.directive';
55
export {ShContextMenuService} from './sh-context-menu.service';

src/lib/src/sh-anchor-for-sub.directive.ts

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

src/lib/src/sh-context-menu.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {NgModule} from '@angular/core';
22
import {CommonModule} from '@angular/common';
33

44
import {ShContextMenuComponent} from './sh-context-menu.component';
5-
import {ShAttachMenuDirective} from './sh-anchor-for.directive';
5+
import {ShAttachMenuDirective} from './sh-attach-menu.directive';
66
import {ShContextMenuService} from './sh-context-menu.service';
77
import {ShContextMenuItemDirective} from './sh-context-menu-item.directive';
88
import {OverlayModule} from '@angular/cdk/overlay';

0 commit comments

Comments
 (0)