-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
foundation.ts
1164 lines (1012 loc) · 40.9 KB
/
foundation.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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license
* Copyright 2020 Google Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import {AnimationFrame} from '@material/animation/animationframe';
import {getCorrectPropertyName} from '@material/animation/util';
import {MDCFoundation} from '@material/base/foundation';
import {SpecificEventListener} from '@material/base/types';
import {MDCSliderAdapter} from './adapter';
import {attributes, cssClasses, numbers} from './constants';
import {Thumb, TickMark} from './types';
enum AnimationKeys {
SLIDER_UPDATE = 'slider_update'
}
// Accessing `window` without a `typeof` check will throw on Node environments.
const HAS_WINDOW = typeof window !== 'undefined';
/**
* Foundation class for slider. Responsibilities include:
* - Updating slider values (internal state and DOM updates) based on client
* 'x' position.
* - Updating DOM after slider property updates (e.g. min, max).
*/
export class MDCSliderFoundation extends MDCFoundation<MDCSliderAdapter> {
static SUPPORTS_POINTER_EVENTS = HAS_WINDOW && Boolean(window.PointerEvent) &&
// #setPointerCapture is buggy on iOS, so we can't use pointer events
// until the following bug is fixed:
// https://bugs.webkit.org/show_bug.cgi?id=220196
!isIOS();
// Whether the initial styles (to position the thumb, before component
// initialization) have been removed.
private initialStylesRemoved = false;
private min!: number; // Assigned in init()
private max!: number; // Assigned in init()
// If `isRange`, this is the value of Thumb.START. Otherwise, defaults to min.
private valueStart!: number; // Assigned in init()
// If `isRange`, this it the value of Thumb.END. Otherwise, it is the
// value of the single thumb.
private value!: number; // Assigned in init()
private rect!: ClientRect; // Assigned in layout() via init()
private isDisabled = false;
private isDiscrete = false;
private step = numbers.STEP_SIZE;
// Number of digits after the decimal point to round to, when computing
// values. This is based on the step size by default and is used to
// avoid floating point precision errors in JS.
private numDecimalPlaces!: number; // Assigned in init()
private hasTickMarks = false;
// The following properties are only set for range sliders.
private isRange = false;
// Tracks the thumb being moved across a slider pointer interaction (down,
// move event).
private thumb: Thumb|null = null;
// `clientX` from the most recent down event. Used in subsequent move
// events to determine which thumb to move (in the case of
// overlapping thumbs).
private downEventClientX: number|null = null;
// `valueStart` before the most recent down event. Used in subsequent up
// events to determine whether to fire the `change` event.
private valueStartBeforeDownEvent!: number; // Assigned in init()
// `value` before the most recent down event. Used in subsequent up
// events to determine whether to fire the `change` event.
private valueBeforeDownEvent!: number; // Assigned in init()
// Width of the start thumb knob.
private startThumbKnobWidth = 0;
// Width of the end thumb knob.
private endThumbKnobWidth = 0;
private readonly animFrame: AnimationFrame;
// Assigned in #initialize.
private mousedownOrTouchstartListener!:
SpecificEventListener<'mousedown'|'touchstart'>;
// Assigned in #initialize.
private moveListener!:
SpecificEventListener<'pointermove'|'mousemove'|'touchmove'>;
private pointerdownListener!:
SpecificEventListener<'pointerdown'>; // Assigned in #initialize.
private pointerupListener!:
SpecificEventListener<'pointerup'>; // Assigned in #initialize.
private thumbMouseenterListener!:
SpecificEventListener<'mouseenter'>; // Assigned in #initialize.
private thumbMouseleaveListener!:
SpecificEventListener<'mouseleave'>; // Assigned in #initialize.
private inputStartChangeListener!:
SpecificEventListener<'change'>; // Assigned in #initialize.
private inputEndChangeListener!:
SpecificEventListener<'change'>; // Assigned in #initialize.
private inputStartFocusListener!:
SpecificEventListener<'focus'>; // Assigned in #initialize.
private inputEndFocusListener!:
SpecificEventListener<'focus'>; // Assigned in #initialize.
private inputStartBlurListener!:
SpecificEventListener<'blur'>; // Assigned in #initialize.
private inputEndBlurListener!:
SpecificEventListener<'blur'>; // Assigned in #initialize.
private resizeListener!:
SpecificEventListener<'resize'>; // Assigned in #initialize.
constructor(adapter?: Partial<MDCSliderAdapter>) {
super({...MDCSliderFoundation.defaultAdapter, ...adapter});
this.animFrame = new AnimationFrame();
}
static get defaultAdapter(): MDCSliderAdapter {
// tslint:disable:object-literal-sort-keys Methods should be in the same
// order as the adapter interface.
return {
hasClass: () => false,
addClass: () => undefined,
removeClass: () => undefined,
addThumbClass: () => undefined,
removeThumbClass: () => undefined,
getAttribute: () => null,
getInputValue: () => '',
setInputValue: () => undefined,
getInputAttribute: () => null,
setInputAttribute: () => null,
removeInputAttribute: () => null,
focusInput: () => undefined,
isInputFocused: () => false,
getThumbKnobWidth: () => 0,
getThumbBoundingClientRect: () =>
({top: 0, right: 0, bottom: 0, left: 0, width: 0, height: 0}),
getBoundingClientRect: () =>
({top: 0, right: 0, bottom: 0, left: 0, width: 0, height: 0}),
isRTL: () => false,
setThumbStyleProperty: () => undefined,
removeThumbStyleProperty: () => undefined,
setTrackActiveStyleProperty: () => undefined,
removeTrackActiveStyleProperty: () => undefined,
setValueIndicatorText: () => undefined,
getValueToAriaValueTextFn: () => null,
updateTickMarks: () => undefined,
setPointerCapture: () => undefined,
emitChangeEvent: () => undefined,
emitInputEvent: () => undefined,
emitDragStartEvent: () => undefined,
emitDragEndEvent: () => undefined,
registerEventHandler: () => undefined,
deregisterEventHandler: () => undefined,
registerThumbEventHandler: () => undefined,
deregisterThumbEventHandler: () => undefined,
registerInputEventHandler: () => undefined,
deregisterInputEventHandler: () => undefined,
registerBodyEventHandler: () => undefined,
deregisterBodyEventHandler: () => undefined,
registerWindowEventHandler: () => undefined,
deregisterWindowEventHandler: () => undefined,
};
// tslint:enable:object-literal-sort-keys
}
init() {
this.isDisabled = this.adapter.hasClass(cssClasses.DISABLED);
this.isDiscrete = this.adapter.hasClass(cssClasses.DISCRETE);
this.hasTickMarks = this.adapter.hasClass(cssClasses.TICK_MARKS);
this.isRange = this.adapter.hasClass(cssClasses.RANGE);
const min = this.convertAttributeValueToNumber(
this.adapter.getInputAttribute(
attributes.INPUT_MIN, this.isRange ? Thumb.START : Thumb.END),
attributes.INPUT_MIN);
const max = this.convertAttributeValueToNumber(
this.adapter.getInputAttribute(attributes.INPUT_MAX, Thumb.END),
attributes.INPUT_MAX);
const value = this.convertAttributeValueToNumber(
this.adapter.getInputAttribute(attributes.INPUT_VALUE, Thumb.END),
attributes.INPUT_VALUE);
const valueStart = this.isRange ?
this.convertAttributeValueToNumber(
this.adapter.getInputAttribute(attributes.INPUT_VALUE, Thumb.START),
attributes.INPUT_VALUE) :
min;
const stepAttr =
this.adapter.getInputAttribute(attributes.INPUT_STEP, Thumb.END);
const step = stepAttr ?
this.convertAttributeValueToNumber(stepAttr, attributes.INPUT_STEP) :
this.step;
this.validateProperties({min, max, value, valueStart, step});
this.min = min;
this.max = max;
this.value = value;
this.valueStart = valueStart;
this.step = step;
this.numDecimalPlaces = getNumDecimalPlaces(this.step);
this.valueBeforeDownEvent = value;
this.valueStartBeforeDownEvent = valueStart;
this.mousedownOrTouchstartListener =
this.handleMousedownOrTouchstart.bind(this);
this.moveListener = this.handleMove.bind(this);
this.pointerdownListener = this.handlePointerdown.bind(this);
this.pointerupListener = this.handlePointerup.bind(this);
this.thumbMouseenterListener = this.handleThumbMouseenter.bind(this);
this.thumbMouseleaveListener = this.handleThumbMouseleave.bind(this);
this.inputStartChangeListener = () => {
this.handleInputChange(Thumb.START);
};
this.inputEndChangeListener = () => {
this.handleInputChange(Thumb.END);
};
this.inputStartFocusListener = () => {
this.handleInputFocus(Thumb.START);
};
this.inputEndFocusListener = () => {
this.handleInputFocus(Thumb.END);
};
this.inputStartBlurListener = () => {
this.handleInputBlur(Thumb.START);
};
this.inputEndBlurListener = () => {
this.handleInputBlur(Thumb.END);
};
this.resizeListener = this.handleResize.bind(this);
this.registerEventHandlers();
}
destroy() {
this.deregisterEventHandlers();
}
setMin(value: number) {
this.min = value;
if (!this.isRange) {
this.valueStart = value;
}
this.updateUI();
}
setMax(value: number) {
this.max = value;
this.updateUI();
}
getMin() {
return this.min;
}
getMax() {
return this.max;
}
/**
* - For single point sliders, returns the thumb value.
* - For range (two-thumb) sliders, returns the end thumb's value.
*/
getValue() {
return this.value;
}
/**
* - For single point sliders, sets the thumb value.
* - For range (two-thumb) sliders, sets the end thumb's value.
*/
setValue(value: number) {
if (this.isRange && value < this.valueStart) {
throw new Error(
`end thumb value (${value}) must be >= start thumb ` +
`value (${this.valueStart})`);
}
this.updateValue(value, Thumb.END);
}
/**
* Only applicable for range sliders.
* @return The start thumb's value.
*/
getValueStart() {
if (!this.isRange) {
throw new Error('`valueStart` is only applicable for range sliders.');
}
return this.valueStart;
}
/**
* Only applicable for range sliders. Sets the start thumb's value.
*/
setValueStart(valueStart: number) {
if (!this.isRange) {
throw new Error('`valueStart` is only applicable for range sliders.');
}
if (this.isRange && valueStart > this.value) {
throw new Error(
`start thumb value (${valueStart}) must be <= end thumb ` +
`value (${this.value})`);
}
this.updateValue(valueStart, Thumb.START);
}
setStep(value: number) {
this.step = value;
this.numDecimalPlaces = getNumDecimalPlaces(value);
this.updateUI();
}
setIsDiscrete(value: boolean) {
this.isDiscrete = value;
this.updateValueIndicatorUI();
this.updateTickMarksUI();
}
getStep() {
return this.step;
}
setHasTickMarks(value: boolean) {
this.hasTickMarks = value;
this.updateTickMarksUI();
}
getDisabled() {
return this.isDisabled;
}
/**
* Sets disabled state, including updating styles and thumb tabindex.
*/
setDisabled(disabled: boolean) {
this.isDisabled = disabled;
if (disabled) {
this.adapter.addClass(cssClasses.DISABLED);
if (this.isRange) {
this.adapter.setInputAttribute(
attributes.INPUT_DISABLED, '', Thumb.START);
}
this.adapter.setInputAttribute(attributes.INPUT_DISABLED, '', Thumb.END);
} else {
this.adapter.removeClass(cssClasses.DISABLED);
if (this.isRange) {
this.adapter.removeInputAttribute(
attributes.INPUT_DISABLED, Thumb.START);
}
this.adapter.removeInputAttribute(attributes.INPUT_DISABLED, Thumb.END);
}
}
/** @return Whether the slider is a range slider. */
getIsRange() {
return this.isRange;
}
/**
* - Syncs slider boundingClientRect with the current DOM.
* - Updates UI based on internal state.
*/
layout({skipUpdateUI}: {skipUpdateUI?: boolean} = {}) {
this.rect = this.adapter.getBoundingClientRect();
if (this.isRange) {
this.startThumbKnobWidth = this.adapter.getThumbKnobWidth(Thumb.START);
this.endThumbKnobWidth = this.adapter.getThumbKnobWidth(Thumb.END);
}
if (!skipUpdateUI) {
this.updateUI();
}
}
/** Handles resize events on the window. */
handleResize() {
this.layout();
}
/**
* Handles pointer down events on the slider root element.
*/
handleDown(event: PointerEvent|MouseEvent|TouchEvent) {
if (this.isDisabled) return;
this.valueStartBeforeDownEvent = this.valueStart;
this.valueBeforeDownEvent = this.value;
const clientX = (event as MouseEvent).clientX != null ?
(event as MouseEvent).clientX :
(event as TouchEvent).targetTouches[0].clientX;
this.downEventClientX = clientX;
const value = this.mapClientXOnSliderScale(clientX);
this.thumb = this.getThumbFromDownEvent(clientX, value);
if (this.thumb === null) return;
this.handleDragStart(event, value, this.thumb);
this.updateValue(value, this.thumb, {emitInputEvent: true});
}
/**
* Handles pointer move events on the slider root element.
*/
handleMove(event: PointerEvent|MouseEvent|TouchEvent) {
if (this.isDisabled) return;
// Prevent scrolling.
event.preventDefault();
const clientX = (event as MouseEvent).clientX != null ?
(event as MouseEvent).clientX :
(event as TouchEvent).targetTouches[0].clientX;
const dragAlreadyStarted = this.thumb != null;
this.thumb = this.getThumbFromMoveEvent(clientX);
if (this.thumb === null) return;
const value = this.mapClientXOnSliderScale(clientX);
if (!dragAlreadyStarted) {
this.handleDragStart(event, value, this.thumb);
this.adapter.emitDragStartEvent(value, this.thumb);
}
this.updateValue(value, this.thumb, {emitInputEvent: true});
}
/**
* Handles pointer up events on the slider root element.
*/
handleUp() {
if (this.isDisabled || this.thumb === null) return;
const oldValue = this.thumb === Thumb.START ?
this.valueStartBeforeDownEvent :
this.valueBeforeDownEvent;
const newValue = this.thumb === Thumb.START ? this.valueStart : this.value;
if (oldValue !== newValue) {
this.adapter.emitChangeEvent(newValue, this.thumb);
}
this.adapter.emitDragEndEvent(newValue, this.thumb);
this.thumb = null;
}
/**
* For range, discrete slider, shows the value indicator on both thumbs.
*/
handleThumbMouseenter() {
if (!this.isDiscrete || !this.isRange) return;
this.adapter.addThumbClass(cssClasses.THUMB_WITH_INDICATOR, Thumb.START);
this.adapter.addThumbClass(cssClasses.THUMB_WITH_INDICATOR, Thumb.END);
}
/**
* For range, discrete slider, hides the value indicator on both thumbs.
*/
handleThumbMouseleave() {
if (!this.isDiscrete || !this.isRange) return;
if (this.adapter.isInputFocused(Thumb.START) ||
this.adapter.isInputFocused(Thumb.END)) {
// Leave value indicator shown if either input is focused.
return;
}
this.adapter.removeThumbClass(cssClasses.THUMB_WITH_INDICATOR, Thumb.START);
this.adapter.removeThumbClass(cssClasses.THUMB_WITH_INDICATOR, Thumb.END);
}
handleMousedownOrTouchstart(event: MouseEvent|TouchEvent) {
const moveEventType =
event.type === 'mousedown' ? 'mousemove' : 'touchmove';
// After a down event on the slider root, listen for move events on
// body (so the slider value is updated for events outside of the
// slider root).
this.adapter.registerBodyEventHandler(moveEventType, this.moveListener);
const upHandler = () => {
this.handleUp();
// Once the drag is finished (up event on body), remove the move
// handler.
this.adapter.deregisterBodyEventHandler(moveEventType, this.moveListener);
// Also stop listening for subsequent up events.
this.adapter.deregisterEventHandler('mouseup', upHandler);
this.adapter.deregisterEventHandler('touchend', upHandler);
};
this.adapter.registerBodyEventHandler('mouseup', upHandler);
this.adapter.registerBodyEventHandler('touchend', upHandler);
this.handleDown(event);
}
handlePointerdown(event: PointerEvent) {
this.adapter.setPointerCapture(event.pointerId);
this.adapter.registerEventHandler('pointermove', this.moveListener);
this.handleDown(event);
}
/**
* Handles input `change` event by setting internal slider value to match
* input's new value.
*/
handleInputChange(thumb: Thumb) {
const value = Number(this.adapter.getInputValue(thumb));
if (thumb === Thumb.START) {
this.setValueStart(value);
} else {
this.setValue(value);
}
this.adapter.emitChangeEvent(
thumb === Thumb.START ? this.valueStart : this.value, thumb);
this.adapter.emitInputEvent(
thumb === Thumb.START ? this.valueStart : this.value, thumb);
}
/** Shows activated state and value indicator on thumb(s). */
handleInputFocus(thumb: Thumb) {
this.adapter.addThumbClass(cssClasses.THUMB_FOCUSED, thumb);
if (!this.isDiscrete) return;
this.adapter.addThumbClass(cssClasses.THUMB_WITH_INDICATOR, thumb);
if (this.isRange) {
const otherThumb = thumb === Thumb.START ? Thumb.END : Thumb.START;
this.adapter.addThumbClass(cssClasses.THUMB_WITH_INDICATOR, otherThumb);
}
}
/** Removes activated state and value indicator from thumb(s). */
handleInputBlur(thumb: Thumb) {
this.adapter.removeThumbClass(cssClasses.THUMB_FOCUSED, thumb);
if (!this.isDiscrete) return;
this.adapter.removeThumbClass(cssClasses.THUMB_WITH_INDICATOR, thumb);
if (this.isRange) {
const otherThumb = thumb === Thumb.START ? Thumb.END : Thumb.START;
this.adapter.removeThumbClass(
cssClasses.THUMB_WITH_INDICATOR, otherThumb);
}
}
/**
* Emits custom dragStart event, along with focusing the underlying input.
*/
private handleDragStart(
event: PointerEvent|MouseEvent|TouchEvent, value: number, thumb: Thumb) {
this.adapter.emitDragStartEvent(value, thumb);
this.adapter.focusInput(thumb);
// Prevent the input (that we just focused) from losing focus.
event.preventDefault();
}
/**
* @return The thumb to be moved based on initial down event.
*/
private getThumbFromDownEvent(clientX: number, value: number): Thumb|null {
// For single point slider, thumb to be moved is always the END (only)
// thumb.
if (!this.isRange) return Thumb.END;
// Check if event press point is in the bounds of any thumb.
const thumbStartRect = this.adapter.getThumbBoundingClientRect(Thumb.START);
const thumbEndRect = this.adapter.getThumbBoundingClientRect(Thumb.END);
const inThumbStartBounds =
clientX >= thumbStartRect.left && clientX <= thumbStartRect.right;
const inThumbEndBounds =
clientX >= thumbEndRect.left && clientX <= thumbEndRect.right;
if (inThumbStartBounds && inThumbEndBounds) {
// Thumbs overlapping. Thumb to be moved cannot be determined yet.
return null;
}
// If press is in bounds for either thumb on down event, that's the thumb
// to be moved.
if (inThumbStartBounds) {
return Thumb.START;
}
if (inThumbEndBounds) {
return Thumb.END;
}
// For presses outside the range, return whichever thumb is closer.
if (value < this.valueStart) {
return Thumb.START;
}
if (value > this.value) {
return Thumb.END;
}
// For presses inside the range, return whichever thumb is closer.
return (value - this.valueStart <= this.value - value) ? Thumb.START :
Thumb.END;
}
/**
* @return The thumb to be moved based on move event (based on drag
* direction from original down event). Only applicable if thumbs
* were overlapping in the down event.
*/
private getThumbFromMoveEvent(clientX: number): Thumb|null {
// Thumb has already been chosen.
if (this.thumb !== null) return this.thumb;
if (this.downEventClientX === null) {
throw new Error('`downEventClientX` is null after move event.');
}
const moveDistanceUnderThreshold =
Math.abs(this.downEventClientX - clientX) < numbers.THUMB_UPDATE_MIN_PX;
if (moveDistanceUnderThreshold) return this.thumb;
const draggedThumbToLeft = clientX < this.downEventClientX;
if (draggedThumbToLeft) {
return this.adapter.isRTL() ? Thumb.END : Thumb.START;
} else {
return this.adapter.isRTL() ? Thumb.START : Thumb.END;
}
}
/**
* Updates UI based on internal state.
* @param thumb Thumb whose value is being updated. If undefined, UI is
* updated for both thumbs based on current internal state.
*/
private updateUI(thumb?: Thumb) {
this.updateThumbAndInputAttributes(thumb);
this.updateThumbAndTrackUI(thumb);
this.updateValueIndicatorUI(thumb);
this.updateTickMarksUI();
}
/**
* Updates thumb and input attributes based on current value.
* @param thumb Thumb whose aria attributes to update.
*/
private updateThumbAndInputAttributes(thumb?: Thumb) {
if (!thumb) return;
const value =
this.isRange && thumb === Thumb.START ? this.valueStart : this.value;
const valueStr = String(value);
this.adapter.setInputAttribute(attributes.INPUT_VALUE, valueStr, thumb);
if (this.isRange && thumb === Thumb.START) {
this.adapter.setInputAttribute(attributes.INPUT_MIN, valueStr, Thumb.END);
} else if (this.isRange && thumb === Thumb.END) {
this.adapter.setInputAttribute(
attributes.INPUT_MAX, valueStr, Thumb.START);
}
// Sync attribute with property.
if (this.adapter.getInputValue(thumb) !== valueStr) {
this.adapter.setInputValue(valueStr, thumb);
}
const valueToAriaValueTextFn = this.adapter.getValueToAriaValueTextFn();
if (valueToAriaValueTextFn) {
this.adapter.setInputAttribute(
attributes.ARIA_VALUETEXT, valueToAriaValueTextFn(value), thumb);
}
}
/**
* Updates value indicator UI based on current value.
* @param thumb Thumb whose value indicator to update. If undefined, all
* thumbs' value indicators are updated.
*/
private updateValueIndicatorUI(thumb?: Thumb) {
if (!this.isDiscrete) return;
const value =
this.isRange && thumb === Thumb.START ? this.valueStart : this.value;
this.adapter.setValueIndicatorText(
value, thumb === Thumb.START ? Thumb.START : Thumb.END);
if (!thumb && this.isRange) {
this.adapter.setValueIndicatorText(this.valueStart, Thumb.START);
}
}
/**
* Updates tick marks UI within slider, based on current min, max, and step.
*/
private updateTickMarksUI() {
if (!this.isDiscrete || !this.hasTickMarks) return;
const numTickMarksInactiveStart = (this.valueStart - this.min) / this.step;
const numTickMarksActive = (this.value - this.valueStart) / this.step + 1;
const numTickMarksInactiveEnd = (this.max - this.value) / this.step;
const tickMarksInactiveStart =
Array.from<TickMark>({length: numTickMarksInactiveStart})
.fill(TickMark.INACTIVE);
const tickMarksActive = Array.from<TickMark>({length: numTickMarksActive})
.fill(TickMark.ACTIVE);
const tickMarksInactiveEnd =
Array.from<TickMark>({length: numTickMarksInactiveEnd})
.fill(TickMark.INACTIVE);
this.adapter.updateTickMarks(tickMarksInactiveStart.concat(tickMarksActive)
.concat(tickMarksInactiveEnd));
}
/** Maps clientX to a value on the slider scale. */
private mapClientXOnSliderScale(clientX: number): number {
const xPos = clientX - this.rect.left;
let pctComplete = xPos / this.rect.width;
if (this.adapter.isRTL()) {
pctComplete = 1 - pctComplete;
}
// Fit the percentage complete between the range [min,max]
// by remapping from [0, 1] to [min, min+(max-min)].
const value = this.min + pctComplete * (this.max - this.min);
if (value === this.max || value === this.min) {
return value;
}
return Number(this.quantize(value).toFixed(this.numDecimalPlaces));
}
/** Calculates the quantized value based on step value. */
private quantize(value: number): number {
const numSteps = Math.round((value - this.min) / this.step);
return this.min + numSteps * this.step;
}
/**
* Updates slider value (internal state and UI) based on the given value.
*/
private updateValue(value: number, thumb: Thumb, {
emitInputEvent,
}: {emitInputEvent?: boolean} = {}) {
value = this.clampValue(value, thumb);
if (this.isRange && thumb === Thumb.START) {
// Exit early if current value is the same as the new value.
if (this.valueStart === value) return;
this.valueStart = value;
} else {
// Exit early if current value is the same as the new value.
if (this.value === value) return;
this.value = value;
}
this.updateUI(thumb);
if (emitInputEvent) {
this.adapter.emitInputEvent(
thumb === Thumb.START ? this.valueStart : this.value, thumb);
}
}
/**
* Clamps the given value for the given thumb based on slider properties:
* - Restricts value within [min, max].
* - If range slider, clamp start value <= end value, and
* end value >= start value.
*/
private clampValue(value: number, thumb: Thumb): number {
// Clamp value to [min, max] range.
value = Math.min(Math.max(value, this.min), this.max);
const thumbStartMovedPastThumbEnd =
this.isRange && thumb === Thumb.START && value > this.value;
if (thumbStartMovedPastThumbEnd) {
return this.value;
}
const thumbEndMovedPastThumbStart =
this.isRange && thumb === Thumb.END && value < this.valueStart;
if (thumbEndMovedPastThumbStart) {
return this.valueStart;
}
return value;
}
/**
* Updates the active track and thumb style properties to reflect current
* value.
*/
private updateThumbAndTrackUI(thumb?: Thumb) {
const {max, min} = this;
const pctComplete = (this.value - this.valueStart) / (max - min);
const rangePx = pctComplete * this.rect.width;
const isRtl = this.adapter.isRTL();
const transformProp =
HAS_WINDOW ? getCorrectPropertyName(window, 'transform') : 'transform';
if (this.isRange) {
const thumbLeftPos = this.adapter.isRTL() ?
(max - this.value) / (max - min) * this.rect.width :
(this.valueStart - min) / (max - min) * this.rect.width;
const thumbRightPos = thumbLeftPos + rangePx;
this.animFrame.request(AnimationKeys.SLIDER_UPDATE, () => {
// Set active track styles, accounting for animation direction by
// setting `transform-origin`.
const trackAnimatesFromRight = (!isRtl && thumb === Thumb.START) ||
(isRtl && thumb !== Thumb.START);
if (trackAnimatesFromRight) {
this.adapter.setTrackActiveStyleProperty('transform-origin', 'right');
this.adapter.setTrackActiveStyleProperty('left', 'unset');
this.adapter.setTrackActiveStyleProperty(
'right', `${this.rect.width - thumbRightPos}px`);
} else {
this.adapter.setTrackActiveStyleProperty('transform-origin', 'left');
this.adapter.setTrackActiveStyleProperty('right', 'unset');
this.adapter.setTrackActiveStyleProperty('left', `${thumbLeftPos}px`);
}
this.adapter.setTrackActiveStyleProperty(
transformProp, `scaleX(${pctComplete})`);
// Set thumb styles.
const thumbStartPos = isRtl ? thumbRightPos : thumbLeftPos;
const thumbEndPos = this.adapter.isRTL() ? thumbLeftPos : thumbRightPos;
if (thumb === Thumb.START || !thumb || !this.initialStylesRemoved) {
this.adapter.setThumbStyleProperty(
transformProp, `translateX(${thumbStartPos}px)`, Thumb.START);
}
if (thumb === Thumb.END || !thumb || !this.initialStylesRemoved) {
this.adapter.setThumbStyleProperty(
transformProp, `translateX(${thumbEndPos}px)`, Thumb.END);
}
this.removeInitialStyles(isRtl);
this.updateOverlappingThumbsUI(thumbStartPos, thumbEndPos, thumb);
});
} else {
this.animFrame.request(AnimationKeys.SLIDER_UPDATE, () => {
const thumbStartPos = isRtl ? this.rect.width - rangePx : rangePx;
this.adapter.setThumbStyleProperty(
transformProp, `translateX(${thumbStartPos}px)`, Thumb.END);
this.adapter.setTrackActiveStyleProperty(
transformProp, `scaleX(${pctComplete})`);
this.removeInitialStyles(isRtl);
});
}
}
/**
* Removes initial inline styles if not already removed. `left:<...>%`
* inline styles can be added to position the thumb correctly before JS
* initialization. However, they need to be removed before the JS starts
* positioning the thumb. This is because the JS uses
* `transform:translateX(<...>)px` (for performance reasons) to position
* the thumb (which is not possible for initial styles since we need the
* bounding rect measurements).
*/
private removeInitialStyles(isRtl: boolean) {
if (this.initialStylesRemoved) return;
// Remove thumb position properties that were added for initial render.
const position = isRtl ? 'right' : 'left';
this.adapter.removeThumbStyleProperty(position, Thumb.END);
if (this.isRange) {
this.adapter.removeThumbStyleProperty(position, Thumb.START);
}
this.initialStylesRemoved = true;
this.resetTrackAndThumbAnimation();
}
/**
* Resets track/thumb animation to prevent animation when adding
* `transform` styles to thumb initially.
*/
private resetTrackAndThumbAnimation() {
if (!this.isDiscrete) return;
// Set transition properties to default (no animation), so that the
// newly added `transform` styles do not animate thumb/track from
// their default positions.
const transitionProp = HAS_WINDOW ?
getCorrectPropertyName(window, 'transition') :
'transition';
const transitionDefault = 'all 0s ease 0s';
this.adapter.setThumbStyleProperty(
transitionProp, transitionDefault, Thumb.END);
if (this.isRange) {
this.adapter.setThumbStyleProperty(
transitionProp, transitionDefault, Thumb.START);
}
this.adapter.setTrackActiveStyleProperty(transitionProp, transitionDefault);
// In the next frame, remove the transition inline styles we just
// added, such that any animations added in the CSS can now take effect.
requestAnimationFrame(() => {
this.adapter.removeThumbStyleProperty(transitionProp, Thumb.END);
this.adapter.removeTrackActiveStyleProperty(transitionProp);
if (this.isRange) {
this.adapter.removeThumbStyleProperty(transitionProp, Thumb.START);
}
});
}
/**
* Adds THUMB_TOP class to active thumb if thumb knobs overlap; otherwise
* removes THUMB_TOP class from both thumbs.
* @param thumb Thumb that is active (being moved).
*/
private updateOverlappingThumbsUI(
thumbStartPos: number, thumbEndPos: number, thumb?: Thumb) {
let thumbsOverlap = false;
if (this.adapter.isRTL()) {
const startThumbLeftEdge = thumbStartPos - this.startThumbKnobWidth / 2;
const endThumbRightEdge = thumbEndPos + this.endThumbKnobWidth / 2;
thumbsOverlap = endThumbRightEdge >= startThumbLeftEdge;
} else {
const startThumbRightEdge = thumbStartPos + this.startThumbKnobWidth / 2;
const endThumbLeftEdge = thumbEndPos - this.endThumbKnobWidth / 2;
thumbsOverlap = startThumbRightEdge >= endThumbLeftEdge;
}
if (thumbsOverlap) {
this.adapter.addThumbClass(
cssClasses.THUMB_TOP,
// If no thumb was dragged (in the case of initial layout), end
// thumb is on top by default.
thumb || Thumb.END);
this.adapter.removeThumbClass(
cssClasses.THUMB_TOP,
thumb === Thumb.START ? Thumb.END : Thumb.START);
} else {
this.adapter.removeThumbClass(cssClasses.THUMB_TOP, Thumb.START);
this.adapter.removeThumbClass(cssClasses.THUMB_TOP, Thumb.END);
}
}
/**
* Converts attribute value to a number, e.g. '100' => 100. Throws errors
* for invalid values.
* @param attributeValue Attribute value, e.g. 100.
* @param attributeName Attribute name, e.g. `aria-valuemax`.
*/
private convertAttributeValueToNumber(
attributeValue: string|null, attributeName: string) {
if (attributeValue === null) {
throw new Error(
`MDCSliderFoundation: \`${attributeName}\` must be non-null.`);
}
const value = Number(attributeValue);
if (isNaN(value)) {
throw new Error(
`MDCSliderFoundation: \`${attributeName}\` value is ` +
`\`${attributeValue}\`, but must be a number.`);
}
return value;
}
/** Checks that the given properties are valid slider values. */
private validateProperties({min, max, value, valueStart, step}: {
min: number,
max: number,
value: number,
valueStart: number,
step: number
}) {
if (min >= max) {
throw new Error(
`MDCSliderFoundation: min must be strictly less than max. ` +
`Current: [min: ${min}, max: ${max}]`);
}
if (step <= 0) {
throw new Error(
`MDCSliderFoundation: step must be a positive number. ` +
`Current step: ${this.step}`);
}
if (this.isRange) {