|
1 |
| -### WIP ! |
2 |
| - |
3 |
| -# ng2-right-click-menu |
| 1 | +## ng2-right-click-menu |
4 | 2 | _Right click context menu for Angular 2+_
|
5 | 3 |
|
6 | 4 | __DEMO__ https://msarsha.github.io/ng2-right-click-menu/
|
7 | 5 |
|
8 |
| -## How to use |
| 6 | +### Dependencies |
9 | 7 |
|
10 |
| -- `npm install --save ng2-right-click-menu` |
11 |
| -- import `ShContextMenuModule` into your app module |
| 8 | +`@angular/cdk` |
12 | 9 |
|
13 |
| -Add the `[sh-context]` directive to the desired element and bind an `IShContextMenuItem` array. |
| 10 | +`@angular/cdk/overlay-prebuilt.css"` |
14 | 11 |
|
15 |
| -Use the `[sh-data-context]` property to inject a context object of type `any`. |
| 12 | +### Setup |
16 | 13 |
|
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` |
22 | 15 |
|
23 |
| -### `[sh-context]` (`IShContextMenuItem[]`) |
| 16 | +import `ShContextMenuModule` |
24 | 17 |
|
25 | 18 | ````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' |
38 | 20 |
|
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 | +}) |
86 | 26 | ````
|
87 | 27 |
|
88 |
| -### Passing a function to the label option |
| 28 | +import css file in your `styles.css`: |
89 | 29 |
|
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"; |
102 | 32 | ````
|
103 | 33 |
|
104 |
| -component: |
| 34 | +## Usage |
105 | 35 |
|
106 |
| -````typescript |
107 |
| -onBefore = (event: BeforeMenuEvent) => { |
108 |
| - event.open([new items]); |
109 |
| - }; |
110 |
| -```` |
| 36 | +#### Defining a Basic Menu Template |
111 | 37 |
|
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: |
120 | 40 |
|
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. |
122 | 42 |
|
123 | 43 | ````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> |
131 | 51 | ````
|
132 | 52 |
|
133 |
| -The options object is of type `IShContextOptions` and currently support the following options: |
| 53 | +#### Attaching Menu To An Element |
134 | 54 |
|
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: |
139 | 56 |
|
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` |
141 | 58 |
|
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 | +``` |
143 | 62 |
|
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 |
161 | 64 |
|
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. |
163 | 66 |
|
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). |
165 | 68 |
|
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> |
171 | 84 | ````
|
172 | 85 |
|
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 |
176 | 87 |
|
| 88 | +#### sh-context-menu |
177 | 89 |
|
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 |
179 | 93 |
|
180 |
| -Both get injected with the object used on the `[sh-data-context]` binding |
| 94 | +#### shContextMenuItem |
181 | 95 |
|
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 |
183 | 103 |
|
184 | 104 |
|
185 | 105 | ### Setting up development env
|
|
0 commit comments