2024-01-03 20:43:05 +01:00
|
|
|
|
(function(){
|
|
|
|
|
const SpaccDotWeb = {};
|
|
|
|
|
|
|
|
|
|
SpaccDotWeb.AppInit = (meta) => {
|
|
|
|
|
// ... load meta from argument or page element and add to object
|
|
|
|
|
return SpaccDotWeb;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-05 23:57:04 +01:00
|
|
|
|
SpaccDotWeb.Sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
2024-01-03 20:43:05 +01:00
|
|
|
|
|
|
|
|
|
SpaccDotWeb.RequireScript = (src, type) => {
|
2024-01-05 23:57:04 +01:00
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
const scriptElem = document.createElement('script');
|
|
|
|
|
//if (type) {
|
|
|
|
|
// scriptElem.type = type;
|
|
|
|
|
//}
|
|
|
|
|
scriptElem.onload = (event) => {
|
|
|
|
|
resolve(event);
|
|
|
|
|
};
|
|
|
|
|
scriptElem.src = src;
|
|
|
|
|
document.body.appendChild(scriptElem);
|
|
|
|
|
});
|
2024-01-03 20:43:05 +01:00
|
|
|
|
}
|
2024-02-23 01:38:08 +01:00
|
|
|
|
// .RequireScripts = (...) => {}
|
2024-01-03 20:43:05 +01:00
|
|
|
|
|
|
|
|
|
SpaccDotWeb.ShowModal = async (params) => {
|
|
|
|
|
// TODO: delete dialogs from DOM after use (garbage collect)?
|
|
|
|
|
if (!window.HTMLDialogElement && !window.dialogPolyfill) {
|
2024-01-05 23:57:04 +01:00
|
|
|
|
await SpaccDotWeb.RequireScript('https://googlechrome.github.io/dialog-polyfill/dist/dialog-polyfill.js');
|
2024-01-03 20:43:05 +01:00
|
|
|
|
}
|
|
|
|
|
let output;
|
|
|
|
|
if (typeof(params) === 'string') {
|
|
|
|
|
params = { label: params }
|
|
|
|
|
}
|
2024-01-05 23:57:04 +01:00
|
|
|
|
//params.deleteOnClose ||= true;
|
2024-01-03 20:43:05 +01:00
|
|
|
|
const modal = document.createElement('dialog');
|
2024-01-05 23:57:04 +01:00
|
|
|
|
const label = (params.label || params.text);
|
2024-01-03 20:43:05 +01:00
|
|
|
|
modal.innerHTML = `
|
2024-01-05 23:57:04 +01:00
|
|
|
|
${label ? `<p>${label}</p>` : ''}
|
2024-01-03 20:43:05 +01:00
|
|
|
|
${params.extraHTML || ''}
|
|
|
|
|
<button name="cancel">🔙️ Cancel</button>
|
|
|
|
|
`;
|
|
|
|
|
if (params.actionConfirm || params.action) {
|
|
|
|
|
modal.innerHTML += `
|
|
|
|
|
<button name="confirm">⏩️ Confirm</button>
|
|
|
|
|
`;
|
|
|
|
|
const buttonConfirm = modal.querySelector('button[name="confirm"]');
|
|
|
|
|
buttonConfirm.onclick = (event) => {
|
|
|
|
|
output = (params.actionConfirm || params.action)(event, buttonConfirm);
|
|
|
|
|
modal.close();
|
|
|
|
|
return output;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
const buttonCancel = modal.querySelector('button[name="cancel"]');
|
|
|
|
|
buttonCancel.onclick = (event) => {
|
|
|
|
|
if (params.actionCancel) {
|
|
|
|
|
output = actionCancel(event, buttonCancel);
|
|
|
|
|
}
|
|
|
|
|
modal.close();
|
|
|
|
|
return output;
|
|
|
|
|
};
|
|
|
|
|
document.querySelector('body').appendChild(modal);
|
|
|
|
|
if (window.dialogPolyfill) {
|
|
|
|
|
dialogPolyfill.registerDialog(modal);
|
|
|
|
|
}
|
|
|
|
|
modal.showModal();
|
|
|
|
|
return modal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.SpaccDotWeb ||= SpaccDotWeb;
|
|
|
|
|
})();
|