8
8
OnInit ,
9
9
Optional ,
10
10
Renderer2 ,
11
- SimpleChanges
11
+ SimpleChanges ,
12
+ ViewChild
12
13
} from '@angular/core' ;
13
14
import { NavigationEnd , Router } from '@angular/router' ;
14
15
@@ -19,15 +20,26 @@ import { SidebarComponent } from '../sidebar/sidebar.component';
19
20
import { Observable , Subscription } from 'rxjs' ;
20
21
import { filter } from 'rxjs/operators' ;
21
22
import { animate , AnimationEvent , state , style , transition , trigger } from '@angular/animations' ;
23
+ import { SidebarNavGroupService } from './sidebar-nav-group.service' ;
22
24
23
25
@Component ( {
24
26
selector : 'c-sidebar-nav' ,
25
27
templateUrl : './sidebar-nav.component.html' ,
26
28
styleUrls : [ './sidebar-nav.component.scss' ]
27
29
} )
28
30
export class SidebarNavComponent implements OnChanges {
31
+
32
+ constructor (
33
+ @Optional ( ) public sidebar : SidebarComponent ,
34
+ public helper : SidebarNavHelper ,
35
+ public router : Router ,
36
+ private renderer : Renderer2 ,
37
+ private hostElement : ElementRef ,
38
+ private sidebarService : SidebarService
39
+ ) { }
40
+
29
41
@Input ( ) navItems ?: INavData [ ] = [ ] ;
30
- @Input ( ) dropdownMode ? : 'closeInactive ' | 'noAction ' | 'openActive ' = 'closeInactive ' ;
42
+ @Input ( ) dropdownMode : 'path ' | 'none ' | 'close ' = 'path ' ;
31
43
@Input ( ) groupItems ?: boolean ;
32
44
@Input ( ) compact ?: boolean ;
33
45
@@ -48,15 +60,6 @@ export class SidebarNavComponent implements OnChanges {
48
60
49
61
public navItemsArray : INavData [ ] = [ ] ;
50
62
51
- constructor (
52
- @Optional ( ) public sidebar : SidebarComponent ,
53
- public helper : SidebarNavHelper ,
54
- public router : Router ,
55
- private renderer : Renderer2 ,
56
- private hostElement : ElementRef ,
57
- private sidebarService : SidebarService
58
- ) { }
59
-
60
63
public ngOnChanges ( changes : SimpleChanges ) : void {
61
64
this . navItemsArray = Array . isArray ( this . navItems ) ? this . navItems . slice ( ) : [ ] ;
62
65
}
@@ -89,53 +92,61 @@ export class SidebarNavComponent implements OnChanges {
89
92
]
90
93
} )
91
94
export class SidebarNavGroupComponent implements OnInit , OnDestroy {
95
+
96
+ constructor (
97
+ private router : Router ,
98
+ private renderer : Renderer2 ,
99
+ private hostElement : ElementRef ,
100
+ public helper : SidebarNavHelper ,
101
+ private sidebarNavGroupService : SidebarNavGroupService
102
+ ) {
103
+ this . navigationEndObservable = router . events . pipe (
104
+ filter ( ( event : any ) => event instanceof NavigationEnd )
105
+ ) as Observable < NavigationEnd > ;
106
+ }
107
+
92
108
@Input ( ) item : any ;
93
- @Input ( ) dropdownMode : 'closeInactive' | 'noAction' | 'openActive' = 'closeInactive' ;
94
- // @ts -ignore
95
- @Input ( ) show : boolean ;
109
+ @Input ( ) dropdownMode : 'path' | 'none' | 'close' = 'path' ;
110
+ @Input ( ) show ?: boolean ;
96
111
97
112
@HostBinding ( 'class' )
98
- // tslint:disable-next-line:typedef
99
113
get hostClasses ( ) : any {
100
114
return {
101
115
'nav-group' : true ,
102
116
show : this . open
103
117
} ;
104
118
}
105
119
106
- // todo: dropdownMode
120
+ @ ViewChild ( SidebarNavComponent , { read : ElementRef } ) sidebarNav ! : ElementRef ;
107
121
108
122
navigationEndObservable : Observable < NavigationEnd > ;
109
- // @ts -ignore
110
- navSubscription : Subscription ;
123
+ navSubscription ! : Subscription ;
124
+ navGroupSubscription ! : Subscription ;
111
125
112
- // @ts -ignore
113
- public open : boolean ;
126
+ public open ! : boolean ;
114
127
public navItems : INavData [ ] = [ ] ;
115
128
public display : any = { display : 'block' } ;
116
129
117
- constructor (
118
- private router : Router ,
119
- public helper : SidebarNavHelper ,
120
- private hostElement : ElementRef
121
- ) {
122
- this . navigationEndObservable = router . events . pipe (
123
- filter ( ( event : any ) => event instanceof NavigationEnd )
124
- ) as Observable < NavigationEnd > ;
125
- }
126
-
127
130
ngOnInit ( ) : void {
128
131
129
132
this . navItems = [ ...this . item . children ] ;
130
133
131
134
this . navSubscription = this . navigationEndObservable . subscribe ( ( event : NavigationEnd ) => {
132
- const samePath = this . samePath ( event . url ) ;
133
- this . openGroup ( samePath ) ;
135
+ if ( this . dropdownMode !== 'none' ) {
136
+ const samePath = this . samePath ( event . url ) ;
137
+ this . openGroup ( samePath ) ;
138
+ }
134
139
} ) ;
135
140
136
141
if ( this . samePath ( this . router . routerState . snapshot . url ) ) {
137
142
this . openGroup ( true ) ;
138
143
}
144
+
145
+ this . navGroupSubscription = this . sidebarNavGroupService . sidebarNavGroupState$ . subscribe ( next => {
146
+ if ( this . dropdownMode === 'close' && next . sidebarNavGroup && next . sidebarNavGroup !== this ) {
147
+ this . openGroup ( false ) ;
148
+ }
149
+ } ) ;
139
150
}
140
151
141
152
samePath ( url : string ) : boolean {
@@ -154,17 +165,22 @@ export class SidebarNavGroupComponent implements OnInit, OnDestroy {
154
165
155
166
toggleGroup ( $event : any ) : void {
156
167
$event . preventDefault ( ) ;
157
- this . open = ! this . open ;
168
+ this . openGroup ( ! this . open ) ;
169
+ if ( this . open ) {
170
+ this . sidebarNavGroupService . toggle ( { open : this . open , sidebarNavGroup : this } ) ;
171
+ }
158
172
}
159
173
160
174
ngOnDestroy ( ) : void {
161
175
this . navSubscription . unsubscribe ( ) ;
162
176
}
163
177
164
178
onAnimationStart ( $event : AnimationEvent ) {
165
- setTimeout ( ( ) => {
166
- this . display = { display : 'block' } ;
167
- } ) ;
179
+ this . display = { display : 'block' } ;
180
+ if ( $event . toState === 'open' ) {
181
+ const host = this . sidebarNav . nativeElement ;
182
+ this . renderer . setStyle ( host , 'height' , `${ host [ 'scrollHeight' ] } px` ) ;
183
+ }
168
184
}
169
185
170
186
onAnimationDone ( $event : AnimationEvent ) {
0 commit comments