1
- import { Component , Input , Inject , OnInit , OnDestroy , Renderer2 , ElementRef } from '@angular/core' ;
2
1
import { DOCUMENT } from '@angular/common' ;
3
-
2
+ import { Component , EventEmitter , HostBinding , Inject , Input , OnDestroy , OnInit , Output , Renderer2 } from '@angular/core' ;
4
3
import { sidebarCssClasses } from '../shared' ;
5
4
6
5
@Component ( {
@@ -11,32 +10,51 @@ export class AppSidebarComponent implements OnInit, OnDestroy {
11
10
@Input ( ) compact : boolean ;
12
11
@Input ( ) display : any ;
13
12
@Input ( ) fixed : boolean ;
14
- @Input ( ) minimized : boolean ;
15
13
@Input ( ) offCanvas : boolean ;
16
14
15
+ @Input ( )
16
+ get minimized ( ) {
17
+ return this . _minimized ;
18
+ }
19
+ set minimized ( value : boolean ) {
20
+ // only update / emit events when the value changes
21
+ if ( this . _minimized !== value ) {
22
+ this . _minimized = value ;
23
+ this . _updateMinimized ( value ) ;
24
+ this . minimizedChange . emit ( value ) ;
25
+ }
26
+ }
27
+ private _minimized = false ;
28
+
29
+ /**
30
+ * Emits whenever the minimized state of the sidebar changes.
31
+ * Primarily used to facilitate two-way binding.
32
+ */
33
+ @Output ( ) minimizedChange = new EventEmitter < boolean > ( ) ;
34
+
35
+ @HostBinding ( 'class.sidebar' ) _sidebar = true ;
36
+
17
37
constructor (
18
38
@Inject ( DOCUMENT ) private document : any ,
19
- private renderer : Renderer2 ,
20
- private hostElement : ElementRef
39
+ private renderer : Renderer2
21
40
) {
22
- renderer . addClass ( hostElement . nativeElement , 'sidebar' ) ;
23
41
}
24
42
25
43
ngOnInit ( ) : void {
26
44
this . displayBreakpoint ( this . display ) ;
27
45
this . isCompact ( this . compact ) ;
28
46
this . isFixed ( this . fixed ) ;
29
- this . isMinimized ( this . minimized ) ;
30
47
this . isOffCanvas ( this . offCanvas ) ;
31
48
}
32
49
33
50
ngOnDestroy ( ) : void {
34
- this . renderer . removeClass ( this . document . body , 'sidebar-fixed' ) ;
51
+ this . minimizedChange . complete ( ) ;
52
+ this . renderer . removeClass ( this . document . body , 'sidebar-fixed' ) ;
35
53
}
36
54
37
55
isCompact ( compact : boolean = this . compact ) : void {
38
56
if ( compact ) {
39
- this . renderer . addClass ( this . document . body , 'sidebar-compact' ) ;
57
+ this . renderer . addClass ( this . document . body , 'sidebar-compact' ) ;
40
58
}
41
59
}
42
60
@@ -46,10 +64,8 @@ export class AppSidebarComponent implements OnInit, OnDestroy {
46
64
}
47
65
}
48
66
49
- isMinimized ( minimized : boolean = this . minimized ) : void {
50
- if ( minimized ) {
51
- this . renderer . addClass ( this . document . body , 'sidebar-minimized' ) ;
52
- }
67
+ toggleMinimized ( ) : void {
68
+ this . minimized = ! this . _minimized ;
53
69
}
54
70
55
71
isOffCanvas ( offCanvas : boolean = this . offCanvas ) : void {
@@ -64,4 +80,16 @@ export class AppSidebarComponent implements OnInit, OnDestroy {
64
80
this . renderer . addClass ( this . document . body , cssClass ) ;
65
81
}
66
82
}
83
+
84
+ private _updateMinimized ( minimized : boolean ) : void {
85
+ const body = this . document . body ;
86
+
87
+ if ( minimized ) {
88
+ this . renderer . addClass ( body , 'sidebar-minimized' ) ;
89
+ this . renderer . addClass ( body , 'brand-minimized' ) ;
90
+ } else {
91
+ this . renderer . removeClass ( body , 'sidebar-minimized' ) ;
92
+ this . renderer . removeClass ( body , 'brand-minimized' ) ;
93
+ }
94
+ }
67
95
}
0 commit comments