stylus/msgbox/msgbox.js

109 lines
3.2 KiB
JavaScript
Raw Normal View History

'use strict';
function messageBox({
2017-08-20 20:14:12 +00:00
// [mandatory] string
title,
// [mandatory] 1) DOM element 2) string
contents,
// string, CSS class name of the message box element
className = '',
// array of strings used as labels
buttons = [],
// function(messageboxElement) invoked after the messagebox is shown
onshow,
// boolean, blocks the page scroll
blockScroll,
// RETURNS: Promise resolved to {button[number], enter[boolean], esc[boolean]}
}) {
initOwnListeners();
bindGlobalListeners();
createElement();
document.body.appendChild(messageBox.element);
if (onshow) {
onshow(messageBox.element);
}
return new Promise(_resolve => {
messageBox.resolve = _resolve;
});
function initOwnListeners() {
messageBox.listeners = messageBox.listeners || {
closeIcon() {
resolveWith({button: -1});
},
button() {
resolveWith({button: this.buttonIndex});
},
key(event) {
2017-04-18 18:56:32 +00:00
const keyCode = event.keyCode || event.which;
if (!event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey
2017-07-16 18:02:00 +00:00
&& (keyCode === 13 || keyCode === 27)) {
event.preventDefault();
event.stopPropagation();
2017-07-16 18:02:00 +00:00
resolveWith(keyCode === 13 ? {enter: true} : {esc: true});
}
},
scroll() {
scrollTo(blockScroll.x, blockScroll.y);
}
};
}
function resolveWith(value) {
unbindGlobalListeners();
setTimeout(messageBox.resolve, 0, value);
animateElement(messageBox.element, {className: 'fadeout', remove: true})
.then(removeSelf);
}
function createElement() {
if (messageBox.element) {
unbindGlobalListeners();
removeSelf();
}
const id = 'message-box';
messageBox.element = $element({id, className, appendChild: [
$element({appendChild: [
2017-07-19 12:56:32 +00:00
$element({id: `${id}-title`, textContent: title}),
$element({id: `${id}-close-icon`, appendChild:
$element({tag: 'SVG#svg', class: 'svg-icon', viewBox: '0 0 20 20', appendChild:
$element({tag: 'SVG#path', d: 'M11.69,10l4.55,4.55-1.69,1.69L10,11.69,' +
'5.45,16.23,3.77,14.55,8.31,10,3.77,5.45,5.45,3.77,10,8.31l4.55-4.55,1.69,1.69Z',
})
}),
2017-04-02 23:18:43 +00:00
onclick: messageBox.listeners.closeIcon}),
2017-07-19 12:56:32 +00:00
$element({id: `${id}-contents`, appendChild: tHTML(contents)}),
$element({id: `${id}-buttons`, appendChild:
buttons.map((textContent, buttonIndex) => textContent &&
$element({
tag: 'button',
buttonIndex,
textContent,
onclick: messageBox.listeners.button,
})
)
}),
]}),
]});
}
function bindGlobalListeners() {
blockScroll = blockScroll && {x: scrollX, y: scrollY};
if (blockScroll) {
window.addEventListener('scroll', messageBox.listeners.scroll);
}
window.addEventListener('keydown', messageBox.listeners.key, true);
}
function unbindGlobalListeners() {
window.removeEventListener('keydown', messageBox.listeners.key, true);
window.removeEventListener('scroll', messageBox.listeners.scroll);
}
function removeSelf() {
messageBox.element.remove();
messageBox.element = null;
messageBox.resolve = null;
}
}