Updated formatting for the change/submit special event logic. Also switched the function declarations to statements. Thanks to Garrett for the recommendation.
This commit is contained in:
parent
fec02aa5cb
commit
5267824aa3
169
src/event.js
169
src/event.js
|
@ -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;
|
||||
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 );
|
||||
}
|
||||
});
|
||||
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;
|
||||
}
|
||||
|
||||
function testChange( e ) {
|
||||
return val;
|
||||
},
|
||||
|
||||
testChange = function testChange( e ) {
|
||||
var elem = e.target, data, val;
|
||||
|
||||
if ( !formElems.test( elem.nodeName ) || elem.readOnly ) {
|
||||
|
@ -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;
|
||||
|
||||
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) );
|
||||
}
|
||||
},
|
||||
|
||||
// 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 );
|
||||
setup: function( data, namespaces ) {
|
||||
for ( var type in changeFilters ) {
|
||||
jQuery.event.add( this, type + ".specialChange", changeFilters[type] );
|
||||
}
|
||||
|
||||
return formElems.test( this.nodeName );
|
||||
},
|
||||
|
||||
// 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) );
|
||||
teardown: function( namespaces ) {
|
||||
for ( var type in changeFilters ) {
|
||||
jQuery.event.remove( this, type + ".specialChange", changeFilters[type] );
|
||||
}
|
||||
|
||||
return formElems.test( this.nodeName );
|
||||
}
|
||||
},
|
||||
|
||||
setup: function( data, namespaces ) {
|
||||
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] );
|
||||
}
|
||||
|
||||
return formElems.test( this.nodeName );
|
||||
}
|
||||
};
|
||||
|
||||
var changeFilters = jQuery.event.special.change.filters;
|
||||
};
|
||||
|
||||
changeFilters = jQuery.event.special.change.filters;
|
||||
}
|
||||
|
||||
function trigger( type, elem, args ) {
|
||||
|
|
Loading…
Reference in a new issue