Skip to content

Commit 6c1bf56

Browse files
committed
Spinner: Moved all event handlers into an object.
1 parent 4587f2d commit 6c1bf56

File tree

1 file changed

+74
-84
lines changed

1 file changed

+74
-84
lines changed

ui/jquery.ui.spinner.js

Lines changed: 74 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ $.widget( "ui.spinner", {
4646
_create: function() {
4747
this._value( this.options.value );
4848
this._draw();
49-
this._mousewheel();
49+
this._bind( this._events );
5050
this._refresh();
5151
},
5252

@@ -64,6 +64,79 @@ $.widget( "ui.spinner", {
6464
return options;
6565
},
6666

67+
_events: {
68+
keydown: function( event ) {
69+
if ( this._start( event ) && this._keydown( event ) ) {
70+
event.preventDefault();
71+
}
72+
},
73+
keyup: "_stop",
74+
focus: function() {
75+
this.uiSpinner.addClass( "ui-state-active" );
76+
this.previous = this.element.val();
77+
},
78+
blur: function( event ) {
79+
// don't clear invalid values on blur
80+
var value = this.element.val();
81+
this._value( value );
82+
if ( this.element.val() === "" ) {
83+
this.element.val( value );
84+
}
85+
this.uiSpinner.removeClass( "ui-state-active" );
86+
// TODO: what should trigger change?
87+
// element.val() or options.value?
88+
if ( this.previous !== this.element.val() ) {
89+
this._trigger( "change", event );
90+
}
91+
},
92+
mousewheel: function( event, delta ) {
93+
if ( !delta ) {
94+
return;
95+
}
96+
if ( !this.spinning && !this._start( event ) ) {
97+
return false;
98+
}
99+
100+
this._spin( (delta > 0 ? 1 : -1) * this.options.step, event );
101+
clearTimeout( this.mousewheelTimer );
102+
this.mousewheelTimer = setTimeout(function() {
103+
if ( this.spinning ) {
104+
this._stop( event );
105+
}
106+
}, 100 );
107+
event.preventDefault();
108+
},
109+
"mousedown .ui-spinner-button": function( event ) {
110+
// ensure focus is on (or stays on) the text field
111+
event.preventDefault();
112+
if ( document.activeElement !== this.element[ 0 ] ) {
113+
this.element.focus();
114+
}
115+
116+
if ( this._start( event ) === false ) {
117+
return;
118+
}
119+
120+
this._repeat( null, $( event.currentTarget ).hasClass( "ui-spinner-up" ) ? 1 : -1, event );
121+
},
122+
"mouseup .ui-spinner-button": "_stop",
123+
"mouseenter .ui-spinner-button": function( event ) {
124+
// button will add ui-state-active if mouse was down while mouseleave and kept down
125+
if ( !$( event.currentTarget ).hasClass( "ui-state-active" ) ) {
126+
return;
127+
}
128+
129+
if ( this._start( event ) === false ) {
130+
return false;
131+
}
132+
this._repeat( null, $( event.currentTarget ).hasClass( "ui-spinner-up" ) ? 1 : -1, event );
133+
},
134+
// TODO: do we really want to consider this a stop?
135+
// shouldn't we just stop the repeater and wait until mouseup before
136+
// we trigger the stop event?
137+
"mouseleave .ui-spinner-button": "_stop"
138+
},
139+
67140
_draw: function() {
68141
var uiSpinner = this.uiSpinner = this.element
69142
.addClass( "ui-spinner-input" )
@@ -75,69 +148,12 @@ $.widget( "ui.spinner", {
75148
this._hoverable( uiSpinner );
76149

77150
this.element.attr( "role", "spinbutton" );
78-
this._bind({
79-
keydown: function( event ) {
80-
if ( this._start( event ) && this._keydown( event ) ) {
81-
event.preventDefault();
82-
}
83-
},
84-
keyup: "_stop",
85-
focus: function() {
86-
uiSpinner.addClass( "ui-state-active" );
87-
this.previous = this.element.val();
88-
},
89-
blur: function( event ) {
90-
// don't clear invalid values on blur
91-
var value = this.element.val();
92-
this._value( value );
93-
if ( this.element.val() === "" ) {
94-
this.element.val( value );
95-
}
96-
uiSpinner.removeClass( "ui-state-active" );
97-
// TODO: what should trigger change?
98-
// element.val() or options.value?
99-
if ( this.previous !== this.element.val() ) {
100-
this._trigger( "change", event );
101-
}
102-
}
103-
});
104151

105152
// button bindings
106153
this.buttons = uiSpinner.find( ".ui-spinner-button" )
107154
.attr( "tabIndex", -1 )
108155
.button()
109156
.removeClass( "ui-corner-all" );
110-
this._bind( this.buttons, {
111-
mousedown: function( event ) {
112-
// ensure focus is on (or stays on) the text field
113-
event.preventDefault();
114-
if ( document.activeElement !== this.element[ 0 ] ) {
115-
this.element.focus();
116-
}
117-
118-
if ( this._start( event ) === false ) {
119-
return;
120-
}
121-
122-
this._repeat( null, $( event.currentTarget ).hasClass( "ui-spinner-up" ) ? 1 : -1, event );
123-
},
124-
mouseup: "_stop",
125-
mouseenter: function( event ) {
126-
// button will add ui-state-active if mouse was down while mouseleave and kept down
127-
if ( !$( event.currentTarget ).hasClass( "ui-state-active" ) ) {
128-
return;
129-
}
130-
131-
if ( this._start( event ) === false ) {
132-
return false;
133-
}
134-
this._repeat( null, $( event.currentTarget ).hasClass( "ui-spinner-up" ) ? 1 : -1, event );
135-
},
136-
// TODO: do we really want to consider this a stop?
137-
// shouldn't we just stop the repeater and wait until mouseup before
138-
// we trigger the stop event?
139-
mouseleave: "_stop"
140-
});
141157

142158
// disable spinner if element was already disabled
143159
if ( this.options.disabled ) {
@@ -169,32 +185,6 @@ $.widget( "ui.spinner", {
169185
return false;
170186
},
171187

172-
_mousewheel: function() {
173-
// need the delta normalization that mousewheel plugin provides
174-
if ( !$.fn.mousewheel ) {
175-
return;
176-
}
177-
this._bind({
178-
mousewheel: function( event, delta ) {
179-
if ( !delta ) {
180-
return;
181-
}
182-
if ( !this.spinning && !this._start( event ) ) {
183-
return false;
184-
}
185-
186-
this._spin( (delta > 0 ? 1 : -1) * this.options.step, event );
187-
clearTimeout( this.mousewheelTimer );
188-
this.mousewheelTimer = setTimeout(function() {
189-
if ( this.spinning ) {
190-
this._stop( event );
191-
}
192-
}, 100 );
193-
event.preventDefault();
194-
}
195-
});
196-
},
197-
198188
_uiSpinnerHtml: function() {
199189
return "<span class='ui-spinner ui-state-default ui-widget ui-widget-content ui-corner-all'></span>";
200190
},

0 commit comments

Comments
 (0)