Skip to content

Commit 52e0daa

Browse files
committed
Mask: Refactor events method to use _bind and _delay, getting rid of var self
1 parent f8e7a2d commit 52e0daa

File tree

1 file changed

+43
-49
lines changed

1 file changed

+43
-49
lines changed

ui/jquery.ui.mask.js

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -136,49 +136,34 @@ $.widget( "ui.mask", {
136136
return value;
137137
},
138138
_events: function() {
139-
var cancelKeypress,
140-
lastUnsavedValue,
141-
that = this,
142-
elem = that.element;
143-
144-
function handlePaste() {
145-
that.currentEvent = event;
146-
setTimeout( function() {
147-
var position = that._parseValue();
148-
that._paint();
149-
that._caret( that._seekRight( position ) );
150-
that.currentEvent = false;
151-
}, 0 );
152-
}
153-
154139
this._bind({
155140
focus: function( event ) {
156-
lastUnsavedValue = elem.val();
157-
that._paint( true );
158-
setTimeout( function() {
159-
that._caret( that._seekRight( that._parseValue() - 1 ) );
141+
this.lastUnsavedValue = this.element.val();
142+
this._paint( true );
143+
this._delay( function() {
144+
this._caret( this._seekRight( this._parseValue() - 1 ) );
160145
}, 0);
161146
},
162147
blur: function( event ) {
163148

164149
// because we are constantly setting the value of the input, the change event
165150
// never fires - we re-introduce the change event here
166-
that._parseValue();
167-
that._paint( false );
168-
if ( elem.val() !== lastUnsavedValue ) {
169-
elem.trigger( "change" );
151+
this._parseValue();
152+
this._paint( false );
153+
if ( this.element.val() !== this.lastUnsavedValue ) {
154+
this.element.trigger( "change" );
170155
}
171156
return;
172157
},
173158
keydown: function( event ) {
174159
var bufferObject,
175160
key = event.keyCode,
176-
position = that._caret();
161+
position = this._caret();
177162

178163
switch ( key ) {
179164
case keyCode.ESCAPE:
180-
elem.val( lastUnsavedValue );
181-
that._caret( 0, that._parseValue() );
165+
this.element.val( this.lastUnsavedValue );
166+
this._caret( 0, this._parseValue() );
182167
event.preventDefault();
183168
return;
184169

@@ -187,31 +172,31 @@ $.widget( "ui.mask", {
187172
event.preventDefault();
188173
if ( position.begin === position.end ) {
189174
position.begin = position.end = ( key === keyCode.DELETE ?
190-
that._seekRight( position.begin - 1) :
191-
that._seekLeft( position.begin )
175+
this._seekRight( position.begin - 1) :
176+
this._seekLeft( position.begin )
192177
);
193178
if ( position.begin < 0 ) {
194-
that._caret( that._seekRight( -1 ) );
179+
this._caret( this._seekRight( -1 ) );
195180
return;
196181
}
197182
}
198-
that._removeValues( position.begin, position.end );
199-
that._paint();
200-
that._caret( position.begin );
183+
this._removeValues( position.begin, position.end );
184+
this._paint();
185+
this._caret( position.begin );
201186
return;
202187

203188
case keyCode.RIGHT:
204189
if ( position.begin === position.end ) {
205-
bufferObject = that.buffer[ position.begin ];
190+
bufferObject = this.buffer[ position.begin ];
206191
if ( bufferObject && bufferObject.length > 1 ) {
207192
bufferObject.value = this._validValue( bufferObject, bufferObject.value );
208-
that._paint();
193+
this._paint();
209194
event.preventDefault();
210195
}
211-
position = that._seekRight( bufferObject.start + bufferObject.length - 1 );
212-
bufferObject = that.buffer[ position ];
196+
position = this._seekRight( bufferObject.start + bufferObject.length - 1 );
197+
bufferObject = this.buffer[ position ];
213198
if ( bufferObject && bufferObject.length > 1 ) {
214-
that._caret( position, position + ( bufferObject && bufferObject.length > 1 ? bufferObject.length : 0 ) );
199+
this._caret( position, position + ( bufferObject && bufferObject.length > 1 ? bufferObject.length : 0 ) );
215200
event.preventDefault();
216201
}
217202
}
@@ -221,17 +206,17 @@ $.widget( "ui.mask", {
221206
keypress: function( event ) {
222207
var tempValue,
223208
key = event.keyCode,
224-
position = that._caret(),
225-
bufferPosition = that._seekRight( position.begin - 1 ),
226-
bufferObject = that.buffer[ bufferPosition ];
209+
position = this._caret(),
210+
bufferPosition = this._seekRight( position.begin - 1 ),
211+
bufferObject = this.buffer[ bufferPosition ];
227212

228-
that.currentEvent = event;
213+
this.currentEvent = event;
229214
// ignore keypresses with special keys, or control characters
230215
if ( event.metaKey || event.altKey || event.ctrlKey || key < 32 ) {
231216
return;
232217
}
233218
if ( position.begin !== position.end ) {
234-
that._removeValues( position.begin, position.end );
219+
this._removeValues( position.begin, position.end );
235220
}
236221
if ( bufferObject ) {
237222
tempValue = String.fromCharCode( key );
@@ -242,19 +227,28 @@ $.widget( "ui.mask", {
242227
tempValue = tempValue.substr( 0, bufferObject.length );
243228
}
244229
if ( this._validValue( bufferObject, tempValue ) ) {
245-
that._shiftRight( position.begin );
230+
this._shiftRight( position.begin );
246231
bufferObject.value = tempValue;
247-
that._paint();
248-
that._caret( that._seekRight( bufferPosition ) );
232+
this._paint();
233+
this._caret( this._seekRight( bufferPosition ) );
249234
}
250235
}
251236
event.preventDefault();
252-
that.currentEvent = false;
237+
this.currentEvent = false;
253238
},
254-
paste: handlePaste,
255-
input: handlePaste
239+
paste: "_paste",
240+
input: "_paste"
256241
});
257242
},
243+
_paste: function(event) {
244+
this.currentEvent = event;
245+
this._delay( function() {
246+
var position = this._parseValue();
247+
this._paint();
248+
this._caret( this._seekRight( position ) );
249+
this.currentEvent = false;
250+
}, 0 );
251+
},
258252
_paint: function( focused ) {
259253
if ( focused === undefined ) {
260254
focused = this.element[ 0 ] === document.activeElement;
@@ -398,7 +392,7 @@ $.widget( "ui.mask", {
398392
bufferLength = this.buffer.length;
399393

400394
for ( destPosition = begin, bufferPosition = this._seekRight( end - 1 );
401-
destPosition < bufferLength;
395+
destPosition < bufferLength;
402396
destPosition += destObject.length ) {
403397
destObject = this.buffer[ destPosition ];
404398
bufferObject = this.buffer[ bufferPosition ];

0 commit comments

Comments
 (0)