ソースを参照

#564 added postProcessOnBlur

Robin Herbots 11 年 前
コミット
90f538d5fe

+ 7 - 0
README.md

@@ -674,6 +674,13 @@ $(selector).inputmask("Regex", {
 	}
 });
 ```
+
+#### postProcessOnBlur
+
+This is a hook todo some postprocessing of the value on the blur event, this overrides the clearOptionalTail functionality
+Args => tmpBuffer, opts
+Manipulations in the tmpBuffer are written to input element
+
 ##Functions
 
 #### mask

+ 8 - 3
dist/inputmask/jquery.inputmask.js

@@ -602,8 +602,12 @@
             } : bl;
         }
         function clearOptionalTail(input) {
-            for (var buffer = getBuffer(), tmpBuffer = buffer.slice(), rl = determineLastRequiredPosition(), lmib = tmpBuffer.length - 1; lmib > rl && !isMask(lmib); lmib--) ;
-            tmpBuffer.splice(rl, lmib + 1 - rl), writeBuffer(input, tmpBuffer);
+            var buffer = getBuffer(), tmpBuffer = buffer.slice();
+            if ($.isFunction(opts.postProcessOnBlur)) opts.postProcessOnBlur.call(input, tmpBuffer, opts); else {
+                for (var rl = determineLastRequiredPosition(), lmib = tmpBuffer.length - 1; lmib > rl && !isMask(lmib); lmib--) ;
+                tmpBuffer.splice(rl, lmib + 1 - rl);
+            }
+            writeBuffer(input, tmpBuffer);
         }
         function isComplete(buffer) {
             if ($.isFunction(opts.isComplete)) return opts.isComplete.call($el, buffer, opts);
@@ -1061,7 +1065,8 @@
                     }
                 },
                 ignorables: [ 8, 9, 13, 19, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 93, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123 ],
-                isComplete: void 0
+                isComplete: void 0,
+                postProcessOnBlur: void 0
             },
             keyCode: {
                 ALT: 18,

+ 4 - 0
dist/inputmask/jquery.inputmask.numeric.extensions.js

@@ -90,6 +90,10 @@
                     return rslt.caret = rslt.pos + 1, rslt;
                 }
             },
+            postProcessOnBlur: function(tmpBuffer, opts) {
+                var tmpBufSplit = "" != opts.radixPoint ? tmpBuffer.join("").split(opts.radixPoint) : [ tmpBuffer.join("") ], matchRslt = tmpBufSplit[0].match(opts.regex.integerPart(opts)), matchRsltDigits = 2 == tmpBufSplit.length ? tmpBufSplit[1].match(opts.regex.integerNPart(opts)) : void 0;
+                matchRslt && "-0" == matchRslt[matchRslt.index] && (void 0 == matchRsltDigits || matchRsltDigits[matchRsltDigits.index].match(/^0+$/)) && tmpBuffer.splice(0, 1);
+            },
             regex: {
                 integerPart: function() {
                     return new RegExp("[-+]?\\d+");

+ 12 - 3
dist/jquery.inputmask.bundle.js

@@ -600,8 +600,12 @@
             } : bl;
         }
         function clearOptionalTail(input) {
-            for (var buffer = getBuffer(), tmpBuffer = buffer.slice(), rl = determineLastRequiredPosition(), lmib = tmpBuffer.length - 1; lmib > rl && !isMask(lmib); lmib--) ;
-            tmpBuffer.splice(rl, lmib + 1 - rl), writeBuffer(input, tmpBuffer);
+            var buffer = getBuffer(), tmpBuffer = buffer.slice();
+            if ($.isFunction(opts.postProcessOnBlur)) opts.postProcessOnBlur.call(input, tmpBuffer, opts); else {
+                for (var rl = determineLastRequiredPosition(), lmib = tmpBuffer.length - 1; lmib > rl && !isMask(lmib); lmib--) ;
+                tmpBuffer.splice(rl, lmib + 1 - rl);
+            }
+            writeBuffer(input, tmpBuffer);
         }
         function isComplete(buffer) {
             if ($.isFunction(opts.isComplete)) return opts.isComplete.call($el, buffer, opts);
@@ -1059,7 +1063,8 @@
                     }
                 },
                 ignorables: [ 8, 9, 13, 19, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 93, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123 ],
-                isComplete: void 0
+                isComplete: void 0,
+                postProcessOnBlur: void 0
             },
             keyCode: {
                 ALT: 18,
@@ -1830,6 +1835,10 @@
                     return rslt.caret = rslt.pos + 1, rslt;
                 }
             },
+            postProcessOnBlur: function(tmpBuffer, opts) {
+                var tmpBufSplit = "" != opts.radixPoint ? tmpBuffer.join("").split(opts.radixPoint) : [ tmpBuffer.join("") ], matchRslt = tmpBufSplit[0].match(opts.regex.integerPart(opts)), matchRsltDigits = 2 == tmpBufSplit.length ? tmpBufSplit[1].match(opts.regex.integerNPart(opts)) : void 0;
+                matchRslt && "-0" == matchRslt[matchRslt.index] && (void 0 == matchRsltDigits || matchRsltDigits[matchRsltDigits.index].match(/^0+$/)) && tmpBuffer.splice(0, 1);
+            },
             regex: {
                 integerPart: function() {
                     return new RegExp("[-+]?\\d+");

ファイルの差分が大きいため隠しています
+ 2 - 2
dist/jquery.inputmask.bundle.min.js


+ 10 - 5
js/jquery.inputmask.js

@@ -1084,11 +1084,15 @@
             }
             function clearOptionalTail(input) {
                 var buffer = getBuffer(), tmpBuffer = buffer.slice();
-                var rl = determineLastRequiredPosition(), lmib = tmpBuffer.length - 1;
-                for (; lmib > rl; lmib--) {
-                    if (isMask(lmib)) break;
+                if ($.isFunction(opts.postProcessOnBlur))
+                    opts.postProcessOnBlur.call(input, tmpBuffer, opts);
+                else {
+                    var rl = determineLastRequiredPosition(), lmib = tmpBuffer.length - 1;
+                    for (; lmib > rl; lmib--) {
+                        if (isMask(lmib)) break;
+                    }
+                    tmpBuffer.splice(rl, lmib + 1 - rl);
                 }
-                tmpBuffer.splice(rl, lmib + 1 - rl);
                 writeBuffer(input, tmpBuffer);
             }
             function isComplete(buffer) { //return true / false / undefined (repeat *)
@@ -1934,7 +1938,8 @@
                 },
                 //specify keyCodes which should not be considered in the keypress event, otherwise the preventDefault will stop their default behavior especially in FF
                 ignorables: [8, 9, 13, 19, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 93, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123],
-                isComplete: undefined //override for isComplete - args => buffer, opts - return true || false
+                isComplete: undefined, //override for isComplete - args => buffer, opts - return true || false
+                postProcessOnBlur: undefined //do some postprocessing of the value on the blur event, this overrides the clearOptionalTail functionality, args => tmpBuffer, opts
             },
             keyCode: {
                 ALT: 18, BACKSPACE: 8, CAPS_LOCK: 20, COMMA: 188, COMMAND: 91, COMMAND_LEFT: 91, COMMAND_RIGHT: 93, CONTROL: 17, DELETE: 46, DOWN: 40, END: 35, ENTER: 13, ESCAPE: 27, HOME: 36, INSERT: 45, LEFT: 37, MENU: 93, NUMPAD_ADD: 107, NUMPAD_DECIMAL: 110, NUMPAD_DIVIDE: 111, NUMPAD_ENTER: 108,

+ 8 - 0
js/jquery.inputmask.numeric.extensions.js

@@ -125,6 +125,14 @@ Optional extensions on the jquery.inputmask base
                     return rslt;
                 }
             },
+            postProcessOnBlur: function (tmpBuffer, opts) {
+                var tmpBufSplit = opts.radixPoint != "" ? tmpBuffer.join('').split(opts.radixPoint) : [tmpBuffer.join('')],
+                    matchRslt = tmpBufSplit[0].match(opts.regex.integerPart(opts)),
+                    matchRsltDigits = tmpBufSplit.length == 2 ? tmpBufSplit[1].match(opts.regex.integerNPart(opts)) : undefined;
+                if (matchRslt && matchRslt[matchRslt.index] == "-0" && (matchRsltDigits == undefined || matchRsltDigits[matchRsltDigits.index].match(/^0+$/))) {
+                    tmpBuffer.splice(0, 1);
+                }
+            },
             regex: {
                 integerPart: function (opts) { return new RegExp('[-\+]?\\d+'); },
                 integerNPart: function (opts) { return new RegExp('\\d+'); }

+ 20 - 1
qunit/tests_numeric.js

@@ -744,7 +744,7 @@ test("inputmask(\"integer\") - -0 - laxmikantG", function () {
 
     $("#testmask")[0].focus();
     $("#testmask").Type("-0");
-
+    $("#testmask").blur();
     equal($("#testmask").val(), "", "Result " + $("#testmask").val());
     $("#testmask").remove();
 });
@@ -777,4 +777,23 @@ test("inputmask(\"decimal\") - val(\"-5000,77\"); - ManRueda", function () {
 
     equal($("#testmask").val(), "-5.000,77", "Result " + $("#testmask").val());
     $("#testmask").remove();
+});
+
+test("inputmask(\"decimal\") - -0 - ManRueda", function () {
+    var $fixture = $("#qunit-fixture");
+    $fixture.append('<input type="text" id="testmask" />');
+    $("#testmask").inputmask('decimal', {
+        integerDigits: 10,
+        groupSeparator: '.',
+        autoGroup: true,
+        digits: 2,
+        radixPoint: ',',
+        groupSize: 3
+    });
+
+    $("#testmask").val("-0");
+    $("#testmask").blur();
+
+    equal($("#testmask").val(), "0", "Result " + $("#testmask").val());
+    $("#testmask").remove();
 });