unbind properly in msgbox; hide Esc/Enter from other handlers

This commit is contained in:
tophf 2017-08-26 08:32:54 +03:00
parent 0d038c59bc
commit 8525ea9017

View File

@ -32,6 +32,7 @@ function messageBox({
if (!event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey if (!event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey
&& (keyCode === 13 || keyCode === 27)) { && (keyCode === 13 || keyCode === 27)) {
event.preventDefault(); event.preventDefault();
event.stopPropagation();
resolveWith(keyCode === 13 ? {enter: true} : {esc: true}); resolveWith(keyCode === 13 ? {enter: true} : {esc: true});
} }
}, },
@ -42,14 +43,16 @@ function messageBox({
} }
function resolveWith(value) { function resolveWith(value) {
unbindGlobalListeners();
setTimeout(messageBox.resolve, 0, value); setTimeout(messageBox.resolve, 0, value);
animateElement(messageBox.element, {className: 'fadeout', remove: true}) animateElement(messageBox.element, {className: 'fadeout', remove: true})
.then(unbindAndRemoveSelf); .then(removeSelf);
} }
function createElement() { function createElement() {
if (messageBox.element) { if (messageBox.element) {
unbindAndRemoveSelf(); unbindGlobalListeners();
removeSelf();
} }
const id = 'message-box'; const id = 'message-box';
messageBox.element = $element({id, className, appendChild: [ messageBox.element = $element({id, className, appendChild: [
@ -82,12 +85,15 @@ function messageBox({
if (blockScroll) { if (blockScroll) {
window.addEventListener('scroll', messageBox.listeners.scroll); window.addEventListener('scroll', messageBox.listeners.scroll);
} }
window.addEventListener('keydown', messageBox.listeners.key); window.addEventListener('keydown', messageBox.listeners.key, true);
} }
function unbindAndRemoveSelf() { function unbindGlobalListeners() {
document.removeEventListener('keydown', messageBox.listeners.key); window.removeEventListener('keydown', messageBox.listeners.key, true);
window.removeEventListener('scroll', messageBox.listeners.scroll); window.removeEventListener('scroll', messageBox.listeners.scroll);
}
function removeSelf() {
messageBox.element.remove(); messageBox.element.remove();
messageBox.element = null; messageBox.element = null;
messageBox.resolve = null; messageBox.resolve = null;