Organized the event functions.

This commit is contained in:
John Resig 2006-06-22 13:25:21 +00:00
parent 07040f8d03
commit 3cc1ba05f3

46
jquery/jquery.js vendored
View File

@ -692,17 +692,17 @@ $.map = function(a,f) {
return r; return r;
}; };
$.event = {}; $.event = {
// Bind an event to an element // Bind an event to an element
// Original by Dean Edwards // Original by Dean Edwards
$.event.add = function(element, type, handler) { add: function(element, type, handler) {
// For whatever reason, IE has trouble passing the window object // For whatever reason, IE has trouble passing the window object
// around, causing it to be cloned in the process // around, causing it to be cloned in the process
if ( $.browser == "msie" && element.setInterval != undefined ) if ( $.browser == "msie" && element.setInterval != undefined )
element = window; element = window;
if (!handler.$$guid) handler.$$guid = $.event.add.guid++; if (!handler.guid) handler.guid = $.event.guid++;
if (!element.events) element.events = {}; if (!element.events) element.events = {};
var handlers = element.events[type]; var handlers = element.events[type];
if (!handlers) { if (!handlers) {
@ -710,33 +710,33 @@ $.event.add = function(element, type, handler) {
if (element["on" + type]) if (element["on" + type])
handlers[0] = element["on" + type]; handlers[0] = element["on" + type];
} }
handlers[handler.$$guid] = handler; handlers[handler.guid] = handler;
element["on" + type] = $.event.handle; element["on" + type] = $.event.handle;
}; },
$.event.add.guid = 1; guid: 1,
// Detach an event or set of events from an element // Detach an event or set of events from an element
$.event.remove = function(element, type, handler) { remove: function(element, type, handler) {
if (element.events) if (element.events)
if (type && element.events[type]) if (type && element.events[type])
if ( handler ) if ( handler )
delete element.events[type][handler.$$guid]; delete element.events[type][handler.guid];
else else
for ( var i in element.events[type] ) for ( var i in element.events[type] )
delete element.events[type][i]; delete element.events[type][i];
else else
for ( var j in element.events ) for ( var j in element.events )
$.event.remove( element, j ); $.event.remove( element, j );
}; },
$.event.trigger = function(element,type,data) { trigger: function(element,type,data) {
data = data || [ $.event.fix({ type: type }) ]; data = data || [ $.event.fix({ type: type }) ];
if ( element && element["on" + type] ) if ( element && element["on" + type] )
element["on" + type].apply( element, data ); element["on" + type].apply( element, data );
}; },
$.event.handle = function(event) { handle: function(event) {
if ( !event && !window.event ) return; if ( !event && !window.event ) return;
var returnValue = true, handlers = []; var returnValue = true, handlers = [];
@ -747,8 +747,8 @@ $.event.handle = function(event) {
for ( var i = 0; i < handlers.length; i++ ) { for ( var i = 0; i < handlers.length; i++ ) {
if ( handlers[i].constructor == Function ) { if ( handlers[i].constructor == Function ) {
this.$$handleEvent = handlers[i]; this.handleEvent = handlers[i];
if (this.$$handleEvent(event) === false) { if (this.handleEvent(event) === false) {
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
returnValue = false; returnValue = false;
@ -756,18 +756,18 @@ $.event.handle = function(event) {
} }
} }
return returnValue; return returnValue;
}; },
$.event.fix = function(event) { fix: function(event) {
event.preventDefault = $.event.fix.preventDefault; event.preventDefault = function() {
event.stopPropagation = $.event.fix.stopPropagation;
return event;
};
$.event.fix.preventDefault = function() {
this.returnValue = false; this.returnValue = false;
}; };
$.event.fix.stopPropagation = function() { event.stopPropagation = function() {
this.cancelBubble = true; this.cancelBubble = true;
}; };
return event;
}
}