1
1
import { CommonModule } from '@angular/common' ;
2
2
import {
3
3
AfterViewInit , Component , ElementRef , EventEmitter ,
4
- forwardRef , HostBinding , Input , NgModule , OnInit , Output , Renderer2 ,
4
+ HostBinding , Input , NgModule , OnInit , Output , Renderer2 ,
5
5
ViewChild ,
6
- TemplateRef
7
6
} from '@angular/core' ;
8
7
import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
9
8
import { EditorProvider } from '../core/edit-provider' ;
@@ -459,10 +458,7 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
459
458
*}
460
459
*```
461
460
*/
462
- public get value ( ) : number | string | IRangeSliderValue | IRangeSliderValueLabel {
463
- // if (this.labelsViewEnabled) {
464
- // return this.getLabelValues(this.lowerValue, this.upperValue);
465
- // }
461
+ public get value ( ) : number | IRangeSliderValue {
466
462
if ( this . isRange ) {
467
463
return {
468
464
lower : this . snapValueToStep ( this . lowerValue ) ,
@@ -476,11 +472,9 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
476
472
477
473
/**
478
474
* Sets the slider value.
479
- * If the slider is of type SLIDER the argument is number. By default if no value is set the default value is
480
- * same as lower upper bound.
481
- * If the slider type is RANGE the the argument is object containing lower and upper properties for the values.
482
- * By default if no value is set the default value is for lower value it is the same as lower bound and if no
483
- * value is set for the upper value it is the same as the upper bound.
475
+ * If the slider is of type SLIDER the argument is number. By default the @value gets the @lowerBound value.
476
+ * If the slider type is RANGE the argument represents an object of lower and upper properties.
477
+ * By default the object lower and upper properties are associated with the @lowerBound and @upperBound property values.
484
478
* ```typescript
485
479
*rangeValue = {
486
480
* lower: 30,
@@ -492,11 +486,7 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
492
486
* ```
493
487
*/
494
488
@Input ( )
495
- public set value ( value : number | string | IRangeSliderValue | IRangeSliderValueLabel ) {
496
- if ( this . labelsViewEnabled ) {
497
- value = this . getLabelIndexes ( value ) ;
498
- }
499
-
489
+ public set value ( value : number | IRangeSliderValue ) {
500
490
if ( ! this . isRange ) {
501
491
this . upperValue = this . snapValueToStep ( value as number ) ;
502
492
} else {
@@ -506,33 +496,115 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
506
496
this . snapValueToStep ( ( value as IRangeSliderValue ) == null ? null : ( value as IRangeSliderValue ) . lower ) ;
507
497
}
508
498
509
- this . _onChangeCallback ( this . value ) ;
499
+ this . labelsViewEnabled ?
500
+ this . _onChangeCallback ( this . getLabelValues ( this . value ) ) :
501
+ this . _onChangeCallback ( this . value ) ;
510
502
511
503
if ( this . hasViewInit ) {
512
504
this . positionHandlesAndUpdateTrack ( ) ;
513
505
}
514
506
}
515
507
516
- public get labelsViewEnabled ( ) {
517
- return ! ! this . stepLabels . length ;
518
- }
519
-
520
- public get lowerLabel ( ) {
508
+ /**
509
+ *Returns the left label value of the `IgxSliderComponent`.
510
+ *```typescript
511
+ * @ViewChild ("slider")
512
+ * public slider: IgxSliderComponent;
513
+ * public leftLabel(){
514
+ * let sliderLeftLabel = this.slider.leftLabel;
515
+ * }
516
+ *```
517
+ */
518
+ public get leftLabel ( ) {
521
519
return this . labelsViewEnabled ?
522
520
this . stepLabels [ this . lowerValue ] :
523
521
this . lowerValue ;
524
522
}
525
523
526
- public get upperLabel ( ) {
524
+ /**
525
+ *Returns the right label value of the `IgxSliderComponent`.
526
+ *```typescript
527
+ * @ViewChild ("slider")
528
+ * public slider: IgxSliderComponent;
529
+ * public rightLabel(){
530
+ * let sliderRightLabel = this.slider.rightLabel;
531
+ * }
532
+ *```
533
+ */
534
+ public get rightLabel ( ) {
527
535
return this . labelsViewEnabled ?
528
536
this . stepLabels [ this . upperValue ] :
529
537
this . upperValue ;
530
538
}
531
539
532
- public get valueLabel ( ) {
533
- return this . labelsViewEnabled ?
534
- this . stepLabels [ this . upperValue ] :
535
- this . value ;
540
+ /**
541
+ * Sets the slider label.
542
+ * If the slider is of type SLIDER the argument is number or string. By default the value takes to @lowerValue
543
+ * or the first element of the @stepLabels collection.
544
+ * If the slider type is RANGE the value represents an object of lower/upper or left/right properties.
545
+ * By default the @value takes the @lowerValue / @upperValue valeus or first and last element of the @stepLabels collection.
546
+ * ```typescript
547
+ * rangeValue = {
548
+ * lower: 30,
549
+ * upper: 60
550
+ *};
551
+ *
552
+ * labels = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
553
+ * labelsRange = {
554
+ * left: 'Tuesday',
555
+ * right: 'Friday'
556
+ * }
557
+ * ```
558
+ * ```html
559
+ * <igx-slider [type]="sliderType" [(ngModel)]="rangeValue" [minValue]="56" [maxValue]="256">
560
+ *
561
+ * ```html
562
+ * <igx-slider [type]="sliderType" [label]="labelsRange" [stepLabels]="labels">
563
+ * ```
564
+ */
565
+ @Input ( )
566
+ public set label ( value : number | string | IRangeSliderValue | IRangeSliderValueLabel ) {
567
+ if ( ! value ) {
568
+ return ;
569
+ }
570
+
571
+ this . value = this . getLabelIndexes ( value ) ;
572
+ }
573
+
574
+ /**
575
+ * Returns the slider label value. If the slider is of type SLIDER the returned value is number or string.
576
+ * If the slider type is RANGE the returned value represents an object of lower/upper or left/right properties as values.
577
+ *```typescript
578
+ *@ViewChild ("slider2")
579
+ *public slider: IgxSliderComponent;
580
+ *public sliderValue(){
581
+ * let sliderLabel = this.slider.label;
582
+ *}
583
+ *```
584
+ */
585
+ public get label ( ) : number | string | IRangeSliderValue | IRangeSliderValueLabel {
586
+ if ( this . labelsViewEnabled ) {
587
+ if ( this . isRange ) {
588
+ const lower = ( this . value as IRangeSliderValue ) . lower ;
589
+ const upper = ( this . value as IRangeSliderValue ) . upper ;
590
+ return {
591
+ left : this . stepLabels [ lower ] ,
592
+ right : this . stepLabels [ upper ]
593
+ } ;
594
+ }
595
+
596
+ return this . stepLabels [ this . upperValue ] ;
597
+ }
598
+
599
+ return this . value ;
600
+ }
601
+
602
+
603
+ /**
604
+ * Returns if the label view is enabled. This is determined by the length of the @stepLabels collection.;
605
+ */
606
+ public get labelsViewEnabled ( ) {
607
+ return ! ! this . stepLabels . length ;
536
608
}
537
609
538
610
/**
@@ -586,7 +658,11 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
586
658
* @hidden
587
659
*/
588
660
public writeValue ( value : any ) : void {
589
- this . value = value ;
661
+ if ( ! this . labelsViewEnabled && typeof value === 'string' ) {
662
+ value = parseInt ( value ) ;
663
+ }
664
+
665
+ this . label = value ;
590
666
}
591
667
592
668
/**
@@ -992,24 +1068,24 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
992
1068
}
993
1069
}
994
1070
995
- private getLabelIndexes ( value : number | string | IRangeSliderValue | IRangeSliderValueLabel ) {
1071
+ private getLabelIndexes ( value : number | string | IRangeSliderValue | IRangeSliderValueLabel ) : number | IRangeSliderValue {
996
1072
if ( typeof value === 'string' && ! this . isRange ) {
997
1073
return this . stepLabels . findIndex ( e => e === value ) ;
998
1074
}
999
1075
1000
1076
const left = ( value as IRangeSliderValueLabel ) . left ;
1001
1077
const right = ( value as IRangeSliderValueLabel ) . right ;
1002
- if ( left || right ) {
1078
+ if ( this . isRange && ( left || right ) ) {
1003
1079
return {
1004
1080
lower : left ? this . stepLabels . findIndex ( e => e === left ) : 0 ,
1005
1081
upper : right ? this . stepLabels . findIndex ( e => e === right ) : this . stepLabels . length - 1
1006
1082
}
1007
1083
}
1008
1084
1009
- return value ;
1085
+ return this . isRange ? ( value as IRangeSliderValue ) : + value ;
1010
1086
}
1011
1087
1012
- private getLabelValues ( value ) {
1088
+ private getLabelValues ( value ) : string | IRangeSliderValueLabel {
1013
1089
if ( this . isRange ) {
1014
1090
return {
1015
1091
left : this . stepLabels [ value . lower ] ,
@@ -1030,14 +1106,14 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
1030
1106
}
1031
1107
1032
1108
private emitValueChanged ( oldValueRef : number | string | IRangeSliderValue | IRangeSliderValueLabel ) {
1033
- let oldVal = oldValueRef , newVal = this . value ;
1034
-
1035
1109
if ( this . labelsViewEnabled ) {
1036
- oldVal = this . getLabelValues ( oldValueRef ) ;
1037
- newVal = this . getLabelValues ( this . value ) ;
1110
+ const oldVal = this . getLabelValues ( oldValueRef ) ,
1111
+ newVal = this . getLabelValues ( this . value ) ;
1112
+ this . onValueChange . emit ( { oldValue : oldVal , value : newVal } ) ;
1113
+ } else {
1114
+ this . onValueChange . emit ( { oldValue : oldValueRef , value : this . value } ) ;
1038
1115
}
1039
1116
1040
- this . onValueChange . emit ( { oldValue : oldVal , value : newVal } ) ;
1041
1117
}
1042
1118
}
1043
1119
0 commit comments