pinafore/routes/_utils/events.js

72 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-01-23 05:47:29 +00:00
export function imgLoadError (node, callback) {
node.addEventListener('error', callback)
return {
destroy () {
2018-01-23 05:47:29 +00:00
node.removeEventListener('error', callback)
}
}
}
export function imgLoad (node, callback) {
node.addEventListener('load', callback)
return {
destroy () {
2018-01-23 05:47:29 +00:00
node.removeEventListener('load', callback)
}
}
}
2018-02-09 06:29:29 +00:00
export function mouseover (node, callback) {
function onMouseEnter () {
callback(true) // eslint-disable-line
}
2018-02-09 06:29:29 +00:00
function onMouseLeave () {
callback(false) // eslint-disable-line
}
node.addEventListener('mouseenter', onMouseEnter)
node.addEventListener('mouseleave', onMouseLeave)
return {
destroy () {
node.removeEventListener('mouseenter', onMouseEnter)
node.removeEventListener('mouseleave', onMouseLeave)
}
}
2018-02-09 06:29:29 +00:00
}
export function focusWithCapture (node, callback) {
node.addEventListener('focus', callback, true)
return {
destroy () {
node.removeEventListener('focus', callback, true)
}
}
}
export function blurWithCapture (node, callback) {
node.addEventListener('blur', callback, true)
return {
destroy () {
node.removeEventListener('blur', callback, true)
}
}
2018-02-11 17:37:13 +00:00
}
2018-03-25 01:04:54 +00:00
export function selectionChange (node, callback) {
let events = ['keyup', 'click', 'focus', 'blur']
let listener = () => {
callback(node.selectionStart)
}
for (let event of events) {
node.addEventListener(event, listener)
}
return {
destroy () {
2018-03-25 01:04:54 +00:00
for (let event of events) {
node.removeEventListener(event, listener)
}
}
}
}