Skip to content

Commit 08db455

Browse files
committed
Mask: Refactor _events method into _events property, pass to _bind on _create. Matches design in Spinner
1 parent 52e0daa commit 08db455

File tree

1 file changed

+95
-97
lines changed

1 file changed

+95
-97
lines changed

ui/jquery.ui.mask.js

Lines changed: 95 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ $.widget( "ui.mask", {
3434
this._parseMask();
3535
this._parseValue();
3636
this._paint();
37-
this._events();
37+
this._bind( this._events );
3838
},
3939

4040
refresh: function() {
@@ -135,110 +135,108 @@ $.widget( "ui.mask", {
135135
}
136136
return value;
137137
},
138-
_events: function() {
139-
this._bind({
140-
focus: function( event ) {
141-
this.lastUnsavedValue = this.element.val();
142-
this._paint( true );
143-
this._delay( function() {
144-
this._caret( this._seekRight( this._parseValue() - 1 ) );
145-
}, 0);
146-
},
147-
blur: function( event ) {
148-
149-
// because we are constantly setting the value of the input, the change event
150-
// never fires - we re-introduce the change event here
151-
this._parseValue();
152-
this._paint( false );
153-
if ( this.element.val() !== this.lastUnsavedValue ) {
154-
this.element.trigger( "change" );
155-
}
138+
_events: {
139+
focus: function( event ) {
140+
this.lastUnsavedValue = this.element.val();
141+
this._paint( true );
142+
this._delay( function() {
143+
this._caret( this._seekRight( this._parseValue() - 1 ) );
144+
}, 0);
145+
},
146+
blur: function( event ) {
147+
148+
// because we are constantly setting the value of the input, the change event
149+
// never fires - we re-introduce the change event here
150+
this._parseValue();
151+
this._paint( false );
152+
if ( this.element.val() !== this.lastUnsavedValue ) {
153+
this.element.trigger( "change" );
154+
}
155+
return;
156+
},
157+
keydown: function( event ) {
158+
var bufferObject,
159+
key = event.keyCode,
160+
position = this._caret();
161+
162+
switch ( key ) {
163+
case keyCode.ESCAPE:
164+
this.element.val( this.lastUnsavedValue );
165+
this._caret( 0, this._parseValue() );
166+
event.preventDefault();
156167
return;
157-
},
158-
keydown: function( event ) {
159-
var bufferObject,
160-
key = event.keyCode,
161-
position = this._caret();
162-
163-
switch ( key ) {
164-
case keyCode.ESCAPE:
165-
this.element.val( this.lastUnsavedValue );
166-
this._caret( 0, this._parseValue() );
167-
event.preventDefault();
168-
return;
169168

170-
case keyCode.BACKSPACE:
171-
case keyCode.DELETE:
172-
event.preventDefault();
173-
if ( position.begin === position.end ) {
174-
position.begin = position.end = ( key === keyCode.DELETE ?
175-
this._seekRight( position.begin - 1) :
176-
this._seekLeft( position.begin )
177-
);
178-
if ( position.begin < 0 ) {
179-
this._caret( this._seekRight( -1 ) );
180-
return;
181-
}
169+
case keyCode.BACKSPACE:
170+
case keyCode.DELETE:
171+
event.preventDefault();
172+
if ( position.begin === position.end ) {
173+
position.begin = position.end = ( key === keyCode.DELETE ?
174+
this._seekRight( position.begin - 1) :
175+
this._seekLeft( position.begin )
176+
);
177+
if ( position.begin < 0 ) {
178+
this._caret( this._seekRight( -1 ) );
179+
return;
182180
}
183-
this._removeValues( position.begin, position.end );
184-
this._paint();
185-
this._caret( position.begin );
186-
return;
181+
}
182+
this._removeValues( position.begin, position.end );
183+
this._paint();
184+
this._caret( position.begin );
185+
return;
187186

188-
case keyCode.RIGHT:
189-
if ( position.begin === position.end ) {
190-
bufferObject = this.buffer[ position.begin ];
191-
if ( bufferObject && bufferObject.length > 1 ) {
192-
bufferObject.value = this._validValue( bufferObject, bufferObject.value );
193-
this._paint();
194-
event.preventDefault();
195-
}
196-
position = this._seekRight( bufferObject.start + bufferObject.length - 1 );
197-
bufferObject = this.buffer[ position ];
198-
if ( bufferObject && bufferObject.length > 1 ) {
199-
this._caret( position, position + ( bufferObject && bufferObject.length > 1 ? bufferObject.length : 0 ) );
200-
event.preventDefault();
201-
}
187+
case keyCode.RIGHT:
188+
if ( position.begin === position.end ) {
189+
bufferObject = this.buffer[ position.begin ];
190+
if ( bufferObject && bufferObject.length > 1 ) {
191+
bufferObject.value = this._validValue( bufferObject, bufferObject.value );
192+
this._paint();
193+
event.preventDefault();
194+
}
195+
position = this._seekRight( bufferObject.start + bufferObject.length - 1 );
196+
bufferObject = this.buffer[ position ];
197+
if ( bufferObject && bufferObject.length > 1 ) {
198+
this._caret( position, position + ( bufferObject && bufferObject.length > 1 ? bufferObject.length : 0 ) );
199+
event.preventDefault();
202200
}
203-
return;
204-
}
205-
},
206-
keypress: function( event ) {
207-
var tempValue,
208-
key = event.keyCode,
209-
position = this._caret(),
210-
bufferPosition = this._seekRight( position.begin - 1 ),
211-
bufferObject = this.buffer[ bufferPosition ];
212-
213-
this.currentEvent = event;
214-
// ignore keypresses with special keys, or control characters
215-
if ( event.metaKey || event.altKey || event.ctrlKey || key < 32 ) {
216-
return;
217201
}
218-
if ( position.begin !== position.end ) {
219-
this._removeValues( position.begin, position.end );
202+
return;
203+
}
204+
},
205+
keypress: function( event ) {
206+
var tempValue,
207+
key = event.keyCode,
208+
position = this._caret(),
209+
bufferPosition = this._seekRight( position.begin - 1 ),
210+
bufferObject = this.buffer[ bufferPosition ];
211+
212+
this.currentEvent = event;
213+
// ignore keypresses with special keys, or control characters
214+
if ( event.metaKey || event.altKey || event.ctrlKey || key < 32 ) {
215+
return;
216+
}
217+
if ( position.begin !== position.end ) {
218+
this._removeValues( position.begin, position.end );
219+
}
220+
if ( bufferObject ) {
221+
tempValue = String.fromCharCode( key );
222+
if ( bufferObject.length > 1 && bufferObject.value ) {
223+
tempValue = bufferObject.value.substr( 0, bufferPosition - bufferObject.start ) +
224+
tempValue +
225+
bufferObject.value.substr( bufferPosition - bufferObject.start + 1 );
226+
tempValue = tempValue.substr( 0, bufferObject.length );
220227
}
221-
if ( bufferObject ) {
222-
tempValue = String.fromCharCode( key );
223-
if ( bufferObject.length > 1 && bufferObject.value ) {
224-
tempValue = bufferObject.value.substr( 0, bufferPosition - bufferObject.start ) +
225-
tempValue +
226-
bufferObject.value.substr( bufferPosition - bufferObject.start + 1 );
227-
tempValue = tempValue.substr( 0, bufferObject.length );
228-
}
229-
if ( this._validValue( bufferObject, tempValue ) ) {
230-
this._shiftRight( position.begin );
231-
bufferObject.value = tempValue;
232-
this._paint();
233-
this._caret( this._seekRight( bufferPosition ) );
234-
}
228+
if ( this._validValue( bufferObject, tempValue ) ) {
229+
this._shiftRight( position.begin );
230+
bufferObject.value = tempValue;
231+
this._paint();
232+
this._caret( this._seekRight( bufferPosition ) );
235233
}
236-
event.preventDefault();
237-
this.currentEvent = false;
238-
},
239-
paste: "_paste",
240-
input: "_paste"
241-
});
234+
}
235+
event.preventDefault();
236+
this.currentEvent = false;
237+
},
238+
paste: "_paste",
239+
input: "_paste"
242240
},
243241
_paste: function(event) {
244242
this.currentEvent = event;

0 commit comments

Comments
 (0)