13
13
*/
14
14
( function ( $ ) {
15
15
16
+ function modifier ( fn ) {
17
+ return function ( ) {
18
+ var previous = this . options . value ;
19
+ fn . apply ( this , arguments ) ;
20
+ this . _format ( ) ;
21
+ this . _aria ( ) ;
22
+ if ( previous !== this . options . value ) {
23
+ this . _trigger ( "change" ) ;
24
+ }
25
+ } ;
26
+ }
27
+
16
28
$ . widget ( "ui.spinner" , {
17
29
version : "@VERSION" ,
18
30
defaultElement : "<input>" ,
@@ -33,7 +45,7 @@ $.widget( "ui.spinner", {
33
45
} ,
34
46
35
47
_create : function ( ) {
36
- this . value ( this . options . value ) ;
48
+ this . _value ( this . options . value ) ;
37
49
this . _draw ( ) ;
38
50
this . _mousewheel ( ) ;
39
51
this . _aria ( ) ;
@@ -73,18 +85,19 @@ $.widget( "ui.spinner", {
73
85
keyup : "_stop" ,
74
86
focus : function ( ) {
75
87
uiSpinner . addClass ( "ui-state-active" ) ;
76
- this . previous = this . options . value ;
88
+ this . previous = this . element . val ( ) ;
77
89
} ,
78
90
blur : function ( event ) {
79
91
// don't clear invalid values on blur
80
- var value = this . element . val ( ) ,
81
- parsed = this . _parse ( value ) ;
82
- this . option ( "value" , parsed ) ;
83
- if ( parsed === null ) {
92
+ var value = this . element . val ( ) ;
93
+ this . _value ( value ) ;
94
+ if ( this . element . val ( ) === "" ) {
84
95
this . element . val ( value ) ;
85
96
}
86
97
uiSpinner . removeClass ( "ui-state-active" ) ;
87
- if ( this . previous !== this . options . value ) {
98
+ // TODO: what should trigger change?
99
+ // element.val() or options.value?
100
+ if ( this . previous !== this . element . val ( ) ) {
88
101
this . _trigger ( "change" , event ) ;
89
102
}
90
103
}
@@ -151,7 +164,7 @@ $.widget( "ui.spinner", {
151
164
this . _repeat ( null , - options . page , event ) ;
152
165
return true ;
153
166
case keyCode . ENTER :
154
- this . value ( this . element . val ( ) ) ;
167
+ this . _value ( this . element . val ( ) ) ;
155
168
}
156
169
157
170
return false ;
@@ -241,7 +254,7 @@ $.widget( "ui.spinner", {
241
254
newVal = this . _trimValue ( newVal ) ;
242
255
243
256
if ( this . _trigger ( "spin" , event , { value : newVal } ) !== false ) {
244
- this . value ( newVal ) ;
257
+ this . _value ( newVal ) ;
245
258
this . counter ++ ;
246
259
}
247
260
} ,
@@ -273,10 +286,12 @@ $.widget( "ui.spinner", {
273
286
} ,
274
287
275
288
_setOption : function ( key , value ) {
276
- if ( key === "value" ) {
277
- value = this . _trimValue ( this . _parse ( value ) ) ;
289
+ if ( key === "value" ) {
290
+ return this . _setOptionValue ( value ) ;
278
291
}
279
292
293
+ this . _super ( "_setOption" , key , value ) ;
294
+
280
295
if ( key === "disabled" ) {
281
296
if ( value ) {
282
297
this . element . prop ( "disabled" , true ) ;
@@ -286,17 +301,24 @@ $.widget( "ui.spinner", {
286
301
this . buttons . button ( "enable" ) ;
287
302
}
288
303
}
304
+ } ,
289
305
290
- this . _super ( "_setOption" , key , value ) ;
306
+ _setOptionValue : function ( value ) {
307
+ var previous = this . options . value ;
308
+ this . _value ( value ) ;
309
+ if ( previous !== this . options . value ) {
310
+ this . _trigger ( "change" ) ;
311
+ }
291
312
} ,
292
313
293
- _setOptions : function ( options ) {
314
+ _setOptions : modifier ( function ( options ) {
294
315
this . _super ( "_setOptions" , options ) ;
295
- if ( "value" in options || "numberFormat" in options ) {
296
- this . _format ( this . options . value ) ;
297
- }
316
+
317
+ // handle any options that might cause value to change, e.g., min
318
+ this . _value ( this . _trimValue ( this . options . value ) ) ;
319
+ this . _format ( ) ;
298
320
this . _aria ( ) ;
299
- } ,
321
+ } ) ,
300
322
301
323
_aria : function ( ) {
302
324
this . element . attr ( {
@@ -313,10 +335,17 @@ $.widget( "ui.spinner", {
313
335
return isNaN ( val ) ? null : val ;
314
336
} ,
315
337
316
- _format : function ( num ) {
338
+ _format : function ( ) {
339
+ var num = this . options . value ;
317
340
this . element . val ( $ . global && this . options . numberFormat ? $ . global . format ( num , this . options . numberFormat ) : num ) ;
318
341
} ,
319
342
343
+ // update the value without triggering change
344
+ _value : function ( value ) {
345
+ this . options . value = this . _trimValue ( this . _parse ( value ) ) ;
346
+ this . _format ( ) ;
347
+ } ,
348
+
320
349
destroy : function ( ) {
321
350
this . element
322
351
. removeClass ( "ui-spinner-input" )
@@ -330,21 +359,27 @@ $.widget( "ui.spinner", {
330
359
this . uiSpinner . replaceWith ( this . element ) ;
331
360
} ,
332
361
333
- stepUp : function ( steps ) {
362
+ stepUp : modifier ( function ( steps ) {
363
+ this . _stepUp ( steps ) ;
364
+ } ) ,
365
+ _stepUp : function ( steps ) {
334
366
this . _spin ( ( steps || 1 ) * this . options . step ) ;
335
367
} ,
336
368
337
- stepDown : function ( steps ) {
369
+ stepDown : modifier ( function ( steps ) {
370
+ this . _stepDown ( steps ) ;
371
+ } ) ,
372
+ _stepDown : function ( steps ) {
338
373
this . _spin ( ( steps || 1 ) * - this . options . step ) ;
339
374
} ,
340
375
341
- pageUp : function ( pages ) {
342
- this . stepUp ( ( pages || 1 ) * this . options . page ) ;
343
- } ,
376
+ pageUp : modifier ( function ( pages ) {
377
+ this . _stepUp ( ( pages || 1 ) * this . options . page ) ;
378
+ } ) ,
344
379
345
- pageDown : function ( pages ) {
346
- this . stepDown ( ( pages || 1 ) * this . options . page ) ;
347
- } ,
380
+ pageDown : modifier ( function ( pages ) {
381
+ this . _stepDown ( ( pages || 1 ) * this . options . page ) ;
382
+ } ) ,
348
383
349
384
value : function ( newVal ) {
350
385
if ( ! arguments . length ) {
0 commit comments