2024-10-22 00:18:41 +02:00
|
|
|
(function(currentScript){
|
2024-01-03 20:43:05 +01:00
|
|
|
const SpaccDotWeb = {};
|
|
|
|
|
|
|
|
SpaccDotWeb.AppInit = (meta) => {
|
|
|
|
// ... load meta from argument or page element and add to object
|
2024-09-14 23:01:50 +02:00
|
|
|
return { ...SpaccDotWeb,
|
|
|
|
LocalStorage: (key, value) => SpaccDotWeb.LocalStorage(`${meta.uuid}/v1`, key, value),
|
|
|
|
};
|
|
|
|
};
|
2024-01-03 20:43:05 +01:00
|
|
|
|
2024-09-14 23:01:50 +02:00
|
|
|
SpaccDotWeb.LocalStorage = (prefix, key, value) => JSON.parse(
|
|
|
|
localStorage[value !== undefined ? 'setItem' : 'getItem'](
|
|
|
|
`${prefix}/${key}`, JSON.stringify(value)) || null);
|
2024-01-03 20:43:05 +01:00
|
|
|
|
2024-10-22 00:18:41 +02:00
|
|
|
SpaccDotWeb.requireScript = (src, params={}) => {
|
2024-09-14 23:01:50 +02:00
|
|
|
return (new Promise((resolve) => {
|
2024-01-05 23:57:04 +01:00
|
|
|
const scriptElem = document.createElement('script');
|
|
|
|
//if (type) {
|
|
|
|
// scriptElem.type = type;
|
|
|
|
//}
|
2024-10-22 00:18:41 +02:00
|
|
|
if (params.useCurrentPath) {
|
|
|
|
const currentPath = currentScript.src;
|
|
|
|
if (currentPath) {
|
|
|
|
src = `${currentPath.split('/').slice(0, -1).join('/')}/${src}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
scriptElem.onload = resolve;
|
2024-01-05 23:57:04 +01:00
|
|
|
scriptElem.src = src;
|
|
|
|
document.body.appendChild(scriptElem);
|
2024-09-14 23:01:50 +02:00
|
|
|
}));
|
|
|
|
};
|
2024-02-23 01:38:08 +01:00
|
|
|
// .RequireScripts = (...) => {}
|
2024-10-22 00:18:41 +02:00
|
|
|
SpaccDotWeb.RequireScript = SpaccDotWeb.requireScript;
|
2024-01-03 20:43:05 +01:00
|
|
|
|
2024-10-22 00:18:41 +02:00
|
|
|
SpaccDotWeb.showModal = async (params) => {
|
2024-01-03 20:43:05 +01:00
|
|
|
// TODO: delete dialogs from DOM after use (garbage collect)?
|
|
|
|
if (!window.HTMLDialogElement && !window.dialogPolyfill) {
|
2024-09-14 23:01:50 +02:00
|
|
|
// TODO include in dependencies, don't load from external server
|
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') {
|
2024-09-14 23:01:50 +02:00
|
|
|
params = { label: params };
|
2024-01-03 20:43:05 +01:00
|
|
|
}
|
2024-01-05 23:57:04 +01:00
|
|
|
//params.deleteOnClose ||= true;
|
2024-09-14 23:01:50 +02:00
|
|
|
params.buttonsPosition ||= 'bottom';
|
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-09-14 23:01:50 +02:00
|
|
|
let buttonCancel = `<button name="cancel">${params.cancelText || '🔙️ Cancel'}</button>`;
|
|
|
|
let buttonConfirm = '';
|
|
|
|
if (params.actionConfirm || params.action) {
|
|
|
|
buttonConfirm = `<button name="confirm">${params.confirmText || '⏩️ Confirm'}</button>`;
|
|
|
|
}
|
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
|
|
|
`;
|
2024-09-14 23:01:50 +02:00
|
|
|
const buttonsHtml = `<p>
|
|
|
|
${buttonCancel}
|
|
|
|
${buttonConfirm}
|
|
|
|
</p>`;
|
|
|
|
if (params.buttonsPosition == 'top') {
|
|
|
|
modal.innerHTML += `${buttonsHtml}`;
|
|
|
|
}
|
|
|
|
modal.innerHTML += `${params.extraHTML || ''}`;
|
|
|
|
if (params.buttonsPosition == 'bottom') {
|
|
|
|
modal.innerHTML += `${buttonsHtml}`;
|
|
|
|
}
|
2024-01-03 20:43:05 +01:00
|
|
|
if (params.actionConfirm || params.action) {
|
2024-09-14 23:01:50 +02:00
|
|
|
buttonConfirm = modal.querySelector('button[name="confirm"]');
|
2024-01-03 20:43:05 +01:00
|
|
|
buttonConfirm.onclick = (event) => {
|
|
|
|
output = (params.actionConfirm || params.action)(event, buttonConfirm);
|
|
|
|
modal.close();
|
|
|
|
return output;
|
|
|
|
};
|
|
|
|
}
|
2024-09-14 23:01:50 +02:00
|
|
|
buttonCancel = modal.querySelector('button[name="cancel"]');
|
2024-01-03 20:43:05 +01:00
|
|
|
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;
|
2024-09-14 23:01:50 +02:00
|
|
|
};
|
2024-10-22 00:18:41 +02:00
|
|
|
SpaccDotWeb.ShowModal = SpaccDotWeb.showModal;
|
2024-09-14 23:01:50 +02:00
|
|
|
|
2024-10-22 00:18:41 +02:00
|
|
|
SpaccDotWeb.sleep = (ms) => (new Promise((resolve) => setTimeout(resolve, ms)));
|
|
|
|
SpaccDotWeb.Sleep = SpaccDotWeb.sleep;
|
2024-01-03 20:43:05 +01:00
|
|
|
|
|
|
|
window.SpaccDotWeb ||= SpaccDotWeb;
|
2024-10-22 00:18:41 +02:00
|
|
|
})(document.currentScript);
|