Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Mask: Updating code with some suggestions from review

* remove docs link in header
* call refresh from _create instead of _parseValue() _paint()
* _paint call from _setOption should be in _setOptions
* add ui-mask class to the element and remove in destroy...
* refactor LEFT & RIGHT to be the same code - remove the bufferObject in these events...
* remove currentEvent
* comment in blur function needs whitespace
* kill return at end of blur
  • Loading branch information...
commit ae3904f8b60b7da846c3316f48b9d1175e94b633 1 parent eda2ac5
Corey Frang authored October 11, 2011

Showing 1 changed file with 19 additions and 29 deletions. Show diff stats Hide diff stats

  1. 48  ui/jquery.ui.mask.js
48  ui/jquery.ui.mask.js
@@ -5,8 +5,6 @@
5 5
  * Dual licensed under the MIT or GPL Version 2 licenses.
6 6
  * http://jquery.org/license
7 7
  *
8  
- * http://docs.jquery.com/UI/Mask
9  
- *
10 8
  * Depends:
11 9
  *	jquery.ui.core.js
12 10
  *	jquery.ui.widget.js
@@ -32,11 +30,15 @@ $.widget( "ui.mask", {
32 30
 
33 31
 	_create: function() {
34 32
 		this._parseMask();
35  
-		this._parseValue();
36  
-		this._paint();
  33
+		this.refresh();
  34
+		this.element.addClass( "ui-mask" );
37 35
 		this._bind( this._events );
38 36
 	},
39 37
 
  38
+	_destroy: function() {
  39
+		this.element.removeClass( "ui-mask" );
  40
+	},
  41
+
40 42
 	refresh: function() {
41 43
 		this._parseValue();
42 44
 		this._paint();
@@ -62,6 +64,9 @@ $.widget( "ui.mask", {
62 64
 			this._parseMask();
63 65
 			this._parseValue();
64 66
 		}
  67
+	},
  68
+	_setOptions: function( options ) {
  69
+		this._super( "_setOptions", options );
65 70
 		this._paint();
66 71
 	},
67 72
 
@@ -150,6 +155,7 @@ $.widget( "ui.mask", {
150 155
 		},
151 156
 		blur: function( event ) {
152 157
 			clearTimeout( this.delayedFocus );
  158
+
153 159
 			// because we are constantly setting the value of the input, the change event
154 160
 			// never fires - we re-introduce the change event here
155 161
 			this._parseValue();
@@ -157,7 +163,6 @@ $.widget( "ui.mask", {
157 163
 			if ( this.element.val() !== this.lastUnsavedValue ) {
158 164
 				this.element.trigger( "change" );
159 165
 			}
160  
-			return;
161 166
 		},
162 167
 		keydown: function( event ) {
163 168
 			var bufferObject,
@@ -190,19 +195,6 @@ $.widget( "ui.mask", {
190 195
 				return;
191 196
 
192 197
 			case keyCode.LEFT:
193  
-				bufferObject = this.buffer[ position.begin ];
194  
-				if ( bufferObject && bufferObject.length > 1 ) {
195  
-					bufferObject.value = this._validValue( bufferObject, bufferObject.value );
196  
-					this._paint();
197  
-					event.preventDefault();
198  
-				}
199  
-				position = this._seekLeft( bufferObject ? bufferObject.start : position.begin );
200  
-				bufferObject = this.buffer[ position ];
201  
-				if ( this._caretSelect( position ) ) {
202  
-					event.preventDefault();
203  
-				}
204  
-				return;
205  
-
206 198
 			case keyCode.RIGHT:
207 199
 				bufferObject = this.buffer[ position.begin ];
208 200
 				if ( bufferObject && bufferObject.length > 1 ) {
@@ -210,13 +202,15 @@ $.widget( "ui.mask", {
210 202
 					this._paint();
211 203
 					event.preventDefault();
212 204
 				}
213  
-				position = this._seekRight( bufferObject ?
214  
-					bufferObject.start + bufferObject.length - 1 :
215  
-					position.end );
216  
-				bufferObject = this.buffer[ position ];
217  
-				if ( this._caretSelect( position ) ) {
218  
-					event.preventDefault();
  205
+				if ( key === keyCode.LEFT ) {
  206
+					position = this._seekLeft( bufferObject ? bufferObject.start : position.begin );
  207
+				} else {
  208
+					position = this._seekRight( bufferObject ?
  209
+						bufferObject.start + bufferObject.length - 1 :
  210
+						position.end );
219 211
 				}
  212
+				this._caretSelect( position );
  213
+				event.preventDefault();
220 214
 				return;
221 215
 			}
222 216
 		},
@@ -227,7 +221,6 @@ $.widget( "ui.mask", {
227 221
 				bufferPosition = this._seekRight( position.begin - 1 ),
228 222
 				bufferObject = this.buffer[ bufferPosition ];
229 223
 
230  
-			this.currentEvent = event;
231 224
 			// ignore keypresses with special keys, or control characters
232 225
 			if ( event.metaKey || event.altKey || event.ctrlKey || key < 32 ) {
233 226
 				return;
@@ -256,18 +249,15 @@ $.widget( "ui.mask", {
256 249
 				}
257 250
 			}
258 251
 			event.preventDefault();
259  
-			this.currentEvent = false;
260 252
 		},
261 253
 		paste: "_paste",
262 254
 		input: "_paste"
263 255
 	},
264 256
 	_paste: function(event) {
265  
-		this.currentEvent = event;
266 257
 		this._delay( function() {
267 258
 			var position = this._parseValue();
268 259
 			this._paint();
269 260
 			this._caret( this._seekRight( position ) );
270  
-			this.currentEvent = false;
271 261
 		}, 0 );
272 262
 	},
273 263
 	_paint: function( focused ) {
@@ -472,7 +462,7 @@ $.widget( "ui.mask", {
472 462
 			}
473 463
 			return bufferObject.valid.test( value ) && value;
474 464
 		}
475  
-		return ( bufferObject.literal === value ) && value ;
  465
+		return ( bufferObject.literal === value ) && value;
476 466
 	}
477 467
 });
478 468
 

0 notes on commit ae3904f

Jörn Zaefferer

Looks like you don't need to return anything anymore. Could remove the return true in caretSelect.

Please sign in to comment.
Something went wrong with that request. Please try again.