1
- import { Component , Input , OnInit } from '@angular/core' ;
1
+ import { AfterViewInit , Component , ElementRef , Input , OnDestroy , OnInit } from '@angular/core' ;
2
+ import { NavigationEnd , Router } from '@angular/router' ;
2
3
import { cloneDeep } from 'lodash-es' ;
4
+ import { Subject } from 'rxjs' ;
5
+ import { filter , takeUntil } from 'rxjs/operators' ;
3
6
import { DevuiCommonsService } from '../devui-commons.service' ;
4
7
5
8
@Component ( {
6
9
selector : 'd-sidebar' ,
7
10
templateUrl : './sidebar.component.html' ,
8
11
styleUrls : [ './sidebar.component.scss' ]
9
12
} )
10
- export class SidebarComponent implements OnInit {
13
+ export class SidebarComponent implements OnInit , AfterViewInit , OnDestroy {
11
14
@Input ( ) sideMenuList ;
12
15
@Input ( ) linkType = 'routerLink' ;
13
16
@Input ( ) text = {
14
17
new : 'New' ,
15
18
sunset : 'Sunset'
16
19
} ;
17
20
_navData ;
21
+ private _currentUrl : string = '' ;
22
+ private destroy$ : Subject < void > = new Subject ( ) ;
18
23
componentsDataDisplay ;
19
24
20
25
@Input ( ) set navData ( data ) {
@@ -26,14 +31,40 @@ export class SidebarComponent implements OnInit {
26
31
return this . _navData ;
27
32
}
28
33
29
- constructor ( private commonsService : DevuiCommonsService ) { }
34
+ constructor ( private commonsService : DevuiCommonsService , private router : Router , private ele : ElementRef ) { }
30
35
31
36
ngOnInit ( ) : void {
37
+ this . _currentUrl = this . router . url ;
32
38
this . commonsService . on < any > ( 'searchEvent' ) . subscribe ( term => {
33
39
this . filterData ( term ) ;
34
40
} ) ;
35
41
}
36
42
43
+ ngAfterViewInit ( ) : void {
44
+ this . router . events
45
+ . pipe (
46
+ filter ( ( event ) => event instanceof NavigationEnd ) ,
47
+ takeUntil ( this . destroy$ )
48
+ )
49
+ . subscribe ( ( event ) => {
50
+ const destUrl = ( event as NavigationEnd ) . urlAfterRedirects ;
51
+ if ( this . _currentUrl . endsWith ( 'overview' ) ) {
52
+ setTimeout ( ( ) => {
53
+ const activeItem : HTMLElement = this . ele . nativeElement . querySelector ( '.devui-router-active' ) ;
54
+ if ( activeItem ) {
55
+ activeItem . scrollIntoView ( { block : 'center' } ) ;
56
+ }
57
+ } ) ;
58
+ }
59
+ this . _currentUrl = destUrl ;
60
+ } ) ;
61
+ }
62
+
63
+ ngOnDestroy ( ) : void {
64
+ this . destroy$ . next ( ) ;
65
+ this . destroy$ . complete ( ) ;
66
+ }
67
+
37
68
filterData ( event ) : void {
38
69
this . componentsDataDisplay = cloneDeep ( this . navData ) . filter ( catalog => {
39
70
catalog . children = catalog . children . filter ( item => {
0 commit comments