Skip to content
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
2 changes: 1 addition & 1 deletion src/lib/slider/slider.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="mat-slider-wrapper">
<div class="mat-slider-wrapper" #sliderWrapper>
<div class="mat-slider-track-wrapper">
<div class="mat-slider-track-background" [ngStyle]="_trackBackgroundStyles"></div>
<div class="mat-slider-track-fill" [ngStyle]="_trackFillStyles"></div>
Expand Down
63 changes: 25 additions & 38 deletions src/lib/slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ViewEncapsulation,
ChangeDetectionStrategy,
ChangeDetectorRef,
ViewChild,
} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
import {coerceBooleanProperty, coerceNumberProperty, HammerInput} from '../core';
Expand Down Expand Up @@ -377,9 +378,6 @@ export class MdSlider extends _MdSliderMixinBase
/** The size of a tick interval as a percentage of the size of the track. */
private _tickIntervalPercent: number = 0;

/** A renderer to handle updating the slider's thumb and fill track. */
private _renderer: SliderRenderer;

/** The dimensions of the slider. */
private _sliderDimensions: ClientRect | null = null;

Expand All @@ -391,6 +389,9 @@ export class MdSlider extends _MdSliderMixinBase
/** The value of the slider when the slide start event fires. */
private _valueOnSlideStart: number | null;

/** Reference to the inner slider wrapper element. */
@ViewChild('sliderWrapper') private _sliderWrapper: ElementRef;

/**
* Whether mouse events should be converted to a slider position by calculating their distance
* from the right or bottom edge of the slider as opposed to the top or left.
Expand All @@ -412,7 +413,6 @@ export class MdSlider extends _MdSliderMixinBase
this._focusOriginMonitor
.monitor(this._elementRef.nativeElement, renderer, true)
.subscribe((origin: FocusOrigin) => this._isActive = !!origin && origin !== 'keyboard');
this._renderer = new SliderRenderer(this._elementRef);
}

ngOnDestroy() {
Expand All @@ -426,7 +426,7 @@ export class MdSlider extends _MdSliderMixinBase

// We save the dimensions of the slider here so we can use them to update the spacing of the
// ticks and determine where on the slider click and slide events happen.
this._sliderDimensions = this._renderer.getSliderDimensions();
this._sliderDimensions = this._getSliderDimensions();
this._updateTickIntervalPercent();
}

Expand All @@ -437,7 +437,7 @@ export class MdSlider extends _MdSliderMixinBase

let oldValue = this.value;
this._isSliding = false;
this._renderer.addFocus();
this._focusHostElement();
this._updateValueFromPosition({x: event.clientX, y: event.clientY});

/* Emit a change and input event if the value changed. */
Expand Down Expand Up @@ -479,7 +479,7 @@ export class MdSlider extends _MdSliderMixinBase
this._onMouseenter();

this._isSliding = true;
this._renderer.addFocus();
this._focusHostElement();
this._valueOnSlideStart = this.value;

if (event) {
Expand All @@ -500,7 +500,7 @@ export class MdSlider extends _MdSliderMixinBase
_onFocus() {
// We save the dimensions of the slider here so we can use them to update the spacing of the
// ticks and determine where on the slider click and slide events happen.
this._sliderDimensions = this._renderer.getSliderDimensions();
this._sliderDimensions = this._getSliderDimensions();
this._updateTickIntervalPercent();
}

Expand Down Expand Up @@ -647,6 +647,23 @@ export class MdSlider extends _MdSliderMixinBase
return Math.max(min, Math.min(value, max));
}

/**
* Get the bounding client rect of the slider track element.
* The track is used rather than the native element to ignore the extra space that the thumb can
* take up.
*/
private _getSliderDimensions() {
return this._sliderWrapper ? this._sliderWrapper.nativeElement.getBoundingClientRect() : null;
}

/**
* Focuses the native element.
* Currently only used to allow a blur event to fire but will be used with keyboard input later.
*/
private _focusHostElement() {
this._elementRef.nativeElement.focus();
}

/**
* Sets the model value. Implemented as part of ControlValueAccessor.
* @param value
Expand Down Expand Up @@ -682,33 +699,3 @@ export class MdSlider extends _MdSliderMixinBase
this.disabled = isDisabled;
}
}

/**
* Renderer class in order to keep all dom manipulation in one place and outside of the main class.
* @docs-private
*/
export class SliderRenderer {
private _sliderElement: HTMLElement;

constructor(elementRef: ElementRef) {
this._sliderElement = elementRef.nativeElement;
}

/**
* Get the bounding client rect of the slider track element.
* The track is used rather than the native element to ignore the extra space that the thumb can
* take up.
*/
getSliderDimensions() {
let wrapperElement = this._sliderElement.querySelector('.mat-slider-wrapper');
return wrapperElement ? wrapperElement.getBoundingClientRect() : null;
}

/**
* Focuses the native element.
* Currently only used to allow a blur event to fire but will be used with keyboard input later.
*/
addFocus() {
this._sliderElement.focus();
}
}