1
0
mirror of https://github.com/morris/vanilla-todo.git synced 2025-02-22 23:22:46 +01:00
vanilla-todo/public/scripts/AppLateBlur.js
Morris Brodersen e36cdfeb11 first commit
2020-10-20 17:27:16 +02:00

40 lines
1.3 KiB
JavaScript

/* global VT */
window.VT = window.VT || {};
/**
* Enables `lateBlur` events on the target element.
* After an actual `blur` event, subsequent interactions with the window
* (e.g. focus, select, mouseup etc.) will dispatch a `lateBlur` event.
*/
VT.AppLateBlur = function (el) {
el.addEventListener('blur', function () {
window.addEventListener('focus', dispatch);
window.addEventListener('select', dispatch);
if (window.navigator.pointerEnabled) {
window.addEventListener('pointerup', dispatch);
} else if (window.navigator.msPointerEnabled) {
window.addEventListener('MSPointerUp', dispatch);
} else {
window.addEventListener('mouseup', dispatch);
window.addEventListener('touchend', dispatch);
}
});
function dispatch() {
window.removeEventListener('focus', dispatch);
window.removeEventListener('select', dispatch);
if (window.navigator.pointerEnabled) {
window.removeEventListener('pointerup', dispatch);
} else if (window.navigator.msPointerEnabled) {
window.removeEventListener('MSPointerUp', dispatch);
} else {
window.removeEventListener('mouseup', dispatch);
window.removeEventListener('touchend', dispatch);
}
el.dispatchEvent(new CustomEvent('lateBlur', { bubbles: true }));
}
};