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
if ( !jQuery.support.submitBubbles ) {

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

if ( (type === "submit" || type === "image") && jQuery( elem ).closest("form").length ) {
return trigger( "submit", this, arguments );
}
});
jQuery.event.special.submit = {
setup: function( data, namespaces ) {
if ( this.nodeName.toLowerCase() !== "form" ) {
jQuery.event.add(this, "click.specialSubmit", function( e ) {
var elem = e.target, type = elem.type;

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

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

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

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

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

}

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

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

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

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

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

} else if ( elem.nodeName.toLowerCase() === "select" ) {
val = elem.selectedIndex;
}
} else if ( type === "select-multiple" ) {
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;

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

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

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

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

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

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

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

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

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

return formElems.test( this.nodeName );
}
};

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

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

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.