Skip to content

Instantly share code, notes, and snippets.

@balupton
Created January 26, 2011 15:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save balupton/796871 to your computer and use it in GitHub Desktop.
Save balupton/796871 to your computer and use it in GitHub Desktop.
Bind and Trigger Custom and Native Events in Prototype
/**
* Bind and Trigger custom and native events in Prototype
* @author Juriy Zaytsev (kangax)
* @author Benjamin Lupton (balupton)
* @copyright MIT license
**/
(function(){
var eventMatchers = {
'HTMLEvents': /^(?:load|unload|abort|error|select|hashchange|popstate|change|submit|reset|focus|blur|resize|scroll)$/,
'MouseEvents': /^(?:click|mouse(?:down|up|over|move|out))$/
};
var defaultOptions = {
pointerX: 0,
pointerY: 0,
button: 0,
ctrlKey: false,
altKey: false,
shiftKey: false,
metaKey: false,
bubbles: true,
cancelable: true
};
Event.hasNativeEvent = function(element, eventName) {
var eventType = null;
element = $(element);
for (var name in eventMatchers) {
if ( eventMatchers[name].test(eventName) ) {
eventType = name;
break;
}
}
return eventType ? true : false;
};
Event.bind = function(element, eventName, eventHandler) {
element = $(element);
if ( Element.hasNativeEvent(element,eventName) ) {
return Element.observe(element,eventName,eventHandler);
}
else {
return Element.observe(element,'custom:'+eventName,eventHandler);
}
};
Event.simulate = function(element, eventName) {
var options = Object.extend(defaultOptions, arguments[2] || { });
var oEvent, eventType = null;
element = $(element);
for (var name in eventMatchers) {
if (eventMatchers[name].test(eventName)) { eventType = name; break; }
}
if ( !eventType ) {
return Element.fire(element,'custom:'+eventName);
}
if (document.createEvent) {
oEvent = document.createEvent(eventType);
if (eventType == 'HTMLEvents') {
oEvent.initEvent(eventName, options.bubbles, options.cancelable);
}
else {
oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
}
element.dispatchEvent(oEvent);
}
else {
options.clientX = options.pointerX;
options.clientY = options.pointerY;
oEvent = Object.extend(document.createEventObject(), options);
element.fireEvent('on' + eventName, oEvent);
}
return element;
};
Element.addMethods({
simulate: Event.simulate,
trigger: Event.simulate,
bind: Event.bind,
hasNativeEvent: Event.hasNativeEvent
});
})();
@balupton
Copy link
Author

Thanks to tfluehr on the prototype IRC channel for the direction :)

@kangax
Copy link

kangax commented Feb 2, 2011

I'd like to see a diff against original version.

@balupton
Copy link
Author

balupton commented Feb 2, 2011

You can't do it?...

@kangax
Copy link

kangax commented Feb 2, 2011

Don't have time at the moment, unfortunately. Will get to it later.

@sindre
Copy link

sindre commented Mar 31, 2011

internet explorer throws an error on trigger when "element" is "window". window.fireEvent doesn't work. We could add some check for "window"...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment