Permalink
Browse files

Mask: Adding the complete event

  • Loading branch information...
1 parent 3ac0bb4 commit 1441608706fa54445be2e87366abec83d0f3afc7 @gnarf gnarf committed Aug 10, 2011
Showing with 33 additions and 16 deletions.
  1. +19 −2 tests/unit/mask/mask_events.js
  2. +14 −14 ui/jquery.ui.mask.js
View
21 tests/unit/mask/mask_events.js
@@ -4,6 +4,23 @@ module( "mask: events" );
/* TODO: Descide behavior of bluring non-valid inputs */
+test( "complete: Complete event fires when typing last character of mask", function() {
+ expect( 3 );
+ var input = $( "#mask1" ).val("").mask({
+ mask: "9",
+ complete: function( event, ui ) {
+ equal( this, input[0], "Complete event fired with correct context" );
+ }
+ }),
+ mask = input.data( "mask" );
+
+ input.focus();
+ mask._caret( 0 );
+ deepEqual( mask._caret(), { begin: 0, end: 0 }, "Caret position correct" );
+ input.simulate( "keypress", { keyCode: "1".charCodeAt( 0 ) } );
+ equal( input.val(), "1", "Value is valid" );
+});
+
asyncTest( "focus: Initial Caret Positioning", function() {
var input = $( "#mask1" ).val("").mask({ mask: "9" }),
mask = input.data( "mask" );
@@ -20,9 +37,9 @@ asyncTest( "focus: Initial Caret Positioning", function() {
setTimeout( function() {
deepEqual( mask._caret(), { begin: 1, end: 1 }, "Caret position correct");
start();
- }, 0);
+ }, 15);
- }, 0);
+ }, 15);
});
View
28 ui/jquery.ui.mask.js
@@ -28,7 +28,7 @@ $.widget( "ui.mask", {
this._parseMask();
this._parseValue();
this._paint();
- this._keyBinding();
+ this._events();
},
refresh: function() {
@@ -37,17 +37,7 @@ $.widget( "ui.mask", {
},
valid: function() {
- var bufferPosition,
- bufferObject,
- bufferLength = this.buffer.length;
-
- for ( bufferPosition = 0; bufferPosition < bufferLength; bufferPosition++ ) {
- bufferObject = this.buffer[ bufferPosition ];
- if ( !( bufferObject.value || bufferObject.literal ) ) {
- return false;
- }
- }
- return true;
+ return this.isValid;
},
// returns (or sets) the value without the mask
@@ -144,17 +134,19 @@ $.widget( "ui.mask", {
}
}
},
- _keyBinding: function() {
+ _events: function() {
var cancelKeypress,
lastUnsavedValue,
that = this,
elem = that.element;
function handlePaste() {
+ that.currentEvent = event;
setTimeout( function() {
var position = that._parseValue();
that._paint();
that._caret( that._seekRight( position ) );
+ that.currentEvent = false;
}, 0 );
}
@@ -208,6 +200,7 @@ $.widget( "ui.mask", {
bufferPosition = that._seekRight( position.begin - 1 ),
bufferObject = that.buffer[ bufferPosition ];
+ that.currentEvent = event;
// ignore keypresses with special keys, or control characters
if ( event.metaKey || event.altKey || event.ctrlKey || key < 32 ) {
return;
@@ -225,6 +218,7 @@ $.widget( "ui.mask", {
}
}
event.preventDefault();
+ that.currentEvent = false;
},
paste: handlePaste,
input: handlePaste
@@ -358,11 +352,13 @@ $.widget( "ui.mask", {
return lastFilledPosition;
},
_getValue: function( raw ) {
- var bufferPosition,
+ var wasValid = this.isValid,
+ bufferPosition,
bufferObject,
bufferLength = this.buffer.length,
value = "";
+ this.isValid = true;
for ( bufferPosition = 0; bufferPosition < bufferLength; bufferPosition += bufferObject.length ) {
bufferObject = this.buffer[ bufferPosition ];
if ( bufferObject.literal && !raw ) {
@@ -371,8 +367,12 @@ $.widget( "ui.mask", {
value += bufferObject.value;
} else if ( !raw ) {
value += this.options.placeholder;
+ this.isValid = false;
}
}
+ if ( !wasValid && this.isValid ) {
+ this._trigger( "complete", this.currentEvent );
+ }
return value;
},
_paint: function() {

0 comments on commit 1441608

Please sign in to comment.