4
4
Component ,
5
5
ElementRef ,
6
6
Input ,
7
+ OnChanges ,
7
8
OnInit ,
9
+ SimpleChanges ,
8
10
ViewEncapsulation
9
11
} from '@angular/core' ;
10
12
import { Platform } from '@angular/cdk/platform' ;
@@ -38,9 +40,11 @@ import {strings, MDCLinearProgressFoundation, MDCLinearProgressAdapter} from '@m
38
40
changeDetection : ChangeDetectionStrategy . OnPush
39
41
} )
40
42
export class MdcLinearProgress extends MDCComponent < MDCLinearProgressFoundation >
41
- implements MDCProgressIndicator , OnInit {
43
+ implements MDCProgressIndicator , OnChanges , OnInit {
42
44
_root ! : Element ;
43
45
46
+ private _initialized : boolean = false ;
47
+
44
48
/* Label indicating how the progress bar should be announced to the user. */
45
49
@Input ( ) label ?: string = undefined ;
46
50
@@ -50,8 +54,6 @@ export class MdcLinearProgress extends MDCComponent<MDCLinearProgressFoundation>
50
54
}
51
55
set progress ( value : number ) {
52
56
this . _progress = coerceNumberProperty ( value ) ;
53
- this . _foundation . setProgress ( this . _progress ) ;
54
- this . _changeDetectorRef . markForCheck ( ) ;
55
57
}
56
58
private _progress = 0 ;
57
59
@@ -61,8 +63,6 @@ export class MdcLinearProgress extends MDCComponent<MDCLinearProgressFoundation>
61
63
}
62
64
set determinate ( value : boolean ) {
63
65
this . _determinate = coerceBooleanProperty ( value ) ;
64
- this . _foundation . setDeterminate ( this . _determinate ) ;
65
- this . _changeDetectorRef . markForCheck ( ) ;
66
66
}
67
67
private _determinate = false ;
68
68
@@ -72,8 +72,6 @@ export class MdcLinearProgress extends MDCComponent<MDCLinearProgressFoundation>
72
72
}
73
73
set buffer ( value : number ) {
74
74
this . _buffer = coerceNumberProperty ( value ) ;
75
- this . _foundation . setBuffer ( this . _buffer ) ;
76
- this . _changeDetectorRef . markForCheck ( ) ;
77
75
}
78
76
private _buffer = 0 ;
79
77
@@ -83,8 +81,7 @@ export class MdcLinearProgress extends MDCComponent<MDCLinearProgressFoundation>
83
81
}
84
82
set reversed ( value : boolean ) {
85
83
this . _reversed = coerceBooleanProperty ( value ) ;
86
- this . _foundation . setReverse ( this . _reversed ) ;
87
- this . _changeDetectorRef . markForCheck ( ) ;
84
+ this . _syncReversedWithFoundation ( ) ;
88
85
}
89
86
private _reversed = false ;
90
87
@@ -113,13 +110,34 @@ export class MdcLinearProgress extends MDCComponent<MDCLinearProgressFoundation>
113
110
114
111
ngOnInit ( ) : void {
115
112
if ( this . _platform . isBrowser ) {
116
- this . _asyncInitializeFoundation ( )
117
- . then ( ( ) => {
118
- this . progress = this . _progress ;
119
- this . buffer = this . _buffer ;
120
- this . determinate = this . _determinate ;
121
- this . _changeDetectorRef . markForCheck ( ) ;
122
- } ) ;
113
+ this . _initialized = true ;
114
+
115
+ this . _foundation . init ( ) ;
116
+ this . _syncProgressWithFoundation ( ) ;
117
+ this . _syncBufferWithFoundation ( ) ;
118
+ this . _syncDeterminateWithFoundation ( ) ;
119
+ this . _syncReversedWithFoundation ( ) ;
120
+
121
+ this . _changeDetectorRef . markForCheck ( ) ;
122
+ }
123
+ }
124
+
125
+ ngOnChanges ( changes : SimpleChanges ) {
126
+ if ( ! this . _initialized ) {
127
+ return ;
128
+ }
129
+
130
+ if ( changes [ 'progress' ] ) {
131
+ this . _syncProgressWithFoundation ( ) ;
132
+ }
133
+ if ( changes [ 'buffer' ] ) {
134
+ this . _syncBufferWithFoundation ( ) ;
135
+ }
136
+ if ( changes [ 'determinate' ] ) {
137
+ this . _syncDeterminateWithFoundation ( ) ;
138
+ }
139
+ if ( changes [ 'reversed' ] ) {
140
+ this . _syncReversedWithFoundation ( ) ;
123
141
}
124
142
}
125
143
@@ -131,7 +149,19 @@ export class MdcLinearProgress extends MDCComponent<MDCLinearProgressFoundation>
131
149
this . _foundation . close ( ) ;
132
150
}
133
151
134
- async _asyncInitializeFoundation ( ) : Promise < void > {
135
- this . _foundation . init ( ) ;
152
+ private _syncProgressWithFoundation ( ) : void {
153
+ this . _foundation . setProgress ( this . progress ) ;
154
+ }
155
+
156
+ private _syncBufferWithFoundation ( ) : void {
157
+ this . _foundation . setBuffer ( this . buffer ) ;
158
+ }
159
+
160
+ private _syncDeterminateWithFoundation ( ) : void {
161
+ this . _foundation . setDeterminate ( this . determinate ) ;
162
+ }
163
+
164
+ private _syncReversedWithFoundation ( ) : void {
165
+ this . _foundation . setReverse ( this . reversed ) ;
136
166
}
137
167
}
0 commit comments