浏览代码

reintroduce radixDance

Robin Herbots 7 年之前
父节点
当前提交
928e050eff
共有 2 个文件被更改,包括 20 次插入10 次删除
  1. 16 6
      js/inputmask.js
  2. 4 4
      js/inputmask.numeric.extensions.js

+ 16 - 6
js/inputmask.js

@@ -86,7 +86,7 @@
                 undoOnEscape: true, //pressing escape reverts the value to the value before focus
                 //numeric basic properties
                 radixPoint: "", //".", // | ","
-                radixPointDefinitionSymbol: undefined, //set the radixPoint definitionSymbol ~ used for awareness of the radixpoint
+                _radixDance: false, //dance around the radixPoint
                 groupSeparator: "", //",", // | "."
                 //numeric basic properties
                 keepStatic: null, //try to keep the mask static while typing. Decisions to alter the mask will be posponed if possible - null see auto selection for multi masks
@@ -918,7 +918,7 @@
             function determineTestTemplate(pos, tests, guessNextBest) {
                 pos = pos > 0 ? pos - 1 : 0;
                 var testPos,
-                    altTest = getTest(pos),
+                    altTest = getTest(pos, tests),
                     altArr = (altTest.alternation !== undefined) ? altTest.locator[altTest.alternation].toString().split(",") : [];
                 for (var ndx = 0; ndx < tests.length; ndx++) {
                     testPos = tests[ndx];
@@ -979,11 +979,11 @@
                 return getMaskSet().validPositions[pos] || determineTestTemplate(pos, getTests(pos, ndxIntlzr ? ndxIntlzr.slice() : ndxIntlzr, tstPs));
             }
 
-            function getTest(pos) {
+            function getTest(pos, tests) {
                 if (getMaskSet().validPositions[pos]) {
                     return getMaskSet().validPositions[pos];
                 }
-                return getTests(pos)[0];
+                return (tests || getTests(pos))[0];
             }
 
             function positionCanMatchDefinition(pos, def) {
@@ -2050,7 +2050,17 @@
                                     begin: ndx,
                                     end: ndx
                                 } : caret(input),
-                                forwardPosition, c = String.fromCharCode(k);
+                                forwardPosition, c = String.fromCharCode(k), offset = 0;
+
+                            if (opts._radixDance && opts.numericInput) {
+                                var caretPos = getBuffer().indexOf(opts.radixPoint.charAt(0)) + 1;
+                                if (pos.begin <= caretPos) {
+                                    if (k === opts.radixPoint.charCodeAt(0)) offset = 1;
+                                    pos.begin -= 1;
+                                    pos.end -= 1;
+                                }
+                            }
+
 
                             getMaskSet().writeOutBuffer = true;
                             var valResult = isValid(pos, c, strict);
@@ -2061,7 +2071,7 @@
                             }
 
 
-                            forwardPosition = (opts.numericInput && valResult.caret === undefined) ? seekPrevious(forwardPosition) : forwardPosition;
+                            forwardPosition = ((opts.numericInput && valResult.caret === undefined) ? seekPrevious(forwardPosition) : forwardPosition) + offset;
                             if (writeOut !== false) {
                                 setTimeout(function () {
                                     opts.onKeyValidation.call(input, k, valResult, opts);

+ 4 - 4
js/inputmask.numeric.extensions.js

@@ -91,14 +91,14 @@
                     mask += "~{1," + opts.integerDigits + "}";
                 } else mask += "~{" + opts.integerDigits + "}";
                 if (opts.digits !== undefined) {
-                    opts.radixPointDefinitionSymbol = opts.decimalProtect ? ":" : opts.radixPoint;
+                    var radixDef = opts.decimalProtect ? ":" : opts.radixPoint;
                     var dq = opts.digits.toString().split(",");
                     if (isFinite(dq[0] && dq[1] && isFinite(dq[1]))) {
-                        mask += opts.radixPointDefinitionSymbol + ";{" + opts.digits + "}";
+                        mask += radixDef + ";{" + opts.digits + "}";
                     } else if (isNaN(opts.digits) || parseInt(opts.digits) > 0) {
                         if (opts.digitsOptional) {
-                            mask += "[" + opts.radixPointDefinitionSymbol + ";{1," + opts.digits + "}]";
-                        } else mask += opts.radixPointDefinitionSymbol + ";{" + opts.digits + "}";
+                            mask += "[" + radixDef + ";{1," + opts.digits + "}]";
+                        } else mask += radixDef + ";{" + opts.digits + "}";
                     }
                 }
                 mask += autoEscape(opts.suffix, opts);