2021-01-01 14:27:58 +00:00
|
|
|
/* global $ $create animateElement focusAccessibility moveFocus */// dom.js
|
|
|
|
/* global t */// localization.js
|
2017-03-24 02:51:44 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
// TODO: convert this singleton mess so we can show many boxes at once
|
|
|
|
/* global messageBox */
|
|
|
|
window.messageBox = {
|
|
|
|
element: null,
|
|
|
|
listeners: null,
|
|
|
|
_blockScroll: null,
|
|
|
|
_originalFocus: null,
|
|
|
|
_resolve: null,
|
|
|
|
};
|
|
|
|
|
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
|
2020-11-18 11:17:15 +00:00
|
|
|
* a string gets parsed via t.HTML,
|
2018-03-11 13:31:25 +00:00
|
|
|
* 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}
|
|
|
|
*/
|
2021-01-01 14:27:58 +00:00
|
|
|
messageBox.show = async ({
|
2018-03-11 13:31:25 +00:00
|
|
|
title,
|
|
|
|
contents,
|
|
|
|
className = '',
|
|
|
|
buttons = [],
|
|
|
|
onshow,
|
|
|
|
blockScroll,
|
2021-01-01 14:27:58 +00:00
|
|
|
}) => {
|
|
|
|
await require(['/js/dlg/message-box.css']);
|
|
|
|
if (!messageBox.listeners) initOwnListeners();
|
2017-03-25 19:50:37 +00:00
|
|
|
createElement();
|
2021-08-12 11:40:03 +00:00
|
|
|
bindGlobalListeners();
|
2017-03-25 19:50:37 +00:00
|
|
|
document.body.appendChild(messageBox.element);
|
2017-12-12 18:33:41 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
messageBox._originalFocus = document.activeElement;
|
|
|
|
// focus the first focusable child but skip the first external link which is usually `feedback`
|
|
|
|
if ((moveFocus(messageBox.element, 0) || {}).target === '_blank') {
|
|
|
|
moveFocus(messageBox.element, 1);
|
|
|
|
}
|
2018-09-06 17:59:04 +00:00
|
|
|
// suppress focus outline when invoked via click
|
2018-09-06 16:05:10 +00:00
|
|
|
if (focusAccessibility.lastFocusedViaClick && document.activeElement) {
|
|
|
|
document.activeElement.dataset.focusedViaClick = '';
|
|
|
|
}
|
|
|
|
|
2017-12-12 18:33:41 +00:00
|
|
|
if (typeof onshow === 'function') {
|
2017-03-25 19:50:37 +00:00
|
|
|
onshow(messageBox.element);
|
|
|
|
}
|
2017-12-12 18:33:41 +00:00
|
|
|
|
2018-08-07 17:05:26 +00:00
|
|
|
if (!$('#message-box-title').textContent) {
|
|
|
|
$('#message-box-title').hidden = true;
|
|
|
|
$('#message-box-close-icon').hidden = true;
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
messageBox._resolve = resolve;
|
2017-03-25 19:50:37 +00:00
|
|
|
});
|
|
|
|
|
2021-07-06 21:19:00 +00:00
|
|
|
function clamp(value, min, max) {
|
|
|
|
return Math.min(Math.max(value, min), max);
|
|
|
|
}
|
|
|
|
|
2017-03-25 19:50:37 +00:00
|
|
|
function initOwnListeners() {
|
2021-07-06 21:19:00 +00:00
|
|
|
let listening = false;
|
|
|
|
let offsetX = 0;
|
|
|
|
let offsetY = 0;
|
|
|
|
let clickX, clickY;
|
2021-01-01 14:27:58 +00:00
|
|
|
messageBox.listeners = {
|
2017-03-25 19:50:37 +00:00
|
|
|
closeIcon() {
|
|
|
|
resolveWith({button: -1});
|
|
|
|
},
|
|
|
|
button() {
|
|
|
|
resolveWith({button: this.buttonIndex});
|
|
|
|
},
|
|
|
|
key(event) {
|
2020-10-13 18:14:54 +00:00
|
|
|
const {key, shiftKey, ctrlKey, altKey, metaKey, target} = event;
|
|
|
|
if (shiftKey && key !== 'Tab' || ctrlKey || altKey || metaKey) {
|
2017-12-09 15:25:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-13 18:14:54 +00:00
|
|
|
switch (key) {
|
|
|
|
case 'Enter':
|
2020-11-08 08:12:42 +00:00
|
|
|
if (focusAccessibility.closest(target)) {
|
2017-12-17 18:57:26 +00:00
|
|
|
return;
|
2017-12-12 18:33:41 +00:00
|
|
|
}
|
|
|
|
break;
|
2020-10-13 18:14:54 +00:00
|
|
|
case 'Escape':
|
2017-12-12 18:33:41 +00:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
break;
|
2020-10-13 18:14:54 +00:00
|
|
|
case 'Tab':
|
2018-07-22 16:37:49 +00:00
|
|
|
moveFocus(messageBox.element, shiftKey ? -1 : 1);
|
2017-12-12 18:33:41 +00:00
|
|
|
event.preventDefault();
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
return;
|
2017-03-25 19:50:37 +00:00
|
|
|
}
|
2020-10-13 18:14:54 +00:00
|
|
|
resolveWith(key === 'Enter' ? {enter: true} : {esc: true});
|
2017-03-25 19:50:37 +00:00
|
|
|
},
|
|
|
|
scroll() {
|
2021-01-01 14:27:58 +00:00
|
|
|
scrollTo(messageBox._blockScroll.x, messageBox._blockScroll.y);
|
2020-11-18 11:17:15 +00:00
|
|
|
},
|
2021-07-06 21:19:00 +00:00
|
|
|
mouseDown(event) {
|
|
|
|
if (event.button !== 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!listening) {
|
|
|
|
window.on('mouseup', messageBox.listeners.mouseUp, {passive: true});
|
|
|
|
window.on('mousemove', messageBox.listeners.mouseMove, {passive: true});
|
|
|
|
listening = true;
|
|
|
|
}
|
|
|
|
clickX = event.clientX - offsetX;
|
|
|
|
clickY = event.clientY - offsetY;
|
|
|
|
},
|
|
|
|
mouseUp(event) {
|
|
|
|
if (event.button !== 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
window.off('mouseup', messageBox.listeners.mouseUp);
|
|
|
|
window.off('mousemove', messageBox.listeners.mouseMove);
|
|
|
|
listening = false;
|
|
|
|
},
|
|
|
|
mouseMove(event) {
|
2021-08-12 11:40:03 +00:00
|
|
|
offsetX = clamp(event.clientX, 30, innerWidth - 30) - clickX;
|
|
|
|
offsetY = clamp(event.clientY, 30, innerHeight - 30) - clickY;
|
|
|
|
messageBox.element.firstChild.style.transform = `translate(${offsetX}px,${offsetY}px)`;
|
2021-07-06 21:19:00 +00:00
|
|
|
},
|
2017-03-25 19:50:37 +00:00
|
|
|
};
|
2017-03-24 02:51:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 19:50:37 +00:00
|
|
|
function resolveWith(value) {
|
2021-01-01 14:27:58 +00:00
|
|
|
setTimeout(messageBox._resolve, 0, value);
|
2017-08-26 05:32:54 +00:00
|
|
|
unbindGlobalListeners();
|
2020-11-06 18:04:10 +00:00
|
|
|
animateElement(messageBox.element, 'fadeout')
|
|
|
|
.then(removeSelf);
|
2017-12-12 18:33:41 +00:00
|
|
|
if (messageBox.element.contains(document.activeElement)) {
|
2021-01-01 14:27:58 +00:00
|
|
|
messageBox._originalFocus.focus();
|
2017-12-12 18:33:41 +00:00
|
|
|
}
|
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([
|
2021-08-12 11:40:03 +00:00
|
|
|
$create(`#${id}-title`, {onmousedown: messageBox.listeners.mouseDown}, title),
|
2017-12-03 21:12:09 +00:00
|
|
|
$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',
|
|
|
|
}))),
|
2020-11-18 11:17:15 +00:00
|
|
|
$create(`#${id}-contents`, t.HTML(contents)),
|
2017-12-03 21:12:09 +00:00
|
|
|
$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
|
|
|
onclick: messageBox.listeners.button,
|
2018-04-12 18:02:02 +00:00
|
|
|
}, typeof content === 'object' ? content : {
|
|
|
|
textContent: 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() {
|
2021-01-01 14:27:58 +00:00
|
|
|
messageBox._blockScroll = blockScroll && {x: scrollX, y: scrollY};
|
2017-03-25 19:50:37 +00:00
|
|
|
if (blockScroll) {
|
2021-01-01 14:27:58 +00:00
|
|
|
window.on('scroll', messageBox.listeners.scroll, {passive: false});
|
2017-03-24 02:51:44 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
window.on('keydown', messageBox.listeners.key, true);
|
2017-03-25 19:50:37 +00:00
|
|
|
}
|
|
|
|
|
2017-08-26 05:32:54 +00:00
|
|
|
function unbindGlobalListeners() {
|
2021-01-01 14:27:58 +00:00
|
|
|
window.off('keydown', messageBox.listeners.key, true);
|
|
|
|
window.off('scroll', messageBox.listeners.scroll);
|
2021-07-06 21:19:00 +00:00
|
|
|
window.off('mouseup', messageBox.listeners.mouseUp);
|
|
|
|
window.off('mousemove', messageBox.listeners.mouseMove);
|
2017-08-26 05:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeSelf() {
|
2017-03-25 19:50:37 +00:00
|
|
|
messageBox.element.remove();
|
|
|
|
messageBox.element = null;
|
2021-01-01 14:27:58 +00:00
|
|
|
messageBox._resolve = null;
|
2017-03-24 02:51:44 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +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
|
2018-08-18 20:17:20 +00:00
|
|
|
* @param {String} [title]
|
2021-01-01 14:27:58 +00:00
|
|
|
* @returns {Promise<Boolean>} same as show()
|
2018-03-11 13:31:25 +00:00
|
|
|
*/
|
2018-08-18 20:17:20 +00:00
|
|
|
messageBox.alert = (contents, className, title) =>
|
2021-01-01 14:27:58 +00:00
|
|
|
messageBox.show({
|
2018-08-18 20:17:20 +00:00
|
|
|
title,
|
2018-03-11 13:31:25 +00:00
|
|
|
contents,
|
|
|
|
className: `center ${className || ''}`,
|
2020-11-18 11:17:15 +00:00
|
|
|
buttons: [t('confirmClose')],
|
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
|
2020-10-25 19:36:41 +00:00
|
|
|
* @param {String} [title]
|
2018-03-11 13:31:25 +00:00
|
|
|
* @returns {Promise<Boolean>} resolves to true when confirmed
|
|
|
|
*/
|
2021-01-01 14:27:58 +00:00
|
|
|
messageBox.confirm = async (contents, className, title) => {
|
|
|
|
const res = await messageBox.show({
|
2020-10-25 19:36:41 +00:00
|
|
|
title,
|
2018-03-11 13:31:25 +00:00
|
|
|
contents,
|
|
|
|
className: `center ${className || ''}`,
|
2020-11-18 11:17:15 +00:00
|
|
|
buttons: [t('confirmYes'), t('confirmNo')],
|
2021-01-01 14:27:58 +00:00
|
|
|
});
|
|
|
|
return res.button === 0 || res.enter;
|
|
|
|
};
|