Update SpaccDotWeb.Alt: ShowModal, RequireScript

This commit is contained in:
octospacc 2024-01-05 23:57:04 +01:00
parent 79925ffe53
commit 5487921ecb
1 changed files with 16 additions and 9 deletions

View File

@ -6,29 +6,36 @@
return SpaccDotWeb; return SpaccDotWeb;
} }
SpaccDotWeb.Sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); SpaccDotWeb.Sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
SpaccDotWeb.RequireScript = (src, type) => { SpaccDotWeb.RequireScript = (src, type) => {
return new Promise((resolve) => {
const scriptElem = document.createElement('script'); const scriptElem = document.createElement('script');
//if (type) {
// scriptElem.type = type;
//}
scriptElem.onload = (event) => {
resolve(event);
};
scriptElem.src = src; scriptElem.src = src;
document.body.appendChild(scriptElem); document.body.appendChild(scriptElem);
});
} }
SpaccDotWeb.ShowModal = async (params) => { SpaccDotWeb.ShowModal = async (params) => {
// TODO: delete dialogs from DOM after use (garbage collect)? // TODO: delete dialogs from DOM after use (garbage collect)?
if (!window.HTMLDialogElement && !window.dialogPolyfill) { if (!window.HTMLDialogElement && !window.dialogPolyfill) {
SpaccDotWeb.RequireScript('https://googlechrome.github.io/dialog-polyfill/dist/dialog-polyfill.js'); await SpaccDotWeb.RequireScript('https://googlechrome.github.io/dialog-polyfill/dist/dialog-polyfill.js');
}
while (!window.HTMLDialogElement && !window.dialogPolyfill) {
await SpaccDotWeb.Sleep(50);
} }
let output; let output;
if (typeof(params) === 'string') { if (typeof(params) === 'string') {
params = { label: params } params = { label: params }
} }
//params.deleteOnClose ||= true;
const modal = document.createElement('dialog'); const modal = document.createElement('dialog');
const label = (params.label || params.text);
modal.innerHTML = ` modal.innerHTML = `
<p>${params.label || params.text || ''}</p> ${label ? `<p>${label}</p>` : ''}
${params.extraHTML || ''} ${params.extraHTML || ''}
<button name="cancel">🔙 Cancel</button> <button name="cancel">🔙 Cancel</button>
`; `;