Skip to content

Commit 2bcd8b2

Browse files
authored
Merge pull request #49 from frehn/master
"Passive" menu item without <a>.
2 parents 89c794c + aa679ec commit 2bcd8b2

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ The older syntax is deprecated and will be removed in version 1.x. (I have no t
2828
<template contextMenuItem let-item (execute)="showMessage($event.item.name + ' said: ' + $event.item.otherProperty)">
2929
Bye, {{item?.name}}
3030
</template>
31+
<template contextMenuItem passive="true">
32+
Input something: <input type="text">
33+
</template>
3134
</context-menu>
3235
```
3336

@@ -54,6 +57,8 @@ export class MyContextMenuClass {
5457
- 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
5558
that triggered the execution and `item` is the current item.
5659
- The `divider` input parameter is optional. Items default to normal menu items. If `divider` is `true`, all the other inputs are ignored.
60+
- The `passive` input parameter is optional. If `passive` is `true`, the menu item will not emit execute events or close
61+
the context menu when clicked.
5762
- The `enabled` input parameter is optional. Items are enabled by default.
5863
This can be a boolean value or a function definition that takes an item and returns a boolean.
5964
- 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.
@@ -169,6 +174,7 @@ The html that is generated for the context menu looks like this:
169174
<ul class="dropdown-menu">
170175
<li>
171176
<a><!-- the template for each context menu item goes here --></a>
177+
<span><!-- the template for each passive context menu item goes here --></span>
172178
</li>
173179
</ul>
174180
</div>

demo/app.component.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ <h3>Enabled and Visible as Functions</h3>
5353
<template contextMenuItem let-item (execute)="showMessage($event.event)">
5454
Bye, {{item?.name}}
5555
</template>
56+
<template contextMenuItem passive="true">
57+
Input something: <input type="text">
58+
</template>
5659
</context-menu>
5760
</div>
5861
</div>

src/contextMenu.component.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ export interface MouseLocation {
3232
@Component({
3333
selector: 'context-menu',
3434
styles: [
35+
`.passive {
36+
display: block;
37+
padding: 3px 20px;
38+
clear: both;
39+
font-weight: normal;
40+
line-height: @line-height-base;
41+
white-space: nowrap;
42+
}`
3543
],
3644
template:
3745
`<div class="dropdown angular2-contextmenu">
@@ -46,11 +54,17 @@ export interface MouseLocation {
4654
<li *ngFor="let menuItem of visibleMenuItems" [class.disabled]="!isMenuItemEnabled(menuItem)"
4755
[class.divider]="menuItem.divider" [class.dropdown-divider]="useBootstrap4 && menuItem.divider"
4856
[attr.role]="menuItem.divider ? 'separator' : undefined">
49-
<a *ngIf="!menuItem.divider" href [class.dropdown-item]="useBootstrap4"
57+
<a *ngIf="!menuItem.divider && !menuItem.passive" href [class.dropdown-item]="useBootstrap4"
5058
[class.disabled]="useBootstrap4 && !isMenuItemEnabled(menuItem)"
5159
(click)="menuItem.triggerExecute(item, $event); $event.preventDefault(); $event.stopPropagation();">
5260
<template [ngTemplateOutlet]="menuItem.template" [ngOutletContext]="{ $implicit: item }"></template>
5361
</a>
62+
63+
<span (click)="stopEvent($event)" (contextmenu)="stopEvent($event)" class="passive"
64+
*ngIf="!menuItem.divider && menuItem.passive" [class.dropdown-item]="useBootstrap4"
65+
[class.disabled]="useBootstrap4 && !isMenuItemEnabled(menuItem)">
66+
<template [ngTemplateOutlet]="menuItem.template" [ngOutletContext]="{ $implicit: item }"></template>
67+
</span>
5468
</li>
5569
</ul>
5670
</div>
@@ -81,6 +95,10 @@ export class ContextMenuComponent implements AfterContentInit {
8195
_contextMenuService.show.subscribe(menuEvent => this.onMenuEvent(menuEvent));
8296
}
8397

98+
stopEvent($event : MouseEvent) {
99+
$event.stopPropagation()
100+
}
101+
84102
get locationCss(): any {
85103
return {
86104
'position': 'fixed',

src/contextMenu.item.directive.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Directive, Input, Output, EventEmitter, TemplateRef } from '@angular/co
77
})
88
export class ContextMenuItemDirective {
99
@Input() public divider: boolean = false;
10+
@Input() public passive: boolean = false;
1011
@Input() public enabled: boolean | ((item: any) => boolean) = true;
1112
@Input() public visible: boolean | ((item: any) => boolean) = true;
1213
@Output() public execute: EventEmitter<{ event: Event, item: any }> = new EventEmitter<{ event: Event, item: any }>();

0 commit comments

Comments
 (0)