1 |
4bd9d9f6
|
Hung Hoang
|
import {Component, OnInit} from '@angular/core';
|
2 |
|
|
import {MenuItem} from '../models/menu-item.model';
|
3 |
|
|
import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
|
4 |
|
|
import {MenuService} from '../services/util/menu.service';
|
5 |
fd5ab42e
|
Hung Hoang
|
|
6 |
0d1b0550
|
Václav Jirák
|
@Component({
|
7 |
|
|
selector: 'app-menu',
|
8 |
|
|
templateUrl: './menu.component.html',
|
9 |
4bd9d9f6
|
Hung Hoang
|
styleUrls: ['./menu.component.sass'],
|
10 |
|
|
providers: [Location, {provide: LocationStrategy, useClass: PathLocationStrategy}]
|
11 |
0d1b0550
|
Václav Jirák
|
})
|
12 |
|
|
export class MenuComponent implements OnInit {
|
13 |
79d7de40
|
Hung Hoang
|
_menuItems: MenuItem[];
|
14 |
|
|
private _selectedMenuItem: MenuItem;
|
15 |
0d1b0550
|
Václav Jirák
|
|
16 |
4bd9d9f6
|
Hung Hoang
|
constructor(private location: Location, private menuService: MenuService) {
|
17 |
fd5ab42e
|
Hung Hoang
|
}
|
18 |
0d1b0550
|
Václav Jirák
|
|
19 |
fd5ab42e
|
Hung Hoang
|
ngOnInit() {
|
20 |
4bd9d9f6
|
Hung Hoang
|
this.menuService.getMenuItems()
|
21 |
|
|
.subscribe((data: MenuItem[]) => {
|
22 |
405b9b17
|
Hung Hoang
|
this._menuItems = data;
|
23 |
|
|
this._selectedMenuItem = this._menuItems[0];
|
24 |
|
|
const path = this.location.path().split('/');
|
25 |
|
|
const endOfPath = path[path.length - 1];
|
26 |
|
|
|
27 |
|
|
this.setCorrectItemMenu(endOfPath);
|
28 |
4bd9d9f6
|
Hung Hoang
|
});
|
29 |
0d1b0550
|
Václav Jirák
|
}
|
30 |
|
|
|
31 |
|
|
onSelect(menuItem: MenuItem): void {
|
32 |
c335ea37
|
Hung Hoang
|
this.menuService.getMenuItems(true)
|
33 |
405b9b17
|
Hung Hoang
|
.subscribe((menuItems: MenuItem[]) => {
|
34 |
|
|
this._menuItems = menuItems;
|
35 |
|
|
this.setCorrectItemMenu(menuItem.routePath);
|
36 |
|
|
});
|
37 |
c335ea37
|
Hung Hoang
|
}
|
38 |
|
|
|
39 |
405b9b17
|
Hung Hoang
|
private setCorrectItemMenu(routePath: string) {
|
40 |
c335ea37
|
Hung Hoang
|
for (const item of this._menuItems) {
|
41 |
405b9b17
|
Hung Hoang
|
if (item.routePath === routePath) {
|
42 |
c335ea37
|
Hung Hoang
|
this._selectedMenuItem = item;
|
43 |
|
|
}
|
44 |
|
|
}
|
45 |
0d1b0550
|
Václav Jirák
|
}
|
46 |
|
|
}
|