Projekt

Obecné

Profil

Stáhnout (1.31 KB) Statistiky
| Větev: | Tag: | Revize:
1
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

    
6
@Component({
7
  selector: 'app-menu',
8
  templateUrl: './menu.component.html',
9
  styleUrls: ['./menu.component.sass'],
10
  providers: [Location, {provide: LocationStrategy, useClass: PathLocationStrategy}]
11
})
12
export class MenuComponent implements OnInit {
13
  _menuItems: MenuItem[];
14
  private _selectedMenuItem: MenuItem;
15

    
16
  constructor(private location: Location, private menuService: MenuService) {
17
  }
18

    
19
  ngOnInit() {
20
    this.menuService.getMenuItems()
21
      .subscribe((data: MenuItem[]) => {
22
        this.setMenuItems(data);
23
      });
24
  }
25

    
26
  onSelect(menuItem: MenuItem): void {
27
    this._selectedMenuItem = menuItem;
28
    this.menuService.getMenuItems(true)
29
      .subscribe((menuItems: MenuItem[]) => this.setMenuItems(menuItems));
30
  }
31

    
32
  private setMenuItems(data: MenuItem[]) {
33
    this._menuItems = data;
34

    
35
    this._selectedMenuItem = this._menuItems[0];
36
    const path = this.location.path().split('/');
37
    const endOfPath = path[path.length - 1];
38

    
39
    for (const item of this._menuItems) {
40
      if (item.routePath === endOfPath) {
41
        this._selectedMenuItem = item;
42
      }
43
    }
44
  }
45
}
(3-3/3)