Aug 13 2009

New changed event

Category: Javascript,Mootoolsfabiomcosta @ 00:02

Have you ever had problems with the ‘change’ event? I did, in particular on text-inputs.

I’ll list the crazy behaviors i don’t like about it:

  • If you change the value of a text input and submit the actual form by pressing enter, on Opera the change event won’t fire!;
  • If you apply keydown, keypress or keyup and any of them returns false (this is used on meioMask and most of the plugins that filters the input from the user), the change event won’t fire!;
  • If you focused on a text input and some script you made changes the value from this input, when you blur it the change even’t won’t fire!;
  • On IE, if you choose one of the native auto-complete options that is shown when you focus without typing anything, change event won’t fire!

That’s why i created this new glossy ‘changed’ event, which is the change event but fixing all these problems i listed. It uses the power of the Mootools custom events to create this new event that can be used like any other event.

I’ve created a page to show it working.

/**
 * @author Fábio Miranda Costa <fabiomcosta [at] gmail [dot] com>
 * 09-07-2009
 * http://www.meiocodigo.com
 * Changed Event for Mootools 1.2.x
 */</p>

<p>(function(){</p>

<pre><code>var $ = document.id || $;
var STORAGE_VALUE = 'changed-event-value-storage';
var lastFocused = null;
var focusFunc = function(){
    lastFocused = this;
    this.store(STORAGE_VALUE, this.get('value'));
};
var submitFunc = function(e){
    if(check.call(lastFocused)){
        lastFocused.fireEvent('changed');
    }
};
var check = function(e){
    var storedValue = this.retrieve(STORAGE_VALUE);
    // this happens when you focus the input before adding the changed event on the input
    if(storedValue === null) return false;
    return this.value !== storedValue;
};

Element.Events.changed = {
    base: 'blur',
    onAdd: function(){
        var evts = this.retrieve('events');
        if(!(evts &amp;&amp; evts.focus &amp;&amp; evts.focus.keys.contains(focusFunc))){
            this.addEvent('focus', focusFunc);
            var formEl = $(this.form), fevts = formEl.retrieve('events');
            if(!(fevts &amp;&amp; fevts.submit &amp;&amp; fevts.submit.keys.contains(submitFunc))){
                formEl.addEvent('submit', submitFunc);
            }
        }
    },
    condition: function(e){
        e.type = 'changed';
        return check.call(this, e);
    },
    onRemove: function(){
        var evts = this.retrieve('events');
        if(!evts.changed.keys.length){
            this.removeEvent('focus', focusFunc);
            if(lastFocused == this) lastFocused = null;
            if(!submitInputs.length){
                $(this.form).removeEvent('submit', submitFunc);
            }
        }   
    }
};
</code></pre>

<p>})();

I’m sorry about the entities errors on the code, its this f…. s…. blog system that simply can’t do it right. If anybody know how to solve this, please let me know.

It won’t hurt if i remember that it should only be used on text inputs (it wont work as expected on radiobuttons and checkboxes) and you should apply the event before you focus the element (its kind of obvious but as i said, it won’t hurt).

Did i tell you that Mootools rocks? ;)


Jun 29 2009

meioMask 1.1.3 version released!

Category: Javascript,jQueryfabiomcosta @ 20:49

meioMask 1.1.3 is out! sorry for this little interval between the versions but the last one would break on IE on a certain situation, explained below on the changelog.

Everyone that has 1.1.2 working ok is hardly encouraged to change to 1.1.3.

meioMask for jQuery

meioMask for Mootools!

meioMask’s page at jquery.com

meioMask’s Git project page

Features

  • Accepts paste event;
  • Has fixed, reverse (currency) and repeat mask types;
  • You can still use your hot keys and others (ex: ctrl+t, ctrl+f5, TAB …);
  • Supports metadata plugin;
  • Works with iPhone;
  • Allow default values;
  • Has callbacks for invalid inputs, valid and overflow;
  • Has function to mask strings;
  • Support for positive and negative numbers on reverse masks;
  • Can auto-focus the next form element when the current input is completely filled.

Changelog

v1.1.3

  • a minor bug on _keyPressReverse that may break 1.1.2 on ie when a char is typed in a text input that had its content selected by any range;
  • FIxed the onchange event on ie in masks that have type fixed.

v1.1.2

  • Set defaultValue property of the input. This fixes the behavior of the reset button on forms. Now the value will be reseted to the masked value;
  • âêô were added to the ‘@’ rule;
  • Fix for the auto-tab feature, it now focus just on visible form elements;
  • Added setSize option. It sets the input size based on the length of the mask (work with fixed and reverse masks only).

v1.1.1

  • Fixed caret bug on ‘repeat’ masks;
  • Fixed keyup event on fixed masks, it is now firing propertly;
  • Added selectCharsOnFocus option;
  • Added textAlign option.

v1.1

  • Mask type ‘infinite’ is now called ‘repeat’ (using ‘infinite’ still works but it is deprecated). It now allows a maxLenght value to be set. MaxLength can be set by the maxLength attribute of the element or the maxLength option;
  • You can easily set an autoTab option that will focus the next form element when the masked input is totally filled. It is true by default but you can put a jQuery selector string to match the next element you want to be focused.
  • Deprecated ‘unmaskVal’ function. This function is too buggy… works for most cases but not all. The best way to unmask a value is by doing it yourself;
  • The fixedChars option is not global anymore, giving more flexibility for the masks;
  • ‘phone-us’ mask is now ‘(999) 999-9999′;
  • Correctly fires the onChange event on reverse masked inputs.

You can see the plugin page here. It contains documentation and examples. Please tell me any bug, new feature, english errors on documentation…. anything! I’ll be glad to hear your feedback and make the fixes. Hope it helps you!


Jun 28 2009

meioMask 1.1.2 version released!

Category: Javascript,jQueryfabiomcosta @ 16:01

meioMask 1.1.2 is out! It basically adds some sugar to the 1.1.1 version. See the changelog on this post for more info.

Added 1 new options:

  • setSize (default=false): sets the input size based on the length of the mask (work with fixed and reverse masks only).

Everyone that has 1.1.1 working ok is encouraged to change to 1.1.2.

meioMask for jQuery

meioMask for Mootools!

meioMask’s page at jquery.com

meioMask’s Git project page

Features

  • Accepts paste event;
  • Haves fixed, reverse (currency) and repeat mask types;
  • You can still use your hot keys and others (ex: ctrl+t, ctrl+f5, TAB …);
  • Supports metadata plugin;
  • Works with iPhone;
  • Allow default values;
  • Haves callbacks for invalid inputs, valid and overflow;
  • Haves function to mask strings;
  • Support for positive and negative numbers on reverse masks;
  • Can auto-focus the next form element when the current input is completely filled.

Changelog

v1.1.2

  • Set defaultValue property of the input. This fixes the behavior of the reset button on forms. Now the value will be reseted to the masked value;
  • âêô were added to the ‘@’ rule;
  • Fix for the auto-tab feature, it now focus just on visible form elements;
  • Added setSize option. It sets the input size based on the length of the mask (work with fixed and reverse masks only).

v1.1.1

  • Fixed caret bug on ‘repeat’ masks;
  • Fixed keyup event on fixed masks, it is now firing propertly;
  • Added selectCharsOnFocus option;
  • Added textAlign option.

v1.1

  • Mask type ‘infinite’ is now called ‘repeat’ (using ‘infinite’ still works but it is deprecated). It now allows a maxLenght value to be set. MaxLength can be set by the maxLength attribute of the element or the maxLength option;
  • You can easily set an autoTab option that will focus the next form element when the masked input is totally filled. It is true by default but you can put a jQuery selector string to match the next element you want to be focused.
  • Deprecated ‘unmaskVal’ function. This function is too buggy… works for most cases but not all. The best way to unmask a value is by doing it yourself;
  • The fixedChars option is not global anymore, giving more flexibility for the masks;
  • ‘phone-us’ mask is now ‘(999) 999-9999′;
  • Correctly fires the onChange event on reverse masked inputs.

You can see the plugin page here. It contains documentation and examples. Please tell me any bug, new feature, english errors on documentation…. anything! I’ll be glad to hear your feedback and make the fixes. Hope it helps you!


Jun 25 2009

MeioCódigo now in Twitter

Category: Uncategorizedvbmendes @ 21:26

We have created a profile for MeioCódigo in Twitter. We will twitt all our blog posts and something related to the blog’s topics. If you like MeioCódigo, start following our twitter.


Jun 08 2009

MeioUpload project created at github

Category: CakePHPvbmendes @ 10:02

Juan Basso created a project hosted at github. I am unable to keep working on this behavior an he is making some updates to the code. I think github is a great tool because you can simply fork the project and make your own changes. Go check his work: http://github.com/jrbasso/MeioUpload/tree/master

Tags: , , , ,


May 03 2009

meioMask 1.1.1 version released!

Category: Uncategorizedfabiomcosta @ 16:59

meioMask 1.1.1 s out!

It doesn’t change a lot of things, but i needed to realease it since there a little bug on the fixed and ‘repeat’ mask (old ‘infinite’). Basically the caret was buggy on ‘repeat’ inputs that accepts more characters than it should (i don’t know how to explain this). Fixed masks weren’t firing the onkeyup event properly, causing little bugs, but it is now ok.

Added 2 new options:

  • selectCharsOnFocus (default=true): will select all the chars from the input when you focus it;
  • textAlign (default=true): will let you control if you want to apply text-align or not on the input. This is usefull if you don’t want to align-right the inputs on reversed masks;

To end changes, the project is being hosted now at github, witch is kind of better than the old one.

Everyone that has 1.1 working ok is encouraged to change to 1.1.1.

meioMask for jQuery

meioMask for Mootools!

meioMask’s page at jquery.com

meioMask’s Git project page

Features

  • Accepts paste event;
  • Haves fixed, reverse (currency) and repeat mask types;
  • You can still use your hot keys and others (ex: ctrl+t, ctrl+f5, TAB …);
  • Supports metadata plugin;
  • Works with iPhone;
  • Allow default values;
  • Haves callbacks for invalid inputs, valid and overflow;
  • Haves function to mask strings;
  • Support for positive and negative numbers on reverse masks;
  • Can auto-focus the next form element when the current input is completely filled.

Changelog

v1.1.1

  • Fixed caret bug on ‘repeat’ masks;
  • Fixed keyup event on fixed masks, it is now firing propertly;
  • Added selectCharsOnFocus option;
  • Added textAlign option.

v1.1

  • Mask type ‘infinite’ is now called ‘repeat’ (using ‘infinite’ still works but it is deprecated). It now allows a maxLenght value to be set. MaxLength can be set by the maxLength attribute of the element or the maxLength option;
  • You can easily set an autoTab option that will focus the next form element when the masked input is totally filled. It is true by default but you can put a jQuery selector string to match the next element you want to be focused.
  • Deprecated ‘unmaskVal’ function. This function is too buggy… works for most cases but not all. The best way to unmask a value is by doing it yourself;
  • The fixedChars option is not global anymore, giving more flexibility for the masks;
  • ‘phone-us’ mask is now ‘(999) 999-9999′;
  • Correctly fires the onChange event on reverse masked inputs.

You can see the plugin page here. It contains documentation and examples. Please tell me any bug, new feature, english errors on documentation…. anything! I’ll be glad to hear your feedback and make the fixes. Hope it helps you!


« Previous PageNext Page »