Skip to content
Snippets Groups Projects
main.js 2.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alexis Poyen's avatar
    Alexis Poyen committed
    import * as Home from "/components/home/home.js";
    import * as Users from "/components/users/users.js";
    
    import * as Management from "/components/management/management.js";
    
    import * as Votes from "/components/vote/vote-page.js";
    
    import * as Visualization from "/components/visualization/visualization-page.js";
    
    Alexis Poyen's avatar
    Alexis Poyen committed
    import * as Login from "/components/login/login.js";
    import * as Navbar from "/components/navbar/navbar.js";
    import { AnimateCSS } from "/services/common/common.js";
    
    const mountPoint = document.getElementById("main");
    const spinner = document.getElementById("spinner");
    
    
    document.addEventListener("DOMContentLoaded", function () {
    
    Alexis Poyen's avatar
    Alexis Poyen committed
      Navbar.mount("navbar");
      window.addEventListener("hashchange", navigate);
      navigate();
    });
    
    async function navigate() {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
      window.clearInterval(window.intervalRefreshResults);
    
    Alexis Poyen's avatar
    Alexis Poyen committed
      switch (location.hash) {
        case "#home":
    
          load(mountPoint, async function () {
    
    Alexis Poyen's avatar
    Alexis Poyen committed
            await Home.mount("main");
          });
          break;
    
        case "#votes":
          load(mountPoint, async function () {
            await Votes.mount("main");
          });
          break;
    
        case "#visualization":
          load(mountPoint, async function () {
            await Visualization.mount("main");
          });
          break;
    
    Alexis Poyen's avatar
    Alexis Poyen committed
        case "#users":
    
          load(mountPoint, async function () {
    
    Alexis Poyen's avatar
    Alexis Poyen committed
            await Users.mount("main");
          });
          break;
    
        case "#management":
          load(mountPoint, async function () {
            await Management.mount("main");
          });
          break;
    
    Alexis Poyen's avatar
    Alexis Poyen committed
        case "#login":
    
          load(mountPoint, async function () {
    
    Alexis Poyen's avatar
    Alexis Poyen committed
            await Login.mount("main");
          });
          break;
        default:
          location.hash = "#home";
          break;
      }
    }
    
    async function load(element, domAlteration) {
    
      AnimateCSS(element, "fadeOut", async function () {
    
    Alexis Poyen's avatar
    Alexis Poyen committed
        element.classList.add("is-hidden");
        spinner.classList.remove("is-hidden");
        AnimateCSS(spinner, "fadeIn");
        if (typeof domAlteration === "function") {
          await domAlteration();
    
          AnimateCSS(spinner, "fadeOut", function () {
    
    Alexis Poyen's avatar
    Alexis Poyen committed
            spinner.classList.add("is-hidden");
          });
          element.classList.remove("is-hidden");
          AnimateCSS(element, "fadeIn");
        }
      });
    }