/
slider.ts
978 lines (841 loc) · 34.6 KB
/
slider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {FocusMonitor, FocusOrigin} from '@angular/cdk/a11y';
import {Directionality} from '@angular/cdk/bidi';
import {
BooleanInput,
coerceBooleanProperty,
coerceNumberProperty,
NumberInput
} from '@angular/cdk/coercion';
import {
DOWN_ARROW,
END,
HOME,
LEFT_ARROW,
PAGE_DOWN,
PAGE_UP,
RIGHT_ARROW,
UP_ARROW,
hasModifierKey,
} from '@angular/cdk/keycodes';
import {
Attribute,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
EventEmitter,
forwardRef,
Inject,
Input,
OnDestroy,
Optional,
Output,
ViewChild,
ViewEncapsulation,
NgZone,
AfterViewInit,
} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
import {
CanColor,
CanDisable,
HasTabIndex,
mixinColor,
mixinDisabled,
mixinTabIndex,
} from '@angular/material/core';
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
import {normalizePassiveListenerOptions} from '@angular/cdk/platform';
import {DOCUMENT} from '@angular/common';
import {Subscription} from 'rxjs';
const activeEventOptions = normalizePassiveListenerOptions({passive: false});
/**
* Visually, a 30px separation between tick marks looks best. This is very subjective but it is
* the default separation we chose.
*/
const MIN_AUTO_TICK_SEPARATION = 30;
/** The thumb gap size for a disabled slider. */
const DISABLED_THUMB_GAP = 7;
/** The thumb gap size for a non-active slider at its minimum value. */
const MIN_VALUE_NONACTIVE_THUMB_GAP = 7;
/** The thumb gap size for an active slider at its minimum value. */
const MIN_VALUE_ACTIVE_THUMB_GAP = 10;
/**
* Provider Expression that allows mat-slider to register as a ControlValueAccessor.
* This allows it to support [(ngModel)] and [formControl].
* @docs-private
*/
export const MAT_SLIDER_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => MatSlider),
multi: true
};
/** A simple change event emitted by the MatSlider component. */
export class MatSliderChange {
/** The MatSlider that changed. */
source: MatSlider;
/** The new value of the source slider. */
value: number | null;
}
// Boilerplate for applying mixins to MatSlider.
/** @docs-private */
const _MatSliderBase = mixinTabIndex(mixinColor(mixinDisabled(class {
constructor(public _elementRef: ElementRef) {}
}), 'accent'));
/**
* Allows users to select from a range of values by moving the slider thumb. It is similar in
* behavior to the native `<input type="range">` element.
*/
@Component({
selector: 'mat-slider',
exportAs: 'matSlider',
providers: [MAT_SLIDER_VALUE_ACCESSOR],
host: {
'(focus)': '_onFocus()',
'(blur)': '_onBlur()',
'(keydown)': '_onKeydown($event)',
'(keyup)': '_onKeyup()',
'(mouseenter)': '_onMouseenter()',
// On Safari starting to slide temporarily triggers text selection mode which
// show the wrong cursor. We prevent it by stopping the `selectstart` event.
'(selectstart)': '$event.preventDefault()',
'class': 'mat-slider mat-focus-indicator',
'role': 'slider',
'[tabIndex]': 'tabIndex',
'[attr.aria-disabled]': 'disabled',
'[attr.aria-valuemax]': 'max',
'[attr.aria-valuemin]': 'min',
'[attr.aria-valuenow]': 'value',
// NVDA and Jaws appear to announce the `aria-valuenow` by calculating its percentage based
// on its value between `aria-valuemin` and `aria-valuemax`. Due to how decimals are handled,
// it can cause the slider to read out a very long value like 0.20000068 if the current value
// is 0.2 with a min of 0 and max of 1. We work around the issue by setting `aria-valuetext`
// to the same value that we set on the slider's thumb which will be truncated.
'[attr.aria-valuetext]': 'valueText == null ? displayValue : valueText',
'[attr.aria-orientation]': 'vertical ? "vertical" : "horizontal"',
'[class.mat-slider-disabled]': 'disabled',
'[class.mat-slider-has-ticks]': 'tickInterval',
'[class.mat-slider-horizontal]': '!vertical',
'[class.mat-slider-axis-inverted]': '_shouldInvertAxis()',
// Class binding which is only used by the test harness as there is no other
// way for the harness to detect if mouse coordinates need to be inverted.
'[class.mat-slider-invert-mouse-coords]': '_shouldInvertMouseCoords()',
'[class.mat-slider-sliding]': '_isSliding',
'[class.mat-slider-thumb-label-showing]': 'thumbLabel',
'[class.mat-slider-vertical]': 'vertical',
'[class.mat-slider-min-value]': '_isMinValue()',
'[class.mat-slider-hide-last-tick]':
'disabled || _isMinValue() && _getThumbGap() && _shouldInvertAxis()',
'[class._mat-animation-noopable]': '_animationMode === "NoopAnimations"',
},
templateUrl: 'slider.html',
styleUrls: ['slider.css'],
inputs: ['disabled', 'color', 'tabIndex'],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MatSlider extends _MatSliderBase
implements ControlValueAccessor, OnDestroy, CanDisable, CanColor, AfterViewInit, HasTabIndex {
/** Whether the slider is inverted. */
@Input()
get invert(): boolean { return this._invert; }
set invert(value: boolean) {
this._invert = coerceBooleanProperty(value);
}
private _invert = false;
/** The maximum value that the slider can have. */
@Input()
get max(): number { return this._max; }
set max(v: number) {
this._max = coerceNumberProperty(v, this._max);
this._percent = this._calculatePercentage(this._value);
// Since this also modifies the percentage, we need to let the change detection know.
this._changeDetectorRef.markForCheck();
}
private _max: number = 100;
/** The minimum value that the slider can have. */
@Input()
get min(): number { return this._min; }
set min(v: number) {
this._min = coerceNumberProperty(v, this._min);
this._percent = this._calculatePercentage(this._value);
// Since this also modifies the percentage, we need to let the change detection know.
this._changeDetectorRef.markForCheck();
}
private _min: number = 0;
/** The values at which the thumb will snap. */
@Input()
get step(): number { return this._step; }
set step(v: number) {
this._step = coerceNumberProperty(v, this._step);
if (this._step % 1 !== 0) {
this._roundToDecimal = this._step.toString().split('.').pop()!.length;
}
// Since this could modify the label, we need to notify the change detection.
this._changeDetectorRef.markForCheck();
}
private _step: number = 1;
/** Whether or not to show the thumb label. */
@Input()
get thumbLabel(): boolean { return this._thumbLabel; }
set thumbLabel(value: boolean) { this._thumbLabel = coerceBooleanProperty(value); }
private _thumbLabel: boolean = false;
/**
* How often to show ticks. Relative to the step so that a tick always appears on a step.
* Ex: Tick interval of 4 with a step of 3 will draw a tick every 4 steps (every 12 values).
*/
@Input()
get tickInterval() { return this._tickInterval; }
set tickInterval(value: 'auto' | number) {
if (value === 'auto') {
this._tickInterval = 'auto';
} else if (typeof value === 'number' || typeof value === 'string') {
this._tickInterval = coerceNumberProperty(value, this._tickInterval as number);
} else {
this._tickInterval = 0;
}
}
private _tickInterval: 'auto' | number = 0;
/** Value of the slider. */
@Input()
get value(): number {
// If the value needs to be read and it is still uninitialized, initialize it to the min.
if (this._value === null) {
this.value = this._min;
}
return this._value || 0;
}
set value(v: number) {
if (v !== this._value) {
let value = coerceNumberProperty(v, 0);
// While incrementing by a decimal we can end up with values like 33.300000000000004.
// Truncate it to ensure that it matches the label and to make it easier to work with.
if (this._roundToDecimal && value !== this.min && value !== this.max) {
value = parseFloat(value.toFixed(this._roundToDecimal));
}
this._value = value;
this._percent = this._calculatePercentage(this._value);
// Since this also modifies the percentage, we need to let the change detection know.
this._changeDetectorRef.markForCheck();
}
}
private _value: number | null = null;
/**
* Function that will be used to format the value before it is displayed
* in the thumb label. Can be used to format very large number in order
* for them to fit into the slider thumb.
*/
@Input() displayWith: (value: number) => string | number;
/** Text corresponding to the slider's value. Used primarily for improved accessibility. */
@Input() valueText: string;
/** Whether the slider is vertical. */
@Input()
get vertical(): boolean { return this._vertical; }
set vertical(value: boolean) {
this._vertical = coerceBooleanProperty(value);
}
private _vertical = false;
/** Event emitted when the slider value has changed. */
@Output() readonly change: EventEmitter<MatSliderChange> = new EventEmitter<MatSliderChange>();
/** Event emitted when the slider thumb moves. */
@Output() readonly input: EventEmitter<MatSliderChange> = new EventEmitter<MatSliderChange>();
/**
* Emits when the raw value of the slider changes. This is here primarily
* to facilitate the two-way binding for the `value` input.
* @docs-private
*/
@Output() readonly valueChange: EventEmitter<number | null> = new EventEmitter<number | null>();
/** The value to be used for display purposes. */
get displayValue(): string | number {
if (this.displayWith) {
// Value is never null but since setters and getters cannot have
// different types, the value getter is also typed to return null.
return this.displayWith(this.value!);
}
// Note that this could be improved further by rounding something like 0.999 to 1 or
// 0.899 to 0.9, however it is very performance sensitive, because it gets called on
// every change detection cycle.
if (this._roundToDecimal && this.value && this.value % 1 !== 0) {
return this.value.toFixed(this._roundToDecimal);
}
return this.value || 0;
}
/** set focus to the host element */
focus(options?: FocusOptions) {
this._focusHostElement(options);
}
/** blur the host element */
blur() {
this._blurHostElement();
}
/** onTouch function registered via registerOnTouch (ControlValueAccessor). */
onTouched: () => any = () => {};
/** The percentage of the slider that coincides with the value. */
get percent(): number { return this._clamp(this._percent); }
private _percent: number = 0;
/**
* Whether or not the thumb is sliding and what the user is using to slide it with.
* Used to determine if there should be a transition for the thumb and fill track.
*/
_isSliding: 'keyboard' | 'pointer' | null = null;
/**
* Whether or not the slider is active (clicked or sliding).
* Used to shrink and grow the thumb as according to the Material Design spec.
*/
_isActive: boolean = false;
/**
* Whether the axis of the slider is inverted.
* (i.e. whether moving the thumb in the positive x or y direction decreases the slider's value).
*/
_shouldInvertAxis() {
// Standard non-inverted mode for a vertical slider should be dragging the thumb from bottom to
// top. However from a y-axis standpoint this is inverted.
return this.vertical ? !this.invert : this.invert;
}
/** Whether the slider is at its minimum value. */
_isMinValue() {
return this.percent === 0;
}
/**
* The amount of space to leave between the slider thumb and the track fill & track background
* elements.
*/
_getThumbGap() {
if (this.disabled) {
return DISABLED_THUMB_GAP;
}
if (this._isMinValue() && !this.thumbLabel) {
return this._isActive ? MIN_VALUE_ACTIVE_THUMB_GAP : MIN_VALUE_NONACTIVE_THUMB_GAP;
}
return 0;
}
/** CSS styles for the track background element. */
_getTrackBackgroundStyles(): { [key: string]: string } {
const axis = this.vertical ? 'Y' : 'X';
const scale = this.vertical ? `1, ${1 - this.percent}, 1` : `${1 - this.percent}, 1, 1`;
const sign = this._shouldInvertMouseCoords() ? '-' : '';
return {
// scale3d avoids some rendering issues in Chrome. See #12071.
transform: `translate${axis}(${sign}${this._getThumbGap()}px) scale3d(${scale})`
};
}
/** CSS styles for the track fill element. */
_getTrackFillStyles(): { [key: string]: string } {
const percent = this.percent;
const axis = this.vertical ? 'Y' : 'X';
const scale = this.vertical ? `1, ${percent}, 1` : `${percent}, 1, 1`;
const sign = this._shouldInvertMouseCoords() ? '' : '-';
return {
// scale3d avoids some rendering issues in Chrome. See #12071.
transform: `translate${axis}(${sign}${this._getThumbGap()}px) scale3d(${scale})`,
// iOS Safari has a bug where it won't re-render elements which start of as `scale(0)` until
// something forces a style recalculation on it. Since we'll end up with `scale(0)` when
// the value of the slider is 0, we can easily get into this situation. We force a
// recalculation by changing the element's `display` when it goes from 0 to any other value.
display: percent === 0 ? 'none' : ''
};
}
/** CSS styles for the ticks container element. */
_getTicksContainerStyles(): { [key: string]: string } {
let axis = this.vertical ? 'Y' : 'X';
// For a horizontal slider in RTL languages we push the ticks container off the left edge
// instead of the right edge to avoid causing a horizontal scrollbar to appear.
let sign = !this.vertical && this._getDirection() == 'rtl' ? '' : '-';
let offset = this._tickIntervalPercent / 2 * 100;
return {
'transform': `translate${axis}(${sign}${offset}%)`
};
}
/** CSS styles for the ticks element. */
_getTicksStyles(): { [key: string]: string } {
let tickSize = this._tickIntervalPercent * 100;
let backgroundSize = this.vertical ? `2px ${tickSize}%` : `${tickSize}% 2px`;
let axis = this.vertical ? 'Y' : 'X';
// Depending on the direction we pushed the ticks container, push the ticks the opposite
// direction to re-center them but clip off the end edge. In RTL languages we need to flip the
// ticks 180 degrees so we're really cutting off the end edge abd not the start.
let sign = !this.vertical && this._getDirection() == 'rtl' ? '-' : '';
let rotate = !this.vertical && this._getDirection() == 'rtl' ? ' rotate(180deg)' : '';
let styles: { [key: string]: string } = {
'backgroundSize': backgroundSize,
// Without translateZ ticks sometimes jitter as the slider moves on Chrome & Firefox.
'transform': `translateZ(0) translate${axis}(${sign}${tickSize / 2}%)${rotate}`
};
if (this._isMinValue() && this._getThumbGap()) {
const shouldInvertAxis = this._shouldInvertAxis();
let side: string;
if (this.vertical) {
side = shouldInvertAxis ? 'Bottom' : 'Top';
} else {
side = shouldInvertAxis ? 'Right' : 'Left';
}
styles[`padding${side}`] = `${this._getThumbGap()}px`;
}
return styles;
}
_getThumbContainerStyles(): { [key: string]: string } {
const shouldInvertAxis = this._shouldInvertAxis();
let axis = this.vertical ? 'Y' : 'X';
// For a horizontal slider in RTL languages we push the thumb container off the left edge
// instead of the right edge to avoid causing a horizontal scrollbar to appear.
let invertOffset =
(this._getDirection() == 'rtl' && !this.vertical) ? !shouldInvertAxis : shouldInvertAxis;
let offset = (invertOffset ? this.percent : 1 - this.percent) * 100;
return {
'transform': `translate${axis}(-${offset}%)`
};
}
/** The size of a tick interval as a percentage of the size of the track. */
private _tickIntervalPercent: number = 0;
/** The dimensions of the slider. */
private _sliderDimensions: ClientRect | null = null;
private _controlValueAccessorChangeFn: (value: any) => void = () => {};
/** Decimal places to round to, based on the step amount. */
private _roundToDecimal: number;
/** Subscription to the Directionality change EventEmitter. */
private _dirChangeSubscription = Subscription.EMPTY;
/** 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.
*/
_shouldInvertMouseCoords() {
const shouldInvertAxis = this._shouldInvertAxis();
return (this._getDirection() == 'rtl' && !this.vertical) ? !shouldInvertAxis : shouldInvertAxis;
}
/** The language direction for this slider element. */
private _getDirection() {
return (this._dir && this._dir.value == 'rtl') ? 'rtl' : 'ltr';
}
/** Keeps track of the last pointer event that was captured by the slider. */
private _lastPointerEvent: MouseEvent | TouchEvent | null;
/** Used to subscribe to global move and end events */
protected _document: Document;
/**
* Identifier used to attribute a touch event to a particular slider.
* Will be undefined if one of the following conditions is true:
* - The user isn't dragging using a touch device.
* - The browser doesn't support `Touch.identifier`.
* - Dragging hasn't started yet.
*/
private _touchId: number | undefined;
constructor(elementRef: ElementRef,
private _focusMonitor: FocusMonitor,
private _changeDetectorRef: ChangeDetectorRef,
@Optional() private _dir: Directionality,
@Attribute('tabindex') tabIndex: string,
private _ngZone: NgZone,
@Inject(DOCUMENT) _document: any,
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string) {
super(elementRef);
this._document = _document;
this.tabIndex = parseInt(tabIndex) || 0;
_ngZone.runOutsideAngular(() => {
const element = elementRef.nativeElement;
element.addEventListener('mousedown', this._pointerDown, activeEventOptions);
element.addEventListener('touchstart', this._pointerDown, activeEventOptions);
});
}
ngAfterViewInit() {
this._focusMonitor
.monitor(this._elementRef, true)
.subscribe((origin: FocusOrigin) => {
this._isActive = !!origin && origin !== 'keyboard';
this._changeDetectorRef.detectChanges();
});
if (this._dir) {
this._dirChangeSubscription = this._dir.change.subscribe(() => {
this._changeDetectorRef.markForCheck();
});
}
}
ngOnDestroy() {
const element = this._elementRef.nativeElement;
element.removeEventListener('mousedown', this._pointerDown, activeEventOptions);
element.removeEventListener('touchstart', this._pointerDown, activeEventOptions);
this._lastPointerEvent = null;
this._removeGlobalEvents();
this._focusMonitor.stopMonitoring(this._elementRef);
this._dirChangeSubscription.unsubscribe();
}
_onMouseenter() {
if (this.disabled) {
return;
}
// 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._getSliderDimensions();
this._updateTickIntervalPercent();
}
_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._getSliderDimensions();
this._updateTickIntervalPercent();
}
_onBlur() {
this.onTouched();
}
_onKeydown(event: KeyboardEvent) {
if (this.disabled || hasModifierKey(event) ||
(this._isSliding && this._isSliding !== 'keyboard')) {
return;
}
const oldValue = this.value;
switch (event.keyCode) {
case PAGE_UP:
this._increment(10);
break;
case PAGE_DOWN:
this._increment(-10);
break;
case END:
this.value = this.max;
break;
case HOME:
this.value = this.min;
break;
case LEFT_ARROW:
// NOTE: For a sighted user it would make more sense that when they press an arrow key on an
// inverted slider the thumb moves in that direction. However for a blind user, nothing
// about the slider indicates that it is inverted. They will expect left to be decrement,
// regardless of how it appears on the screen. For speakers ofRTL languages, they probably
// expect left to mean increment. Therefore we flip the meaning of the side arrow keys for
// RTL. For inverted sliders we prefer a good a11y experience to having it "look right" for
// sighted users, therefore we do not swap the meaning.
this._increment(this._getDirection() == 'rtl' ? 1 : -1);
break;
case UP_ARROW:
this._increment(1);
break;
case RIGHT_ARROW:
// See comment on LEFT_ARROW about the conditions under which we flip the meaning.
this._increment(this._getDirection() == 'rtl' ? -1 : 1);
break;
case DOWN_ARROW:
this._increment(-1);
break;
default:
// Return if the key is not one that we explicitly handle to avoid calling preventDefault on
// it.
return;
}
if (oldValue != this.value) {
this._emitInputEvent();
this._emitChangeEvent();
}
this._isSliding = 'keyboard';
event.preventDefault();
}
_onKeyup() {
if (this._isSliding === 'keyboard') {
this._isSliding = null;
}
}
/** Called when the user has put their pointer down on the slider. */
private _pointerDown = (event: TouchEvent | MouseEvent) => {
// Don't do anything if the slider is disabled or the
// user is using anything other than the main mouse button.
if (this.disabled || this._isSliding || (!isTouchEvent(event) && event.button !== 0)) {
return;
}
this._ngZone.run(() => {
this._touchId = isTouchEvent(event) ?
getTouchIdForSlider(event, this._elementRef.nativeElement) : undefined;
const pointerPosition = getPointerPositionOnPage(event, this._touchId);
if (pointerPosition) {
const oldValue = this.value;
this._isSliding = 'pointer';
this._lastPointerEvent = event;
event.preventDefault();
this._focusHostElement();
this._onMouseenter(); // Simulate mouseenter in case this is a mobile device.
this._bindGlobalEvents(event);
this._focusHostElement();
this._updateValueFromPosition(pointerPosition);
this._valueOnSlideStart = oldValue;
// Emit a change and input event if the value changed.
if (oldValue != this.value) {
this._emitInputEvent();
}
}
});
}
/**
* Called when the user has moved their pointer after
* starting to drag. Bound on the document level.
*/
private _pointerMove = (event: TouchEvent | MouseEvent) => {
if (this._isSliding === 'pointer') {
const pointerPosition = getPointerPositionOnPage(event, this._touchId);
if (pointerPosition) {
// Prevent the slide from selecting anything else.
event.preventDefault();
const oldValue = this.value;
this._lastPointerEvent = event;
this._updateValueFromPosition(pointerPosition);
// Native range elements always emit `input` events when the value changed while sliding.
if (oldValue != this.value) {
this._emitInputEvent();
}
}
}
}
/** Called when the user has lifted their pointer. Bound on the document level. */
private _pointerUp = (event: TouchEvent | MouseEvent) => {
if (this._isSliding === 'pointer') {
if (!isTouchEvent(event) || typeof this._touchId !== 'number' ||
// Note that we use `changedTouches`, rather than `touches` because it
// seems like in most cases `touches` is empty for `touchend` events.
findMatchingTouch(event.changedTouches, this._touchId)) {
event.preventDefault();
this._removeGlobalEvents();
this._isSliding = null;
this._touchId = undefined;
if (this._valueOnSlideStart != this.value && !this.disabled) {
this._emitChangeEvent();
}
this._valueOnSlideStart = this._lastPointerEvent = null;
}
}
}
/** Called when the window has lost focus. */
private _windowBlur = () => {
// If the window is blurred while dragging we need to stop dragging because the
// browser won't dispatch the `mouseup` and `touchend` events anymore.
if (this._lastPointerEvent) {
this._pointerUp(this._lastPointerEvent);
}
}
/** Use defaultView of injected document if available or fallback to global window reference */
private _getWindow(): Window {
return this._document.defaultView || window;
}
/**
* Binds our global move and end events. They're bound at the document level and only while
* dragging so that the user doesn't have to keep their pointer exactly over the slider
* as they're swiping across the screen.
*/
private _bindGlobalEvents(triggerEvent: TouchEvent | MouseEvent) {
// Note that we bind the events to the `document`, because it allows us to capture
// drag cancel events where the user's pointer is outside the browser window.
const document = this._document;
const isTouch = isTouchEvent(triggerEvent);
const moveEventName = isTouch ? 'touchmove' : 'mousemove';
const endEventName = isTouch ? 'touchend' : 'mouseup';
document.addEventListener(moveEventName, this._pointerMove, activeEventOptions);
document.addEventListener(endEventName, this._pointerUp, activeEventOptions);
if (isTouch) {
document.addEventListener('touchcancel', this._pointerUp, activeEventOptions);
}
const window = this._getWindow();
if (typeof window !== 'undefined' && window) {
window.addEventListener('blur', this._windowBlur);
}
}
/** Removes any global event listeners that we may have added. */
private _removeGlobalEvents() {
const document = this._document;
document.removeEventListener('mousemove', this._pointerMove, activeEventOptions);
document.removeEventListener('mouseup', this._pointerUp, activeEventOptions);
document.removeEventListener('touchmove', this._pointerMove, activeEventOptions);
document.removeEventListener('touchend', this._pointerUp, activeEventOptions);
document.removeEventListener('touchcancel', this._pointerUp, activeEventOptions);
const window = this._getWindow();
if (typeof window !== 'undefined' && window) {
window.removeEventListener('blur', this._windowBlur);
}
}
/** Increments the slider by the given number of steps (negative number decrements). */
private _increment(numSteps: number) {
this.value = this._clamp((this.value || 0) + this.step * numSteps, this.min, this.max);
}
/** Calculate the new value from the new physical location. The value will always be snapped. */
private _updateValueFromPosition(pos: {x: number, y: number}) {
if (!this._sliderDimensions) {
return;
}
let offset = this.vertical ? this._sliderDimensions.top : this._sliderDimensions.left;
let size = this.vertical ? this._sliderDimensions.height : this._sliderDimensions.width;
let posComponent = this.vertical ? pos.y : pos.x;
// The exact value is calculated from the event and used to find the closest snap value.
let percent = this._clamp((posComponent - offset) / size);
if (this._shouldInvertMouseCoords()) {
percent = 1 - percent;
}
// Since the steps may not divide cleanly into the max value, if the user
// slid to 0 or 100 percent, we jump to the min/max value. This approach
// is slightly more intuitive than using `Math.ceil` below, because it
// follows the user's pointer closer.
if (percent === 0) {
this.value = this.min;
} else if (percent === 1) {
this.value = this.max;
} else {
const exactValue = this._calculateValue(percent);
// This calculation finds the closest step by finding the closest
// whole number divisible by the step relative to the min.
const closestValue = Math.round((exactValue - this.min) / this.step) * this.step + this.min;
// The value needs to snap to the min and max.
this.value = this._clamp(closestValue, this.min, this.max);
}
}
/** Emits a change event if the current value is different from the last emitted value. */
private _emitChangeEvent() {
this._controlValueAccessorChangeFn(this.value);
this.valueChange.emit(this.value);
this.change.emit(this._createChangeEvent());
}
/** Emits an input event when the current value is different from the last emitted value. */
private _emitInputEvent() {
this.input.emit(this._createChangeEvent());
}
/** Updates the amount of space between ticks as a percentage of the width of the slider. */
private _updateTickIntervalPercent() {
if (!this.tickInterval || !this._sliderDimensions) {
return;
}
if (this.tickInterval == 'auto') {
let trackSize = this.vertical ? this._sliderDimensions.height : this._sliderDimensions.width;
let pixelsPerStep = trackSize * this.step / (this.max - this.min);
let stepsPerTick = Math.ceil(MIN_AUTO_TICK_SEPARATION / pixelsPerStep);
let pixelsPerTick = stepsPerTick * this.step;
this._tickIntervalPercent = pixelsPerTick / trackSize;
} else {
this._tickIntervalPercent = this.tickInterval * this.step / (this.max - this.min);
}
}
/** Creates a slider change object from the specified value. */
private _createChangeEvent(value = this.value): MatSliderChange {
let event = new MatSliderChange();
event.source = this;
event.value = value;
return event;
}
/** Calculates the percentage of the slider that a value is. */
private _calculatePercentage(value: number | null) {
return ((value || 0) - this.min) / (this.max - this.min);
}
/** Calculates the value a percentage of the slider corresponds to. */
private _calculateValue(percentage: number) {
return this.min + percentage * (this.max - this.min);
}
/** Return a number between two numbers. */
private _clamp(value: number, min = 0, max = 1) {
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(options?: FocusOptions) {
this._elementRef.nativeElement.focus(options);
}
/** Blurs the native element. */
private _blurHostElement() {
this._elementRef.nativeElement.blur();
}
/**
* Sets the model value. Implemented as part of ControlValueAccessor.
* @param value
*/
writeValue(value: any) {
this.value = value;
}
/**
* Registers a callback to be triggered when the value has changed.
* Implemented as part of ControlValueAccessor.
* @param fn Callback to be registered.
*/
registerOnChange(fn: (value: any) => void) {
this._controlValueAccessorChangeFn = fn;
}
/**
* Registers a callback to be triggered when the component is touched.
* Implemented as part of ControlValueAccessor.
* @param fn Callback to be registered.
*/
registerOnTouched(fn: any) {
this.onTouched = fn;
}
/**
* Sets whether the component should be disabled.
* Implemented as part of ControlValueAccessor.
* @param isDisabled
*/
setDisabledState(isDisabled: boolean) {
this.disabled = isDisabled;
}
static ngAcceptInputType_invert: BooleanInput;
static ngAcceptInputType_max: NumberInput;
static ngAcceptInputType_min: NumberInput;
static ngAcceptInputType_step: NumberInput;
static ngAcceptInputType_thumbLabel: BooleanInput;
static ngAcceptInputType_tickInterval: NumberInput;
static ngAcceptInputType_value: NumberInput;
static ngAcceptInputType_vertical: BooleanInput;
static ngAcceptInputType_disabled: BooleanInput;
static ngAcceptInputType_tabIndex: NumberInput;
}
/** Returns whether an event is a touch event. */
function isTouchEvent(event: MouseEvent | TouchEvent): event is TouchEvent {
// This function is called for every pixel that the user has dragged so we need it to be
// as fast as possible. Since we only bind mouse events and touch events, we can assume
// that if the event's name starts with `t`, it's a touch event.
return event.type[0] === 't';
}
/** Gets the coordinates of a touch or mouse event relative to the viewport. */
function getPointerPositionOnPage(event: MouseEvent | TouchEvent, id: number|undefined) {
let point: {clientX: number, clientY: number}|undefined;
if (isTouchEvent(event)) {
// The `identifier` could be undefined if the browser doesn't support `TouchEvent.identifier`.
// If that's the case, attribute the first touch to all active sliders. This should still cover
// the most common case while only breaking multi-touch.
if (typeof id === 'number') {
point = findMatchingTouch(event.touches, id) || findMatchingTouch(event.changedTouches, id);
} else {
// `touches` will be empty for start/end events so we have to fall back to `changedTouches`.
point = event.touches[0] || event.changedTouches[0];
}
} else {
point = event;
}
return point ? {x: point.clientX, y: point.clientY} : undefined;
}
/** Finds a `Touch` with a specific ID in a `TouchList`. */
function findMatchingTouch(touches: TouchList, id: number): Touch | undefined {
for (let i = 0; i < touches.length; i++) {
if (touches[i].identifier === id) {
return touches[i];
}
}
return undefined;
}
/** Gets the unique ID of a touch that matches a specific slider. */
function getTouchIdForSlider(event: TouchEvent, sliderHost: HTMLElement): number | undefined {
for (let i = 0; i < event.touches.length; i++) {
const target = event.touches[i].target as HTMLElement;
if (sliderHost === target || sliderHost.contains(target)) {
return event.touches[i].identifier;
}
}
return undefined;
}