Browse Source

Merge pull request #590 from swd120/3.x

Fix for #586 - Numeric Grouping Delete
Robin Herbots 11 years ago
parent
commit
ede29e00e1
1 changed files with 137 additions and 137 deletions
  1. 137 137
      js/jquery.inputmask.numeric.extensions.js

+ 137 - 137
js/jquery.inputmask.numeric.extensions.js

@@ -1,117 +1,117 @@
-/*
-Input Mask plugin extensions
-http://github.com/RobinHerbots/jquery.inputmask
-Copyright (c) 2010 - 2014 Robin Herbots
-Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 0.0.0
-
-Optional extensions on the jquery.inputmask base
-*/
-(function ($) {
-    //number aliases
+/*
+Input Mask plugin extensions
+http://github.com/RobinHerbots/jquery.inputmask
+Copyright (c) 2010 - 2014 Robin Herbots
+Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
+Version: 0.0.0
+
+Optional extensions on the jquery.inputmask base
+*/
+(function ($) {
+    //number aliases
     $.extend($.inputmask.defaults.aliases, {
     $.extend($.inputmask.defaults.aliases, {
         'numeric': {
         'numeric': {
             mask: function (opts) {
             mask: function (opts) {
                 if (opts.repeat !== 0 && isNaN(opts.integerDigits)) {
                 if (opts.repeat !== 0 && isNaN(opts.integerDigits)) {
                     opts.integerDigits = opts.repeat;
                     opts.integerDigits = opts.repeat;
-                }
-                opts.repeat = 0;
-
-                opts.autoGroup = opts.autoGroup && opts.groupSeparator != "";
-
+                }
+                opts.repeat = 0;
+
+                opts.autoGroup = opts.autoGroup && opts.groupSeparator != "";
+
                 if (opts.autoGroup && isFinite(opts.integerDigits)) {
                 if (opts.autoGroup && isFinite(opts.integerDigits)) {
-                    var seps = Math.floor(opts.integerDigits / opts.groupSize);
-                    var mod = opts.integerDigits % opts.groupSize;
+                    var seps = Math.floor(opts.integerDigits / opts.groupSize);
+                    var mod = opts.integerDigits % opts.groupSize;
                     opts.integerDigits += mod == 0 ? seps - 1 : seps;
                     opts.integerDigits += mod == 0 ? seps - 1 : seps;
-                }
-
-                opts.definitions[":"].placeholder = opts.radixPoint;
-
-                var mask = opts.prefix;
-                mask += "[+]";
-                mask += "~{1," + opts.integerDigits + "}";
+                }
+
+                opts.definitions[":"].placeholder = opts.radixPoint;
+
+                var mask = opts.prefix;
+                mask += "[+]";
+                mask += "~{1," + opts.integerDigits + "}";
                 if (opts.digits != undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) {
                 if (opts.digits != undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) {
-                    if (opts.digitsOptional)
-                        mask += "[" + ":" + "~{" + opts.digits + "}]";
+                    if (opts.digitsOptional)
+                        mask += "[" + ":" + "~{" + opts.digits + "}]";
                     else mask += ":" + "~{" + opts.digits + "}";
                     else mask += ":" + "~{" + opts.digits + "}";
-                }
-                mask += opts.suffix;
+                }
+                mask += opts.suffix;
                 return mask;
                 return mask;
-            },
-            placeholder: "",
-            greedy: false,
-            digits: "*", //number of fractionalDigits
-            digitsOptional: true,
-            groupSeparator: "",//",", // | "."
-            radixPoint: ".",
-            groupSize: 3,
-            autoGroup: false,
-            allowPlus: true,
-            allowMinus: true,
-            integerDigits: "+", //number of integerDigits
-            prefix: "",
-            suffix: "",
-            rightAlign: true,
+            },
+            placeholder: "",
+            greedy: false,
+            digits: "*", //number of fractionalDigits
+            digitsOptional: true,
+            groupSeparator: "",//",", // | "."
+            radixPoint: ".",
+            groupSize: 3,
+            autoGroup: false,
+            allowPlus: true,
+            allowMinus: true,
+            integerDigits: "+", //number of integerDigits
+            prefix: "",
+            suffix: "",
+            rightAlign: true,
             postFormat: function (buffer, pos, reformatOnly, opts) {  //this needs to be removed // this is crap
             postFormat: function (buffer, pos, reformatOnly, opts) {  //this needs to be removed // this is crap
-                var needsRefresh = false, charAtPos = buffer[pos];
-                if (opts.groupSeparator == "" ||
-                    ($.inArray(opts.radixPoint, buffer) != -1 && pos >= $.inArray(opts.radixPoint, buffer)) ||
-                    new RegExp('[-\+]').test(charAtPos)
-                    ) return { pos: pos };
-                var cbuf = buffer.slice();
+                var needsRefresh = false, charAtPos = buffer[pos];
+                if (opts.groupSeparator == "" ||
+                    ($.inArray(opts.radixPoint, buffer) != -1 && pos >= $.inArray(opts.radixPoint, buffer)) ||
+                    new RegExp('[-\+]').test(charAtPos)
+                    ) return { pos: pos };
+                var cbuf = buffer.slice();
                 if (charAtPos == opts.groupSeparator) {
                 if (charAtPos == opts.groupSeparator) {
                     cbuf.splice(pos--, 1);
                     cbuf.splice(pos--, 1);
                     charAtPos = cbuf[pos];
                     charAtPos = cbuf[pos];
-                }
-                if (reformatOnly) cbuf[pos] = "?"; else cbuf.splice(pos, 0, "?"); //set position indicator
-                var bufVal = cbuf.join('');
+                }
+                if (reformatOnly) cbuf[pos] = "?"; else cbuf.splice(pos, 0, "?"); //set position indicator
+                var bufVal = cbuf.join('');
                 if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
                 if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
-                    var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
-                    needsRefresh = bufVal.indexOf(opts.groupSeparator) == 0;
-                    bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), '');
-                    var radixSplit = bufVal.split(opts.radixPoint);
-                    bufVal = radixSplit[0];
-                    if (bufVal != (opts.prefix + "?0") && bufVal.length > (opts.groupSize + opts.prefix.length)) {
-                        needsRefresh = true;
-                        var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
+                    var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
+                    needsRefresh = bufVal.indexOf(opts.groupSeparator) == 0;
+                    bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), '');
+                    var radixSplit = bufVal.split(opts.radixPoint);
+                    bufVal = radixSplit[0];
+                    if (bufVal != (opts.prefix + "?0") && bufVal.length >= (opts.groupSize + opts.prefix.length)) {
+                        needsRefresh = true;
+                        var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
                         while (reg.test(bufVal)) {
                         while (reg.test(bufVal)) {
-                            bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
+                            bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
                             bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
                             bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
                         }
                         }
-                    }
-                    if (radixSplit.length > 1)
+                    }
+                    if (radixSplit.length > 1)
                         bufVal += opts.radixPoint + radixSplit[1];
                         bufVal += opts.radixPoint + radixSplit[1];
-                }
-                buffer.length = bufVal.length; //align the length
+                }
+                buffer.length = bufVal.length; //align the length
                 for (var i = 0, l = bufVal.length; i < l; i++) {
                 for (var i = 0, l = bufVal.length; i < l; i++) {
                     buffer[i] = bufVal.charAt(i);
                     buffer[i] = bufVal.charAt(i);
-                }
+                }
                 var newPos = $.inArray("?", buffer);
                 var newPos = $.inArray("?", buffer);
-                if (reformatOnly) buffer[newPos] = charAtPos; else buffer.splice(newPos, 1);
-
+                if (reformatOnly) buffer[newPos] = charAtPos; else buffer.splice(newPos, 1);
+
                 return { pos: newPos, "refreshFromBuffer": needsRefresh };
                 return { pos: newPos, "refreshFromBuffer": needsRefresh };
-            },
+            },
             onKeyDown: function (e, buffer, caretPos, opts) {
             onKeyDown: function (e, buffer, caretPos, opts) {
                 if (opts.autoGroup && (e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE)) {
                 if (opts.autoGroup && (e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE)) {
-                    var rslt = opts.postFormat(buffer, caretPos - 1, true, opts);
-                    rslt.caret = rslt.pos + 1;
+                    var rslt = opts.postFormat(buffer, caretPos - 1, true, opts);
+                    rslt.caret = rslt.pos + 1;
                     return rslt;
                     return rslt;
                 }
                 }
-            },
+            },
             onKeyPress: function (e, buffer, caretPos, opts) {
             onKeyPress: function (e, buffer, caretPos, opts) {
                 if (opts.autoGroup /*&& String.fromCharCode(k) == opts.radixPoint*/) {
                 if (opts.autoGroup /*&& String.fromCharCode(k) == opts.radixPoint*/) {
-                    var rslt = opts.postFormat(buffer, caretPos - 1, true, opts);
-                    rslt.caret = rslt.pos + 1;
+                    var rslt = opts.postFormat(buffer, caretPos - 1, true, opts);
+                    rslt.caret = rslt.pos + 1;
                     return rslt;
                     return rslt;
                 }
                 }
-            },
+            },
             regex: {
             regex: {
                 integerPart: function (opts) { return new RegExp('[-\+]?\\d+'); }
                 integerPart: function (opts) { return new RegExp('[-\+]?\\d+'); }
-            },
+            },
             negationhandler: function (chrs, buffer, pos, strict, opts) {
             negationhandler: function (chrs, buffer, pos, strict, opts) {
                 if (!strict && opts.allowMinus && chrs === "-") {
                 if (!strict && opts.allowMinus && chrs === "-") {
-                    var matchRslt = buffer.join('').match(opts.regex.integerPart(opts));
-
+                    var matchRslt = buffer.join('').match(opts.regex.integerPart(opts));
+
                     if (matchRslt.length > 0) {
                     if (matchRslt.length > 0) {
                         if (buffer[matchRslt.index] == "+") {
                         if (buffer[matchRslt.index] == "+") {
                             return { "pos": matchRslt.index, "c": "-", "remove": matchRslt.index, "caret": pos };
                             return { "pos": matchRslt.index, "c": "-", "remove": matchRslt.index, "caret": pos };
@@ -121,102 +121,102 @@ Optional extensions on the jquery.inputmask base
                             return { "pos": matchRslt.index, "c": "-", "caret": pos + 1 };
                             return { "pos": matchRslt.index, "c": "-", "caret": pos + 1 };
                         }
                         }
                     }
                     }
-                }
+                }
                 return false;
                 return false;
-            },
+            },
             definitions: {
             definitions: {
                 '~': {
                 '~': {
                     validator: function (chrs, maskset, pos, strict, opts) {
                     validator: function (chrs, maskset, pos, strict, opts) {
-                        var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
+                        var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
                         if (!isValid) {
                         if (!isValid) {
-                            isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
-                            if (isValid === true) isValid = { pos: pos };
-                            if (isValid != false && !strict) {
-                                //handle 0 for integerpart
-                                var matchRslt = maskset.buffer.join('').match(opts.regex.integerPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
+                            isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
+                            if (isValid === true) isValid = { pos: pos };
+                            if (isValid != false && !strict) {
+                                //handle 0 for integerpart
+                                var matchRslt = maskset.buffer.join('').match(opts.regex.integerPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
                                 if (matchRslt) {
                                 if (matchRslt) {
                                     if (matchRslt["0"][0].indexOf("0") == 0 && pos >= opts.prefix.length) {
                                     if (matchRslt["0"][0].indexOf("0") == 0 && pos >= opts.prefix.length) {
                                         if (radixPosition == -1 || (pos <= radixPosition && maskset["validPositions"][radixPosition] == undefined)) {
                                         if (radixPosition == -1 || (pos <= radixPosition && maskset["validPositions"][radixPosition] == undefined)) {
-                                            maskset.buffer.splice(matchRslt.index, 1);
-                                            pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
+                                            maskset.buffer.splice(matchRslt.index, 1);
+                                            pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
                                             $.extend(isValid, { "pos": pos, "remove": matchRslt.index });
                                             $.extend(isValid, { "pos": pos, "remove": matchRslt.index });
                                         } else if (pos > matchRslt.index && pos <= radixPosition) {
                                         } else if (pos > matchRslt.index && pos <= radixPosition) {
-                                            maskset.buffer.splice(matchRslt.index, 1);
-                                            pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
+                                            maskset.buffer.splice(matchRslt.index, 1);
+                                            pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
                                             $.extend(isValid, { "pos": pos, "remove": matchRslt.index });
                                             $.extend(isValid, { "pos": pos, "remove": matchRslt.index });
                                         }
                                         }
                                     } else if (chrs == "0" && pos <= matchRslt.index) {
                                     } else if (chrs == "0" && pos <= matchRslt.index) {
                                         return false;
                                         return false;
                                     }
                                     }
-                                }
-                                //handle overwrite when fixed precision
+                                }
+                                //handle overwrite when fixed precision
                                 if (opts.digitsOptional === false && pos > radixPosition) {
                                 if (opts.digitsOptional === false && pos > radixPosition) {
                                     return { "pos": pos, "remove": pos };
                                     return { "pos": pos, "remove": pos };
                                 }
                                 }
                             }
                             }
-                        }
-
+                        }
+
                         return isValid;
                         return isValid;
-                    },
-                    cardinality: 1,
+                    },
+                    cardinality: 1,
                     prevalidator: null
                     prevalidator: null
-                },
+                },
                 '+': {
                 '+': {
                     validator: function (chrs, maskset, pos, strict, opts) {
                     validator: function (chrs, maskset, pos, strict, opts) {
-                        var signed = "[";
-                        if (opts.allowMinus === true) signed += "-";
-                        if (opts.allowPlus === true) signed += "\+";
-                        signed += "]";
-                        var isValid = new RegExp(signed).test(chrs);
+                        var signed = "[";
+                        if (opts.allowMinus === true) signed += "-";
+                        if (opts.allowPlus === true) signed += "\+";
+                        signed += "]";
+                        var isValid = new RegExp(signed).test(chrs);
                         return isValid;
                         return isValid;
-                    },
-                    cardinality: 1,
+                    },
+                    cardinality: 1,
                     prevalidator: null
                     prevalidator: null
-                },
+                },
                 ':': {
                 ':': {
                     validator: function (chrs, maskset, pos, strict, opts) {
                     validator: function (chrs, maskset, pos, strict, opts) {
-                        var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
+                        var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
                         if (!isValid) {
                         if (!isValid) {
-                            var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
+                            var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
                             isValid = new RegExp(radix).test(chrs);
                             isValid = new RegExp(radix).test(chrs);
                             if (isValid && maskset["validPositions"][pos] && maskset["validPositions"][pos]["match"].placeholder == opts.radixPoint) {
                             if (isValid && maskset["validPositions"][pos] && maskset["validPositions"][pos]["match"].placeholder == opts.radixPoint) {
                                 isValid = { "pos": pos, "remove": pos };
                                 isValid = { "pos": pos, "remove": pos };
                             }
                             }
-                        }
+                        }
                         return isValid;
                         return isValid;
-                    },
-                    cardinality: 1,
-                    prevalidator: null,
-                    placeholder: "" //radixpoint will be set in the mask function
+                    },
+                    cardinality: 1,
+                    prevalidator: null,
+                    placeholder: "" //radixpoint will be set in the mask function
                 }
                 }
-
-            },
-            insertMode: true,
-            autoUnmask: false,
+
+            },
+            insertMode: true,
+            autoUnmask: false,
             onUnMask: function (maskedValue, unmaskedValue, opts) {
             onUnMask: function (maskedValue, unmaskedValue, opts) {
-                var processValue = maskedValue.replace(opts.prefix, "");
-                processValue = processValue.replace(opts.suffix, "");
-                processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
-                //processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
+                var processValue = maskedValue.replace(opts.prefix, "");
+                processValue = processValue.replace(opts.suffix, "");
+                processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
+                //processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
                 return processValue;
                 return processValue;
-            },
+            },
             isComplete: function (buffer, opts) {
             isComplete: function (buffer, opts) {
-                var maskedValue = buffer.join(''), bufClone = buffer.slice();
-                //verify separator positions
-                opts.postFormat(bufClone, 0, true, opts);
-                if (bufClone.join('') != maskedValue) return false;
-
-                var processValue = maskedValue.replace(opts.prefix, "");
-                processValue = processValue.replace(opts.suffix, "");
-                processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
-                processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
+                var maskedValue = buffer.join(''), bufClone = buffer.slice();
+                //verify separator positions
+                opts.postFormat(bufClone, 0, true, opts);
+                if (bufClone.join('') != maskedValue) return false;
+
+                var processValue = maskedValue.replace(opts.prefix, "");
+                processValue = processValue.replace(opts.suffix, "");
+                processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
+                processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
                 return isFinite(processValue);
                 return isFinite(processValue);
-            },
+            },
             onBeforeMask: function (initialValue, opts) {
             onBeforeMask: function (initialValue, opts) {
                 if (isFinite(initialValue)) {
                 if (isFinite(initialValue)) {
                     return initialValue.toString().replace(".", opts.radixPoint);
                     return initialValue.toString().replace(".", opts.radixPoint);
                 } else {
                 } else {
-                    var kommaMatches = initialValue.match(/,/g);
+                    var kommaMatches = initialValue.match(/,/g);
                     var dotMatches = initialValue.match(/\./g);
                     var dotMatches = initialValue.match(/\./g);
                     if (dotMatches && kommaMatches) {
                     if (dotMatches && kommaMatches) {
                         if (dotMatches.length > kommaMatches.length) {
                         if (dotMatches.length > kommaMatches.length) {
@@ -232,13 +232,13 @@ Optional extensions on the jquery.inputmask base
                     return initialValue;
                     return initialValue;
                 }
                 }
             }
             }
-        },
+        },
         'decimal': {
         'decimal': {
             alias: "numeric"
             alias: "numeric"
-        },
+        },
         'integer': {
         'integer': {
-            alias: "numeric",
+            alias: "numeric",
             digits: "0"
             digits: "0"
         }
         }
     });
     });
-})(jQuery);
+})(jQuery);