9
9
} from '@angular/core' ;
10
10
import { NG_VALUE_ACCESSOR , ControlValueAccessor , FormsModule } from '@angular/forms' ;
11
11
import { HAMMER_GESTURE_CONFIG } from '@angular/platform-browser' ;
12
- import { MdGestureConfig , coerceBooleanProperty } from '../core' ;
12
+ import { MdGestureConfig , coerceBooleanProperty , coerceNumberProperty } from '../core' ;
13
13
import { Input as HammerInput } from 'hammerjs' ;
14
+ import { CommonModule } from '@angular/common' ;
14
15
15
16
/**
16
17
* Visually, a 30px separation between tick marks looks best. This is very subjective but it is
@@ -33,25 +34,21 @@ export const MD_SLIDER_VALUE_ACCESSOR: any = {
33
34
selector : 'md-slider' ,
34
35
providers : [ MD_SLIDER_VALUE_ACCESSOR ] ,
35
36
host : {
36
- '(blur)' : 'onBlur()' ,
37
- '(click)' : 'onClick($event)' ,
38
- '(mouseenter)' : 'onMouseenter()' ,
39
- '(slide)' : 'onSlide($event)' ,
40
- '(slideend)' : 'onSlideEnd()' ,
41
- '(slidestart)' : 'onSlideStart($event)' ,
42
- '(window:resize)' : 'onResize()' ,
43
-
37
+ '(blur)' : '_onBlur()' ,
38
+ '(click)' : '_onClick($event)' ,
39
+ '(mouseenter)' : '_onMouseenter()' ,
40
+ '(slide)' : '_onSlide($event)' ,
41
+ '(slideend)' : '_onSlideEnd()' ,
42
+ '(slidestart)' : '_onSlideStart($event)' ,
44
43
'tabindex' : '0' ,
45
-
46
44
'[attr.aria-disabled]' : 'disabled' ,
47
45
'[attr.aria-valuemax]' : 'max' ,
48
46
'[attr.aria-valuemin]' : 'min' ,
49
47
'[attr.aria-valuenow]' : 'value' ,
50
-
51
- '[class.md-slider-active]' : 'isActive' ,
48
+ '[class.md-slider-active]' : '_isActive' ,
52
49
'[class.md-slider-disabled]' : 'disabled' ,
53
50
'[class.md-slider-has-ticks]' : 'tickInterval' ,
54
- '[class.md-slider-sliding]' : 'isSliding ' ,
51
+ '[class.md-slider-sliding]' : '_isSliding ' ,
55
52
'[class.md-slider-thumb-label-showing]' : 'thumbLabel' ,
56
53
} ,
57
54
templateUrl : 'slider.html' ,
@@ -87,32 +84,21 @@ export class MdSlider implements ControlValueAccessor {
87
84
/**
88
85
* Whether or not the thumb is sliding.
89
86
* Used to determine if there should be a transition for the thumb and fill track.
90
- * TODO: internal
91
87
*/
92
- isSliding : boolean = false ;
88
+ _isSliding : boolean = false ;
93
89
94
90
/**
95
91
* Whether or not the slider is active (clicked or sliding).
96
92
* Used to shrink and grow the thumb as according to the Material Design spec.
97
- * TODO: internal
98
93
*/
99
- isActive : boolean = false ;
94
+ _isActive : boolean = false ;
100
95
101
96
/** The values at which the thumb will snap. */
102
97
private _step : number = 1 ;
103
98
104
99
@Input ( )
105
- get step ( ) {
106
- return this . _step ;
107
- }
108
- set step ( v ) {
109
- // Only set the value to a valid number. v is casted to an any as we know it will come in as a
110
- // string but it is labeled as a number which causes parseFloat to not accept it.
111
- if ( isNaN ( parseFloat ( < any > v ) ) ) {
112
- return ;
113
- }
114
- this . _step = Number ( v ) ;
115
- }
100
+ get step ( ) { return this . _step ; }
101
+ set step ( v ) { this . _step = coerceNumberProperty ( v , this . _step ) ; }
116
102
117
103
/**
118
104
* How often to show ticks. Relative to the step so that a tick always appears on a step.
@@ -122,23 +108,15 @@ export class MdSlider implements ControlValueAccessor {
122
108
123
109
@Input ( 'tick-interval' )
124
110
get tickInterval ( ) { return this . _tickInterval ; }
125
- set tickInterval ( v : 'auto' | number ) {
126
- // Only set the tickInterval to a valid number. v is casted to an any as we know it will come
127
- // in as a string but it is labeled as a number which causes parseInt to not accept it.
128
- if ( v == 'auto' ) {
129
- this . _tickInterval = v ;
130
- } else {
131
- let intV = parseInt ( < any > v ) ;
132
- if ( ! isNaN ( intV ) ) {
133
- this . _tickInterval = intV ;
134
- }
135
- }
111
+ set tickInterval ( v ) {
112
+ this . _tickInterval = ( v == 'auto' ) ? v : coerceNumberProperty ( v , < number > this . _tickInterval ) ;
136
113
}
137
114
138
115
/** The size of a tick interval as a percentage of the size of the track. */
139
116
private _tickIntervalPercent : number = 0 ;
140
117
141
118
get tickIntervalPercent ( ) { return this . _tickIntervalPercent ; }
119
+ get halfTickIntervalPercent ( ) { return this . _tickIntervalPercent / 2 ; }
142
120
143
121
/** The percentage of the slider that coincides with the value. */
144
122
private _percent : number = 0 ;
@@ -157,13 +135,7 @@ export class MdSlider implements ControlValueAccessor {
157
135
return this . _value ;
158
136
}
159
137
set value ( v : number ) {
160
- // Only set the value to a valid number. v is casted to an any as we know it will come in as a
161
- // string but it is labeled as a number which causes parseFloat to not accept it.
162
- if ( isNaN ( parseFloat ( < any > v ) ) ) {
163
- return ;
164
- }
165
-
166
- this . _value = Number ( v ) ;
138
+ this . _value = coerceNumberProperty ( v , this . _value ) ;
167
139
this . _percent = this . _calculatePercentage ( this . _value ) ;
168
140
this . _controlValueAccessorChangeFn ( this . _value ) ;
169
141
}
@@ -176,14 +148,7 @@ export class MdSlider implements ControlValueAccessor {
176
148
return this . _min ;
177
149
}
178
150
set min ( v : number ) {
179
- // Only set the value to a valid number. v is casted to an any as we know it will come in as a
180
- // string but it is labeled as a number which causes parseFloat to not accept it.
181
- if ( isNaN ( parseFloat ( < any > v ) ) ) {
182
- return ;
183
- }
184
-
185
- // This has to be forced as a number to handle the math later.
186
- this . _min = Number ( v ) ;
151
+ this . _min = coerceNumberProperty ( v , this . _min ) ;
187
152
188
153
// If the value wasn't explicitly set by the user, set it to the min.
189
154
if ( this . _value === null ) {
@@ -200,38 +165,37 @@ export class MdSlider implements ControlValueAccessor {
200
165
return this . _max ;
201
166
}
202
167
set max ( v : number ) {
203
- this . _max = Number ( v ) ;
168
+ this . _max = coerceNumberProperty ( v , this . _max ) ;
204
169
this . _percent = this . _calculatePercentage ( this . value ) ;
205
170
}
206
171
207
172
constructor ( elementRef : ElementRef ) {
208
173
this . _renderer = new SliderRenderer ( elementRef ) ;
209
174
}
210
175
211
- /** TODO: internal */
212
- onMouseenter ( ) {
176
+ _onMouseenter ( ) {
213
177
if ( this . disabled ) {
214
178
return ;
215
179
}
216
180
181
+ // We save the dimensions of the slider here so we can use them to update the spacing of the
182
+ // ticks and determine where on the slider click and slide events happen.
217
183
this . _sliderDimensions = this . _renderer . getSliderDimensions ( ) ;
218
184
this . _updateTickIntervalPercent ( ) ;
219
185
}
220
186
221
- /** TODO: internal */
222
- onClick ( event : MouseEvent ) {
187
+ _onClick ( event : MouseEvent ) {
223
188
if ( this . disabled ) {
224
189
return ;
225
190
}
226
191
227
- this . isActive = true ;
228
- this . isSliding = false ;
192
+ this . _isActive = true ;
193
+ this . _isSliding = false ;
229
194
this . _renderer . addFocus ( ) ;
230
195
this . _updateValueFromPosition ( event . clientX ) ;
231
196
}
232
197
233
- /** TODO: internal */
234
- onSlide ( event : HammerInput ) {
198
+ _onSlide ( event : HammerInput ) {
235
199
if ( this . disabled ) {
236
200
return ;
237
201
}
@@ -241,36 +205,24 @@ export class MdSlider implements ControlValueAccessor {
241
205
this . _updateValueFromPosition ( event . center . x ) ;
242
206
}
243
207
244
- /** TODO: internal */
245
- onSlideStart ( event : HammerInput ) {
208
+ _onSlideStart ( event : HammerInput ) {
246
209
if ( this . disabled ) {
247
210
return ;
248
211
}
249
212
250
213
event . preventDefault ( ) ;
251
- this . isSliding = true ;
252
- this . isActive = true ;
214
+ this . _isSliding = true ;
215
+ this . _isActive = true ;
253
216
this . _renderer . addFocus ( ) ;
254
217
this . _updateValueFromPosition ( event . center . x ) ;
255
218
}
256
219
257
- /** TODO: internal */
258
- onSlideEnd ( ) {
259
- this . isSliding = false ;
260
- }
261
-
262
- /** TODO: internal */
263
- onResize ( ) {
264
- // Consider the slider to be sliding during resize to prevent animation.
265
- this . isSliding = true ;
266
- setTimeout ( ( ) => {
267
- this . isSliding = false ;
268
- } , 0 ) ;
220
+ _onSlideEnd ( ) {
221
+ this . _isSliding = false ;
269
222
}
270
223
271
- /** TODO: internal */
272
- onBlur ( ) {
273
- this . isActive = false ;
224
+ _onBlur ( ) {
225
+ this . _isActive = false ;
274
226
this . onTouched ( ) ;
275
227
}
276
228
@@ -337,23 +289,20 @@ export class MdSlider implements ControlValueAccessor {
337
289
338
290
/**
339
291
* Implemented as part of ControlValueAccessor.
340
- * TODO: internal
341
292
*/
342
293
writeValue ( value : any ) {
343
294
this . value = value ;
344
295
}
345
296
346
297
/**
347
298
* Implemented as part of ControlValueAccessor.
348
- * TODO: internal
349
299
*/
350
300
registerOnChange ( fn : ( value : any ) => void ) {
351
301
this . _controlValueAccessorChangeFn = fn ;
352
302
}
353
303
354
304
/**
355
305
* Implemented as part of ControlValueAccessor.
356
- * TODO: internal
357
306
*/
358
307
registerOnTouched ( fn : any ) {
359
308
this . onTouched = fn ;
@@ -398,7 +347,7 @@ export class SliderRenderer {
398
347
399
348
400
349
@NgModule ( {
401
- imports : [ FormsModule ] ,
350
+ imports : [ FormsModule , CommonModule ] ,
402
351
exports : [ MdSlider ] ,
403
352
declarations : [ MdSlider ] ,
404
353
providers : [
0 commit comments