2017-03-24 02:51:44 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-03-25 19:50:37 +00:00
|
|
|
function messageBox({
|
2017-07-19 12:56:32 +00:00
|
|
|
title, // [mandatory] string
|
|
|
|
contents, // [mandatory] 1) DOM element 2) string
|
2017-03-25 19:50:37 +00:00
|
|
|
className = '', // string, CSS class name of the message box element
|
2017-09-01 06:46:00 +00:00
|
|
|
buttons = [], // array of strings or objects like {textContent[string], onclick[function]}.
|
2017-03-25 19:50:37 +00:00
|
|
|
onshow, // function(messageboxElement) invoked after the messagebox is shown
|
|
|
|
blockScroll, // boolean, blocks the page scroll
|
|
|
|
}) { // RETURNS: Promise resolved to {button[number], enter[boolean], esc[boolean]}
|
|
|
|
initOwnListeners();
|
|
|
|
bindGlobalListeners();
|
|
|
|
createElement();
|
|
|
|
document.body.appendChild(messageBox.element);
|
2017-12-05 21:14:21 +00:00
|
|
|
if (location.href.includes('popup.html')) {
|
|
|
|
messageBox.isPopup = true;
|
|
|
|
messageBox.element.classList.add('stylus-popup');
|
|
|
|
|
|
|
|
// calculate size
|
|
|
|
messageBox.element.classList.add('calculate-size');
|
|
|
|
const {offsetWidth, offsetHeight} = messageBox.element.children[0];
|
|
|
|
messageBox.element.classList.remove('calculate-size');
|
|
|
|
|
|
|
|
// for colorpicker
|
|
|
|
const MIN_WIDTH = 350;
|
|
|
|
const MIN_HEIGHT = 250;
|
|
|
|
|
|
|
|
const width = Math.max(Math.min(offsetWidth / 0.9 + 2, 800), MIN_WIDTH);
|
|
|
|
const height = Math.max(Math.min(offsetHeight / 0.9 + 2, 600), MIN_HEIGHT);
|
|
|
|
|
|
|
|
document.body.style.minWidth = `${width}px`;
|
|
|
|
document.body.style.minHeight = `${height}px`;
|
|
|
|
}
|
2017-03-25 19:50:37 +00:00
|
|
|
if (onshow) {
|
|
|
|
onshow(messageBox.element);
|
|
|
|
}
|
2017-11-28 17:03:50 +00:00
|
|
|
messageBox.element.focus();
|
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-04-18 18:56:32 +00:00
|
|
|
const keyCode = event.keyCode || event.which;
|
2017-03-25 19:50:37 +00:00
|
|
|
if (!event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey
|
2017-07-16 18:02:00 +00:00
|
|
|
&& (keyCode === 13 || keyCode === 27)) {
|
2017-03-25 19:50:37 +00:00
|
|
|
event.preventDefault();
|
2017-08-26 05:32:54 +00:00
|
|
|
event.stopPropagation();
|
2017-07-16 18:02:00 +00:00
|
|
|
resolveWith(keyCode === 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-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 &&
|
|
|
|
$create('button', {
|
|
|
|
buttonIndex,
|
|
|
|
textContent: content.textContent || content,
|
|
|
|
onclick: content.onclick || messageBox.listeners.button,
|
|
|
|
}))),
|
|
|
|
]),
|
|
|
|
]);
|
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-12-05 21:14:21 +00:00
|
|
|
if (messageBox.isPopup) {
|
|
|
|
document.body.style.minWidth = '';
|
|
|
|
document.body.style.minHeight = '';
|
|
|
|
}
|
2017-03-24 02:51:44 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-09 06:35:10 +00:00
|
|
|
|
|
|
|
messageBox.alert = text =>
|
|
|
|
messageBox({
|
|
|
|
contents: text,
|
|
|
|
className: 'pre center',
|
|
|
|
buttons: [t('confirmClose')]
|
|
|
|
});
|
|
|
|
|
|
|
|
messageBox.confirm = text =>
|
|
|
|
messageBox({
|
|
|
|
contents: text,
|
|
|
|
className: 'pre center',
|
|
|
|
buttons: [t('confirmYes'), t('confirmNo')]
|
|
|
|
}).then(result => result.button === 0 || result.enter);
|