Skip to content

Commit

Permalink
chore(slider): switch to OnPush change detection
Browse files Browse the repository at this point in the history
* Switches `md-slider` to `OnPush` change detection.
* Fixes a few properties not reacting to external changes.

Relates to angular#5035.
  • Loading branch information
crisbeto committed Jul 6, 2017
1 parent ac3e21a commit e468234
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions src/lib/slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import {
Optional,
Output,
Renderer2,
ViewEncapsulation
ViewEncapsulation,
ChangeDetectionStrategy,
ChangeDetectorRef,
} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
import {coerceBooleanProperty, coerceNumberProperty, HammerInput} from '../core';
Expand Down Expand Up @@ -118,31 +120,32 @@ export const _MdSliderMixinBase = mixinDisabled(MdSliderBase);
styleUrls: ['slider.css'],
inputs: ['disabled'],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MdSlider extends _MdSliderMixinBase
implements ControlValueAccessor, OnDestroy, CanDisable {
/** Whether the slider is inverted. */
@Input()
get invert() { return this._invert; }
set invert(value: any) { this._invert = coerceBooleanProperty(value); }
set invert(value: any) {
this._invert = coerceBooleanProperty(value);
this._changeDetectorRef.markForCheck();
}
private _invert = false;

/** The maximum value that the slider can have. */
@Input()
get max() {
return this._max;
}
get max() { return this._max; }
set max(v: number) {
this._max = coerceNumberProperty(v, this._max);
this._percent = this._calculatePercentage(this._value);
this._changeDetectorRef.markForCheck();
}
private _max: number = 100;

/** The minimum value that the slider can have. */
@Input()
get min() {
return this._min;
}
get min() { return this._min; }
set min(v: number) {
this._min = coerceNumberProperty(v, this._min);

Expand All @@ -151,6 +154,7 @@ export class MdSlider extends _MdSliderMixinBase
this.value = this._min;
}
this._percent = this._calculatePercentage(this._value);
this._changeDetectorRef.markForCheck();
}
private _min: number = 0;

Expand All @@ -163,6 +167,8 @@ export class MdSlider extends _MdSliderMixinBase
if (this._step % 1 !== 0) {
this._roundLabelTo = this._step.toString().split('.').pop()!.length;
}

this._changeDetectorRef.markForCheck();
}
private _step: number = 1;

Expand Down Expand Up @@ -191,6 +197,8 @@ export class MdSlider extends _MdSliderMixinBase
} else {
this._tickInterval = 0;
}

this._changeDetectorRef.markForCheck();
}
private _tickInterval: 'auto' | number = 0;

Expand All @@ -209,15 +217,21 @@ export class MdSlider extends _MdSliderMixinBase
return this._value;
}
set value(v: number | null) {
this._value = coerceNumberProperty(v, this._value || 0);
this._percent = this._calculatePercentage(this._value);
if (v !== this._value) {
this._value = coerceNumberProperty(v, this._value || 0);
this._percent = this._calculatePercentage(this._value);
this._changeDetectorRef.markForCheck();
}
}
private _value: number | null = null;

/** Whether the slider is vertical. */
@Input()
get vertical() { return this._vertical; }
set vertical(value: any) { this._vertical = coerceBooleanProperty(value); }
set vertical(value: any) {
this._vertical = coerceBooleanProperty(value);
this._changeDetectorRef.markForCheck();
}
private _vertical = false;

@Input() color: 'primary' | 'accent' | 'warn' = 'accent';
Expand Down Expand Up @@ -392,9 +406,11 @@ export class MdSlider extends _MdSliderMixinBase

constructor(renderer: Renderer2, private _elementRef: ElementRef,
private _focusOriginMonitor: FocusOriginMonitor,
private _changeDetectorRef: ChangeDetectorRef,
@Optional() private _dir: Directionality) {
super();
this._focusOriginMonitor.monitor(this._elementRef.nativeElement, renderer, true)
this._focusOriginMonitor
.monitor(this._elementRef.nativeElement, renderer, true)
.subscribe((origin: FocusOrigin) => this._isActive = !!origin && origin !== 'keyboard');
this._renderer = new SliderRenderer(this._elementRef);
}
Expand Down

0 comments on commit e468234

Please sign in to comment.