Permalink
Browse files

Should remove characters in correct order when backspacing over a for…

…matted character.
1 parent 99f4366 commit 96acb283fe7a2b8f07e629ffde8b6926bbfdfc55 @jaridmargolin jaridmargolin committed May 9, 2014
View
@@ -1,6 +1,6 @@
{
"name": "formatter.js",
- "version": "0.1.3",
+ "version": "0.1.4",
"devDependencies": {
"easy-amdtest": "~0.0.1",
"requirejs": "~2.1.11",
View
@@ -221,7 +221,7 @@ Formatter.prototype._focus = function () {
// @private
// Using the provided key information, alter el value.
//
-Formatter.prototype._processKey = function (chars, delKey,ingoreCaret) {
+Formatter.prototype._processKey = function (chars, delKey, ignoreCaret) {
// Get current state
this.sel = inptSel.get(this.el);
this.val = this.el.value;
@@ -243,16 +243,9 @@ Formatter.prototype._processKey = function (chars, delKey,ingoreCaret) {
} else if (delKey && this.sel.begin - 1 >= 0) {
// Always have a delta of at least -1 for the character being deleted.
+ this.val = utils.removeChars(this.val, this.sel.end -1, this.sel.end);
this.delta -= 1;
- // Count number of additional format chars to be deleted. (A group of multiple format chars should be deleted like one value char.)
- while (this.chars[this.focus-1]) {
- this.delta--;
- this.focus--;
- }
-
- this.val = utils.removeChars(this.val, this.sel.end + this.delta, this.sel.end);
-
// or Backspace and at start - exit
} else if (delKey) {
return true;
@@ -266,7 +259,7 @@ Formatter.prototype._processKey = function (chars, delKey,ingoreCaret) {
}
// Format el.value (also handles updating caret position)
- this._formatValue(ingoreCaret);
+ this._formatValue(ignoreCaret);
};
//
@@ -337,7 +330,7 @@ Formatter.prototype._removeChars = function () {
if (this.sel.end > this.focus) {
this.delta += this.sel.end - this.focus;
}
-
+
// Account for shifts during removal
var shift = 0;
@@ -413,10 +406,11 @@ Formatter.prototype._addChars = function () {
this.focus++;
}
} else {
- // Avoid caching val.length and this.focus, as they may change in _addChar.
+ // Avoid caching val.length, as they may change in _addChar.
for (var j = 0; j <= this.val.length; j++) {
- // When moving backwards, i.e. delting characters, don't add format characters past focus point.
- if ( (this.delta <= 0 && j === this.focus && this.chars[j] === undefined) || (this.focus === 0) ) { return true; }
+ // When moving backwards there are some race conditions where we
+ // dont want to add the character
+ if (this.delta <= 0 && (j === this.focus)) { return true; }
// Place character in current position of the formatted string.
this._addChar(j);
View
@@ -219,7 +219,7 @@ Formatter.prototype._focus = function () {
// @private
// Using the provided key information, alter el value.
//
-Formatter.prototype._processKey = function (chars, delKey,ingoreCaret) {
+Formatter.prototype._processKey = function (chars, delKey, ignoreCaret) {
// Get current state
this.sel = inptSel.get(this.el);
this.val = this.el.value;
@@ -241,16 +241,9 @@ Formatter.prototype._processKey = function (chars, delKey,ingoreCaret) {
} else if (delKey && this.sel.begin - 1 >= 0) {
// Always have a delta of at least -1 for the character being deleted.
+ this.val = utils.removeChars(this.val, this.sel.end -1, this.sel.end);
this.delta -= 1;
- // Count number of additional format chars to be deleted. (A group of multiple format chars should be deleted like one value char.)
- while (this.chars[this.focus-1]) {
- this.delta--;
- this.focus--;
- }
-
- this.val = utils.removeChars(this.val, this.sel.end + this.delta, this.sel.end);
-
// or Backspace and at start - exit
} else if (delKey) {
return true;
@@ -264,7 +257,7 @@ Formatter.prototype._processKey = function (chars, delKey,ingoreCaret) {
}
// Format el.value (also handles updating caret position)
- this._formatValue(ingoreCaret);
+ this._formatValue(ignoreCaret);
};
//
@@ -335,7 +328,7 @@ Formatter.prototype._removeChars = function () {
if (this.sel.end > this.focus) {
this.delta += this.sel.end - this.focus;
}
-
+
// Account for shifts during removal
var shift = 0;
@@ -411,10 +404,11 @@ Formatter.prototype._addChars = function () {
this.focus++;
}
} else {
- // Avoid caching val.length and this.focus, as they may change in _addChar.
+ // Avoid caching val.length, as they may change in _addChar.
for (var j = 0; j <= this.val.length; j++) {
- // When moving backwards, i.e. delting characters, don't add format characters past focus point.
- if ( (this.delta <= 0 && j === this.focus && this.chars[j] === undefined) || (this.focus === 0) ) { return true; }
+ // When moving backwards there are some race conditions where we
+ // dont want to add the character
+ if (this.delta <= 0 && (j === this.focus)) { return true; }
// Place character in current position of the formatted string.
this._addChar(j);
View
@@ -1,5 +1,5 @@
/*!
- * v0.1.3
+ * v0.1.4
* Copyright (c) 2014 First Opinion
* formatter.js is open sourced under the MIT license.
*
@@ -630,7 +630,7 @@ var formatter = function (patternMatcher, inptSel, utils) {
// @private
// Using the provided key information, alter el value.
//
- Formatter.prototype._processKey = function (chars, delKey, ingoreCaret) {
+ Formatter.prototype._processKey = function (chars, delKey, ignoreCaret) {
// Get current state
this.sel = inptSel.get(this.el);
this.val = this.el.value;
@@ -644,13 +644,8 @@ var formatter = function (patternMatcher, inptSel, utils) {
this._delete();
} else if (delKey && this.sel.begin - 1 >= 0) {
// Always have a delta of at least -1 for the character being deleted.
+ this.val = utils.removeChars(this.val, this.sel.end - 1, this.sel.end);
this.delta -= 1;
- // Count number of additional format chars to be deleted. (A group of multiple format chars should be deleted like one value char.)
- while (this.chars[this.focus - 1]) {
- this.delta--;
- this.focus--;
- }
- this.val = utils.removeChars(this.val, this.sel.end + this.delta, this.sel.end);
} else if (delKey) {
return true;
}
@@ -661,7 +656,7 @@ var formatter = function (patternMatcher, inptSel, utils) {
this.delta += chars.length;
}
// Format el.value (also handles updating caret position)
- this._formatValue(ingoreCaret);
+ this._formatValue(ignoreCaret);
};
//
// @private
@@ -782,10 +777,11 @@ var formatter = function (patternMatcher, inptSel, utils) {
this.focus++;
}
} else {
- // Avoid caching val.length and this.focus, as they may change in _addChar.
+ // Avoid caching val.length, as they may change in _addChar.
for (var j = 0; j <= this.val.length; j++) {
- // When moving backwards, i.e. delting characters, don't add format characters past focus point.
- if (this.delta <= 0 && j === this.focus && this.chars[j] === undefined || this.focus === 0) {
+ // When moving backwards there are some race conditions where we
+ // dont want to add the character
+ if (this.delta <= 0 && j === this.focus) {
return true;
}
// Place character in current position of the formatted string.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -1,5 +1,5 @@
/*!
- * v0.1.3
+ * v0.1.4
* Copyright (c) 2014 First Opinion
* formatter.js is open sourced under the MIT license.
*
@@ -618,7 +618,7 @@ var formatter = function (patternMatcher, inptSel, utils) {
// @private
// Using the provided key information, alter el value.
//
- Formatter.prototype._processKey = function (chars, delKey, ingoreCaret) {
+ Formatter.prototype._processKey = function (chars, delKey, ignoreCaret) {
// Get current state
this.sel = inptSel.get(this.el);
this.val = this.el.value;
@@ -632,13 +632,8 @@ var formatter = function (patternMatcher, inptSel, utils) {
this._delete();
} else if (delKey && this.sel.begin - 1 >= 0) {
// Always have a delta of at least -1 for the character being deleted.
+ this.val = utils.removeChars(this.val, this.sel.end - 1, this.sel.end);
this.delta -= 1;
- // Count number of additional format chars to be deleted. (A group of multiple format chars should be deleted like one value char.)
- while (this.chars[this.focus - 1]) {
- this.delta--;
- this.focus--;
- }
- this.val = utils.removeChars(this.val, this.sel.end + this.delta, this.sel.end);
} else if (delKey) {
return true;
}
@@ -649,7 +644,7 @@ var formatter = function (patternMatcher, inptSel, utils) {
this.delta += chars.length;
}
// Format el.value (also handles updating caret position)
- this._formatValue(ingoreCaret);
+ this._formatValue(ignoreCaret);
};
//
// @private
@@ -770,10 +765,11 @@ var formatter = function (patternMatcher, inptSel, utils) {
this.focus++;
}
} else {
- // Avoid caching val.length and this.focus, as they may change in _addChar.
+ // Avoid caching val.length, as they may change in _addChar.
for (var j = 0; j <= this.val.length; j++) {
- // When moving backwards, i.e. delting characters, don't add format characters past focus point.
- if (this.delta <= 0 && j === this.focus && this.chars[j] === undefined || this.focus === 0) {
+ // When moving backwards there are some race conditions where we
+ // dont want to add the character
+ if (this.delta <= 0 && j === this.focus) {
return true;
}
// Place character in current position of the formatted string.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -40,7 +40,7 @@
</div>
<script src="http://code.jquery.com/jquery-1.10.1.js"></script>
<script src="javascripts/scale.fix.js"></script>
- <script src="javascripts/formatter.min.js"></script>
+ <script src="javascripts/formatter.js"></script>
{% raw %}
<script>
// $('#phone-input').formatter({
@@ -190,16 +190,14 @@ var utils = function () {
var keys = {
'backspace': {
'which': 8,
- 'keyCode': 8,
- 'shiftKey': false
+ 'keyCode': 8
},
'delete': {
'which': 46,
- 'keyCode': 46,
- 'shiftKey': false
+ 'keyCode': 46
}
};
- return getMatchingKey(which, keyCode, keys);
+ return utils.getMatchingKey(which, keyCode, keys);
};
//
// Returns true/false if k is a del keyPress
@@ -216,8 +214,25 @@ var utils = function () {
'keyCode': 46
}
};
- return getMatchingKey(which, keyCode, keys);
- };
+ return utils.getMatchingKey(which, keyCode, keys);
+ };
+ // //
+ // // Determine if keydown relates to specialKey
+ // //
+ // utils.isSpecialKeyDown = function (which, keyCode) {
+ // var keys = {
+ // 'tab': { 'which': 9, 'keyCode': 9 },
+ // 'enter': { 'which': 13, 'keyCode': 13 },
+ // 'end': { 'which': 35, 'keyCode': 35 },
+ // 'home': { 'which': 36, 'keyCode': 36 },
+ // 'leftarrow': { 'which': 37, 'keyCode': 37 },
+ // 'uparrow': { 'which': 38, 'keyCode': 38 },
+ // 'rightarrow': { 'which': 39, 'keyCode': 39 },
+ // 'downarrow': { 'which': 40, 'keyCode': 40 },
+ // 'F5': { 'which': 116, 'keyCode': 116 }
+ // };
+ // return utils.getMatchingKey(which, keyCode, keys);
+ // };
//
// Determine if keypress relates to specialKey
//
@@ -232,11 +247,11 @@ var utils = function () {
'keyCode': 13
},
'end': {
- 'which': 35,
+ 'which': 0,
'keyCode': 35
},
'home': {
- 'which': 36,
+ 'which': 0,
'keyCode': 36
},
'leftarrow': {
@@ -260,7 +275,7 @@ var utils = function () {
'keyCode': 116
}
};
- return getMatchingKey(which, keyCode, keys);
+ return utils.getMatchingKey(which, keyCode, keys);
};
//
// Returns true/false if modifier key is held down
@@ -615,7 +630,7 @@ var formatter = function (patternMatcher, inptSel, utils) {
// @private
// Using the provided key information, alter el value.
//
- Formatter.prototype._processKey = function (chars, delKey, ingoreCaret) {
+ Formatter.prototype._processKey = function (chars, delKey, ignoreCaret) {
// Get current state
this.sel = inptSel.get(this.el);
this.val = this.el.value;
@@ -629,13 +644,8 @@ var formatter = function (patternMatcher, inptSel, utils) {
this._delete();
} else if (delKey && this.sel.begin - 1 >= 0) {
// Always have a delta of at least -1 for the character being deleted.
+ this.val = utils.removeChars(this.val, this.sel.end - 1, this.sel.end);
this.delta -= 1;
- // Count number of additional format chars to be deleted. (A group of multiple format chars should be deleted like one value char.)
- while (this.chars[this.focus - 1]) {
- this.delta--;
- this.focus--;
- }
- this.val = utils.removeChars(this.val, this.sel.end + this.delta, this.sel.end);
} else if (delKey) {
return true;
}
@@ -646,7 +656,7 @@ var formatter = function (patternMatcher, inptSel, utils) {
this.delta += chars.length;
}
// Format el.value (also handles updating caret position)
- this._formatValue(ingoreCaret);
+ this._formatValue(ignoreCaret);
};
//
// @private
@@ -767,10 +777,11 @@ var formatter = function (patternMatcher, inptSel, utils) {
this.focus++;
}
} else {
- // Avoid caching val.length and this.focus, as they may change in _addChar.
+ // Avoid caching val.length, as they may change in _addChar.
for (var j = 0; j <= this.val.length; j++) {
- // When moving backwards, i.e. delting characters, don't add format characters past focus point.
- if (this.delta <= 0 && j === this.focus && this.chars[j] === undefined || this.focus === 0) {
+ // When moving backwards there are some race conditions where we
+ // dont want to add the character
+ if (this.delta <= 0 && j === this.focus) {
return true;
}
// Place character in current position of the formatted string.
Oops, something went wrong.

0 comments on commit 96acb28

Please sign in to comment.