Browse Source

Some fixes in the events = focus vs blur

Robin Herbots 15 years ago
parent
commit
575f503b38
1 changed files with 23 additions and 30 deletions
  1. 23 30
      jquery.inputmask.js

+ 23 - 30
jquery.inputmask.js

@@ -3,7 +3,7 @@ Input Mask plugin for jquery
 http://github.com/RobinHerbots/jquery.inputmask
 http://github.com/RobinHerbots/jquery.inputmask
 Copyright (c) 2010 Robin Herbots
 Copyright (c) 2010 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 0.1.6
+Version: 0.1.8
    
    
 This plugin is based on the masked input plugin written by Josh Bush (digitalbush.com)
 This plugin is based on the masked input plugin written by Josh Bush (digitalbush.com)
 */
 */
@@ -64,19 +64,19 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
 
 
     $.fn.inputmask = function(fn, options) {
     $.fn.inputmask = function(fn, options) {
         var opts = $.extend({}, $.inputmask.defaults, options);
         var opts = $.extend({}, $.inputmask.defaults, options);
-        var pasteEventName = $.browser.msie ? 'paste' : 'input';
+        var pasteEventName = $.browser.msie ? 'paste.inputmask' : 'input.inputmask';
         var iPhone = (window.orientation != undefined);
         var iPhone = (window.orientation != undefined);
 
 
         var _val = $.inputmask.val;
         var _val = $.inputmask.val;
         if (opts.patch_val && $.fn.val.inputmaskpatch != true) {
         if (opts.patch_val && $.fn.val.inputmaskpatch != true) {
             $.fn.val = function() {
             $.fn.val = function() {
                 if (opts.autounmask && arguments.length == 0) {
                 if (opts.autounmask && arguments.length == 0) {
-                    return $.isFunction(this.inputmask) ? this.inputmask('unmaskedvalue') : _val.apply(this, arguments); ;
+                    return this.data('inputmask') ? this.inputmask('unmaskedvalue') : _val.apply(this, arguments); ;
                 }
                 }
                 else {
                 else {
                     var result = _val.apply(this, arguments);
                     var result = _val.apply(this, arguments);
-                    if (arguments.length > 0 && $.isFunction(this.inputmask)) {
-                        this.trigger('blur');
+                    if (arguments.length > 0 && this.data('inputmask')) {
+                        this.triggerHandler('setvalue.inputmask');
                     }
                     }
                     return result;
                     return result;
                 }
                 }
@@ -254,7 +254,7 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
         //functionality fn
         //functionality fn
         function setvalue(el, value) {
         function setvalue(el, value) {
             _val.call(el, value);
             _val.call(el, value);
-            el.trigger('blur');
+            el.triggerHandler('setvalue.inputmask');
         }
         }
 
 
         function unmaskedvalue(el) {
         function unmaskedvalue(el) {
@@ -278,6 +278,7 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
             input.data('_buffer', _buffer);
             input.data('_buffer', _buffer);
             input.data('greedy', opts.greedy);
             input.data('greedy', opts.greedy);
             input.data('repeat', opts.repeat);
             input.data('repeat', opts.repeat);
+            input.data('inputmask', true);
 
 
             //init buffer
             //init buffer
             var buffer = _buffer.slice();
             var buffer = _buffer.slice();
@@ -286,39 +287,31 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
 
 
             //bind events
             //bind events
             if (!input.attr("readonly")) {
             if (!input.attr("readonly")) {
-                input.bind("focus", function() {
-                    input.addClass('focus');
-                    undoBuffer = _val.call(input);
-                    setTimeout(function() {
-                        var pos = checkVal(input, buffer, true);
-                        if (pos >= _buffer.length)
-                            caret(input, 0, pos);
-                        else
-                            caret(input, pos);
-                    }, 0);
-                }).bind("mouseenter", function() {
-                    if (!input.hasClass('focus') && _val.call(input).length == 0) {
+                input.bind("mouseenter.inputmask", function() {
+                    if (!input.hasClass('focus.inputmask') && _val.call(input).length == 0) {
                         buffer = _buffer.slice();
                         buffer = _buffer.slice();
                         writeBuffer(input, buffer);
                         writeBuffer(input, buffer);
                     }
                     }
-                }).bind("blur", function() {
-                    input.removeClass('focus');
-                    checkVal(input, buffer, true);
-                    if (_val.call(input) == _buffer.join('')) {
-                        _val.call(input, '');
-                    } else {
-                        if (_val.call(input) != undoBuffer)
-                            input.change();
+                }).bind("blur.inputmask", function() {
+                    if (_val.call(input) != undoBuffer) {
+                        checkVal(input, buffer, true);
+                        input.change();
                     }
                     }
-                }).bind("mouseleave", function() {
-                    if (!input.hasClass('focus') && _val.call(input) == _buffer.join(''))
+                }).bind("click.inputmask", function() {
+                    input.addClass('focus.inputmask');
+                    undoBuffer = _val.call(input);
+                    caret(input, checkVal(input, buffer, true));
+                }).bind("mouseleave.inputmask", function() {
+                    if (!input.hasClass('focus.inputmask') && _val.call(input) == _buffer.join(''))
                         _val.call(input, '');
                         _val.call(input, '');
-                }).bind("keydown", keydownEvent
-                ).bind("keypress", keypressEvent
+                }).bind("keydown.inputmask", keydownEvent
+                ).bind("keypress.inputmask", keypressEvent
                 ).bind(pasteEventName, function() {
                 ).bind(pasteEventName, function() {
                     setTimeout(function() {
                     setTimeout(function() {
                         caret(input, checkVal(input, buffer, true));
                         caret(input, checkVal(input, buffer, true));
                     }, 0);
                     }, 0);
+                }).bind('setvalue.inputmask', function() {
+                    checkVal(input, buffer, true);
                 });
                 });
 
 
             }
             }