r2389 - Moved non-event functions out of _init()
Author: powella
Date: Fri Mar 27 07:57:34 2009
New Revision: 2389
Modified:
branches/dev/mask/ui/ui.mask.js
Log:
Moved non-event functions out of _init()
Replaced 'widget' with 'self'
Replaced number literals with $.ui.keyCode equiv.
Reworked shiftL() method to fix removing selections issue.
Modified: branches/dev/mask/ui/ui.mask.js
==============================================================================
--- branches/dev/mask/ui/ui.mask.js (original)
+++ branches/dev/mask/ui/ui.mask.js Fri Mar 27 07:57:34 2009
@@ -15,7 +15,6 @@
(function($) {
var pasteEventName = ($.browser.msie ? 'paste' : 'input') + ".mask",
- iPhone = (window.orientation != undefined),
undefined;
$.widget("ui.mask", {
@@ -25,16 +24,15 @@
if(!this.options.mask || !this.options.mask.length) return; //no mask
pattern defined. no point in continuing.
if(!this.options.placeholder || !this.options.placeholder.length)
this.options.placeholder = '_'; //in case the user decided to nix a
placeholder.
- var widget = this,
+ var self = this,
input = this.element,
- opts = this.options,
- mask = opts.mask,
+ options = this.options,
+ mask = options.mask,
defs = $.ui.mask.definitions,
tests = [],
partialPosition = mask.length,
firstNonMaskPos = null,
- len = mask.length,
- caret = function(begin, end){ return $.ui.mask.caret(input, begin,
end); };
+ len = mask.length;
//if we're applying the mask to an element which is not an input, it
won't have a val() method. fake one for our purposes.
if(!input.is(':input')) input.val = input.html;
@@ -54,181 +52,110 @@
}
});
- var buffer = $.map(mask.split(""), function(c, i) { if (c != '?') return
defs[c] ? opts.placeholder : c }),
- ignore = false, //Variable for ignoring control keys
- focusText = input.val();
-
- input.data("buffer", buffer).data("tests", tests);
-
- function seekNext(pos) {
- while (++pos <= len && !tests[pos]);
- return pos;
- };
-
- function shiftL(pos) {
- while (!tests[pos] && --pos >= 0);
- for (var i = pos; i < len; i++) {
- if (tests[i]) {
- buffer[i] = opts.placeholder;
- var j = seekNext(i);
- if (j < len && tests[i].test(buffer[j])) {
- buffer[i] = buffer[j];
- } else
- break;
- }
- }
- writeBuffer();
- caret(Math.max(firstNonMaskPos, pos));
- };
+ $.extend(this, {
+ buffer: $.map(mask.split(""), function(c, i) { if (c != '?') return
defs[c] ? options.placeholder : c }),
+ tests: tests,
+ firstNonMaskPos: firstNonMaskPos,
+ partialPosition: partialPosition,
+ caret: function(begin, end){ return $.ui.mask.caret(input, begin, end);
}
+ });
- function shiftR(pos) {
- for (var i = pos, c = opts.placeholder; i < len; i++) {
- if (tests[i]) {
- var j = seekNext(i);
- var t = buffer[i];
- buffer[i] = c;
- if (j < len && tests[j].test(t))
- c = t;
- else
- break;
- }
- }
- };
+ var ignore = false, //Variable for ignoring control keys
+ focusText = input.val();
function keydownEvent(e) {
- var pos = caret();
- var k = e.keyCode;
- ignore = (k < 16 || (k > 16 && k < 32) || (k > 32 && k < 41));
+ var pos = self.caret(),
+ k = e.keyCode,
+ keyCode = $.ui.keyCode;
+
+ ignore = (k < keyCode.SHIFT || (k > keyCode.SHIFT && k < keyCode.SPACE)
|| (k > keyCode.SPACE && k < 41));
//delete selection before proceeding
- if ((pos.begin - pos.end) != 0 && (!ignore || k == 8 || k == 46))
- clearBuffer(pos.begin, pos.end);
+ if ((pos.begin - pos.end) != 0 && (!ignore || k == keyCode.BACKSPACE ||
k == keyCode.DELETE))
+ self._clearBuffer(pos.begin, pos.end);
//backspace, delete, and escape get special treatment
- if (k == 8 || k == 46 || (iPhone && k == 127)) {//backspace/delete
- shiftL(pos.begin + ((k == 46 || (k == 8 && pos.begin!=pos.end)) ? 0 :
-1));
+ if (k == keyCode.BACKSPACE || k == keyCode.DELETE) {//backspace/delete
+ self._shiftL(pos.begin + ((k == keyCode.DELETE || (k ==
keyCode.BACKSPACE && pos.begin!=pos.end)) ? 0 : -1), Math.abs(pos.begin -
pos.end));
return false;
- } else if (k == 27) {//escape
+ }
+ else if (k == keyCode.ESCAPE) {//escape
input.val(focusText);
- input.caret(0, checkVal());
+ self.caret(0, self._checkVal());
return false;
}
};
function keypressEvent(e) {
+
+ var k = e.charCode || e.keyCode || e.which,
+ keyCode = $.ui.keyCode;
+
if (ignore) {
ignore = false;
//Fixes Mac FF bug on backspace
- return (e.keyCode == 8) ? false : null;
+ return (e.keyCode == keyCode.BACKSPACE) ? false : null;
}
e = e || window.event;
- var k = e.charCode || e.keyCode || e.which;
- var pos = caret();
+ var pos = self.caret();
if (e.ctrlKey || e.altKey || e.metaKey) {//Ignore
return true;
- } else if ((k >= 32 && k <= 125) || k > 186) {//typeable characters
- var p = seekNext(pos.begin - 1);
+ }
+ else if ((k >= keyCode.SPACE && k <= 125) || k > 186) {//typeable
characters
+ var p = self._seekNext(pos.begin - 1);
if (p < len) {
var c = String.fromCharCode(k);
if (tests[p].test(c)) {
- shiftR(p);
- buffer[p] = c;
- writeBuffer();
- var next = seekNext(p);
- caret(next);
- if (opts.completed && next == len)
- opts.completed.call(input);
+ self._shiftR(p);
+ self.buffer[p] = c;
+ self._writeBuffer();
+ var next = self._seekNext(p);
+ self.caret(next);
+ if (options.completed && next == len)
+ options.completed.call(input);
}
}
}
return false;
};
- function clearBuffer(start, end) {
- for (var i = start; i < end && i < len; i++) {
- if (tests[i])
- buffer[i] = opts.placeholder;
- }
- };
-
- function writeBuffer() { return input.val(buffer.join('')).val(); };
-
- function checkVal(allow) {
- //try to place characters where they belong
- var test = input.val();
- var lastMatch = -1;
- for (var i = 0, pos = 0; i < len; i++) {
- if (tests[i]) {
- buffer[i] = opts.placeholder;
- while (pos++ < test.length) {
- var c = test.charAt(pos - 1);
- if (tests[i].test(c)) {
- buffer[i] = c;
- lastMatch = i;
- break;
- }
- }
- if (pos > test.length)
- break;
- } else if (buffer[i] == test[pos] && i!=partialPosition) {
- pos++;
- lastMatch = i;
- }
- }
- if (!allow && lastMatch + 1 < partialPosition) {
- if(!opts.allowPartials || !widget.value().length){
- input.val("");
- clearBuffer(0, len);
- }
- else //if we're allowing partial input/inital values, and the element
we're masking isnt an input, then we need to allow the mask to apply.
- if(!input.is(':input')) writeBuffer();
-
- } else if (allow || lastMatch + 1 >= partialPosition) {
- writeBuffer();
- if (!allow) input.val(input.val().substring(0, lastMatch + 1));
- }
- return (partialPosition ? i : firstNonMaskPos);
- };
-
input
.one("unmask", function() {
input
.unbind(".mask")
- .removeData("buffer")
- .removeData("tests");
})
if (!input.attr("readonly"))
input
.bind("focus.mask", function() {
focusText = input.val();
- var pos = checkVal();
- writeBuffer();
+ var pos = self._checkVal();
+ self._writeBuffer();
setTimeout(function() {
if (pos == mask.length)
- caret(0, pos);
+ self.caret(0, pos);
else
- caret(pos);
+ self.caret(pos);
}, 0);
})
.bind("blur.mask", function() {
- checkVal();
+ self._checkVal();
if (input.val() != focusText)
input.change();
})
.bind('apply.mask', function(){ //changing the value of an input
without keyboard input requires re-applying the mask.
focusText = input.val();
- var pos = checkVal();
- writeBuffer();
+ var pos = self._checkVal();
+ self._writeBuffer();
})
.bind("keydown.mask", keydownEvent)
.bind("keypress.mask", keypressEvent)
.bind(pasteEventName, function() {
- setTimeout(function() { caret(checkVal(true)); }, 0);
+ setTimeout(function() { self.caret(self._checkVal(true)); }, 0);
});
- checkVal((input.val().length && opts.allowPartials)); //Perform initial
check for existing values
+ this._checkVal((input.val().length && options.allowPartials)); //Perform
initial check for existing values
},
@@ -238,8 +165,8 @@
value: function() {
var input = this.element,
- tests = input.data("tests"),
- res = $.map(input.data("buffer"), function(c, i){ return tests[i] ? c :
null; }).join(''),
+ self = this,
+ res = $.map(self.buffer, function(c, i){ return self.tests[i] ? c :
null; }).join(''),
r = new RegExp('\\'+this.options.placeholder, 'gi');
return res.replace(r, '');
},
@@ -252,7 +179,109 @@
apply: function(){
this.element.trigger('apply.mask');
+ },
+
+ _writeBuffer: function(){
+ return this.element.val(this.buffer.join('')).val();
+ },
+
+ _clearBuffer: function(start, end){
+ var len = this.options.mask.length;
+ for (var i = start; i < end && i < len; i++) {
+ if (this.tests[i])
+ this.buffer[i] = this.options.placeholder;
+ }
+ },
+
+ _seekNext: function(pos){
+ var len = this.options.mask.length;
+ while (++pos <= len && !this.tests[pos]);
+ return pos;
+ },
+
+ _shiftR: function(pos){
+ var len = this.options.mask.length;
+ for (var i = pos, c = this.options.placeholder; i < len; i++) {
+ if (this.tests[i]) {
+ var j = this._seekNext(i);
+ var t = this.buffer[i];
+ this.buffer[i] = c;
+ if (j < len && this.tests[j].test(t)){
+ c = t;
+ }
+ else{
+ break;
+ }
+ }
+ }
+ },
+
+ _shiftL: function(pos, length){
+
+ while (!this.tests[pos] && --pos >= 0);
+
+ var originalPos = pos,
+ len = this.options.mask.length,
+ placeholder = this.options.placeholder;
+
+ for(var i = pos; i < len && (i >= 0 || length > 1); i++) {
+ if (this.tests[i]) {
+ this.buffer[i] = placeholder;
+ var j = this._seekNext(i);
+ if (j < len && this.tests[i].test(this.buffer[j])) {
+ this.buffer[pos] = this.buffer[j];
+ this.buffer[j] = placeholder;
+ pos++;
+ while(!this.tests[pos]) pos++;
+ }
+ }
+ }
+ this._writeBuffer();
+ this.caret(Math.max(this.firstNonMaskPos, originalPos));
+ },
+
+ _checkVal: function(allow){
+ //try to place characters where they belong
+ var input = this.element,
+ test = input.val(),
+ len = this.options.mask.length,
+ lastMatch = -1;
+
+ for (var i = 0, pos = 0; i < len; i++) {
+ if (this.tests[i]) {
+ this.buffer[i] = this.options.placeholder;
+ while (pos++ < test.length) {
+ var c = test.charAt(pos - 1);
+ if (this.tests[i].test(c)) {
+ this.buffer[i] = c;
+ lastMatch = i;
+ break;
+ }
+ }
+ if (pos > test.length)
+ break;
+ }
+ else if (this.buffer[i] == test[pos] && i != this.partialPosition) {
+ pos++;
+ lastMatch = i;
+ }
+ }
+ if (!allow && lastMatch + 1 < this.partialPosition) {
+ if(!this.options.allowPartials || !this.value().length){
+ input.val("");
+ this._clearBuffer(0, len);
+ }
+ else //if we're allowing partial input/inital values, and the element
we're masking isnt an input, then we need to allow the mask to apply.
+ if(!input.is(':input')) this._writeBuffer();
+
+ }
+ else if (allow || lastMatch + 1 >= this.partialPosition) {
+ this._writeBuffer();
+ if (!allow) input.val(input.val().substring(0, lastMatch + 1));
+ }
+ return (this.partialPosition ? i : this.firstNonMaskPos);
}
+
});
$.extend($.ui.mask, {