馃悕馃悕馃悕
at dev 111 lines 4.1 kB view raw
1"use strict"; 2let m = {}; 3let userPreferences = { 4 setTheme: (theme) => { 5 if (theme === null) 6 theme = 'void'; 7 userPreferences.theme = theme; 8 localStorage.setItem('theme', theme); 9 const themeLink = document.getElementById('themeLink'); 10 themeLink.href = `style/theme/${theme}.css`; 11 }, 12 setLoadButton: (show) => { 13 if (show === null) 14 show = 'show'; 15 userPreferences.loadButton = show; 16 localStorage.setItem('loadButton', show); 17 }, 18 toggleTheme: () => { 19 if (userPreferences.theme === 'void') { 20 userPreferences.setTheme('parchment'); 21 } 22 else { 23 userPreferences.setTheme('void'); 24 } 25 }, 26 toggleLoadButton: () => { 27 if (userPreferences.loadButton === 'show') { 28 userPreferences.setLoadButton('hide'); 29 } 30 else { 31 userPreferences.setLoadButton('show'); 32 } 33 } 34}; 35userPreferences.setTheme(localStorage.getItem('theme')); 36userPreferences.setLoadButton(localStorage.getItem('loadButton')); 37// Put a loading animation on the page and return a function for removing it. 38let finishedLoading = (() => { 39 let fragment = document.createDocumentFragment(); 40 let loader = document.createElement('div'); 41 let orb0 = document.createElement('div'); 42 let orb1 = document.createElement('div'); 43 let orb2 = document.createElement('div'); 44 let button = document.createElement('button'); 45 button.innerText = 'enter'; 46 button.classList.add('loader-button'); 47 loader.classList.add('loader'); 48 orb0.classList.add('loader-orb'); 49 orb0.classList.add('0'); 50 orb1.classList.add('loader-orb'); 51 orb1.classList.add('1'); 52 orb2.classList.add('loader-orb'); 53 orb2.classList.add('2'); 54 fragment.appendChild(loader); 55 fragment.appendChild(button); 56 loader.appendChild(orb0); 57 orb0.appendChild(orb1); 58 orb1.appendChild(orb2); 59 document.body.appendChild(fragment); 60 let initialElements = Array.prototype.slice.call(document.body.children); 61 let removeLoader = (continuation) => { 62 loader.style.opacity = '0'; 63 button.style.opacity = '0'; 64 orb0.style.left = '50%'; 65 orb1.style.left = '50%'; 66 orb2.style.left = '50%'; 67 setTimeout(() => { 68 initialElements.forEach((element) => { 69 document.body.removeChild(element); 70 }); 71 finishedLoading = () => console.warn('finishedLoading should not be called twice.'); 72 continuation(); 73 }, 1500); 74 }; 75 return userPreferences.loadButton === 'hide' ? removeLoader : (continuation) => { 76 setTimeout(() => { 77 button.style.opacity = '1'; 78 button.addEventListener('click', (event) => removeLoader(continuation)); 79 }, 100); 80 button.style.display = 'initial'; 81 }; 82})(); 83let loadModule = async (module, overwrite = false) => { 84 if (m[module] !== undefined && !overwrite) { 85 console.warn(`Module ${module} is already loaded. Aborting extraneous load.`); 86 return; 87 } 88 m[module] = { ...await import(`./module/${module}.js`) }; 89}; 90fetch('autoload_modules', { method: 'GET' }) 91 .then(response => response.json()) 92 .then(json => { 93 json.modules.reduce((acc, module) => acc.then(async () => { m[module] = { ...await import(`./module/${module}.js`) }; }), Promise.resolve()).finally(() => { 94 finishedLoading(() => { 95 fetch('autorun_modules', { method: 'GET' }) 96 .then(response => response.json()) 97 .then(json => { 98 json.modules.forEach((module) => { 99 if ('instantiate' in m[module]) { 100 m[module].instantiate(document.body); 101 } 102 else { 103 console.warn(`attempted to instantiate non-instantiable module '${module}'`); 104 } 105 }); 106 }) 107 .catch(console.error); 108 }); 109 }); 110}) 111 .catch(console.error);