2017-12-12 18:33:41 +00:00
|
|
|
/* global focusAccessibility */
|
2017-03-24 02:51:44 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-03-11 13:31:25 +00:00
|
|
|
/**
|
|
|
|
* @param {Object} params
|
|
|
|
* @param {String} params.title
|
|
|
|
* @param {String|Node|Object|Array<String|Node|Object>} params.contents
|
|
|
|
* a string gets parsed via tHTML,
|
|
|
|
* a non-string is passed as is to $create()
|
|
|
|
* @param {String} [params.className]
|
|
|
|
* CSS class name of the message box element
|
|
|
|
* @param {Array<String|{textContent: String, onclick: Function, ...etc}>} [params.buttons]
|
|
|
|
* ...etc means anything $create() can handle
|
|
|
|
* @param {Function(messageboxElement)} [params.onshow]
|
|
|
|
* invoked after the messagebox is shown
|
|
|
|
* @param {Boolean} [params.blockScroll]
|
|
|
|
* blocks the page scroll
|
|
|
|
* @returns {Promise}
|
|
|
|
* resolves to an object with optionally present properties depending on the interaction:
|
|
|
|
* {button: Number, enter: Boolean, esc: Boolean}
|
|
|
|
*/
|
2017-03-25 19:50:37 +00:00
|
|
|
function messageBox({
|
2018-03-11 13:31:25 +00:00
|
|
|
title,
|
|
|
|
contents,
|
|
|
|
className = '',
|
|
|
|
buttons = [],
|
|
|
|
onshow,
|
|
|
|
blockScroll,
|
|
|
|
}) {
|
2017-03-25 19:50:37 +00:00
|
|
|
initOwnListeners();
|
|
|
|
bindGlobalListeners();
|
|
|
|
createElement();
|
|
|
|
document.body.appendChild(messageBox.element);
|
2017-12-12 18:33:41 +00:00
|
|
|
|
|
|
|
messageBox.originalFocus = document.activeElement;
|
|
|
|
moveFocus(1);
|
|
|
|
|
|
|
|
if (typeof onshow === 'function') {
|
2017-03-25 19:50:37 +00:00
|
|
|
onshow(messageBox.element);
|
|
|
|
}
|
2017-12-12 18:33:41 +00:00
|
|
|
|
2017-03-25 19:50:37 +00:00
|
|
|
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-12-12 18:33:41 +00:00
|
|
|
const {which, shiftKey, ctrlKey, altKey, metaKey, target} = event;
|
|
|
|
if (shiftKey && which !== 9 || ctrlKey || altKey || metaKey) {
|
2017-12-09 15:25:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-12-12 18:33:41 +00:00
|
|
|
switch (which) {
|
|
|
|
case 13:
|
2017-12-17 18:57:26 +00:00
|
|
|
if (target.closest(focusAccessibility.ELEMENTS.join(','))) {
|
|
|
|
return;
|
2017-12-12 18:33:41 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 27:
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
moveFocus(shiftKey ? -1 : 1);
|
|
|
|
event.preventDefault();
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
return;
|
2017-03-25 19:50:37 +00:00
|
|
|
}
|
2017-12-12 18:33:41 +00:00
|
|
|
resolveWith(which === 13 ? {enter: true} : {esc: true});
|
2017-03-25 19:50:37 +00:00
|
|
|
},
|
|
|
|
scroll() {
|
|
|
|
scrollTo(blockScroll.x, blockScroll.y);
|
|
|
|
}
|
|
|
|
};
|
2017-03-24 02:51:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 19:50:37 +00:00
|
|
|
function resolveWith(value) {
|
2017-08-26 05:32:54 +00:00
|
|
|
unbindGlobalListeners();
|
2017-03-25 19:50:37 +00:00
|
|
|
setTimeout(messageBox.resolve, 0, value);
|
2017-08-31 10:23:12 +00:00
|
|
|
animateElement(messageBox.element, {
|
|
|
|
className: 'fadeout',
|
|
|
|
onComplete: removeSelf,
|
|
|
|
});
|
2017-12-12 18:33:41 +00:00
|
|
|
if (messageBox.element.contains(document.activeElement)) {
|
|
|
|
messageBox.originalFocus.focus();
|
|
|
|
}
|
2017-03-24 02:51:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 19:50:37 +00:00
|
|
|
function createElement() {
|
|
|
|
if (messageBox.element) {
|
2017-08-26 05:32:54 +00:00
|
|
|
unbindGlobalListeners();
|
|
|
|
removeSelf();
|
2017-03-24 02:51:44 +00:00
|
|
|
}
|
2017-03-25 19:50:37 +00:00
|
|
|
const id = 'message-box';
|
2017-12-03 21:12:09 +00:00
|
|
|
messageBox.element =
|
|
|
|
$create({id, className}, [
|
|
|
|
$create([
|
|
|
|
$create(`#${id}-title`, title),
|
|
|
|
$create(`#${id}-close-icon`, {onclick: messageBox.listeners.closeIcon},
|
|
|
|
$create('SVG:svg.svg-icon', {viewBox: '0 0 20 20'},
|
|
|
|
$create('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',
|
|
|
|
}))),
|
|
|
|
$create(`#${id}-contents`, tHTML(contents)),
|
|
|
|
$create(`#${id}-buttons`,
|
|
|
|
buttons.map((content, buttonIndex) => content &&
|
2017-12-05 23:59:12 +00:00
|
|
|
$create('button', Object.assign({
|
2017-12-03 21:12:09 +00:00
|
|
|
buttonIndex,
|
2017-12-05 23:59:12 +00:00
|
|
|
textContent: typeof content === 'object' ? '' : content,
|
|
|
|
onclick: messageBox.listeners.button,
|
|
|
|
}, typeof content === 'object' && content)))),
|
2017-12-03 21:12:09 +00:00
|
|
|
]),
|
|
|
|
]);
|
2017-03-24 02:51:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 19:50:37 +00:00
|
|
|
function bindGlobalListeners() {
|
|
|
|
blockScroll = blockScroll && {x: scrollX, y: scrollY};
|
|
|
|
if (blockScroll) {
|
|
|
|
window.addEventListener('scroll', messageBox.listeners.scroll);
|
2017-03-24 02:51:44 +00:00
|
|
|
}
|
2017-08-26 05:32:54 +00:00
|
|
|
window.addEventListener('keydown', messageBox.listeners.key, true);
|
2017-03-25 19:50:37 +00:00
|
|
|
}
|
|
|
|
|
2017-08-26 05:32:54 +00:00
|
|
|
function unbindGlobalListeners() {
|
|
|
|
window.removeEventListener('keydown', messageBox.listeners.key, true);
|
2017-03-25 19:50:37 +00:00
|
|
|
window.removeEventListener('scroll', messageBox.listeners.scroll);
|
2017-08-26 05:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeSelf() {
|
2017-03-25 19:50:37 +00:00
|
|
|
messageBox.element.remove();
|
|
|
|
messageBox.element = null;
|
|
|
|
messageBox.resolve = null;
|
2017-03-24 02:51:44 +00:00
|
|
|
}
|
2017-12-12 18:33:41 +00:00
|
|
|
|
|
|
|
function moveFocus(dir) {
|
|
|
|
const elements = [...messageBox.element.getElementsByTagName('*')];
|
2017-12-23 04:18:40 +00:00
|
|
|
const activeIndex = Math.max(0, elements.indexOf(document.activeElement));
|
2017-12-12 18:33:41 +00:00
|
|
|
const num = elements.length;
|
|
|
|
for (let i = 1; i < num; i++) {
|
|
|
|
const elementIndex = (activeIndex + i * dir + num) % num;
|
|
|
|
// we don't use positive tabindex so we stop at any valid value
|
|
|
|
const el = elements[elementIndex];
|
|
|
|
if (!el.disabled && el.tabIndex >= 0) {
|
|
|
|
el.focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-24 02:51:44 +00:00
|
|
|
}
|
2017-11-09 06:35:10 +00:00
|
|
|
|
2018-03-11 13:31:25 +00:00
|
|
|
/**
|
|
|
|
* @param {String|Node|Array<String|Node>} contents
|
|
|
|
* @param {String} [className] like 'pre' for monospace font
|
|
|
|
* @returns {Promise<Boolean>} same as messageBox
|
|
|
|
*/
|
|
|
|
messageBox.alert = (contents, className) =>
|
2017-11-09 06:35:10 +00:00
|
|
|
messageBox({
|
2018-03-11 13:31:25 +00:00
|
|
|
contents,
|
|
|
|
className: `center ${className || ''}`,
|
2017-11-09 06:35:10 +00:00
|
|
|
buttons: [t('confirmClose')]
|
|
|
|
});
|
|
|
|
|
2018-03-11 13:31:25 +00:00
|
|
|
/**
|
|
|
|
* @param {String|Node|Array<String|Node>} contents
|
|
|
|
* @param {String} [className] like 'pre' for monospace font
|
|
|
|
* @returns {Promise<Boolean>} resolves to true when confirmed
|
|
|
|
*/
|
|
|
|
messageBox.confirm = (contents, className) =>
|
2017-11-09 06:35:10 +00:00
|
|
|
messageBox({
|
2018-03-11 13:31:25 +00:00
|
|
|
contents,
|
|
|
|
className: `center ${className || ''}`,
|
2017-11-09 06:35:10 +00:00
|
|
|
buttons: [t('confirmYes'), t('confirmNo')]
|
|
|
|
}).then(result => result.button === 0 || result.enter);
|