Last active
February 2, 2016 18:18
-
-
Save runspired/68d710c7d8914d416e34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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