Skip to content

Instantly share code, notes, and snippets.

@runspired
Last active February 2, 2016 18:18
Show Gist options
  • Save runspired/68d710c7d8914d416e34 to your computer and use it in GitHub Desktop.
Save runspired/68d710c7d8914d416e34 to your computer and use it in GitHub Desktop.
const EVENT_DEFAULTS = {
bubbles: true
};
export default triggerEvent(element, name, options) {
const eventInit = Object.assign({}, EVENT_DEFAULTS, getLocation(element), options);
const event = new getConstructor(name)(name, eventInit);
element.dispatchEvent(event);
}
function getLocation(element) {
const rect = element.getBoundingClientRect();
const X = rect.left + 1;
const Y = rect.top + 1;
return {
target: element,
pageX: X,
offsetX: X,
clientX: X,
screenX: X + 5,
pageY: Y,
offsetY: Y,
clientY: Y,
screenY: Y + 95
};
}
function getConstructor(name) {
if (name.indexOf('mouse')) {
return MouseEvent;
}
if (name.indexOf('key')) {
return KeyboardEvent;
}
if (name.indexOf('touch')) {
return TouchEvent;
}
return Event;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment