@@ -2,7 +2,8 @@ import { CommonModule } from '@angular/common';
2
2
import {
3
3
AfterViewInit , Component , ElementRef , EventEmitter ,
4
4
forwardRef , HostBinding , Input , NgModule , OnInit , Output , Renderer2 ,
5
- ViewChild
5
+ ViewChild ,
6
+ TemplateRef
6
7
} from '@angular/core' ;
7
8
import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
8
9
import { EditorProvider } from '../core/edit-provider' ;
@@ -28,9 +29,14 @@ export interface IRangeSliderValue {
28
29
upper : number ;
29
30
}
30
31
32
+ export interface IRangeSliderValueLabel {
33
+ left : string ;
34
+ right : string ;
35
+ }
36
+
31
37
export interface ISliderValueChangeEventArgs {
32
- oldValue : number | IRangeSliderValue ;
33
- value : number | IRangeSliderValue ;
38
+ oldValue : number | string | IRangeSliderValue | IRangeSliderValueLabel ;
39
+ value : number | string | IRangeSliderValue | IRangeSliderValueLabel ;
34
40
}
35
41
36
42
const noop = ( ) => {
@@ -65,6 +71,9 @@ let NEXT_ID = 0;
65
71
} )
66
72
export class IgxSliderComponent implements ControlValueAccessor , EditorProvider , OnInit , AfterViewInit {
67
73
74
+ private _step = 1 ;
75
+ private _oldValue ;
76
+
68
77
/**
69
78
* An @Input property that sets the value of the `id` attribute.
70
79
* If not provided it will be automatically generated.
@@ -117,6 +126,10 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
117
126
@Input ( )
118
127
public thumbLabelVisibilityDuration = 750 ;
119
128
129
+
130
+ @Input ( )
131
+ public stepLabels : [ ] = [ ] ;
132
+
120
133
/**
121
134
* An @Input property that sets the incremental/decremental step of the value when dragging the thumb.
122
135
* The default step is 1, and step should not be less or equal than 0.
@@ -125,7 +138,17 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
125
138
* ```
126
139
*/
127
140
@Input ( )
128
- public step = 1 ;
141
+ public set step ( step : number ) {
142
+ this . _step = step ;
143
+ }
144
+
145
+ public get step ( ) {
146
+ if ( this . stepLabels . length > 0 ) {
147
+ return 1 ;
148
+ }
149
+
150
+ return this . _step ;
151
+ }
129
152
130
153
/**
131
154
* This event is emitted when user has stopped interacting the thumb and value is changed.
@@ -163,7 +186,6 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
163
186
@ViewChild ( 'thumbTo' )
164
187
private thumbTo : ElementRef ;
165
188
166
-
167
189
// Measures & Coordinates
168
190
private width = 0 ;
169
191
private xOffset = 0 ;
@@ -220,6 +242,10 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
220
242
* ```
221
243
*/
222
244
public get maxValue ( ) : number {
245
+ if ( this . labelsViewEnabled ) {
246
+ return this . stepLabels . length - 1 ;
247
+ }
248
+
223
249
return this . _maxValue ;
224
250
}
225
251
@@ -255,6 +281,10 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
255
281
*```
256
282
*/
257
283
public get minValue ( ) : number {
284
+ if ( this . labelsViewEnabled ) {
285
+ return 0 ;
286
+ }
287
+
258
288
return this . _minValue ;
259
289
}
260
290
@@ -429,7 +459,10 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
429
459
*}
430
460
*```
431
461
*/
432
- public get value ( ) : number | IRangeSliderValue {
462
+ public get value ( ) : number | string | IRangeSliderValue | IRangeSliderValueLabel {
463
+ // if (this.labelsViewEnabled) {
464
+ // return this.getLabelValues(this.lowerValue, this.upperValue);
465
+ // }
433
466
if ( this . isRange ) {
434
467
return {
435
468
lower : this . snapValueToStep ( this . lowerValue ) ,
@@ -459,7 +492,11 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
459
492
* ```
460
493
*/
461
494
@Input ( )
462
- public set value ( value : number | IRangeSliderValue ) {
495
+ public set value ( value : number | string | IRangeSliderValue | IRangeSliderValueLabel ) {
496
+ if ( this . labelsViewEnabled ) {
497
+ value = this . getLabelIndexes ( value ) ;
498
+ }
499
+
463
500
if ( ! this . isRange ) {
464
501
this . upperValue = this . snapValueToStep ( value as number ) ;
465
502
} else {
@@ -476,6 +513,28 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
476
513
}
477
514
}
478
515
516
+ public get labelsViewEnabled ( ) {
517
+ return ! ! this . stepLabels . length ;
518
+ }
519
+
520
+ public get lowerLabel ( ) {
521
+ return this . labelsViewEnabled ?
522
+ this . stepLabels [ this . lowerValue ] :
523
+ this . lowerValue ;
524
+ }
525
+
526
+ public get upperLabel ( ) {
527
+ return this . labelsViewEnabled ?
528
+ this . stepLabels [ this . upperValue ] :
529
+ this . upperValue ;
530
+ }
531
+
532
+ public get valueLabel ( ) {
533
+ return this . labelsViewEnabled ?
534
+ this . stepLabels [ this . upperValue ] :
535
+ this . value ;
536
+ }
537
+
479
538
/**
480
539
* @hidden
481
540
*/
@@ -520,7 +579,7 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
520
579
public ngAfterViewInit ( ) {
521
580
this . hasViewInit = true ;
522
581
this . positionHandlesAndUpdateTrack ( ) ;
523
- this . setTickInterval ( ) ;
582
+ this . setTickInterval ( this . stepLabels . length ) ;
524
583
}
525
584
526
585
/**
@@ -552,7 +611,11 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
552
611
/**
553
612
* @hidden
554
613
*/
555
- public showThumbsLabels ( ) {
614
+ public showThumbsLabels ( evt ) {
615
+ if ( evt ) {
616
+ this . _oldValue = this . value ;
617
+ }
618
+
556
619
if ( this . disabled ) {
557
620
return ;
558
621
}
@@ -589,7 +652,9 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
589
652
*/
590
653
public onPanEnd ( $event ) {
591
654
this . hideThumbsLabels ( ) ;
592
- this . emitValueChanged ( null ) ;
655
+ if ( this . hasValueChanged ( this . _oldValue ) ) {
656
+ this . emitValueChanged ( this . _oldValue ) ;
657
+ }
593
658
}
594
659
/**
595
660
*
@@ -621,7 +686,8 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
621
686
return ;
622
687
}
623
688
624
- const value = this . value ;
689
+ // const value = this.value;
690
+ this . _oldValue = this . value ;
625
691
626
692
if ( this . isRange ) {
627
693
if ( this . activeHandle === SliderHandle . FROM ) {
@@ -653,25 +719,25 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
653
719
this . value = this . value as number + incrementSign * this . step ;
654
720
}
655
721
656
- if ( this . hasValueChanged ( value ) ) {
657
- this . emitValueChanged ( value ) ;
722
+ if ( this . hasValueChanged ( this . _oldValue ) ) {
723
+ this . emitValueChanged ( this . _oldValue ) ;
658
724
}
659
725
660
- this . showThumbsLabels ( ) ;
726
+ this . showThumbsLabels ( null ) ;
661
727
}
662
728
/**
663
729
*
664
730
* @hidden
665
731
*/
666
732
public onTap ( $event ) {
667
- const value = this . value ;
733
+ // const value = this.value;
734
+ this . _oldValue = this . value ;
668
735
this . update ( $event ) ;
669
736
670
- if ( this . hasValueChanged ( value ) ) {
671
- this . emitValueChanged ( value ) ;
737
+ if ( this . hasValueChanged ( this . _oldValue ) ) {
738
+ this . emitValueChanged ( this . _oldValue ) ;
672
739
}
673
740
}
674
-
675
741
/**
676
742
*
677
743
* @hidden
@@ -829,12 +895,18 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
829
895
}
830
896
}
831
897
832
- private setTickInterval ( ) {
898
+ private setTickInterval ( stepLabels ) {
833
899
if ( this . isContinuous ) {
834
900
return ;
835
901
}
836
902
837
- const interval = this . step > 1 ? this . step : null ;
903
+ let interval ;
904
+ if ( stepLabels ) {
905
+ // Calc ticks depending on the labels length;
906
+ interval = ( ( 100 / ( stepLabels - 1 ) * 10 ) ) / 10 ;
907
+ } else {
908
+ interval = this . step > 1 ? this . step : null ;
909
+ }
838
910
this . renderer . setStyle ( this . ticks . nativeElement , 'background' , this . generateTickMarks ( 'white' , interval ) ) ;
839
911
}
840
912
@@ -919,6 +991,39 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
919
991
this . track . nativeElement . style . width = `${ positionGap * 100 } %` ;
920
992
}
921
993
}
994
+
995
+ private getLabelIndexes ( value : number | string | IRangeSliderValue | IRangeSliderValueLabel ) {
996
+ if ( typeof value === 'string' && ! this . isRange ) {
997
+ return this . stepLabels . findIndex ( e => e === value ) ;
998
+ }
999
+
1000
+ const left = ( value as IRangeSliderValueLabel ) . left ;
1001
+ const right = ( value as IRangeSliderValueLabel ) . right ;
1002
+ if ( left || right ) {
1003
+ return {
1004
+ lower : left ? this . stepLabels . findIndex ( e => e === left ) : 0 ,
1005
+ upper : right ? this . stepLabels . findIndex ( e => e === right ) : this . stepLabels . length - 1
1006
+ }
1007
+ }
1008
+
1009
+ return value ;
1010
+ }
1011
+
1012
+ private getLabelValues ( value ) {
1013
+ if ( ! value ) {
1014
+ return value ;
1015
+ }
1016
+
1017
+ if ( this . isRange ) {
1018
+ return {
1019
+ left : this . stepLabels [ value . lower ] ,
1020
+ right : this . stepLabels [ value . upper ]
1021
+ } ;
1022
+ }
1023
+
1024
+ return this . stepLabels [ value ] ;
1025
+ }
1026
+
922
1027
private hasValueChanged ( oldValue ) {
923
1028
const isSliderWithDifferentValue : boolean = ! this . isRange && oldValue !== this . value ;
924
1029
const isRangeWithOneDifferentValue : boolean = this . isRange &&
@@ -928,8 +1033,15 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
928
1033
return isSliderWithDifferentValue || isRangeWithOneDifferentValue ;
929
1034
}
930
1035
931
- private emitValueChanged ( oldValue : number | IRangeSliderValue ) {
932
- this . onValueChange . emit ( { oldValue, value : this . value } ) ;
1036
+ private emitValueChanged ( oldValueRef : number | string | IRangeSliderValue | IRangeSliderValueLabel ) {
1037
+ let oldVal = oldValueRef , newVal = this . value ;
1038
+
1039
+ if ( this . labelsViewEnabled ) {
1040
+ oldVal = this . getLabelValues ( oldValueRef ) ;
1041
+ newVal = this . getLabelValues ( this . value ) ;
1042
+ }
1043
+
1044
+ this . onValueChange . emit ( { oldValue : oldVal , value : newVal } ) ;
933
1045
}
934
1046
}
935
1047
0 commit comments