mirror of
https://gitlab.com/SpaccInc/SpaccDotWeb.git
synced 2025-06-05 21:29:12 +02:00
Alt. updates; Build system tweaks
This commit is contained in:
@ -3,13 +3,17 @@
|
||||
|
||||
SpaccDotWeb.AppInit = (meta) => {
|
||||
// ... load meta from argument or page element and add to object
|
||||
return SpaccDotWeb;
|
||||
}
|
||||
return { ...SpaccDotWeb,
|
||||
LocalStorage: (key, value) => SpaccDotWeb.LocalStorage(`${meta.uuid}/v1`, key, value),
|
||||
};
|
||||
};
|
||||
|
||||
SpaccDotWeb.Sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
SpaccDotWeb.LocalStorage = (prefix, key, value) => JSON.parse(
|
||||
localStorage[value !== undefined ? 'setItem' : 'getItem'](
|
||||
`${prefix}/${key}`, JSON.stringify(value)) || null);
|
||||
|
||||
SpaccDotWeb.RequireScript = (src, type) => {
|
||||
return new Promise((resolve) => {
|
||||
return (new Promise((resolve) => {
|
||||
const scriptElem = document.createElement('script');
|
||||
//if (type) {
|
||||
// scriptElem.type = type;
|
||||
@ -19,39 +23,52 @@
|
||||
};
|
||||
scriptElem.src = src;
|
||||
document.body.appendChild(scriptElem);
|
||||
});
|
||||
}
|
||||
}));
|
||||
};
|
||||
// .RequireScripts = (...) => {}
|
||||
|
||||
SpaccDotWeb.ShowModal = async (params) => {
|
||||
// TODO: delete dialogs from DOM after use (garbage collect)?
|
||||
if (!window.HTMLDialogElement && !window.dialogPolyfill) {
|
||||
// TODO include in dependencies, don't load from external server
|
||||
await SpaccDotWeb.RequireScript('https://googlechrome.github.io/dialog-polyfill/dist/dialog-polyfill.js');
|
||||
}
|
||||
let output;
|
||||
if (typeof(params) === 'string') {
|
||||
params = { label: params }
|
||||
params = { label: params };
|
||||
}
|
||||
//params.deleteOnClose ||= true;
|
||||
params.buttonsPosition ||= 'bottom';
|
||||
const modal = document.createElement('dialog');
|
||||
const label = (params.label || params.text);
|
||||
let buttonCancel = `<button name="cancel">${params.cancelText || '🔙️ Cancel'}</button>`;
|
||||
let buttonConfirm = '';
|
||||
if (params.actionConfirm || params.action) {
|
||||
buttonConfirm = `<button name="confirm">${params.confirmText || '⏩️ Confirm'}</button>`;
|
||||
}
|
||||
modal.innerHTML = `
|
||||
${label ? `<p>${label}</p>` : ''}
|
||||
${params.extraHTML || ''}
|
||||
<button name="cancel">🔙️ Cancel</button>
|
||||
`;
|
||||
const buttonsHtml = `<p>
|
||||
${buttonCancel}
|
||||
${buttonConfirm}
|
||||
</p>`;
|
||||
if (params.buttonsPosition == 'top') {
|
||||
modal.innerHTML += `${buttonsHtml}`;
|
||||
}
|
||||
modal.innerHTML += `${params.extraHTML || ''}`;
|
||||
if (params.buttonsPosition == 'bottom') {
|
||||
modal.innerHTML += `${buttonsHtml}`;
|
||||
}
|
||||
if (params.actionConfirm || params.action) {
|
||||
modal.innerHTML += `
|
||||
<button name="confirm">⏩️ Confirm</button>
|
||||
`;
|
||||
const buttonConfirm = modal.querySelector('button[name="confirm"]');
|
||||
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 = modal.querySelector('button[name="cancel"]');
|
||||
buttonCancel.onclick = (event) => {
|
||||
if (params.actionCancel) {
|
||||
output = actionCancel(event, buttonCancel);
|
||||
@ -65,7 +82,9 @@
|
||||
}
|
||||
modal.showModal();
|
||||
return modal;
|
||||
}
|
||||
};
|
||||
|
||||
SpaccDotWeb.Sleep = (ms) => (new Promise((resolve) => setTimeout(resolve, ms)));
|
||||
|
||||
window.SpaccDotWeb ||= SpaccDotWeb;
|
||||
})();
|
||||
|
Reference in New Issue
Block a user