Skip to content

Commit b50c7e5

Browse files
Angular Material Teammmalerba
Angular Material Team
authored andcommitted
Project import generated by Copybara.
PiperOrigin-RevId: 352859006
1 parent 182aba4 commit b50c7e5

File tree

1 file changed

+85
-73
lines changed

1 file changed

+85
-73
lines changed

src/material-experimental/mdc-progress-bar/progress-bar.ts

Lines changed: 85 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,26 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

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';
2311
import {CanColor, CanColorCtor, mixinColor} from '@angular/material-experimental/mdc-core';
24-
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
2512
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';
2816
import {filter} from 'rxjs/operators';
29-
import {Directionality} from '@angular/cdk/bidi';
17+
3018

3119
// Boilerplate for applying mixins to MatProgressBar.
3220
/** @docs-private */
3321
class MatProgressBarBase {
34-
constructor(public _elementRef: ElementRef) { }
22+
constructor(public _elementRef: ElementRef) {}
3523
}
3624

37-
const _MatProgressBarMixinBase: CanColorCtor & typeof MatProgressBarBase =
25+
const _MatProgressBarMixinBase: CanColorCtor&typeof MatProgressBarBase =
3826
mixinColor(MatProgressBarBase, 'primary');
3927

40-
export type ProgressBarMode = 'determinate' | 'indeterminate' | 'buffer' | 'query';
28+
export type ProgressBarMode = 'determinate'|'indeterminate'|'buffer'|'query';
4129

4230
@Component({
4331
selector: 'mat-progress-bar',
@@ -46,7 +34,8 @@ export type ProgressBarMode = 'determinate' | 'indeterminate' | 'buffer' | 'quer
4634
'role': 'progressbar',
4735
'aria-valuemin': '0',
4836
'aria-valuemax': '100',
49-
'[attr.aria-valuenow]': '(mode === "indeterminate" || mode === "query") ? null : value',
37+
'[attr.aria-valuenow]':
38+
'(mode === "indeterminate" || mode === "query") ? null : value',
5039
'[attr.mode]': 'mode',
5140
'class': 'mat-mdc-progress-bar',
5241
'[class._mat-animation-noopable]': '_isNoopAnimation',
@@ -57,58 +46,71 @@ export type ProgressBarMode = 'determinate' | 'indeterminate' | 'buffer' | 'quer
5746
changeDetection: ChangeDetectionStrategy.OnPush,
5847
encapsulation: ViewEncapsulation.None,
5948
})
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) {
6756
super(_elementRef);
6857
this._isNoopAnimation = _animationMode === 'NoopAnimations';
6958
if (_dir) {
70-
this._dirChangeSubscription = _dir.change.subscribe(() => this._syncFoundation());
59+
this._dirChangeSubscription =
60+
_dir.change.subscribe(() => this._syncFoundation());
7161
}
7262
}
7363

7464
/** Implements all of the logic of the MDC progress bar. */
75-
private _foundation: MDCLinearProgressFoundation | undefined;
65+
private _foundation: MDCLinearProgressFoundation|undefined;
7666

7767
/** Adapter used by MDC to interact with the DOM. */
7868
private _adapter: MDCLinearProgressAdapter = {
7969
addClass: (className: string) => this._rootElement.classList.add(className),
8070
forceLayout: () => this._rootElement.offsetWidth,
8171
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+
},
9490
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+
}
104104
};
105105

106106
/** Flag that indicates whether NoopAnimations mode is set to true. */
107107
_isNoopAnimation = false;
108108

109109
/** Value of the progress bar. Defaults to zero. Mirrored to aria-valuenow. */
110110
@Input()
111-
get value(): number { return this._value; }
111+
get value(): number {
112+
return this._value;
113+
}
112114
set value(v: number) {
113115
this._value = clamp(v || 0);
114116
this._syncFoundation();
@@ -117,7 +119,9 @@ export class MatProgressBar extends _MatProgressBarMixinBase implements AfterVie
117119

118120
/** Buffer value of the progress bar. Defaults to zero. */
119121
@Input()
120-
get bufferValue(): number { return this._bufferValue || 0; }
122+
get bufferValue(): number {
123+
return this._bufferValue || 0;
124+
}
121125
set bufferValue(v: number) {
122126
this._bufferValue = clamp(v || 0);
123127
this._syncFoundation();
@@ -129,9 +133,9 @@ export class MatProgressBar extends _MatProgressBarMixinBase implements AfterVie
129133
private _bufferBar: HTMLElement;
130134

131135
/**
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).
135139
*/
136140
@Output() animationEnd = new EventEmitter<ProgressAnimationEnd>();
137141

@@ -144,12 +148,13 @@ export class MatProgressBar extends _MatProgressBarMixinBase implements AfterVie
144148
/**
145149
* Mode of the progress bar.
146150
*
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.
150153
*/
151154
@Input()
152-
get mode(): ProgressBarMode { return this._mode; }
155+
get mode(): ProgressBarMode {
156+
return this._mode;
157+
}
153158
set mode(value: ProgressBarMode) {
154159
// Note that we don't technically need a getter and a setter here,
155160
// 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
161166
ngAfterViewInit() {
162167
const element = this._elementRef.nativeElement;
163168

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;
167175

168176
this._foundation = new MDCLinearProgressFoundation(this._adapter);
169177
this._foundation.init();
170178
this._syncFoundation();
171179

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)
174183
this._ngZone.runOutsideAngular((() => {
175184
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+
});
183195
}));
184196
}
185197

0 commit comments

Comments
 (0)