6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import {
10
- ChangeDetectionStrategy ,
11
- Component ,
12
- ViewEncapsulation ,
13
- ElementRef ,
14
- NgZone ,
15
- Optional ,
16
- Inject ,
17
- Input ,
18
- Output ,
19
- EventEmitter ,
20
- AfterViewInit ,
21
- OnDestroy ,
22
- } from '@angular/core' ;
9
+ import { Directionality } from '@angular/cdk/bidi' ;
10
+ import { AfterViewInit , ChangeDetectionStrategy , Component , ElementRef , EventEmitter , Inject , Input , NgZone , OnDestroy , Optional , Output , ViewEncapsulation , } from '@angular/core' ;
23
11
import { CanColor , CanColorCtor , mixinColor } from '@angular/material-experimental/mdc-core' ;
24
- import { ANIMATION_MODULE_TYPE } from '@angular/platform-browser/animations' ;
25
12
import { ProgressAnimationEnd } from '@angular/material/progress-bar' ;
26
- import { MDCLinearProgressAdapter , MDCLinearProgressFoundation } from '@material/linear-progress' ;
27
- import { Subscription , fromEvent , Observable } from 'rxjs' ;
13
+ import { ANIMATION_MODULE_TYPE } from '@angular/platform-browser/animations' ;
14
+ import { MDCLinearProgressAdapter , MDCLinearProgressFoundation , WithMDCResizeObserver } from '@material/linear-progress' ;
15
+ import { fromEvent , Observable , Subscription } from 'rxjs' ;
28
16
import { filter } from 'rxjs/operators' ;
29
- import { Directionality } from '@angular/cdk/bidi' ;
17
+
30
18
31
19
// Boilerplate for applying mixins to MatProgressBar.
32
20
/** @docs -private */
33
21
class MatProgressBarBase {
34
- constructor ( public _elementRef : ElementRef ) { }
22
+ constructor ( public _elementRef : ElementRef ) { }
35
23
}
36
24
37
- const _MatProgressBarMixinBase : CanColorCtor & typeof MatProgressBarBase =
25
+ const _MatProgressBarMixinBase : CanColorCtor & typeof MatProgressBarBase =
38
26
mixinColor ( MatProgressBarBase , 'primary' ) ;
39
27
40
- export type ProgressBarMode = 'determinate' | 'indeterminate' | 'buffer' | 'query' ;
28
+ export type ProgressBarMode = 'determinate' | 'indeterminate' | 'buffer' | 'query' ;
41
29
42
30
@Component ( {
43
31
selector : 'mat-progress-bar' ,
@@ -46,7 +34,8 @@ export type ProgressBarMode = 'determinate' | 'indeterminate' | 'buffer' | 'quer
46
34
'role' : 'progressbar' ,
47
35
'aria-valuemin' : '0' ,
48
36
'aria-valuemax' : '100' ,
49
- '[attr.aria-valuenow]' : '(mode === "indeterminate" || mode === "query") ? null : value' ,
37
+ '[attr.aria-valuenow]' :
38
+ '(mode === "indeterminate" || mode === "query") ? null : value' ,
50
39
'[attr.mode]' : 'mode' ,
51
40
'class' : 'mat-mdc-progress-bar' ,
52
41
'[class._mat-animation-noopable]' : '_isNoopAnimation' ,
@@ -57,58 +46,71 @@ export type ProgressBarMode = 'determinate' | 'indeterminate' | 'buffer' | 'quer
57
46
changeDetection : ChangeDetectionStrategy . OnPush ,
58
47
encapsulation : ViewEncapsulation . None ,
59
48
} )
60
- export class MatProgressBar extends _MatProgressBarMixinBase implements AfterViewInit , OnDestroy ,
61
- CanColor {
62
-
63
- constructor ( public _elementRef : ElementRef < HTMLElement > ,
64
- private _ngZone : NgZone ,
65
- @Optional ( ) private _dir ?: Directionality ,
66
- @ Optional ( ) @ Inject ( ANIMATION_MODULE_TYPE ) public _animationMode ?: string ) {
49
+ export class MatProgressBar extends _MatProgressBarMixinBase implements
50
+ AfterViewInit , OnDestroy , CanColor {
51
+ constructor (
52
+ public _elementRef : ElementRef < HTMLElement > , private _ngZone : NgZone ,
53
+ @ Optional ( ) private _dir ?: Directionality ,
54
+ @Optional ( ) @ Inject ( ANIMATION_MODULE_TYPE ) public _animationMode ?:
55
+ string ) {
67
56
super ( _elementRef ) ;
68
57
this . _isNoopAnimation = _animationMode === 'NoopAnimations' ;
69
58
if ( _dir ) {
70
- this . _dirChangeSubscription = _dir . change . subscribe ( ( ) => this . _syncFoundation ( ) ) ;
59
+ this . _dirChangeSubscription =
60
+ _dir . change . subscribe ( ( ) => this . _syncFoundation ( ) ) ;
71
61
}
72
62
}
73
63
74
64
/** Implements all of the logic of the MDC progress bar. */
75
- private _foundation : MDCLinearProgressFoundation | undefined ;
65
+ private _foundation : MDCLinearProgressFoundation | undefined ;
76
66
77
67
/** Adapter used by MDC to interact with the DOM. */
78
68
private _adapter : MDCLinearProgressAdapter = {
79
69
addClass : ( className : string ) => this . _rootElement . classList . add ( className ) ,
80
70
forceLayout : ( ) => this . _rootElement . offsetWidth ,
81
71
removeAttribute : ( name : string ) => this . _rootElement . removeAttribute ( name ) ,
82
- setAttribute : ( name : string , value : string ) => this . _rootElement . setAttribute ( name , value ) ,
83
- hasClass : ( className : string ) => this . _rootElement . classList . contains ( className ) ,
84
- removeClass : ( className : string ) => this . _rootElement . classList . remove ( className ) ,
85
- setPrimaryBarStyle : ( styleProperty : string , value : string ) => {
86
- ( this . _primaryBar . style as any ) [ styleProperty ] = value ;
87
- } ,
88
- setBufferBarStyle : ( styleProperty : string , value : string ) => {
89
- ( this . _bufferBar . style as any ) [ styleProperty ] = value ;
90
- } ,
91
- setStyle : ( styleProperty : string , value : string ) => {
92
- ( this . _rootElement . style as any ) [ styleProperty ] = value ;
93
- } ,
72
+ setAttribute : ( name : string , value : string ) =>
73
+ this . _rootElement . setAttribute ( name , value ) ,
74
+ hasClass : ( className : string ) =>
75
+ this . _rootElement . classList . contains ( className ) ,
76
+ removeClass : ( className : string ) =>
77
+ this . _rootElement . classList . remove ( className ) ,
78
+ setPrimaryBarStyle :
79
+ ( styleProperty : string , value : string ) => {
80
+ ( this . _primaryBar . style as any ) [ styleProperty ] = value ;
81
+ } ,
82
+ setBufferBarStyle :
83
+ ( styleProperty : string , value : string ) => {
84
+ ( this . _bufferBar . style as any ) [ styleProperty ] = value ;
85
+ } ,
86
+ setStyle :
87
+ ( styleProperty : string , value : string ) => {
88
+ ( this . _rootElement . style as any ) [ styleProperty ] = value ;
89
+ } ,
94
90
getWidth : ( ) => this . _rootElement . offsetWidth ,
95
- attachResizeObserver : ( callback ) => {
96
- if ( ( typeof window !== 'undefined' ) && window . ResizeObserver ) {
97
- const ro = new ResizeObserver ( callback ) ;
98
- ro . observe ( this . _rootElement ) ;
99
- return ro ;
100
- }
101
-
102
- return null ;
103
- }
91
+ attachResizeObserver :
92
+ ( callback ) => {
93
+ if ( ( typeof window !== 'undefined' ) &&
94
+ ( window as unknown as WithMDCResizeObserver ) . ResizeObserver ) {
95
+ const RO =
96
+ ( window as unknown as WithMDCResizeObserver ) . ResizeObserver ;
97
+ const ro = new RO ( callback ) ;
98
+ ro . observe ( this . _rootElement ) ;
99
+ return ro ;
100
+ }
101
+
102
+ return null ;
103
+ }
104
104
} ;
105
105
106
106
/** Flag that indicates whether NoopAnimations mode is set to true. */
107
107
_isNoopAnimation = false ;
108
108
109
109
/** Value of the progress bar. Defaults to zero. Mirrored to aria-valuenow. */
110
110
@Input ( )
111
- get value ( ) : number { return this . _value ; }
111
+ get value ( ) : number {
112
+ return this . _value ;
113
+ }
112
114
set value ( v : number ) {
113
115
this . _value = clamp ( v || 0 ) ;
114
116
this . _syncFoundation ( ) ;
@@ -117,7 +119,9 @@ export class MatProgressBar extends _MatProgressBarMixinBase implements AfterVie
117
119
118
120
/** Buffer value of the progress bar. Defaults to zero. */
119
121
@Input ( )
120
- get bufferValue ( ) : number { return this . _bufferValue || 0 ; }
122
+ get bufferValue ( ) : number {
123
+ return this . _bufferValue || 0 ;
124
+ }
121
125
set bufferValue ( v : number ) {
122
126
this . _bufferValue = clamp ( v || 0 ) ;
123
127
this . _syncFoundation ( ) ;
@@ -129,9 +133,9 @@ export class MatProgressBar extends _MatProgressBarMixinBase implements AfterVie
129
133
private _bufferBar : HTMLElement ;
130
134
131
135
/**
132
- * Event emitted when animation of the primary progress bar completes. This event will not
133
- * be emitted when animations are disabled, nor will it be emitted for modes with continuous
134
- * animations (indeterminate and query).
136
+ * Event emitted when animation of the primary progress bar completes. This
137
+ * event will not be emitted when animations are disabled, nor will it be
138
+ * emitted for modes with continuous animations (indeterminate and query).
135
139
*/
136
140
@Output ( ) animationEnd = new EventEmitter < ProgressAnimationEnd > ( ) ;
137
141
@@ -144,12 +148,13 @@ export class MatProgressBar extends _MatProgressBarMixinBase implements AfterVie
144
148
/**
145
149
* Mode of the progress bar.
146
150
*
147
- * Input must be one of these values: determinate, indeterminate, buffer, query, defaults to
148
- * 'determinate'.
149
- * Mirrored to mode attribute.
151
+ * Input must be one of these values: determinate, indeterminate, buffer,
152
+ * query, defaults to 'determinate'. Mirrored to mode attribute.
150
153
*/
151
154
@Input ( )
152
- get mode ( ) : ProgressBarMode { return this . _mode ; }
155
+ get mode ( ) : ProgressBarMode {
156
+ return this . _mode ;
157
+ }
153
158
set mode ( value : ProgressBarMode ) {
154
159
// Note that we don't technically need a getter and a setter here,
155
160
// but we use it to match the behavior of the existing mat-progress-bar.
@@ -161,25 +166,32 @@ export class MatProgressBar extends _MatProgressBarMixinBase implements AfterVie
161
166
ngAfterViewInit ( ) {
162
167
const element = this . _elementRef . nativeElement ;
163
168
164
- this . _rootElement = element . querySelector ( '.mdc-linear-progress' ) as HTMLElement ;
165
- this . _primaryBar = element . querySelector ( '.mdc-linear-progress__primary-bar' ) as HTMLElement ;
166
- this . _bufferBar = element . querySelector ( '.mdc-linear-progress__buffer-bar' ) as HTMLElement ;
169
+ this . _rootElement =
170
+ element . querySelector ( '.mdc-linear-progress' ) as HTMLElement ;
171
+ this . _primaryBar = element . querySelector (
172
+ '.mdc-linear-progress__primary-bar' ) as HTMLElement ;
173
+ this . _bufferBar = element . querySelector (
174
+ '.mdc-linear-progress__buffer-bar' ) as HTMLElement ;
167
175
168
176
this . _foundation = new MDCLinearProgressFoundation ( this . _adapter ) ;
169
177
this . _foundation . init ( ) ;
170
178
this . _syncFoundation ( ) ;
171
179
172
- // Run outside angular so change detection didn't get triggered on every transition end
173
- // instead only on the animation that we care about (primary value bar's transitionend)
180
+ // Run outside angular so change detection didn't get triggered on every
181
+ // transition end instead only on the animation that we care about (primary
182
+ // value bar's transitionend)
174
183
this . _ngZone . runOutsideAngular ( ( ( ) => {
175
184
this . _animationEndSubscription =
176
- ( fromEvent ( this . _primaryBar , 'transitionend' ) as Observable < TransitionEvent > )
177
- . pipe ( filter ( ( ( e : TransitionEvent ) => e . target === this . _primaryBar ) ) )
178
- . subscribe ( ( ) => {
179
- if ( this . mode === 'determinate' || this . mode === 'buffer' ) {
180
- this . _ngZone . run ( ( ) => this . animationEnd . next ( { value : this . value } ) ) ;
181
- }
182
- } ) ;
185
+ ( fromEvent ( this . _primaryBar , 'transitionend' ) as
186
+ Observable < TransitionEvent > )
187
+ . pipe ( filter (
188
+ ( ( e : TransitionEvent ) => e . target === this . _primaryBar ) ) )
189
+ . subscribe ( ( ) => {
190
+ if ( this . mode === 'determinate' || this . mode === 'buffer' ) {
191
+ this . _ngZone . run (
192
+ ( ) => this . animationEnd . next ( { value : this . value } ) ) ;
193
+ }
194
+ } ) ;
183
195
} ) ) ;
184
196
}
185
197
0 commit comments