Organized the event functions.

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

146
jquery/jquery.js vendored
View File

@ -692,82 +692,82 @@ $.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) {
handlers = element.events[type] = {}; handlers = element.events[type] = {};
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
for ( var i in element.events[type] )
delete element.events[type][i];
else else
for ( var i in element.events[type] ) for ( var j in element.events )
delete element.events[type][i]; $.event.remove( element, j );
else },
for ( var j in element.events )
$.event.remove( element, j ); trigger: function(element,type,data) {
}; data = data || [ $.event.fix({ type: type }) ];
if ( element && element["on" + type] )
$.event.trigger = function(element,type,data) { element["on" + type].apply( element, data );
data = data || [ $.event.fix({ type: type }) ]; },
if ( element && element["on" + type] )
element["on" + type].apply( element, data ); handle: function(event) {
}; if ( !event && !window.event ) return;
$.event.handle = function(event) { var returnValue = true, handlers = [];
if ( !event && !window.event ) return; event = event || $.event.fix(window.event);
var returnValue = true, handlers = []; for ( var j in this.events[event.type] )
event = event || $.event.fix(window.event); handlers[handlers.length] = this.events[event.type][j];
for ( var j in this.events[event.type] ) for ( var i = 0; i < handlers.length; i++ ) {
handlers[handlers.length] = this.events[event.type][j]; if ( handlers[i].constructor == Function ) {
this.handleEvent = handlers[i];
for ( var i = 0; i < handlers.length; i++ ) { if (this.handleEvent(event) === false) {
if ( handlers[i].constructor == Function ) { event.preventDefault();
this.$$handleEvent = handlers[i]; event.stopPropagation();
if (this.$$handleEvent(event) === false) { returnValue = false;
event.preventDefault(); }
event.stopPropagation();
returnValue = false;
} }
} }
return returnValue;
},
fix: function(event) {
event.preventDefault = function() {
this.returnValue = false;
};
event.stopPropagation = function() {
this.cancelBubble = true;
};
return event;
} }
return returnValue;
};
$.event.fix = function(event) { }
event.preventDefault = $.event.fix.preventDefault;
event.stopPropagation = $.event.fix.stopPropagation;
return event;
};
$.event.fix.preventDefault = function() {
this.returnValue = false;
};
$.event.fix.stopPropagation = function() {
this.cancelBubble = true;
};