|
| 1 | +const dSurface = document.getElementById("dSurface"); |
| 2 | + |
| 3 | +async function PreloadMenu(menuId) { |
| 4 | + // fetch menu/html/{menuId}.html and menu/js/{menuId}.js, return nothing and do nothing with it |
| 5 | + await Promise.all([ |
| 6 | + fetch(`menu/html/${menuId}.html`).then(response => response.text()), |
| 7 | + fetch(`menu/js/${menuId}.js`).then(response => response.text()) |
| 8 | + ]); |
| 9 | +} |
| 10 | + |
| 11 | +async function LoadMenu(menuId, background) { |
| 12 | + // fetch menu/html/{menuId}.html and menu/js/{menuId}.js. run the js code after injecting the html into the #dSurface element. |
| 13 | + const [htmlResponse, jsResponse] = await Promise.all([ |
| 14 | + fetch(`menu/html/${menuId}.html`), |
| 15 | + fetch(`menu/js/${menuId}.js`) |
| 16 | + ]); |
| 17 | + |
| 18 | + const html = await htmlResponse.text(); |
| 19 | + const js = await jsResponse.text(); |
| 20 | + |
| 21 | + // make menuContainer for menu |
| 22 | + const menuContainer = document.createElement("div"); |
| 23 | + menuContainer.id = `dMenu_${menuId}`; |
| 24 | + menuContainer.style.display = "block"; |
| 25 | + menuContainer.style.position = "absolute"; |
| 26 | + menuContainer.style.width = "100%"; |
| 27 | + menuContainer.style.height = "100%"; |
| 28 | + menuContainer.style.top = "0"; |
| 29 | + menuContainer.style.left = "0"; |
| 30 | + if (background) { |
| 31 | + menuContainer.style.background = `url(${background}) no-repeat`; |
| 32 | + } |
| 33 | + menuContainer.innerHTML = html; |
| 34 | + dSurface.appendChild(menuContainer); |
| 35 | + |
| 36 | + // run the js code (provide menuContainer to the menu script) |
| 37 | + const runMenuScript = new Function("menuContainer", "menuId", `"use strict";\n${js}\n`); |
| 38 | + runMenuScript(menuContainer, menuId, background); |
| 39 | + |
| 40 | + return menuContainer; |
| 41 | +} |
| 42 | + |
| 43 | +async function UnloadMenu(menuId) { |
| 44 | + // remove the menu menuContainer from #dSurface |
| 45 | + const menuContainer = document.getElementById(`dMenu_${menuId}`); |
| 46 | + if (menuContainer) { |
| 47 | + dSurface.removeChild(menuContainer); |
| 48 | + } |
| 49 | +} |
0 commit comments