6
6
Input ,
7
7
ElementRef ,
8
8
NgZone ,
9
- Renderer , Directive
9
+ Renderer2 ,
10
+ Directive ,
11
+ ViewChild ,
10
12
} from '@angular/core' ;
11
13
12
14
@@ -24,6 +26,8 @@ const startIndeterminate = 3;
24
26
const endIndeterminate = 80 ;
25
27
/* Maximum angle for the arc. The angle can't be exactly 360, because the arc becomes hidden. */
26
28
const MAX_ANGLE = 359.99 / 100 ;
29
+ /** Whether the user's browser supports requestAnimationFrame. */
30
+ const HAS_RAF = typeof requestAnimationFrame !== 'undefined' ;
27
31
28
32
export type ProgressSpinnerMode = 'determinate' | 'indeterminate' ;
29
33
@@ -67,7 +71,7 @@ export class MdProgressSpinner implements OnDestroy {
67
71
private _interdeterminateInterval : number ;
68
72
69
73
/** The SVG <path> node that is used to draw the circle. */
70
- private _path : SVGPathElement ;
74
+ @ ViewChild ( 'path' ) private _path : ElementRef ;
71
75
72
76
private _mode : ProgressSpinnerMode = 'determinate' ;
73
77
private _value : number ;
@@ -107,7 +111,11 @@ export class MdProgressSpinner implements OnDestroy {
107
111
@Input ( )
108
112
get color ( ) : string { return this . _color ; }
109
113
set color ( value : string ) {
110
- this . _updateColor ( value ) ;
114
+ if ( value ) {
115
+ this . _renderer . removeClass ( this . _elementRef . nativeElement , `mat-${ this . _color } ` ) ;
116
+ this . _renderer . addClass ( this . _elementRef . nativeElement , `mat-${ value } ` ) ;
117
+ this . _color = value ;
118
+ }
111
119
}
112
120
113
121
/** Value of the progress circle. It is bound to the host as the attribute aria-valuenow. */
@@ -152,8 +160,7 @@ export class MdProgressSpinner implements OnDestroy {
152
160
constructor (
153
161
private _ngZone : NgZone ,
154
162
private _elementRef : ElementRef ,
155
- private _renderer : Renderer
156
- ) { }
163
+ private _renderer : Renderer2 ) { }
157
164
158
165
159
166
/**
@@ -178,7 +185,10 @@ export class MdProgressSpinner implements OnDestroy {
178
185
this . _renderArc ( animateTo , rotation ) ;
179
186
} else {
180
187
let animation = ( ) => {
181
- let elapsedTime = Math . max ( 0 , Math . min ( Date . now ( ) - startTime , duration ) ) ;
188
+ // If there is no requestAnimationFrame, skip ahead to the end of the animation.
189
+ let elapsedTime = HAS_RAF ?
190
+ Math . max ( 0 , Math . min ( Date . now ( ) - startTime , duration ) ) :
191
+ duration ;
182
192
183
193
this . _renderArc (
184
194
ease ( elapsedTime , animateFrom , changeInValue , duration ) ,
@@ -237,30 +247,8 @@ export class MdProgressSpinner implements OnDestroy {
237
247
* DOM attribute on the `<path>`.
238
248
*/
239
249
private _renderArc ( currentValue : number , rotation = 0 ) {
240
- // Caches the path reference so it doesn't have to be looked up every time.
241
- let path = this . _path = this . _path || this . _elementRef . nativeElement . querySelector ( 'path' ) ;
242
-
243
- // Ensure that the path was found. This may not be the case if the
244
- // animation function fires too early.
245
- if ( path ) {
246
- path . setAttribute ( 'd' , getSvgArc ( currentValue , rotation ) ) ;
247
- }
248
- }
249
-
250
- /**
251
- * Updates the color of the progress-spinner by adding the new palette class to the element
252
- * and removing the old one.
253
- */
254
- private _updateColor ( newColor : string ) {
255
- this . _setElementColor ( this . _color , false ) ;
256
- this . _setElementColor ( newColor , true ) ;
257
- this . _color = newColor ;
258
- }
259
-
260
- /** Sets the given palette class on the component element. */
261
- private _setElementColor ( color : string , isAdd : boolean ) {
262
- if ( color != null && color != '' ) {
263
- this . _renderer . setElementClass ( this . _elementRef . nativeElement , `mat-${ color } ` , isAdd ) ;
250
+ if ( this . _path ) {
251
+ this . _renderer . setAttribute ( this . _path . nativeElement , 'd' , getSvgArc ( currentValue , rotation ) ) ;
264
252
}
265
253
}
266
254
}
@@ -285,7 +273,7 @@ export class MdProgressSpinner implements OnDestroy {
285
273
} )
286
274
export class MdSpinner extends MdProgressSpinner implements OnDestroy {
287
275
288
- constructor ( elementRef : ElementRef , ngZone : NgZone , renderer : Renderer ) {
276
+ constructor ( elementRef : ElementRef , ngZone : NgZone , renderer : Renderer2 ) {
289
277
super ( ngZone , elementRef , renderer ) ;
290
278
this . mode = 'indeterminate' ;
291
279
}
0 commit comments