Skip to content

Commit a37e497

Browse files
committed
refactor(slider): remove redundant renderer class
Gets rid of the `SliderRenderer` class which doesn't really do much, apart from holding a couple of methods. The methods have been moved into the slider itself.
1 parent 5967f6e commit a37e497

File tree

2 files changed

+26
-39
lines changed

2 files changed

+26
-39
lines changed

src/lib/slider/slider.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="mat-slider-wrapper">
1+
<div class="mat-slider-wrapper" #sliderWrapper>
22
<div class="mat-slider-track-wrapper">
33
<div class="mat-slider-track-background" [ngStyle]="_trackBackgroundStyles"></div>
44
<div class="mat-slider-track-fill" [ngStyle]="_trackFillStyles"></div>

src/lib/slider/slider.ts

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
ViewEncapsulation,
2020
ChangeDetectionStrategy,
2121
ChangeDetectorRef,
22+
ViewChild,
2223
} from '@angular/core';
2324
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
2425
import {coerceBooleanProperty, coerceNumberProperty, HammerInput} from '../core';
@@ -377,9 +378,6 @@ export class MdSlider extends _MdSliderMixinBase
377378
/** The size of a tick interval as a percentage of the size of the track. */
378379
private _tickIntervalPercent: number = 0;
379380

380-
/** A renderer to handle updating the slider's thumb and fill track. */
381-
private _renderer: SliderRenderer;
382-
383381
/** The dimensions of the slider. */
384382
private _sliderDimensions: ClientRect | null = null;
385383

@@ -394,6 +392,9 @@ export class MdSlider extends _MdSliderMixinBase
394392
/** Decimal places to round to, based on the step amount. */
395393
private _roundLabelTo: number;
396394

395+
/** Reference to the inner slider wrapper element. */
396+
@ViewChild('sliderWrapper') private _sliderWrapper: ElementRef;
397+
397398
/**
398399
* Whether mouse events should be converted to a slider position by calculating their distance
399400
* from the right or bottom edge of the slider as opposed to the top or left.
@@ -415,7 +416,6 @@ export class MdSlider extends _MdSliderMixinBase
415416
this._focusOriginMonitor
416417
.monitor(this._elementRef.nativeElement, renderer, true)
417418
.subscribe((origin: FocusOrigin) => this._isActive = !!origin && origin !== 'keyboard');
418-
this._renderer = new SliderRenderer(this._elementRef);
419419
}
420420

421421
ngOnDestroy() {
@@ -429,7 +429,7 @@ export class MdSlider extends _MdSliderMixinBase
429429

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

@@ -439,7 +439,7 @@ export class MdSlider extends _MdSliderMixinBase
439439
}
440440

441441
this._isSliding = false;
442-
this._renderer.addFocus();
442+
this._focusHostElement();
443443
this._updateValueFromPosition({x: event.clientX, y: event.clientY});
444444

445445
/* Emits a change and input event if the value changed. */
@@ -475,7 +475,7 @@ export class MdSlider extends _MdSliderMixinBase
475475
this._onMouseenter();
476476

477477
this._isSliding = true;
478-
this._renderer.addFocus();
478+
this._focusHostElement();
479479

480480
if (event) {
481481
this._updateValueFromPosition({x: event.center.x, y: event.center.y});
@@ -491,7 +491,7 @@ export class MdSlider extends _MdSliderMixinBase
491491
_onFocus() {
492492
// We save the dimensions of the slider here so we can use them to update the spacing of the
493493
// ticks and determine where on the slider click and slide events happen.
494-
this._sliderDimensions = this._renderer.getSliderDimensions();
494+
this._sliderDimensions = this._getSliderDimensions();
495495
this._updateTickIntervalPercent();
496496
}
497497

@@ -641,6 +641,23 @@ export class MdSlider extends _MdSliderMixinBase
641641
return Math.max(min, Math.min(value, max));
642642
}
643643

644+
/**
645+
* Get the bounding client rect of the slider track element.
646+
* The track is used rather than the native element to ignore the extra space that the thumb can
647+
* take up.
648+
*/
649+
private _getSliderDimensions() {
650+
return this._sliderWrapper ? this._sliderWrapper.nativeElement.getBoundingClientRect() : null;
651+
}
652+
653+
/**
654+
* Focuses the native element.
655+
* Currently only used to allow a blur event to fire but will be used with keyboard input later.
656+
*/
657+
private _focusHostElement() {
658+
this._elementRef.nativeElement.focus();
659+
}
660+
644661
/**
645662
* Sets the model value. Implemented as part of ControlValueAccessor.
646663
* @param value
@@ -676,33 +693,3 @@ export class MdSlider extends _MdSliderMixinBase
676693
this.disabled = isDisabled;
677694
}
678695
}
679-
680-
/**
681-
* Renderer class in order to keep all dom manipulation in one place and outside of the main class.
682-
* @docs-private
683-
*/
684-
export class SliderRenderer {
685-
private _sliderElement: HTMLElement;
686-
687-
constructor(elementRef: ElementRef) {
688-
this._sliderElement = elementRef.nativeElement;
689-
}
690-
691-
/**
692-
* Get the bounding client rect of the slider track element.
693-
* The track is used rather than the native element to ignore the extra space that the thumb can
694-
* take up.
695-
*/
696-
getSliderDimensions() {
697-
let wrapperElement = this._sliderElement.querySelector('.mat-slider-wrapper');
698-
return wrapperElement ? wrapperElement.getBoundingClientRect() : null;
699-
}
700-
701-
/**
702-
* Focuses the native element.
703-
* Currently only used to allow a blur event to fire but will be used with keyboard input later.
704-
*/
705-
addFocus() {
706-
this._sliderElement.focus();
707-
}
708-
}

0 commit comments

Comments
 (0)