From 1374d68bd7e04e3061d28e5d86fb680e1473b0a8 Mon Sep 17 00:00:00 2001 From: octospacc Date: Tue, 22 Oct 2024 00:18:41 +0200 Subject: [PATCH] [Alt] alternative function names; add RequireScript options --- BuildLib.sh | 2 +- SpaccDotWeb.Alt.js | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/BuildLib.sh b/BuildLib.sh index 3e2b08f..189e5b5 100755 --- a/BuildLib.sh +++ b/BuildLib.sh @@ -1,4 +1,4 @@ #!/bin/sh -for file in ./SpaccDotWeb*.js +for file in ./SpaccDotWeb.js ./SpaccDotWeb.*.js do node ./SpaccDotWeb.Build.js "BuildScriptFile('${file}')" done diff --git a/SpaccDotWeb.Alt.js b/SpaccDotWeb.Alt.js index 891d698..1478145 100644 --- a/SpaccDotWeb.Alt.js +++ b/SpaccDotWeb.Alt.js @@ -1,4 +1,4 @@ -(function(){ +(function(currentScript){ const SpaccDotWeb = {}; SpaccDotWeb.AppInit = (meta) => { @@ -12,22 +12,27 @@ localStorage[value !== undefined ? 'setItem' : 'getItem']( `${prefix}/${key}`, JSON.stringify(value)) || null); - SpaccDotWeb.RequireScript = (src, type) => { + SpaccDotWeb.requireScript = (src, params={}) => { return (new Promise((resolve) => { const scriptElem = document.createElement('script'); //if (type) { // scriptElem.type = type; //} - scriptElem.onload = (event) => { - resolve(event); - }; + if (params.useCurrentPath) { + const currentPath = currentScript.src; + if (currentPath) { + src = `${currentPath.split('/').slice(0, -1).join('/')}/${src}`; + } + } + scriptElem.onload = resolve; scriptElem.src = src; document.body.appendChild(scriptElem); })); }; // .RequireScripts = (...) => {} + SpaccDotWeb.RequireScript = SpaccDotWeb.requireScript; - SpaccDotWeb.ShowModal = async (params) => { + 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 @@ -83,8 +88,10 @@ modal.showModal(); return modal; }; + SpaccDotWeb.ShowModal = SpaccDotWeb.showModal; - SpaccDotWeb.Sleep = (ms) => (new Promise((resolve) => setTimeout(resolve, ms))); + SpaccDotWeb.sleep = (ms) => (new Promise((resolve) => setTimeout(resolve, ms))); + SpaccDotWeb.Sleep = SpaccDotWeb.sleep; window.SpaccDotWeb ||= SpaccDotWeb; -})(); +})(document.currentScript);