Last active
February 3, 2017 14:39
-
-
Save clohr/e3ce2e9e5130e41b422d34a09232bf84 to your computer and use it in GitHub Desktop.
Capture mouse click and touch events
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 head = ([h, ...tail]) => h | |
| const defaultTo = (defaultValue, testValue) => Boolean(testValue) ? testValue : defaultValue | |
| const capture = (evt) => { | |
| const eventTarget = evt.target | |
| const touchTarget = evt.touches && evt.touches.length > 0 | |
| const xPos = touchTarget ? head(touchTarget).clientX : evt.clientX | |
| const yPos = touchTarget ? head(touchTarget).clientY : evt.clientY | |
| const href = defaultTo(eventTarget.href, eventTarget.parentNode.href) | |
| const id = defaultTo(eventTarget.name, eventTarget.id) | |
| const touchEvent = Boolean(touchTarget) | |
| const data = { xPos, yPos, href, id, touchEvent } | |
| if (data.href) { | |
| console.log('Event data', data) | |
| } | |
| } | |
| const init = () => | |
| ['click', 'touchstart'].forEach(evt => | |
| document.body.addEventListener('click', capture) | |
| ) | |
| export default init |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment