Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(progress-spinner): fix color input on md-spinner #2396

Merged
merged 1 commit into from
Jan 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/lib/progress-spinner/progress-spinner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ describe('MdProgressSpinner', () => {
BasicProgressSpinner,
IndeterminateProgressSpinner,
ProgressSpinnerWithValueAndBoundMode,
ProgressSpinnerWithColor,
IndeterminateProgressSpinnerWithNgIf,
SpinnerWithNgIf,
SpinnerWithColor
],
});

Expand Down Expand Up @@ -105,6 +107,37 @@ describe('MdProgressSpinner', () => {

expect(progressElement.componentInstance.interdeterminateInterval).toBeFalsy();
});

it('should set the color class on the md-spinner', () => {
let fixture = TestBed.createComponent(SpinnerWithColor);
fixture.detectChanges();

let progressElement = fixture.debugElement.query(By.css('md-spinner'));

expect(progressElement.nativeElement.classList).toContain('md-primary');

fixture.debugElement.componentInstance.color = 'accent';
fixture.detectChanges();

expect(progressElement.nativeElement.classList).toContain('md-accent');
expect(progressElement.nativeElement.classList).not.toContain('md-primary');
});

it('should set the color class on the md-progress-spinner', () => {
let fixture = TestBed.createComponent(ProgressSpinnerWithColor);
fixture.detectChanges();

let progressElement = fixture.debugElement.query(By.css('md-progress-spinner'));

expect(progressElement.nativeElement.classList).toContain('md-primary');

fixture.debugElement.componentInstance.color = 'accent';
fixture.detectChanges();

expect(progressElement.nativeElement.classList).toContain('md-accent');
expect(progressElement.nativeElement.classList).not.toContain('md-primary');
});

});


Expand All @@ -123,3 +156,9 @@ class IndeterminateProgressSpinnerWithNgIf { }

@Component({template: `<md-spinner *ngIf="!isHidden"></md-spinner>`})
class SpinnerWithNgIf { }

@Component({template: `<md-spinner [color]="color"></md-spinner>`})
class SpinnerWithColor { color: string = 'primary'; }

@Component({template: `<md-progress-spinner value="50" [color]="color"></md-progress-spinner>`})
class ProgressSpinnerWithColor { color: string = 'primary'; }
52 changes: 38 additions & 14 deletions src/lib/progress-spinner/progress-spinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import {
ModuleWithProviders,
Component,
HostBinding,
ChangeDetectorRef,
ChangeDetectionStrategy,
OnDestroy,
Input,
ElementRef,
NgZone
NgZone,
Renderer
} from '@angular/core';
import {DefaultStyleCompatibilityModeModule} from '../core';

Expand Down Expand Up @@ -43,10 +43,7 @@ type EasingFn = (currentTime: number, startValue: number,
host: {
'role': 'progressbar',
'[attr.aria-valuemin]': '_ariaValueMin',
'[attr.aria-valuemax]': '_ariaValueMax',
'[class.md-primary]': 'color == "primary"',
'[class.md-accent]': 'color == "accent"',
'[class.md-warn]': 'color == "warn"',
'[attr.aria-valuemax]': '_ariaValueMax'
},
templateUrl: 'progress-spinner.html',
styleUrls: ['progress-spinner.css'],
Expand All @@ -62,6 +59,10 @@ export class MdProgressSpinner implements OnDestroy {
/** The SVG <path> node that is used to draw the circle. */
private _path: SVGPathElement;

private _mode: ProgressSpinnerMode = 'determinate';
private _value: number;
private _color: string = 'primary';

/**
* Values for aria max and min are only defined as numbers when in a determinate mode. We do this
* because voiceover does not report the progress indicator as indeterminate if the aria min
Expand Down Expand Up @@ -92,8 +93,14 @@ export class MdProgressSpinner implements OnDestroy {
this._cleanupIndeterminateAnimation();
}

/** The color of the progress-spinner. Can be primary, accent, or warn. */
@Input()
get color(): string { return this._color; }
set color(value: string) {
this._updateColor(value);
}

/** Value of the progress circle. It is bound to the host as the attribute aria-valuenow. */
private _value: number;
@Input()
@HostBinding('attr.aria-valuenow')
get value() {
Expand Down Expand Up @@ -128,14 +135,11 @@ export class MdProgressSpinner implements OnDestroy {
}
this._mode = m;
}
private _mode: ProgressSpinnerMode = 'determinate';

@Input() color: 'primary' | 'accent' | 'warn' = 'primary';

constructor(
private _changeDetectorRef: ChangeDetectorRef,
private _ngZone: NgZone,
private _elementRef: ElementRef
private _elementRef: ElementRef,
private _renderer: Renderer
) {}


Expand Down Expand Up @@ -229,6 +233,23 @@ export class MdProgressSpinner implements OnDestroy {
path.setAttribute('d', getSvgArc(currentValue, rotation));
}
}

/**
* Updates the color of the progress-spinner by adding the new palette class to the element
* and removing the old one.
*/
private _updateColor(newColor: string) {
this._setElementColor(this._color, false);
this._setElementColor(newColor, true);
this._color = newColor;
}

/** Sets the given palette class on the component element. */
private _setElementColor(color: string, isAdd: boolean) {
if (color != null && color != '') {
this._renderer.setElementClass(this._elementRef.nativeElement, `md-${color}`, isAdd);
}
}
}


Expand All @@ -245,12 +266,15 @@ export class MdProgressSpinner implements OnDestroy {
'role': 'progressbar',
'mode': 'indeterminate',
},
// Due to the class extending we need to explicitly say that the input exists.
inputs: ['color'],
templateUrl: 'progress-spinner.html',
styleUrls: ['progress-spinner.css'],
})
export class MdSpinner extends MdProgressSpinner implements OnDestroy {
constructor(changeDetectorRef: ChangeDetectorRef, elementRef: ElementRef, ngZone: NgZone) {
super(changeDetectorRef, ngZone, elementRef);

constructor(elementRef: ElementRef, ngZone: NgZone, renderer: Renderer) {
super(ngZone, elementRef, renderer);
this.mode = 'indeterminate';
}

Expand Down