Skip to content
Snippets Groups Projects
rounds-card.js 10.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • // Imports
    import * as Auth from "/services/auth/auth.js";
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    import * as Common from "/services/common/common.js";
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    import * as ElectionModel from "/services/model/election-model.js";
    import * as RoundModel from "/services/model/round-model.js";
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    import * as Messages from "/services/messages/messages.js";
    import { Delete } from "/services/common/delete.js";
    
    
    // DOM elements
    
    export async function mount(where, parent) {
      const roundsComponent = new Round(parent);
      await roundsComponent.mount(where);
      return roundsComponent;
    }
    
    class Round {
      constructor(parent) {
        this.method = null;
        this.parent = parent;
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        this.ElectionModel = ElectionModel.getElectionModel();
        this.RoundModel = RoundModel.getRoundModel();
    
      }
    
      async mount(where) {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        this.ElectionModel.current_user = await Auth.GetUser();
        this.RoundModel.current_user = await Auth.GetUser();
    
        this.RoundModel.refreshRounds();
    
        const mountpoint = where;
        document.getElementById(mountpoint).innerHTML = /* HTML */ `
          <header class="card-header">
            <p class="card-header-title">
              Tours
            </p>
          </header>
          <div class="card-content">
    
    Alexis POYEN's avatar
    Alexis POYEN committed
            <div id="round-list" class="content">Liste des tours</div>
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        this.mountModal("round-modal");
        this.handleDom();
    
        await this.displayRounds();
    
    Alexis POYEN's avatar
    Alexis POYEN committed
      }
    
      handleDom() {
        let roundHandler = this;
    
        document
          .getElementById(`round-modal-close`)
          .addEventListener("click", function () {
            Common.toggleModal("round-modal", "round-modal-card");
          });
        document
          .getElementById(`round-modal-cancel`)
          .addEventListener("click", function () {
            Common.toggleModal("round-modal", "round-modal-card");
          });
        document
          .getElementById(`round-modal-save`)
          .addEventListener("click", async function () {
            if (document.getElementById("round-modal-election-id").value === "0") {
              Messages.Show("is-danger", "Veuillez sélectionner une élection.");
              return;
            }
            await roundHandler.saveRound();
          });
      }
    
      async refreshElections() {
        let selectElection = document.getElementById("round-modal-election-id");
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        let elections = await this.ElectionModel.getElections();
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        for (let i = selectElection.options.length - 1; i >= 0; i--) {
          selectElection.remove(i);
        }
    
        let el = document.createElement("option");
        el.textContent = "Veuillez sélectionner une élection";
        el.value = 0;
        selectElection.appendChild(el);
        elections.forEach((election) => {
          el = document.createElement("option");
          el.textContent = election.Name;
          el.value = election.ID;
          selectElection.appendChild(el);
        });
      }
    
      mountModal(where) {
        const mountpoint = where;
        document.getElementById(mountpoint).innerHTML = /* HTML */ `
          <div class="modal-background"></div>
          <div class="modal-card" id="round-modal-card">
            <header class="modal-card-head">
              <p class="modal-card-title">Ajout/modification d'un tour</p>
              <button
                class="delete"
                aria-label="close"
                id="round-modal-close"
              ></button>
            </header>
            <section class="modal-card-body">
              <div class="field">
                <label>Id</label>
                <div class="control">
                  <input class="input" type="number" id="round-modal-id" disabled />
                </div>
              </div>
              <div class="field">
                <label>Tour de l'élection</label><br />
                <div class="control select">
                  <select name="ballot-type" id="round-modal-election-id"></select>
                </div>
              </div>
              <div class="field">
                <label>Date</label>
                <div class="control">
                  <input class="input" type="date" id="round-modal-date" />
                </div>
              </div>
              <div class="field">
                <label>Tour n°</label>
                <div class="control">
                  <input class="input" type="number" id="round-modal-round" />
                </div>
              </div>
            </section>
            <footer class="modal-card-foot">
              <button id="round-modal-save" class="button is-success">
                Sauvegarder
              </button>
              <button id="round-modal-cancel" class="button">Annuler</button>
            </footer>
          </div>
        `;
      }
    
      async displayRounds() {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        document.getElementById("round-list").innerHTML = /* HTML */ `<button
          id="round-new"
          class="button large-button is-success"
        >
          <span class="icon is-small">
            <i class="fas fa-plus"></i>
          </span>
        </button>`;
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        let rounds = await this.RoundModel.getRounds();
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        const markup = rounds.map((round) => this.roundTemplate(round)).join("");
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        document.getElementById("round-list").innerHTML += markup;
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    
        let roundHandler = this;
        rounds.map(async (round) => {
          document
            .getElementById(`rounds-round-edit-${round.ID}`)
            .addEventListener("click", function () {
              roundHandler.editRound(round);
            });
    
    Alexis POYEN's avatar
    Alexis POYEN committed
          let election = await this.ElectionModel.getElection(round.ElectionID);
    
    Alexis POYEN's avatar
    Alexis POYEN committed
          document.getElementById(`rounds-round-desc-${round.ID}`).innerHTML =
            election.Name +
            " " +
            document.getElementById(`rounds-round-desc-${round.ID}`).innerHTML;
          document
            .getElementById(`rounds-round-delete-${round.ID}`)
            .addEventListener("click", function () {
              new Delete(() => {
                roundHandler.deleteRound(round);
              });
            });
          document
            .getElementById(`rounds-round-${round.ID}`)
    
            .addEventListener("click", async function () {
              roundHandler.activateRound(round);
    
    Alexis POYEN's avatar
    Alexis POYEN committed
            });
        });
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    
        document.getElementById(`round-new`).addEventListener("click", () => {
          this.newRound();
        });
    
    Alexis POYEN's avatar
    Alexis POYEN committed
      }
    
      roundTemplate(round) {
        return /* HTML */ `<div class="card card-list">
    
          <div id="rounds-round-${round.ID}" class="card-content clickable">
    
    Alexis POYEN's avatar
    Alexis POYEN committed
            <div class="content">
              <nav class="level">
                <div id="rounds-round-desc-${round.ID}" class="level-left">
                  (tour : ${round.Round}, date :
                  ${new Date(round.Date).toLocaleDateString()})
                </div>
                <div class="level-right">
                  <a
                    id="rounds-round-edit-${round.ID}"
                    class="button is-link is-small"
                    title="Modifier"
                  >
                    <span class="icon is-small">
                      <i class="fas fa-pen"></i>
                    </span>
                  </a>
                  <a
                    id="rounds-round-delete-${round.ID}"
                    class="button is-danger is-small"
                    title="Supprimer"
                  >
                    <span class="icon is-small">
                      <i class="fas fa-times"></i>
                    </span>
                  </a>
                </div>
              </nav>
            </div>
          </div>
        </div>`;
      }
    
    
      async activateRound(roundToActivate) {
    
        let election = await this.ElectionModel.getElection(
          roundToActivate.ElectionID
        );
        document.getElementById("candidate-list-title").innerHTML =
          "Liste des candidats par circonscription (" +
          election.Name +
          ", tour : " +
          roundToActivate.Round +
          ", date : " +
          new Date(roundToActivate.Date).toLocaleDateString() +
          ")";
        document.getElementById("desk-round-title").innerHTML =
          "Bureaux de votes (" +
          election.Name +
          ", tour : " +
          roundToActivate.Round +
          ", date : " +
          new Date(roundToActivate.Date).toLocaleDateString() +
          ")";
        this.parent.deskRoundsHandler.round = roundToActivate;
        await this.parent.deskRoundsHandler.displayDesks();
        this.parent.candidateListHandler.round = roundToActivate;
        await this.parent.candidateListHandler.displayAreas();
        this.hideRounds();
    
      async newRound() {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        this.method = "POST";
    
        await this.refreshElections();
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        document.getElementById("round-modal-id").value = null;
        document.getElementById("round-modal-date").value = null;
        document.getElementById("round-modal-round").value = null;
        Common.toggleModal("round-modal", "round-modal-card");
      }
    
    
      async editRound(round) {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        this.method = "PUT";
    
        await this.refreshElections();
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        document.getElementById("round-modal-id").value = round.ID;
        document.getElementById("round-modal-election-id").value = round.ElectionID;
    
        document.getElementById("round-modal-date").value = new Date(round.Date)
          .toISOString()
          .split("T")[0];
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        document.getElementById("round-modal-round").value = round.Round;
        Common.toggleModal("round-modal", "round-modal-card");
      }
    
      async saveRound() {
        if (this.method == "POST")
          document.getElementById("round-modal-id").value = null;
    
    
        let round = await this.RoundModel.saveRound(
    
    Alexis POYEN's avatar
    Alexis POYEN committed
          this.method,
          parseInt(document.getElementById("round-modal-id").value),
    
          parseInt(document.getElementById("round-modal-election-id").value),
    
    Alexis POYEN's avatar
    Alexis POYEN committed
          document.getElementById("round-modal-date").value,
          parseInt(document.getElementById("round-modal-round").value)
        );
        await this.displayRounds();
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        Common.toggleModal("round-modal", "round-modal-card");
        this.activateRound(round);
        return round;
      }
    
      async deleteRound(round) {
    
        await this.RoundModel.deleteRound(round.ID);
    
        document.getElementById("display-rounds").click();
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        document
          .getElementById("candidate-list-new")
          .setAttribute("disabled", "true");
    
    
      hideRounds() {
        let roundsHandler = this;
        document.getElementById("rounds-list").innerHTML =
          '<p id="display-rounds">Afficher la liste des tours</p>';
        document.getElementById("rounds-list").className = "upper-text";
        document.getElementById("rounds-list").parentElement.className =
          "column upper-text";
        document.getElementById("candidate-lists").parentElement.className =
          "column is-full";
        document
          .getElementById("display-rounds")
          .addEventListener("click", async function () {
            await roundsHandler.mount("rounds-list");
            document.getElementById("rounds-list").className = "card";
            document.getElementById("rounds-list").parentElement.className =
              "column is-full";
            document.getElementById("candidate-lists").parentElement.className =
              "column";
          });
      }