misuzu/assets/misuzu.js/msgbox.jsx

74 lines
2.2 KiB
React
Raw Normal View History

2024-01-24 21:53:26 +00:00
#include utility.js
2024-01-30 23:47:02 +00:00
const MszShowConfirmBox = async (text, title, target) => {
let result = false;
await MszShowMessageBox(text, title, [
{ text: 'Yes', callback: async () => result = true },
{ text: 'No' },
], target);
return result;
};
const MszShowMessageBox = (text, title, buttons, target) => {
2024-02-02 21:42:40 +00:00
if(typeof text !== 'string') {
if(text !== undefined && text !== null && typeof text.toString === 'function')
text = text.toString();
else throw 'text must be a string';
}
2024-01-24 21:53:26 +00:00
if(!(target instanceof Element))
target = document.body;
if(typeof title !== 'string')
title = 'Information';
if(!Array.isArray(buttons))
buttons = [];
2024-01-30 23:47:02 +00:00
return new Promise((resolve, reject) => {
if(target.querySelector('.messagebox')) {
reject();
return;
2024-01-24 21:53:26 +00:00
}
2024-01-30 23:47:02 +00:00
let buttonsElem;
const html = <div class="messagebox">
<div class="container messagebox__container">
<div class="container__title">
<div class="container__title__background"/>
<div class="container__title__text">{title}</div>
</div>
<div class="container__content">{text}</div>
{buttonsElem = <div class="messagebox__buttons"/>}
</div>
</div>;
let firstButton;
if(buttons.length < 1) {
firstButton = <button class="input__button" onclick={() => {
html.remove();
resolve();
}}>OK</button>;
buttonsElem.appendChild(firstButton);
} else {
for(const button of buttons) {
const buttonElem = <button class="input__button" onclick={() => {
html.remove();
if(typeof button.callback === 'function')
button.callback().finally(() => resolve());
else
resolve();
}}>{button.text}</button>;
buttonsElem.appendChild(buttonElem);
if(firstButton === undefined)
firstButton = buttonElem;
}
}
2024-01-24 21:53:26 +00:00
2024-01-30 23:47:02 +00:00
target.appendChild(html);
firstButton.focus();
});
2024-01-24 21:53:26 +00:00
};