Robin Herbots 11 years ago
parent
commit
72067ce16d
3 changed files with 517 additions and 442 deletions
  1. 20 15
      js/jquery.inputmask.js
  2. 25 2
      js/jquery.inputmask.phone.extensions.js
  3. 472 425
      qunit/tests.js

+ 20 - 15
js/jquery.inputmask.js

@@ -578,7 +578,7 @@
                 var position = pos;
                 var position = pos;
                 if (position <= 0) return 0;
                 if (position <= 0) return 0;
 
 
-                while (--position > 0 && !isMask(position) && (opts.nojumps !== true || opts.nojumpsThreshold > position)) {
+                while (--position > 0 && !isMask(position) /*&& (opts.nojumps !== true || opts.nojumpsThreshold > position)*/) {
                 };
                 };
                 return position;
                 return position;
             }
             }
@@ -690,7 +690,7 @@
                 } else {
                 } else {
                     if (!$(input).is(':visible')) {
                     if (!$(input).is(':visible')) {
                         var data = $(input).data('_inputmask') || {};
                         var data = $(input).data('_inputmask') || {};
-                        return data["caret"] || { begin: 0, end: 0 };
+                        return data["caret"] || { "begin": TranslatePosition(0), "end": TranslatePosition(0) };
                     }
                     }
                     if (npt.setSelectionRange) {
                     if (npt.setSelectionRange) {
                         begin = npt.selectionStart;
                         begin = npt.selectionStart;
@@ -1399,7 +1399,11 @@
 
 
             var activeMasksetIndex = 0,
             var activeMasksetIndex = 0,
                 elmasks = $.map(masksets, function (msk, ndx) {
                 elmasks = $.map(masksets, function (msk, ndx) {
-                    var elmask = $('<input type="text" value="' + $el.attr("value") + '" />')[0];
+                    var elMaskStr = '<input type="text" ';
+                    if ($el.attr("value")) elMaskStr += 'value="' + $el.attr("value") + '" ';
+                    if ($el.attr("dir")) elMaskStr += 'dir="' + $el.attr("dir") + '" ';
+                    elMaskStr += '/>';
+                    var elmask = $(elMaskStr)[0];
                     maskScope($.extend(true, {}, msk), opts, { "action": "mask", "el": elmask });
                     maskScope($.extend(true, {}, msk), opts, { "action": "mask", "el": elmask });
                     return elmask;
                     return elmask;
                 });
                 });
@@ -1410,14 +1414,15 @@
                     $.each(elmasks, function (ndx, lmsk) {
                     $.each(elmasks, function (ndx, lmsk) {
                         var data = $(lmsk).data('_inputmask');
                         var data = $(lmsk).data('_inputmask');
                         var maskset = data["maskset"];
                         var maskset = data["maskset"];
-                        var lastValidPosition = -1, positionCount = -1;
+                        var lastValidPosition = -1, positionCount = 0;
                         for (var posNdx in maskset["validPositions"]) {
                         for (var posNdx in maskset["validPositions"]) {
                             var psNdx = parseInt(posNdx);
                             var psNdx = parseInt(posNdx);
                             if (psNdx > lastValidPosition) lastValidPosition = psNdx;
                             if (psNdx > lastValidPosition) lastValidPosition = psNdx;
                             positionCount++;
                             positionCount++;
                         }
                         }
+                        //console.log(positionCount + " " + lastValidPosition);
                         if (positionCount >= lpc) {
                         if (positionCount >= lpc) {
-                            if ((lastValidPosition < lvp && positionCount == lpc) || positionCount > lpc) {
+                            if ((lastValidPosition > lvp && positionCount == lpc) || positionCount > lpc) {
                                 lvp = lastValidPosition;
                                 lvp = lastValidPosition;
                                 lpc = positionCount;
                                 lpc = positionCount;
                                 activeMasksetIndex = ndx;
                                 activeMasksetIndex = ndx;
@@ -1428,12 +1433,9 @@
                     if ($.isFunction(opts.determineActiveMasksetIndex)) activeMasksetIndex = opts.determineActiveMasksetIndex.call($el, eventType, elmasks);
                     if ($.isFunction(opts.determineActiveMasksetIndex)) activeMasksetIndex = opts.determineActiveMasksetIndex.call($el, eventType, elmasks);
                 }
                 }
 
 
-                if ($(el).val() == "") {
-                    $el.val(elmasks[activeMasksetIndex]._valueGet());
-                    mcaret(el, 0, $el.val().length);
-                }
                 if (["focus"].indexOf(eventType) == -1 && $el.val() != elmasks[activeMasksetIndex]._valueGet()) {
                 if (["focus"].indexOf(eventType) == -1 && $el.val() != elmasks[activeMasksetIndex]._valueGet()) {
-                    $el.val(elmasks[activeMasksetIndex]._valueGet());
+                    var value = $(elmasks[activeMasksetIndex]).val() == "" ? elmasks[activeMasksetIndex]._valueGet() : $(elmasks[activeMasksetIndex]).val();
+                    $el.val(value);
                 }
                 }
                 if (["blur", "focus"].indexOf(eventType) == -1) {
                 if (["blur", "focus"].indexOf(eventType) == -1) {
                     if ($(elmasks[activeMasksetIndex]).hasClass("focus.inputmask")) {
                     if ($(elmasks[activeMasksetIndex]).hasClass("focus.inputmask")) {
@@ -1464,7 +1466,7 @@
                     if (e.type == "keydown") {
                     if (e.type == "keydown") {
                         k = e.keyCode;
                         k = e.keyCode;
 
 
-                        if (k == opts.keyCode.BACKSPACE & lmnt._valueGet().length < caretPos.begin) {
+                        if (k == opts.keyCode.BACKSPACE && lmnt._valueGet().length < caretPos.begin) {
                             return;
                             return;
                         } else if (k == opts.keyCode.TAB) {
                         } else if (k == opts.keyCode.TAB) {
                             goDetermine = false;
                             goDetermine = false;
@@ -1484,9 +1486,10 @@
                         return;
                         return;
                     }
                     }
 
 
-                    if (["click", "keydown"].indexOf(e.type) != -1) {
+                    if (["click"].indexOf(e.type) != -1 || (["keydown"].indexOf(e.type) != -1 && caretPos.begin != caretPos.end)) {
                         mcaret(lmnt, caretPos.begin, caretPos.end);
                         mcaret(lmnt, caretPos.begin, caretPos.end);
                     }
                     }
+
                     $(lmnt).triggerHandler(e);
                     $(lmnt).triggerHandler(e);
                 });
                 });
                 if (goDetermine) {
                 if (goDetermine) {
@@ -1496,9 +1499,11 @@
                 }
                 }
             });
             });
 
 
-            setTimeout(function () {
-                determineActiveMask("init", elmasks);
-            }, 0);
+            if ($el.attr("value") != "") {
+                setTimeout(function () {
+                    determineActiveMask("init", elmasks);
+                }, 0);
+            }
         };
         };
 
 
         $.inputmask = {
         $.inputmask = {

+ 25 - 2
js/jquery.inputmask.phone.extensions.js

@@ -42,14 +42,37 @@ When using this extension make sure you specify the correct url to get the masks
                     }
                     }
                 });
                 });
 
 
-                //maskList.splice(0, 0, "+p(ppp)ppp-pppp");
+                maskList.splice(0, 0, "+p(ppp)ppp-pppp");
                 return maskList;
                 return maskList;
             },
             },
             nojumpsThreshold: 1
             nojumpsThreshold: 1
         },
         },
         'phonebe': {
         'phonebe': {
-            alias: "phone",
             url: "phone-codes/phone-be.json",
             url: "phone-codes/phone-be.json",
+            mask: function (opts) {
+                opts.definitions = {
+                    'p': {
+                        validator: function () { return false; },
+                        cardinality: 1
+                    },
+                    '#': {
+                        validator: "[0-9]",
+                        cardinality: 1
+                    }
+                };
+                var maskList = [];
+                $.ajax({
+                    url: opts.url,
+                    async: false,
+                    dataType: 'json',
+                    success: function (response) {
+                        maskList = response;
+                    }
+                });
+
+                maskList.splice(0, 0, "+32(ppp)ppp-pppp");
+                return maskList;
+            },
             nojumpsThreshold: 4
             nojumpsThreshold: 4
         }
         }
     });
     });

File diff suppressed because it is too large
+ 472 - 425
qunit/tests.js