|
1 |
| -# ng2-right-click-menu |
| 1 | +## ng2-right-click-menu |
2 | 2 | _Right click context menu for Angular 2+_
|
3 | 3 |
|
4 | 4 | __DEMO__ https://msarsha.github.io/ng2-right-click-menu/
|
@@ -36,189 +36,70 @@ import css file in your `styles.css`:
|
36 | 36 | #### Defining a Basic Menu Template
|
37 | 37 |
|
38 | 38 | 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: |
40 | 40 |
|
41 | 41 | The `shContextMenuItem` directive provide a template variable (`let-data`) that gives you access to the data object attached to the menu.
|
42 | 42 |
|
43 | 43 | ````html
|
44 | 44 | <sh-context-menu #menu>
|
45 |
| - <ng-template shContextMenuItem let-data> |
| 45 | + <ng-template shContextMenuItem let-data (click)="onClick($event)"> |
46 | 46 | <div>
|
47 | 47 | Menu Item - {{data.label}}
|
48 | 48 | </div>
|
49 | 49 | </ng-template>
|
50 | 50 | </sh-context-menu>
|
51 | 51 | ````
|
52 | 52 |
|
53 |
| -#### Attaching The Menu To An Element |
| 53 | +#### Attaching Menu To An Element |
54 | 54 |
|
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` |
56 | 58 |
|
57 | 59 | ```html
|
58 |
| -<div [shAnchorFor]="menu" [shMenuData]="data">Right Click Me</div> |
| 60 | +<div [shAttachMenu]="menu" [shMenuData]="data">Right Click Me</div> |
59 | 61 | ```
|
60 | 62 |
|
| 63 | +## Sub Menus |
61 | 64 |
|
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. |
159 | 66 |
|
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). |
161 | 68 |
|
162 | 69 | ````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> |
210 | 84 | ````
|
211 | 85 |
|
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 |
215 | 87 |
|
| 88 | +#### sh-context-menu |
216 | 89 |
|
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 |
218 | 93 |
|
219 |
| -Both get injected with the object used on the `[sh-data-context]` binding |
| 94 | +#### shContextMenuItem |
220 | 95 |
|
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 |
222 | 103 |
|
223 | 104 |
|
224 | 105 | ### Setting up development env
|
|
0 commit comments