r2365 - Implemented applying mask to a non-input, allowPartial and 'apply' method.

r2365 - Implemented applying mask to a non-input, allowPartial and 'apply' method.


Author: powella
Date: Mon Mar 23 15:04:39 2009
New Revision: 2365
Modified:
branches/dev/mask/ui/ui.mask.js
Log:
Implemented applying mask to a non-input, allowPartial and 'apply' method.
Modified: branches/dev/mask/ui/ui.mask.js
==============================================================================
--- branches/dev/mask/ui/ui.mask.js    (original)
+++ branches/dev/mask/ui/ui.mask.js    Mon Mar 23 15:04:39 2009
@@ -34,6 +34,9 @@
            firstNonMaskPos = null,
            len = mask.length,
            caret = function(begin, end){ return $.ui.mask.caret(input, begin,
end); };
+
+        //if we're applying the mask to an element which is not an input, it
won't have a val() method. fake one for our purposes.
+        if(!input.is(':input')) input.val = input.html;
            
        $.each(mask.split(""), function(i, c) {
            if (c == '?') {
@@ -141,7 +144,6 @@
            return false;
        };
-
        function clearBuffer(start, end) {
            for (var i = start; i < end && i < len; i++) {
                if (tests[i])
@@ -174,8 +176,13 @@
                }
            }
            if (!allow && lastMatch + 1 < partialPosition) {
-                input.val("");
-                clearBuffer(0, len);
+                if(!opts.allowPartials){
+                    input.val("");
+                    clearBuffer(0, len);
+                }
+                else //if we're allowing partial input/inital values, and the element
we're masking isnt an input, then we need to allow the mask to apply.
+                    if(!input.is(':input')) writeBuffer();
+                    
            } else if (allow || lastMatch + 1 >= partialPosition) {
                writeBuffer();
                if (!allow) input.val(input.val().substring(0, lastMatch + 1));
@@ -209,6 +216,11 @@
                    if (input.val() != focusText)
                        input.change();
                })
+                .bind('apply.mask', function(){ //changing the value of an input
without keyboard input requires re-applying the mask.
+                    focusText = input.val();
+                    var pos = checkVal();
+                    writeBuffer();                    
+                })                
                .bind("keydown.mask", keydownEvent)
                .bind("keypress.mask", keypressEvent)
                .bind(pasteEventName, function() {
@@ -224,15 +236,21 @@
    },
    value: function() {
-        var input = this.element, tests = input.data("tests");
-        return $.map(input.data("buffer"), function(c, i){
-            return tests[i] ? c : null;
-        })
-        .join('');
+        var input = this.element,
+            tests = input.data("tests"),
+            res = $.map(input.data("buffer"), function(c, i){ return tests[i] ? c :
null; }).join(''),
+            r = new RegExp('\\'+this.options.placeholder, 'gi');
+        return res.replace(r, '');
    },
    
    formatted: function(){
-        return this.element.val().replace(this.options.placeholder, '');
+        var r = new RegExp('\\'+this.options.placeholder, 'gi'),
+            res = this.element.val();
+        return res.replace(r, '');
+    },
+    
+    apply: function(){
+        this.element.trigger('apply.mask');
    }
});
@@ -242,7 +260,8 @@
    defaults: {
        mask: '',
        placeholder: '_',
-        completed: null
+        completed: null,
+        allowPartials: false
    },
    //Predefined character definitions
    definitions: {