1
0
mirror of https://github.com/nolanlawson/pinafore synced 2024-12-12 18:28:03 +01:00
Pinafore-Web-Client-Frontend/routes/_utils/delegate.js
2018-02-28 19:16:11 -08:00

44 lines
1008 B
JavaScript

// Delegate certain events to the global document for perf purposes.
import { mark, stop } from './marks'
const callbacks = {}
if (process.browser && process.env.NODE_ENV !== 'production') {
window.delegateCallbacks = callbacks
}
function onEvent (e) {
let { type, keyCode, target } = e
if (!(type === 'click' || (type === 'keydown' && keyCode === 13))) {
// we're not interested in any non-click or non-Enter events
return
}
mark('delegate onEvent')
let key
let element = target
while (element) {
if ((key = element.getAttribute('delegate-key'))) {
break
}
element = element.parentElement
}
if (key && callbacks[key]) {
callbacks[key](e)
}
stop('delegate onEvent')
}
export function registerClickDelegate (key, callback) {
callbacks[key] = callback
}
export function unregisterClickDelegate (key) {
delete callbacks[key]
}
if (process.browser) {
document.addEventListener('click', onEvent)
document.addEventListener('keydown', onEvent)
}