Skip to content

Commit cbf8ef4

Browse files
iveysaurdevversion
authored andcommitted
Move slider native element into own variable
1 parent 5bac945 commit cbf8ef4

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/components/slider/slider.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ describe('MdSlider', () => {
155155

156156
expect(containerElement.classList).toContain('md-slider-active');
157157

158-
// The test won't activate the onBlur function. Tried clicking/focusing the body and neither
159-
// worked. Should update this if a way is ever found.
158+
// Call the `onBlur` handler directly because we cannot simulate a focus event in unit tests.
160159
sliderInstance.onBlur();
161160
fixture.detectChanges();
162161

src/components/slider/slider.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,19 @@ export class MdSlider implements AfterContentInit {
204204
* Renderer class in order to keep all dom manipulation in one place and outside of the main class.
205205
*/
206206
export class SliderRenderer {
207-
constructor(private _elementRef: ElementRef) { }
207+
private _sliderElement: HTMLElement;
208+
209+
constructor(private _elementRef: ElementRef) {
210+
this._sliderElement = this._elementRef.nativeElement;
211+
}
208212

209213
/**
210214
* Get the bounding client rect of the slider track element.
211215
* The track is used rather than the native element to ignore the extra space that the thumb can
212216
* take up.
213217
*/
214218
getSliderDimensions() {
215-
let trackElement = this._elementRef.nativeElement.querySelector('.md-slider-track');
219+
let trackElement = this._sliderElement.querySelector('.md-slider-track');
216220
return trackElement.getBoundingClientRect();
217221
}
218222

@@ -221,11 +225,11 @@ export class SliderRenderer {
221225
*/
222226
updateThumbAndFillPosition(percent: number, width: number) {
223227
// The actual thumb element. Needed to get the exact width of the thumb for calculations.
224-
let thumbElement = this._elementRef.nativeElement.querySelector('.md-slider-thumb');
228+
let thumbElement = this._sliderElement.querySelector('.md-slider-thumb');
225229
// A container element that is used to avoid overwriting the transform on the thumb itself.
226230
let thumbPositionElement =
227-
this._elementRef.nativeElement.querySelector('.md-slider-thumb-position');
228-
let fillTrackElement = this._elementRef.nativeElement.querySelector('.md-slider-track-fill');
231+
<HTMLElement>this._sliderElement.querySelector('.md-slider-thumb-position');
232+
let fillTrackElement = <HTMLElement>this._sliderElement.querySelector('.md-slider-track-fill');
229233
let thumbWidth = thumbElement.getBoundingClientRect().width;
230234

231235
let position = percent * width;
@@ -242,6 +246,6 @@ export class SliderRenderer {
242246
* Currently only used to allow a blur event to fire but will be used with keyboard input later.
243247
*/
244248
addFocus() {
245-
this._elementRef.nativeElement.focus();
249+
this._sliderElement.focus();
246250
}
247251
}

0 commit comments

Comments
 (0)