Skip to content

Commit

Permalink
Updated formatting for the change/submit special event logic. Also sw…
Browse files Browse the repository at this point in the history
…itched the function declarations to statements. Thanks to Garrett for the recommendation.
  • Loading branch information
jeresig committed Feb 9, 2010
1 parent fec02aa commit 5267824
Showing 1 changed file with 84 additions and 83 deletions.
167 changes: 84 additions & 83 deletions src/event.js
Expand Up @@ -652,64 +652,66 @@ jQuery.each({
// submit delegation // submit delegation
if ( !jQuery.support.submitBubbles ) { if ( !jQuery.support.submitBubbles ) {


jQuery.event.special.submit = { jQuery.event.special.submit = {
setup: function( data, namespaces ) { setup: function( data, namespaces ) {
if ( this.nodeName.toLowerCase() !== "form" ) { if ( this.nodeName.toLowerCase() !== "form" ) {
jQuery.event.add(this, "click.specialSubmit", function( e ) { jQuery.event.add(this, "click.specialSubmit", function( e ) {
var elem = e.target, type = elem.type; var elem = e.target, type = elem.type;


if ( (type === "submit" || type === "image") && jQuery( elem ).closest("form").length ) { if ( (type === "submit" || type === "image") && jQuery( elem ).closest("form").length ) {
return trigger( "submit", this, arguments ); return trigger( "submit", this, arguments );
} }
}); });


jQuery.event.add(this, "keypress.specialSubmit", function( e ) { jQuery.event.add(this, "keypress.specialSubmit", function( e ) {
var elem = e.target, type = elem.type; var elem = e.target, type = elem.type;


if ( (type === "text" || type === "password") && jQuery( elem ).closest("form").length && e.keyCode === 13 ) { if ( (type === "text" || type === "password") && jQuery( elem ).closest("form").length && e.keyCode === 13 ) {
return trigger( "submit", this, arguments ); return trigger( "submit", this, arguments );
} }
}); });


} else { } else {
return false; return false;
} }
}, },


teardown: function( namespaces ) { teardown: function( namespaces ) {
jQuery.event.remove( this, "click.specialSubmit" ); jQuery.event.remove( this, "click.specialSubmit" );
jQuery.event.remove( this, "keypress.specialSubmit" ); jQuery.event.remove( this, "keypress.specialSubmit" );
} }
}; };


} }


// change delegation, happens here so we have bind. // change delegation, happens here so we have bind.
if ( !jQuery.support.changeBubbles ) { if ( !jQuery.support.changeBubbles ) {


var formElems = /textarea|input|select/i; var formElems = /textarea|input|select/i,


function getVal( elem ) { changeFilters,
var type = elem.type, val = elem.value;


if ( type === "radio" || type === "checkbox" ) { getVal = function( elem ) {
val = elem.checked; var type = elem.type, val = elem.value;


} else if ( type === "select-multiple" ) { if ( type === "radio" || type === "checkbox" ) {
val = elem.selectedIndex > -1 ? val = elem.checked;
jQuery.map( elem.options, function( elem ) {
return elem.selected;
}).join("-") :
"";


} else if ( elem.nodeName.toLowerCase() === "select" ) { } else if ( type === "select-multiple" ) {
val = elem.selectedIndex; val = elem.selectedIndex > -1 ?
} jQuery.map( elem.options, function( elem ) {
return elem.selected;
}).join("-") :
"";


return val; } else if ( elem.nodeName.toLowerCase() === "select" ) {
} val = elem.selectedIndex;
}

return val;
},


function testChange( e ) { testChange = function testChange( e ) {
var elem = e.target, data, val; var elem = e.target, data, val;


if ( !formElems.test( elem.nodeName ) || elem.readOnly ) { if ( !formElems.test( elem.nodeName ) || elem.readOnly ) {
Expand All @@ -732,60 +734,59 @@ function testChange( e ) {
e.type = "change"; e.type = "change";
return jQuery.event.trigger( e, arguments[1], elem ); return jQuery.event.trigger( e, arguments[1], elem );
} }
} };


jQuery.event.special.change = { jQuery.event.special.change = {
filters: { filters: {
focusout: testChange, focusout: testChange,


click: function( e ) { click: function( e ) {
var elem = e.target, type = elem.type; var elem = e.target, type = elem.type;


if ( type === "radio" || type === "checkbox" || elem.nodeName.toLowerCase() === "select" ) { if ( type === "radio" || type === "checkbox" || elem.nodeName.toLowerCase() === "select" ) {
return testChange.call( this, e ); return testChange.call( this, e );
} }
}, },

// Change has to be called before submit
// Keydown will be called before keypress, which is used in submit-event delegation
keydown: function( e ) {
var elem = e.target, type = elem.type;


// Change has to be called before submit if ( (e.keyCode === 13 && elem.nodeName.toLowerCase() !== "textarea") ||
// Keydown will be called before keypress, which is used in submit-event delegation (e.keyCode === 32 && (type === "checkbox" || type === "radio")) ||
keydown: function( e ) { type === "select-multiple" ) {
var elem = e.target, type = elem.type; return testChange.call( this, e );
}
},


if ( (e.keyCode === 13 && elem.nodeName.toLowerCase() !== "textarea") || // Beforeactivate happens also before the previous element is blurred
(e.keyCode === 32 && (type === "checkbox" || type === "radio")) || // with this event you can't trigger a change event, but you can store
type === "select-multiple" ) { // information/focus[in] is not needed anymore
return testChange.call( this, e ); beforeactivate: function( e ) {
var elem = e.target;
jQuery.data( elem, "_change_data", getVal(elem) );
} }
}, },


// Beforeactivate happens also before the previous element is blurred setup: function( data, namespaces ) {
// with this event you can't trigger a change event, but you can store for ( var type in changeFilters ) {
// information/focus[in] is not needed anymore jQuery.event.add( this, type + ".specialChange", changeFilters[type] );
beforeactivate: function( e ) { }
var elem = e.target;
jQuery.data( elem, "_change_data", getVal(elem) );
}
},


setup: function( data, namespaces ) { return formElems.test( this.nodeName );
for ( var type in changeFilters ) { },
jQuery.event.add( this, type + ".specialChange", changeFilters[type] );
}


return formElems.test( this.nodeName ); teardown: function( namespaces ) {
}, for ( var type in changeFilters ) {
jQuery.event.remove( this, type + ".specialChange", changeFilters[type] );
}


teardown: function( namespaces ) { return formElems.test( this.nodeName );
for ( var type in changeFilters ) {
jQuery.event.remove( this, type + ".specialChange", changeFilters[type] );
} }
};


return formElems.test( this.nodeName ); changeFilters = jQuery.event.special.change.filters;
}
};

var changeFilters = jQuery.event.special.change.filters;

} }


function trigger( type, elem, args ) { function trigger( type, elem, args ) {
Expand Down

2 comments on commit 5267824

@scottgonzalez
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't the teardown be reduced to a single line by just using the namespace?

jQuery.event.remove( this, ".specialSubmit" );

@jeresig
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call - fixed in 35c3790.

Please sign in to comment.