Skip to content

Commit 45d4f8d

Browse files
committed
Added context menu example
1 parent 7b82663 commit 45d4f8d

File tree

8 files changed

+294
-27
lines changed

8 files changed

+294
-27
lines changed

example/cli/dist/main.js

Lines changed: 164 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/cli/dist/main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/cli/src/app/app-routing.module.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { VirtualscrollComponent } from './virtualscroll/virtualscroll.component'
1616
import { ApiComponent } from './api/api.component';
1717
import { ActionsComponent } from './actions/actions.component';
1818
import { ScrollContainerComponent } from './scrollcontainer/scrollcontainer.component';
19+
import { ContextmenuComponent } from './contextmenu/contextmenu.component';
1920

2021
const routes: Routes = [
2122
{
@@ -77,6 +78,10 @@ const routes: Routes = [
7778
{
7879
path: 'scroll-container',
7980
component: ScrollContainerComponent
81+
},
82+
{
83+
path: 'context-menu',
84+
component: ContextmenuComponent
8085
}
8186
];
8287

example/cli/src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { VirtualscrollComponent } from './virtualscroll/virtualscroll.component'
2121
import { ApiComponent } from './api/api.component';
2222
import { ActionsComponent } from './actions/actions.component';
2323
import { ScrollContainerComponent } from './scrollcontainer/scrollcontainer.component';
24+
import { ContextmenuComponent } from './contextmenu/contextmenu.component';
2425

2526
@NgModule({
2627
declarations: [
@@ -39,7 +40,8 @@ import { ScrollContainerComponent } from './scrollcontainer/scrollcontainer.comp
3940
VirtualscrollComponent,
4041
ApiComponent,
4142
ActionsComponent,
42-
ScrollContainerComponent
43+
ScrollContainerComponent,
44+
ContextmenuComponent
4345
],
4446
imports: [
4547
BrowserModule,

example/cli/src/app/contextmenu/contextmenu.component.css

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p>
2+
contextmenu works!
3+
</p>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { ContextmenuComponent } from './contextmenu.component';
4+
5+
describe('ContextmenuComponent', () => {
6+
let component: ContextmenuComponent;
7+
let fixture: ComponentFixture<ContextmenuComponent>;
8+
9+
beforeEach(async(() => {
10+
TestBed.configureTestingModule({
11+
declarations: [ ContextmenuComponent ]
12+
})
13+
.compileComponents();
14+
}));
15+
16+
beforeEach(() => {
17+
fixture = TestBed.createComponent(ContextmenuComponent);
18+
component = fixture.componentInstance;
19+
fixture.detectChanges();
20+
});
21+
22+
it('should create', () => {
23+
expect(component).toBeTruthy();
24+
});
25+
});
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { Component, HostListener } from '@angular/core';
2+
import { ITreeOptions, TREE_ACTIONS } from 'angular-tree-component';
3+
4+
@Component({
5+
selector: 'app-contextmenu',
6+
template: `
7+
<tree-root [focused]="true" [options]="options" [nodes]="nodes"></tree-root>
8+
<div class="menu" *ngIf="contextMenu" [style.left.px]="contextMenu.x" [style.top.px]="contextMenu.y">
9+
<div>Menu for {{ contextMenu.node.data.name }}</div>
10+
<hr>
11+
<ul>
12+
<li><a (click)="closeMenu()">Copy</a></li>
13+
<li><a (click)="closeMenu()">Paste</a></li>
14+
<li><a (click)="closeMenu()">Cut</a></li>
15+
</ul>
16+
</div>
17+
<br>
18+
<p>Keys:</p>
19+
down | up | left | right | space | enter
20+
`,
21+
styles: [
22+
`.menu {
23+
position: absolute;
24+
background: rgba(255, 255, 255, 0.9);
25+
padding: 7px;
26+
border-radius: 5px;
27+
box-shadow: 0 0 2px 2px rgba(0,0,0,0.2);
28+
}`,
29+
`ul {
30+
list-style: none;
31+
padding: 0;
32+
margin: 0;
33+
}`,
34+
`li {
35+
padding: 7px;
36+
border-radius: 3px;
37+
cursor: pointer;
38+
}`,
39+
`li:hover {
40+
background-color: aliceblue;
41+
}`,
42+
]
43+
})
44+
export class ContextmenuComponent {
45+
contextMenu = null;
46+
nodes = [
47+
{
48+
name: 'root1',
49+
children: [
50+
{ name: 'child1' },
51+
{ name: 'child2' }
52+
]
53+
},
54+
{
55+
name: 'root2',
56+
children: [
57+
{ name: 'child2.1', children: [] },
58+
{ name: 'child2.2', children: [
59+
{name: 'grandchild2.2.1'}
60+
] }
61+
]
62+
},
63+
{ name: 'root3' },
64+
{ name: 'root4', children: [] },
65+
{ name: 'root5', children: null }
66+
];
67+
68+
options: ITreeOptions = {
69+
actionMapping: {
70+
mouse: {
71+
contextMenu: (treeModel, treeNode, e) => {
72+
e.preventDefault();
73+
if (this.contextMenu && treeNode === this.contextMenu.node) {
74+
return this.closeMenu();
75+
}
76+
this.contextMenu = {
77+
node: treeNode,
78+
x: e.pageX,
79+
y: e.pageY
80+
};
81+
},
82+
click: (treeModel, treeNode, e) => {
83+
this.closeMenu();
84+
TREE_ACTIONS.TOGGLE_ACTIVE(treeModel, treeNode, e);
85+
}
86+
}
87+
}
88+
};
89+
90+
closeMenu = () => {
91+
this.contextMenu = null;
92+
}
93+
}

0 commit comments

Comments
 (0)