Permalink
Browse files

Close #18

1 parent 5e93881 commit 04507e7ea25f59a5e4813b63e942f8b52373631b @jaridmargolin jaridmargolin committed Nov 14, 2013
Showing with 350 additions and 171 deletions.
  1. +48 −35 lib/formatter.js
  2. +1 −1 lib/formatter.min.js
  3. +48 −35 lib/jquery.formatter.js
  4. +1 −1 lib/jquery.formatter.min.js
  5. +48 −35 src/formatter.js
  6. +204 −64 test/formatter.js
View
@@ -203,27 +203,19 @@ Formatter.prototype._processKey = function (chars, delKey) {
this.delta = (-1) * Math.abs(this.sel.begin - this.sel.end);
this.val = utils.removeChars(this.val, this.sel.begin, this.sel.end);
}
- // If delKey
- else if (delKey) {
- // Delete
- if (delKey && delKey == 46) {
- // Adjust focus to make sure its not on a formatted char
- while (this.chars[this.sel.begin]) {
- this._nextPos();
- }
- // As long as we are not at the end
- if (this.sel.begin < this.val.length) {
- // We will simulate a delete by moving the caret to the next char
- // and then deleting
- this._nextPos();
- this.val = utils.removeChars(this.val, this.sel.end -1, this.sel.end);
- this.delta = -1;
- }
- // or Backspace and not at start
- } else if (delKey && this.sel.begin - 1 >= 0) {
- this.val = utils.removeChars(this.val, this.sel.end -1, this.sel.end);
- this.delta = -1;
- }
+
+ // Delete key (moves opposite direction)
+ else if (delKey && delKey == 46) {
+ this._delete();
+
+ // or Backspace and not at start
+ } else if (delKey && this.sel.begin - 1 >= 0) {
+ this.val = utils.removeChars(this.val, this.sel.end -1, this.sel.end);
+ this.delta = -1;
+
+ // or Backspace and at start - exit
+ } else if (delKey) {
+ return true;
}
// If the key is not a del key, it should convert to a str
@@ -239,6 +231,26 @@ Formatter.prototype._processKey = function (chars, delKey) {
//
// @private
+// Deletes the character in front of it
+//
+Formatter.prototype._delete = function () {
+ // Adjust focus to make sure its not on a formatted char
+ while (this.chars[this.sel.begin]) {
+ this._nextPos();
+ }
+
+ // As long as we are not at the end
+ if (this.sel.begin < this.val.length) {
+ // We will simulate a delete by moving the caret to the next char
+ // and then deleting
+ this._nextPos();
+ this.val = utils.removeChars(this.val, this.sel.end -1, this.sel.end);
+ this.delta = -1;
+ }
+};
+
+//
+// @private
// Quick helper method to move the caret to the next pos
//
Formatter.prototype._nextPos = function () {
@@ -317,16 +329,18 @@ Formatter.prototype._validateInpts = function () {
// Get char inpt type
var inptType = this.inpts[i];
- // If improper type, or char doesnt match, remove
- if (!inptRegs[inptType] || !inptRegs[inptType].test(this.val.charAt(i))) {
- // Check bounds
- if (this.inpts[i]) {
- this.val = utils.removeChars(this.val, i, i + 1);
- this.focusStart--;
- this.newPos--;
- this.delta--;
- i--;
- }
+ // Checks
+ var isBadType = !inptRegs[inptType],
+ isInvalid = !inptRegs[inptType].test(this.val.charAt(i)),
+ inBounds = this.inpts[i];
+
+ // Remove if incorrect and inbounds
+ if ((isBadType || isInvalid) && inBounds) {
+ this.val = utils.removeChars(this.val, i, i + 1);
+ this.focusStart--;
+ this.newPos--;
+ this.delta--;
+ i--;
}
}
};
@@ -354,6 +368,9 @@ Formatter.prototype._addChars = function () {
} else {
// Avoid caching val.length, as it changes during manipulations
for (var j = 0; j <= this.val.length; j++) {
+ // 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; }
this._addChar(j);
}
}
@@ -380,10 +397,6 @@ Formatter.prototype._addChar = function (i) {
this.focus++;
}
- // When moving backwards there are some race conditions where we
- // dont want to add the character
- if (this.delta < 0 && (this.val.charAt(i) == char )) { return true; }
-
// Updateholder
if (this.hldrs[i]) {
delete this.hldrs[i];
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -199,27 +199,19 @@ Formatter.prototype._processKey = function (chars, delKey) {
this.delta = (-1) * Math.abs(this.sel.begin - this.sel.end);
this.val = utils.removeChars(this.val, this.sel.begin, this.sel.end);
}
- // If delKey
- else if (delKey) {
- // Delete
- if (delKey && delKey == 46) {
- // Adjust focus to make sure its not on a formatted char
- while (this.chars[this.sel.begin]) {
- this._nextPos();
- }
- // As long as we are not at the end
- if (this.sel.begin < this.val.length) {
- // We will simulate a delete by moving the caret to the next char
- // and then deleting
- this._nextPos();
- this.val = utils.removeChars(this.val, this.sel.end -1, this.sel.end);
- this.delta = -1;
- }
- // or Backspace and not at start
- } else if (delKey && this.sel.begin - 1 >= 0) {
- this.val = utils.removeChars(this.val, this.sel.end -1, this.sel.end);
- this.delta = -1;
- }
+
+ // Delete key (moves opposite direction)
+ else if (delKey && delKey == 46) {
+ this._delete();
+
+ // or Backspace and not at start
+ } else if (delKey && this.sel.begin - 1 >= 0) {
+ this.val = utils.removeChars(this.val, this.sel.end -1, this.sel.end);
+ this.delta = -1;
+
+ // or Backspace and at start - exit
+ } else if (delKey) {
+ return true;
}
// If the key is not a del key, it should convert to a str
@@ -235,6 +227,26 @@ Formatter.prototype._processKey = function (chars, delKey) {
//
// @private
+// Deletes the character in front of it
+//
+Formatter.prototype._delete = function () {
+ // Adjust focus to make sure its not on a formatted char
+ while (this.chars[this.sel.begin]) {
+ this._nextPos();
+ }
+
+ // As long as we are not at the end
+ if (this.sel.begin < this.val.length) {
+ // We will simulate a delete by moving the caret to the next char
+ // and then deleting
+ this._nextPos();
+ this.val = utils.removeChars(this.val, this.sel.end -1, this.sel.end);
+ this.delta = -1;
+ }
+};
+
+//
+// @private
// Quick helper method to move the caret to the next pos
//
Formatter.prototype._nextPos = function () {
@@ -313,16 +325,18 @@ Formatter.prototype._validateInpts = function () {
// Get char inpt type
var inptType = this.inpts[i];
- // If improper type, or char doesnt match, remove
- if (!inptRegs[inptType] || !inptRegs[inptType].test(this.val.charAt(i))) {
- // Check bounds
- if (this.inpts[i]) {
- this.val = utils.removeChars(this.val, i, i + 1);
- this.focusStart--;
- this.newPos--;
- this.delta--;
- i--;
- }
+ // Checks
+ var isBadType = !inptRegs[inptType],
+ isInvalid = !inptRegs[inptType].test(this.val.charAt(i)),
+ inBounds = this.inpts[i];
+
+ // Remove if incorrect and inbounds
+ if ((isBadType || isInvalid) && inBounds) {
+ this.val = utils.removeChars(this.val, i, i + 1);
+ this.focusStart--;
+ this.newPos--;
+ this.delta--;
+ i--;
}
}
};
@@ -350,6 +364,9 @@ Formatter.prototype._addChars = function () {
} else {
// Avoid caching val.length, as it changes during manipulations
for (var j = 0; j <= this.val.length; j++) {
+ // 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; }
this._addChar(j);
}
}
@@ -376,10 +393,6 @@ Formatter.prototype._addChar = function (i) {
this.focus++;
}
- // When moving backwards there are some race conditions where we
- // dont want to add the character
- if (this.delta < 0 && (this.val.charAt(i) == char )) { return true; }
-
// Updateholder
if (this.hldrs[i]) {
delete this.hldrs[i];
Oops, something went wrong.

0 comments on commit 04507e7

Please sign in to comment.