Skip to content

Commit

Permalink
event模块升级完毕
Browse files Browse the repository at this point in the history
  • Loading branch information
RubyLouvre committed Dec 19, 2012
1 parent d5ac5a5 commit 8083681
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 93 deletions.
3 changes: 1 addition & 2 deletions event.js
@@ -1,6 +1,5 @@

define("event", ["$node"],function( $ ){
//top.dispatchEvent ? ["$node"]: ["$event_fix"],
define("event", top.dispatchEvent ? ["$node"]: ["$node","$event_fix"],function( $ ){
var facade = $.event || ($.event = {
special: {}
});
Expand Down
181 changes: 90 additions & 91 deletions event_fix.js
@@ -1,112 +1,111 @@
//=========================================
// 事件补丁模块
//==========================================
define("event_fix", !!document.dispatchEvent, ["node"], function( $ ){
define("event_fix", !!document.dispatchEvent, ["mass"], function( $ ){
//模拟IE678的reset,submit,change的事件代理
var rformElems = /^(?:input|select|textarea)$/i
return $;
$.event = {
special: {
change: {
setup: function() {
if ( rformElems.test( this.nodeName ) ) {
// IE doesn't fire change on a check/radio until blur; trigger it on click
// after a propertychange. Eat the blur-change in special.change.handle.
// This still fires onchange a second time for check/radio after blur.
if ( this.type === "checkbox" || this.type === "radio" ) {
$( this ).bind( "propertychange._change", function( event ) {
if ( event.originalEvent.propertyName === "checked" ) {
this._just_changed = true;
}
});
$( this). bind("click._change", function( event ) {
if ( this._just_changed && !event.isTrigger ) {
this._just_changed = false;
}
// Allow triggered, simulated change events (#11500)
$.event.simulate( "change", this, event, true );
} );
}
return false;
}
// Delegated event; lazy-add a change handler on descendant inputs
$ (this).bind( "beforeactivate._change", function( e ) {
var elem = e.target;
if ( rformElems.test( elem.nodeName ) && !$._data( elem, "_change_attached" ) ) {
$( elem ).bind( "change._change", function( event ) {
if ( this.parentNode && !event.isSimulated && !event.isTrigger ) {
$.event.simulate( "change", this.parentNode, event, true );
}
$._data( elem, "_change_attached", true );
})
var facade = $.event = {
special: {}
};
var special = facade.special
special.change = {
setup: function() {
if ( rformElems.test( this.nodeName ) ) {
// IE doesn't fire change on a check/radio until blur; trigger it on click
// after a propertychange. Eat the blur-change in special.change.handle.
// This still fires onchange a second time for check/radio after blur.
if ( this.type === "checkbox" || this.type === "radio" ) {
$( this ).bind( "propertychange._change", function( event ) {
if ( event.originalEvent.propertyName === "checked" ) {
this._just_changed = true;
}
});
},
handle: function( event ) {
var elem = event.target;
// Swallow native change events from checkbox/radio, we already triggered them above
if ( this !== elem || event.isSimulated || event.isTrigger || (elem.type !== "radio" && elem.type !== "checkbox") ) {
return event.handleObj.handler.apply( this, arguments );
}
},
teardown: function() {
$.event.remove( this, "._change" );
return !rformElems.test( this.nodeName );
$( this). bind("click._change", function( event ) {
if ( this._just_changed && !event.isTrigger ) {
this._just_changed = false;
}
// Allow triggered, simulated change events (#11500)
facade.simulate( "change", this, event, true );
} );
}
},
submit: {
setup: function() {
// Only need this for delegated form submit events
if ( this.tagName === "FORM" ) {
return false;
}
// Lazy-add a submit handler when a descendant form may potentially be submitted
$( this).bind( "click._submit keypress._submit", function( e ) {
// Node name check avoids a VML-related crash in IE (#9807)
var elem = e.target,
form = /input|button/i.test(elem.tagName) ? elem.form : undefined;
if ( form && !$._data( form, "_submit_attached" ) ) {
$.event.bind( form,{
type: "submit._submit",
callback: function( event ) {
event._submit_bubble = true;
}
});
$._data( form, "_submit_attached", true );
return false;
}
// Delegated event; lazy-add a change handler on descendant inputs
$ (this).bind( "beforeactivate._change", function( e ) {
var elem = e.target;
if ( rformElems.test( elem.nodeName ) && !$._data( elem, "_change_attached" ) ) {
$( elem ).bind( "change._change", function( event ) {
if ( this.parentNode && !event.isSimulated && !event.isTrigger ) {
facade.simulate( "change", this.parentNode, event, true );
}
});
// return undefined since we don't need an event listener
},

postDispatch: function( event ) {
// If form was submitted by the user, bubble the event up the tree
if ( event._submit_bubble ) {
delete event._submit_bubble;
if ( this.parentNode && !event.isTrigger ) {
$.event.simulate( "submit", this.parentNode, event, true );
$._data( elem, "_change_attached", true );
})
}
});
},
handle: function( event ) {
var elem = event.target;
// Swallow native change events from checkbox/radio, we already triggered them above
if ( this !== elem || event.isSimulated || event.isTrigger || (elem.type !== "radio" && elem.type !== "checkbox") ) {
return event.handleObj.handler.apply( this, arguments );
}
},
teardown: function() {
facade.remove( this, "._change" );
return !rformElems.test( this.nodeName );
}
}
special.submit = {
setup: function() {
// Only need this for delegated form submit events
if ( this.tagName === "FORM" ) {
return false;
}
// Lazy-add a submit handler when a descendant form may potentially be submitted
$( this).bind( "click._submit keypress._submit", function( e ) {
// Node name check avoids a VML-related crash in IE (#9807)
var elem = e.target,
form = /input|button/i.test(elem.tagName) ? elem.form : undefined;
if ( form && !$._data( form, "_submit_attached" ) ) {
facade.bind( form,{
type: "submit._submit",
callback: function( event ) {
event._submit_bubble = true;
}
}
},

teardown: function() {
// Only need this for delegated form submit events
if ( this.tagName == "FORM" ) {
return false;
}
});
$._data( form, "_submit_attached", true );
}
});
// return undefined since we don't need an event listener
},

// Remove delegated handlers; cleanData eventually reaps submit handlers attached above
$.event.remove( this, "._submit" );
postDispatch: function( event ) {
// If form was submitted by the user, bubble the event up the tree
if ( event._submit_bubble ) {
delete event._submit_bubble;
if ( this.parentNode && !event.isTrigger ) {
facade.simulate( "submit", this.parentNode, event, true );
}
}
},

teardown: function() {
// Only need this for delegated form submit events
if ( this.tagName == "FORM" ) {
return false;
}

// Remove delegated handlers; cleanData eventually reaps submit handlers attached above
facade.remove( this, "._submit" );
}
}

})

/*
* input事件的支持情况:IE9+,chrome+, gecko2+, opera10+,safari+
* 2012.5.1 fix delegate BUG将submit与reset这两个适配器合而为一
* 2012.10.18 重构reset, change, submit的事件代理
* input事件的支持情况:IE9+,chrome+, gecko2+, opera10+,safari+
* 2012.5.1 fix delegate BUG将submit与reset这两个适配器合而为一
* 2012.10.18 重构reset, change, submit的事件代理
<!DOCTYPE HTML>
<html>
<head>
Expand Down Expand Up @@ -141,4 +140,4 @@ define("event_fix", !!document.dispatchEvent, ["node"], function( $ ){
</body>
</html>
*/
*/

0 comments on commit 8083681

Please sign in to comment.