Skip to content

Commit 48d3a0c

Browse files
crisbetommalerba
authored andcommitted
refactor(slider): remove redundant renderer class (#6281)
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 e94f26c commit 48d3a0c

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

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

392+
/** Reference to the inner slider wrapper element. */
393+
@ViewChild('sliderWrapper') private _sliderWrapper: ElementRef;
394+
394395
/**
395396
* Whether mouse events should be converted to a slider position by calculating their distance
396397
* from the right or bottom edge of the slider as opposed to the top or left.
@@ -412,7 +413,6 @@ export class MdSlider extends _MdSliderMixinBase
412413
this._focusOriginMonitor
413414
.monitor(this._elementRef.nativeElement, renderer, true)
414415
.subscribe((origin: FocusOrigin) => this._isActive = !!origin && origin !== 'keyboard');
415-
this._renderer = new SliderRenderer(this._elementRef);
416416
}
417417

418418
ngOnDestroy() {
@@ -426,7 +426,7 @@ export class MdSlider extends _MdSliderMixinBase
426426

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

@@ -437,7 +437,7 @@ export class MdSlider extends _MdSliderMixinBase
437437

438438
let oldValue = this.value;
439439
this._isSliding = false;
440-
this._renderer.addFocus();
440+
this._focusHostElement();
441441
this._updateValueFromPosition({x: event.clientX, y: event.clientY});
442442

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

481481
this._isSliding = true;
482-
this._renderer.addFocus();
482+
this._focusHostElement();
483483
this._valueOnSlideStart = this.value;
484484

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

@@ -647,6 +647,23 @@ export class MdSlider extends _MdSliderMixinBase
647647
return Math.max(min, Math.min(value, max));
648648
}
649649

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

0 commit comments

Comments
 (0)