/** * Values represent classes. * * classname: Only visible in class-state. * not-classname: Not visible in class-state. */ const States = { PUBLIC: "public", IDLE: "idle", RECORDING: "recording", SETUP: "setup", }; Object.freeze(States); let previousState = States.PUBLIC; let currentState = States.PUBLIC; let callbacks = { leaving: {}, entering: {}, }; function updateVisibility() { u("." + previousState).addClass("hidden"); u("." + currentState).removeClass("hidden"); u(".not-" + previousState).removeClass("hidden"); u(".not-" + currentState).addClass("hidden"); processCallbacks(); } function processCallbacks() { var cb = callbacks.leaving[previousState]; if (cb !== undefined) cb(); cb = callbacks.entering[currentState]; if (cb !== undefined) cb(); } function setState(state) { if (Object.values(States).includes(state) === false) { return; } previousState = currentState; currentState = state; localStorage.state = state; updateVisibility(); } function initState() { var savedState = localStorage.state; if (savedState !== undefined) currentState = localStorage.state; }