forked from syncfusion/ej2-javascript-ui-controls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslider.ts
3499 lines (3298 loc) · 147 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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Component, EventHandler, Property, Event, EmitType, Complex, Collection } from '@syncfusion/ej2-base';
import { L10n, Internationalization, NumberFormatOptions } from '@syncfusion/ej2-base';
import { NotifyPropertyChanges, INotifyPropertyChanged, ChildProperty } from '@syncfusion/ej2-base';
import { attributes, addClass, removeClass, setStyleAttribute, detach, closest } from '@syncfusion/ej2-base';
import { isNullOrUndefined, formatUnit, Browser, SanitizeHtmlHelper, initializeCSPTemplate } from '@syncfusion/ej2-base';
import { Tooltip, Position, TooltipEventArgs, getZindexPartial } from '@syncfusion/ej2-popups';
import { SliderModel, TicksDataModel, TooltipDataModel, LimitDataModel, ColorRangeDataModel } from './slider-model';
/**
* Configures the ticks data of the Slider.
*/
export class TicksData extends ChildProperty<TicksData> {
/**
* It is used to denote the position of the ticks in the Slider. The available options are:
*
* * before - Ticks are placed in the top of the horizontal slider bar or at the left of the vertical slider bar.
* * after - Ticks are placed in the bottom of the horizontal slider bar or at the right of the vertical slider bar.
* * both - Ticks are placed on the both side of the Slider bar.
* * none - Ticks are not shown.
* {% codeBlock src='slider/placement/index.md' %}{% endcodeBlock %}
*
* @default 'None'
*/
@Property('None')
public placement: Placement;
/**
* It is used to denote the distance between two major (large) ticks from the scale of the Slider.
* {% codeBlock src='slider/largestep/index.md' %}{% endcodeBlock %}
*
* @default 10
*/
@Property(10)
public largeStep: number;
/**
* It is used to denote the distance between two minor (small) ticks from the scale of the Slider.
* {% codeBlock src='slider/smallstep/index.md' %}{% endcodeBlock %}
*
* @default 1
*/
@Property(1)
public smallStep: number;
/**
* We can show or hide the small ticks in the Slider, which will be appeared in between the largeTicks.
*
* @default false
*/
@Property(false)
public showSmallTicks: boolean;
/**
* It is used to customize the Slider scale value to the desired format using Internationalization or events(custom formatting).
* {% codeBlock src='slider/format/index.md' %}{% endcodeBlock %}
*/
@Property(null)
public format: string;
}
/**
* It is used to denote the TooltipChange Event arguments.
*/
export interface SliderTooltipEventArgs {
/**
* It is used to get the value of the Slider.
*
* @isGenericType true
*/
value: number | number[];
/**
* It is used to get the text shown in the Slider tooltip.
*/
text: string;
}
/**
* It is used to denote the Slider Change/Changed Event arguments.
*/
export interface SliderChangeEventArgs {
/**
* It is used to get the current value of the Slider.
*
* @isGenericType true
*/
value: number | number[];
/**
* It is used to get the previous value of the Slider.
*
* @isGenericType true
*/
previousValue: number | number[];
/**
* It is used to get the current text or formatted text of the Slider, which is placed in tooltip.
*/
text?: string;
/**
* It is used to get the action applied on the Slider.
*/
action: string;
/**
* It is used to check whether the event triggered is via user or programmatic way.
*/
isInteracted: boolean;
}
/**
* It is used to denote the TicksRender Event arguments.
*/
export interface SliderTickEventArgs {
/**
* It is used to get the value of the tick.
*/
value: number;
/**
* It is used to get the label text of the tick.
*/
text: string;
/**
* It is used to get the current tick element.
*/
tickElement: Element;
}
/**
* It is used t denote the ticks rendered Event arguments.
*/
export interface SliderTickRenderedEventArgs {
/**
* It returns the wrapper of the ticks element.
*/
ticksWrapper: HTMLElement;
/**
* It returns the collection of tick elements.
*/
tickElements: HTMLElement[];
}
/**
* It illustrates the color track data in slider.
* {% codeBlock src='slider/colorrange/index.md' %}{% endcodeBlock %}
*/
export class ColorRangeData extends ChildProperty<ColorRangeData> {
/**
* It is used to set the color in the slider bar.
*
* @default ''
*/
@Property(null)
public color: string;
/**
* It is used to get the starting value for applying color.
*
* @default null
*/
@Property(null)
public start: number;
/**
* It is used to get the end value for applying color.
*
* @default null
*/
@Property(null)
public end: number;
}
/**
* It illustrates the limit data in slider.
* {% codeBlock src='slider/limits/index.md' %}{% endcodeBlock %}
*/
export class LimitData extends ChildProperty<LimitData> {
/**
* It is used to enable the limit in the slider.
*
* @default false
*/
@Property(false)
public enabled: boolean;
/**
* It is used to set the minimum start limit value.
*
* @default null
*/
@Property(null)
public minStart: number;
/**
* It is used to set the minimum end limit value.
*
* @default null
*/
@Property(null)
public minEnd: number;
/**
* It is used to set the maximum start limit value.
*
* @default null
*/
@Property(null)
public maxStart: number;
/**
* It is used to set the maximum end limit value.
*
* @default null
*/
@Property(null)
public maxEnd: number;
/**
* It is used to lock the first handle.
* {% codeBlock src='slider/limitStartHandleFixed/index.md' %}{% endcodeBlock %}
*
* @default false
*/
@Property(false)
public startHandleFixed: boolean;
/**
* It is used to lock the second handle.
* {% codeBlock src='slider/limitEndHandleFixed/index.md' %}{% endcodeBlock %}
*
* @default false
*/
@Property(false)
public endHandleFixed: boolean;
}
/**
* It illustrates the tooltip data in slider.
*/
export class TooltipData extends ChildProperty<TooltipData> {
/**
* It is used to customize the Tooltip which accepts custom CSS class names that define
* specific user-defined styles and themes to be applied on the Tooltip element.
*
* @default ''
*/
@Property('')
public cssClass: string;
/**
* It is used to denote the position for the tooltip element in the Slider. The available options are:
* {% codeBlock src='slider/tooltipplacement/index.md' %}{% endcodeBlock %}
* * Before - Tooltip is shown in the top of the horizontal slider bar or at the left of the vertical slider bar.
* * After - Tooltip is shown in the bottom of the horizontal slider bar or at the right of the vertical slider bar.
*/
@Property('Before')
public placement: TooltipPlacement;
/**
* It is used to determine the device mode to show the Tooltip.
* If it is in desktop, it will show the Tooltip content when hovering on the target element.
* If it is in touch device. It will show the Tooltip content when tap and holding on the target element.
* {% codeBlock src='slider/tooltipShowOn/index.md' %}{% endcodeBlock %}
*
* @default 'Auto'
*/
@Property('Focus')
public showOn: TooltipShowOn;
/**
* It is used to show or hide the Tooltip of Slider Component.
* {% codeBlock src='slider/tooltipIsVisible/index.md' %}{% endcodeBlock %}
*/
@Property(false)
public isVisible: boolean;
/**
* It is used to customize the Tooltip content to the desired format
* using internationalization or events (custom formatting).
*/
@Property(null)
public format: string;
}
/**
* Ticks Placement.
* ```props
* Before :- Ticks are placed in the top of the horizontal slider bar or at the left of the vertical slider bar.
* After :- Ticks are placed in the bottom of the horizontal slider bar or at the right of the vertical slider bar.
* Both :- Ticks are placed on the both side of the slider bar.
* None :- Ticks are not shown.
* ```
*/
export type Placement = 'Before' | 'After' | 'Both' | 'None';
/**
* Tooltip Placement.
* ```props
* Before :- Tooltip is shown in the top of the horizontal slider bar or at the left of the vertical slider bar.
* After :- Tooltip is shown in the bottom of the horizontal slider bar or at the right of the vertical slider bar.
* ```
*/
export type TooltipPlacement = 'Before' | 'After';
/**
* Tooltip ShowOn.
* ```props
* Focus :- Tooltip is shown while focusing the Slider handle.
* Hover :- Tooltip is shown while hovering the Slider handle.
* Always :- Tooltip is shown always.
* Auto :- Tooltip is shown while hovering the Slider handle in desktop and tap and hold in touch devices.
* ```
*/
export type TooltipShowOn = 'Focus' | 'Hover' | 'Always' | 'Auto';
/**
* Slider type.
* ```props
* Default :- Allows to select a single value in the Slider.
* MinRange :- Allows to select a single value in the Slider, it display’s a shadow from the start to the current value.
* Range :- Allows to select a range of values in the Slider.
* ```
*/
export type SliderType = 'Default' | 'MinRange' | 'Range';
/**
* Slider orientation.
* ```props
* Horizontal :- Renders the slider in horizontal orientation.
* Vertical :- Renders the slider in vertical orientation.
* ```
*/
export type SliderOrientation = 'Horizontal' | 'Vertical';
type SliderHandleNumber = 1 | 2;
const bootstrapTooltipOffset: number = 6;
const bootstrap4TooltipOffset: number = 3;
const classNames: { [key: string]: string } = {
root: 'e-slider',
rtl: 'e-rtl',
sliderHiddenInput: 'e-slider-input',
controlWrapper: 'e-control-wrapper',
sliderHandle: 'e-handle',
rangeBar: 'e-range',
sliderButton: 'e-slider-button',
firstButton: 'e-first-button',
secondButton: 'e-second-button',
scale: 'e-scale',
tick: 'e-tick',
large: 'e-large',
tickValue: 'e-tick-value',
sliderTooltip: 'e-slider-tooltip',
sliderHover: 'e-slider-hover',
sliderFirstHandle: 'e-handle-first',
sliderSecondHandle: 'e-handle-second',
sliderDisabled: 'e-disabled',
sliderContainer: 'e-slider-container',
horizontalTooltipBefore: 'e-slider-horizontal-before',
horizontalTooltipAfter: 'e-slider-horizontal-after',
verticalTooltipBefore: 'e-slider-vertical-before',
verticalTooltipAfter: 'e-slider-vertical-after',
materialTooltip: 'e-material-tooltip',
materialTooltipOpen: 'e-material-tooltip-open',
materialTooltipActive: 'e-tooltip-active',
materialSlider: 'e-material-slider',
sliderTrack: 'e-slider-track',
sliderHorizantalColor: 'e-slider-horizantal-color',
sliderVerticalColor: 'e-slider-vertical-color',
sliderHandleFocused: 'e-handle-focused',
verticalSlider: 'e-vertical',
horizontalSlider: 'e-horizontal',
sliderHandleStart: 'e-handle-start',
sliderTooltipStart: 'e-material-tooltip-start',
sliderTabHandle: 'e-tab-handle',
sliderButtonIcon: 'e-button-icon',
sliderSmallSize: 'e-small-size',
sliderTickPosition: 'e-tick-pos',
sliderFirstTick: 'e-first-tick',
sliderLastTick: 'e-last-tick',
sliderButtonClass: 'e-slider-btn',
sliderTooltipWrapper: 'e-tooltip-wrap',
sliderTabTrack: 'e-tab-track',
sliderTabRange: 'e-tab-range',
sliderActiveHandle: 'e-handle-active',
sliderMaterialHandle: 'e-material-handle',
sliderMaterialRange: 'e-material-range',
sliderMaterialDefault: 'e-material-default',
materialTooltipShow: 'e-material-tooltip-show',
materialTooltipHide: 'e-material-tooltip-hide',
readonly: 'e-read-only',
limits: 'e-limits',
limitBarDefault: 'e-limit-bar',
limitBarFirst: 'e-limit-first',
limitBarSecond: 'e-limit-second',
dragHorizontal: 'e-drag-horizontal',
dragVertical: 'e-drag-vertical'
};
/**
* The Slider component allows the user to select a value or range
* of values in-between a min and max range, by dragging the handle over the slider bar.
* ```html
* <div id='slider'></div>
* ```
* ```typescript
* <script>
* var sliderObj = new Slider({ value: 10 });
* sliderObj.appendTo('#slider');
* </script>
* ```
*/
@NotifyPropertyChanges
export class Slider extends Component<HTMLElement> implements INotifyPropertyChanged {
/* Internal variables */
private hiddenInput: HTMLInputElement;
private firstHandle: HTMLElement;
private sliderContainer: HTMLElement;
private secondHandle: HTMLElement;
private rangeBar: HTMLElement;
private onresize: EventListener;
private isElementFocused: boolean;
private handlePos1: number;
private handlePos2: number;
private rtl: boolean;
private preHandlePos1: number;
private preHandlePos2: number;
private handleVal1: number;
private handleVal2: number;
private val: number;
private activeHandle: number;
private sliderTrack: HTMLElement;
private materialHandle: HTMLElement;
private firstBtn: HTMLElement;
private tooltipObj: Tooltip;
private tooltipElement: HTMLElement;
private isMaterialTooltip: boolean;
private secondBtn: HTMLElement;
private ul: HTMLElement;
private firstChild: Element;
private tooltipCollidedPosition: string;
private tooltipTarget: HTMLElement;
private lastChild: Element;
private previousTooltipClass: string;
private horDir: string = 'left';
private verDir: string = 'bottom';
private transition: { [key: string]: string } = {
handle: 'left .4s cubic-bezier(.25, .8, .25, 1), right .4s cubic-bezier(.25, .8, .25, 1), ' +
'top .4s cubic-bezier(.25, .8, .25, 1) , bottom .4s cubic-bezier(.25, .8, .25, 1)',
rangeBar: 'all .4s cubic-bezier(.25, .8, .25, 1)'
};
private transitionOnMaterialTooltip: { [key: string]: string } = {
handle: 'left 1ms ease-out, right 1ms ease-out, bottom 1ms ease-out, top 1ms ease-out',
rangeBar: 'left 1ms ease-out, right 1ms ease-out, bottom 1ms ease-out, width 1ms ease-out, height 1ms ease-out'
};
private scaleTransform: string = 'transform .4s cubic-bezier(.25, .8, .25, 1)';
private previousVal: number | number[];
private previousChanged: number | number[];
// eslint-disable-next-line
private repeatInterval: any;
private isMaterial: boolean;
private isMaterial3: boolean;
private isBootstrap: boolean;
private isBootstrap4: boolean;
private isTailwind: boolean;
private isTailwind3: boolean;
private isBootstrap5: boolean;
private isFluent: boolean;
private isFluent2: boolean;
private isBootstrap5Dot3: boolean;
private zIndex: number;
private l10n: L10n;
private internationalization: Internationalization;
private tooltipFormatInfo: NumberFormatOptions;
private ticksFormatInfo: NumberFormatOptions;
private customAriaText: string = null;
private noOfDecimals: number;
private tickElementCollection: HTMLElement[];
private limitBarFirst: HTMLElement;
private limitBarSecond: HTMLElement;
private firstPartRemain: number;
private secondPartRemain: number;
private minDiff: number;
private drag: boolean = true;
private isForm: boolean;
private formElement: HTMLFormElement;
private formResetValue: number | number[];
private rangeBarDragged: boolean;
private isDragComplete: boolean = false;
public initialTooltip: boolean = true;
/**
* It is used to denote the current value of the Slider.
* The value should be specified in array of number when render Slider type as range.
*
* {% codeBlock src="slider/value-api/index.ts" %}{% endcodeBlock %}
*
* @default null
* @isGenericType true
*/
@Property(null)
public value: number | number[];
/**
* Specifies an array of slider values in number or string type.
* The min and max step values are not considered.
*
* @default null
*/
@Property(null)
public customValues: string[] | number[];
/**
* Specifies the step value for each value change when the increase / decrease
* button is clicked or on arrow keys press or on dragging the thumb.
* Refer the documentation [here](../../slider/ticks#step)
* to know more about this property.
*
* {% codeBlock src="slider/step-api/index.ts" %}{% endcodeBlock %}
*
* @default 1
*/
@Property(1)
public step: number;
/**
* Specifies the width of the Slider.
*
* @default null
*/
@Property(null)
public width: number | string;
/**
* Gets/Sets the minimum value of the slider.
*
* {% codeBlock src="slider/min-max-api/index.ts" %}{% endcodeBlock %}
*
* @default 0
*/
@Property(0)
public min: number;
/**
* Gets/Sets the maximum value of the slider.
*
* {% codeBlock src="slider/min-max-api/index.ts" %}{% endcodeBlock %}
*
* @default 100
*/
@Property(100)
public max: number;
/**
* Specifies whether the render the slider in read-only mode to restrict any user interaction.
* The slider rendered with user defined values and can’t be interacted with user actions.
*
* @default false
*/
@Property(false)
public readonly: boolean;
/**
* Defines the type of the Slider. The available options are:
* * default - Allows to a single value in the Slider.
* * minRange - Allows to select a single value in the Slider. It display’s a shadow from the start to the current value.
* * range - Allows to select a range of values in the Slider. It displays shadow in-between the selection range.
* {% codeBlock src='slider/types/index.md' %}{% endcodeBlock %}
*
* @default 'Default'
*/
@Property('Default')
public type: SliderType;
/**
* Specifies the color to the slider based on given value.
*/
@Collection<ColorRangeDataModel>([{}], ColorRangeData)
public colorRange: ColorRangeDataModel[];
/**
* It is used to render the slider ticks options such as placement and step values.
* Refer the documentation [here](../../slider/ticks)
* to know more about this property with demo.
* {% codeBlock src='slider/ticks/index.md' %}{% endcodeBlock %}
* {% codeBlock src="slider/ticks-api/index.ts" %}{% endcodeBlock %}
*
* @default { placement: 'before' }
*/
@Complex<TicksDataModel>({}, TicksData)
public ticks: TicksDataModel;
/**
* Specified the limit within which the slider to be moved.
* Refer the documentation [here](../../slider/limits)
* to know more about this property.
*
* {% codeBlock src="slider/limits-api/index.ts" %}{% endcodeBlock %}
*
* @default { enabled: false }
*/
@Complex<LimitDataModel>({}, LimitData)
public limits: LimitDataModel;
/**
* Enable or Disable the slider.
*
* @default true
*/
@Property(true)
public enabled: boolean;
/**
* Specifies the visibility, position of the tooltip over the slider element.
*
* {% codeBlock src="slider/tooltip-api/index.ts" %}{% endcodeBlock %}
*
* @default { placement: 'Before', isVisible: false, showOn: 'Focus', format: null }
*/
@Complex<TooltipDataModel>({}, TooltipData)
public tooltip: TooltipDataModel;
/**
* Specifies whether to show or hide the increase/decrease buttons
* of Slider to change the slider value.
* Refer the documentation [here](../../slider/getting-started#buttons)
* to know more about this property.
*
* {% codeBlock src="slider/showButtons-api/index.ts" %}{% endcodeBlock %}
*
* @default false
*/
@Property(false)
public showButtons: boolean;
/**
* Enable or Disable the animation for slider movement.
*
* @default true
*/
@Property(true)
public enableAnimation: boolean;
/**
* Specifies whether to render the slider in vertical or horizontal orientation.
* Refer the documentation [here](../../slider/orientation/)
* to know more about this property.
*
* @default 'Horizontal'
*/
@Property('Horizontal')
public orientation: SliderOrientation;
/**
* Specifies the custom classes to be added to the element used to customize the slider.
* {% codeBlock src='slider/cssClass/index.md' %}{% endcodeBlock %}
*
* @default ''
*/
@Property('')
public cssClass: string;
/**
* Specifies whether to display or remove the untrusted HTML values in the Slider component.
* If 'enableHtmlSanitizer' set to true, the component will sanitize any suspected untrusted strings and scripts before rendering them.
*
* @default true
*/
@Property(true)
public enableHtmlSanitizer: boolean;
/**
* Triggers when the Slider is successfully created.
*
* @event created
*/
@Event()
public created: EmitType<Object>;
/**
* We can trigger change event whenever Slider value is changed.
* In other term, this event will be triggered while drag the slider thumb.
* {% codeBlock src='slider/changeEvent/index.md' %}{% endcodeBlock %}
*
* @event change
*/
@Event()
public change: EmitType<SliderChangeEventArgs>;
/**
* Fires whenever the Slider value is changed.
* In other term, this event will be triggered, while drag the slider thumb completed.
*
* @event changed
*/
@Event()
public changed: EmitType<SliderChangeEventArgs>;
/**
* Triggers on rendering the ticks element in the Slider,
* which is used to customize the ticks labels dynamically.
* {% codeBlock src='slider/renderingticksEvent/index.md' %}{% endcodeBlock %}
*
* @event renderingTicks
*/
@Event()
public renderingTicks: EmitType<SliderTickEventArgs>;
/**
* Triggers when the ticks are rendered on the Slider.
* {% codeBlock src='slider/renderedticksEvent/index.md' %}{% endcodeBlock %}
*
* @event renderedTicks
*/
@Event()
public renderedTicks: EmitType<SliderTickRenderedEventArgs>;
/**
* Triggers when the Sider tooltip value is changed.
* {% codeBlock src='slider/tooltipChangeEvent/index.md' %}{% endcodeBlock %}
*
* @event tooltipChange
*/
@Event()
public tooltipChange: EmitType<SliderTooltipEventArgs>;
constructor(options?: SliderModel, element?: string | HTMLElement) {
super(options, <HTMLElement | string>element);
}
protected preRender(): void {
const localeText: object = { incrementTitle: 'Increase', decrementTitle: 'Decrease' };
this.l10n = new L10n('slider', localeText, this.locale);
this.isElementFocused = false;
this.tickElementCollection = [];
this.tooltipFormatInfo = {};
this.ticksFormatInfo = {};
this.initCultureInfo();
this.initCultureFunc();
this.formChecker();
}
private formChecker(): void {
const formElement: Element = closest(this.element, 'form');
if (formElement) {
this.isForm = true;
// this condition needs to be checked, if the slider is going to be refreshed by `refresh()`
// then we need to revert the slider `value` back to `formResetValue` to preserve the initial value
if (!isNullOrUndefined(this.formResetValue)) {
this.setProperties({ 'value': this.formResetValue }, true);
}
this.formResetValue = this.value;
if (this.type === 'Range' &&
(isNullOrUndefined(this.formResetValue) || typeof (this.formResetValue) !== 'object')) {
this.formResetValue = [parseFloat(formatUnit(this.min)), parseFloat(formatUnit(this.max))];
} else if (isNullOrUndefined(this.formResetValue)) {
this.formResetValue = parseFloat(formatUnit(this.min));
}
this.formElement = formElement as HTMLFormElement;
} else {
this.isForm = false;
}
}
private initCultureFunc(): void {
this.internationalization = new Internationalization(this.locale);
}
private initCultureInfo(): void {
this.tooltipFormatInfo.format = (!isNullOrUndefined(this.tooltip.format)) ? this.tooltip.format : null;
this.ticksFormatInfo.format = (!isNullOrUndefined(this.ticks.format)) ? this.ticks.format : null;
}
private formatString(value: number, formatInfo: NumberFormatOptions): { elementVal: string, formatString: string } {
let formatValue: string = null;
let formatString: string = null;
if ((value || value === 0)) {
formatValue = this.formatNumber(value);
const numberOfDecimals: number = this.numberOfDecimals(value);
formatString = this.internationalization.getNumberFormat(formatInfo)(this.makeRoundNumber(value, numberOfDecimals));
}
return { elementVal: formatValue, formatString: formatString };
}
private formatNumber(value: number): string {
const numberOfDecimals: number = this.numberOfDecimals(value);
return this.internationalization.getNumberFormat({
maximumFractionDigits: numberOfDecimals,
minimumFractionDigits: numberOfDecimals, useGrouping: false
})(value);
}
private numberOfDecimals(value: number | string): number {
const decimalPart: string = value.toString().split('.')[1];
const numberOfDecimals: number = !decimalPart || !decimalPart.length ? 0 : decimalPart.length;
return numberOfDecimals;
}
private makeRoundNumber(value: number, precision: number): number {
const decimals: number = precision || 0;
return Number(value.toFixed(decimals));
}
private fractionalToInteger(value: number | string): number {
value = (this.numberOfDecimals(value) === 0) ? Number(value).toFixed(this.noOfDecimals) : value;
let tens: number = 1;
for (let i: number = 0; i < this.noOfDecimals; i++) { tens *= 10; }
value = Number((<number>value * tens).toFixed(0));
return value;
}
/**
* To Initialize the control rendering
*
* @returns {void}
* @private
*/
public render(): void {
this.initialize();
this.initRender();
this.wireEvents();
this.setZindex();
this.renderComplete();
if (this.element.tagName === 'EJS-SLIDER') {
if (this.getTheme(this.sliderContainer) === 'none') {
setTimeout(() => {
this.refresh();
}, 0);
}
}
}
private initialize(): void {
addClass([this.element], classNames.root);
this.setCSSClass();
}
private setElementWidth(width: number | string): void {
if (!isNullOrUndefined(width) && !isNullOrUndefined(this.sliderContainer)) {
if (typeof width === 'number') {
this.sliderContainer.style.width = formatUnit(width);
} else if (typeof width === 'string') {
this.sliderContainer.style.width = (width.match(/px|%|em/)) ? <string>(width) : <string>(formatUnit(width));
}
}
}
private setCSSClass(oldCSSClass?: string): void {
if (oldCSSClass) {
removeClass([this.element], oldCSSClass.split(' '));
}
if (this.cssClass) {
addClass([this.element], this.cssClass.split(' '));
}
}
private setEnabled(): void {
if (!this.enabled) {
addClass([this.sliderContainer], [classNames.sliderDisabled]);
if (this.tooltip.isVisible && this.tooltipElement && this.tooltip.showOn === 'Always') {
this.tooltipElement.classList.add(classNames.sliderDisabled);
}
this.unwireEvents();
} else {
removeClass([this.sliderContainer], [classNames.sliderDisabled]);
if (this.tooltip.isVisible && this.tooltipElement && this.tooltip.showOn === 'Always') {
this.tooltipElement.classList.remove(classNames.sliderDisabled);
}
this.wireEvents();
}
}
private getTheme(container: HTMLElement): string {
const theme: string = window.getComputedStyle(container as Element, ':after').getPropertyValue('content');
return theme.replace(/['"]+/g, '');
}
/**
* Initialize the rendering
*
* @returns {void}
* @private
*/
private initRender(): void {
this.sliderContainer = this.createElement('div', { className: classNames.sliderContainer + ' ' + classNames.controlWrapper });
this.element.parentNode.insertBefore(this.sliderContainer, this.element);
this.sliderContainer.appendChild(this.element);
this.sliderTrack = this.createElement('div', { className: classNames.sliderTrack });
this.element.appendChild(this.sliderTrack);
this.setElementWidth(this.width);
this.element.tabIndex = -1;
this.getThemeInitialization();
this.setHandler();
this.createRangeBar();
if (this.limits.enabled) {
this.createLimitBar();
}
this.setOrientClass();
this.hiddenInput = <HTMLInputElement>(this.createElement('input', {
attrs: {
type: 'hidden', value: (isNullOrUndefined(this.value) ? (isNullOrUndefined(this.min) ? '0' : this.min.toString()) : this.value.toString()),
name: this.element.getAttribute('name') || this.element.getAttribute('id') ||
'_' + (Math.random() * 1000).toFixed(0) + 'slider', class: classNames.sliderHiddenInput
}
}));
this.hiddenInput.tabIndex = -1;
this.sliderContainer.appendChild(this.hiddenInput);
if (this.showButtons) { this.setButtons(); }
this.setEnableRTL();
if (this.type === 'Range') {
this.rangeValueUpdate();
} else {
this.value = isNullOrUndefined(this.value) ? (isNullOrUndefined(this.min) ? 0 :
parseFloat(formatUnit(this.min.toString()))) : this.value;
}
this.previousVal = this.type !== 'Range' ? this.checkHandleValue(parseFloat(formatUnit(this.value.toString()))) :
[this.checkHandleValue(parseFloat(formatUnit((this.value as number[])[0].toString()))),
this.checkHandleValue(parseFloat(formatUnit((this.value as number[])[1].toString())))];
this.previousChanged = this.previousVal;
if (!isNullOrUndefined(this.element.hasAttribute('name'))) {
this.element.removeAttribute('name');
}
this.setValue();
if (this.limits.enabled) {
this.setLimitBar();
}
if (this.ticks.placement !== 'None') {
this.renderScale();
}
if (this.tooltip.isVisible) {
this.renderTooltip();
}
if (!this.enabled) {
addClass([this.sliderContainer], [classNames.sliderDisabled]);
} else {
removeClass([this.sliderContainer], [classNames.sliderDisabled]);
}
if (this.readonly) {
addClass([this.sliderContainer], [classNames.readonly]);
} else {
removeClass([this.sliderContainer], [classNames.readonly]);
}
}
private getThemeInitialization(): void {
this.isMaterial = this.getTheme(this.sliderContainer) === 'material'
|| this.getTheme(this.sliderContainer) === 'material-dark';
this.isMaterial3 = this.getTheme(this.sliderContainer) === 'Material3'
|| this.getTheme(this.sliderContainer) === 'Material3-dark';
this.isBootstrap = this.getTheme(this.sliderContainer) === 'bootstrap'
|| this.getTheme(this.sliderContainer) === 'bootstrap-dark';
this.isBootstrap4 = this.getTheme(this.sliderContainer) === 'bootstrap4';
this.isTailwind = this.getTheme(this.sliderContainer) === 'tailwind' || this.getTheme(this.sliderContainer) === 'tailwind-dark';
this.isTailwind3 = this.getTheme(this.sliderContainer) === 'tailwind3' || this.getTheme(this.sliderContainer) === 'tailwind3-dark';
this.isBootstrap5 = this.getTheme(this.sliderContainer) === 'bootstrap5';
this.isFluent = this.getTheme(this.sliderContainer) === 'FluentUI';
this.isFluent2 = this.getTheme(this.sliderContainer) === 'fluent2';
this.isBootstrap5Dot3 = this.getTheme(this.sliderContainer) === 'bootstrap5.3';
this.isMaterialTooltip = (this.isMaterial || this.isMaterial3) && this.type !== 'Range' && this.tooltip.isVisible;
}
private createRangeBar(): void {
if (this.type !== 'Default') {
this.rangeBar = <HTMLElement>(this.createElement('div', { attrs: { class: classNames.rangeBar } }));
this.element.appendChild(this.rangeBar);
if (this.drag && this.type === 'Range') {
if (this.orientation === 'Horizontal') {
this.rangeBar.classList.add(classNames.dragHorizontal);
} else {
this.rangeBar.classList.add(classNames.dragVertical);
}
}
}
}
private createLimitBar(): void {
let firstElementClassName: string = this.type !== 'Range' ? classNames.limitBarDefault :
classNames.limitBarFirst;
firstElementClassName += ' ' + classNames.limits;
this.limitBarFirst = <HTMLElement>(this.createElement('div', {
attrs: { class: firstElementClassName }
}));
this.element.appendChild(this.limitBarFirst);
if (this.type === 'Range') {
this.limitBarSecond = <HTMLElement>(this.createElement('div', {
attrs: {
class: classNames.limitBarSecond + ' ' + classNames.limits
}
}));
this.element.appendChild(this.limitBarSecond);
}
}
private setOrientClass(): void {
if (this.orientation !== 'Vertical') {
this.sliderContainer.classList.remove(classNames.verticalSlider);
this.sliderContainer.classList.add(classNames.horizontalSlider);
this.firstHandle.setAttribute('aria-orientation', 'horizontal');
if (this.type === 'Range') {
this.secondHandle.setAttribute('aria-orientation', 'horizontal');
}
} else {
this.sliderContainer.classList.remove(classNames.horizontalSlider);
this.sliderContainer.classList.add(classNames.verticalSlider);
this.firstHandle.setAttribute('aria-orientation', 'vertical');
if (this.type === 'Range') {