Skip to content
Snippets Groups Projects
desk.js 7.95 KiB
Newer Older
  • Learn to ignore specific revisions
  • // Imports
    import * as Auth from "/services/auth/auth.js";
    import * as Common from "/services/common/common.js";
    import * as Messages from "/services/messages/messages.js";
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    import * as DeskModel from "/services/model/desk-model.js";
    
    import { Delete } from "/services/common/delete.js";
    
    // DOM elements
    
    export async function mount(where) {
      const deskComponent = new Desk();
      await deskComponent.mount(where);
      return deskComponent;
    }
    
    class Desk {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
      constructor() {
        this.DeskModel = DeskModel.getDeskModel();
      }
    
    
      async mount(where) {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        this.DeskModel.current_user = await Auth.GetUser();
    
        const mountpoint = where;
        document.getElementById(mountpoint).innerHTML = /* HTML */ `
          <header class="card-header">
            <p class="card-header-title" id="desk-title">
              Bureaux
            </p>
            <button id="desk-new" class="button is-success" disabled>
              <span class="icon is-small">
                <i class="fas fa-plus"></i>
              </span>
            </button>
          </header>
          <div class="card-content">
            <div id="desk-list" class="content">
              Sélectionner une section.
            </div>
          </div>
        `;
        this.mountModal("desk-modal");
        this.handleDom();
      }
    
      handleDom() {
        let deskHandler = this;
        document.getElementById(`desk-new`).addEventListener("click", function () {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
          deskHandler.newDesk(deskHandler.section);
    
        });
    
        document
          .getElementById(`desk-modal-close`)
          .addEventListener("click", function () {
            Common.toggleModal("desk-modal", "desk-modal-card");
          });
        document
          .getElementById(`desk-modal-cancel`)
          .addEventListener("click", function () {
            Common.toggleModal("desk-modal", "desk-modal-card");
          });
        document
          .getElementById(`desk-modal-save`)
          .addEventListener("click", async function () {
            await deskHandler.saveDesk();
          });
        document
          .getElementById(`desk-modal-save-desk`)
          .addEventListener("click", async function () {
            await deskHandler.saveDesk();
            await new Promise((r) => setTimeout(r, 800));
            deskHandler.newDesk(deskHandler.section);
          });
      }
    
      mountModal(where) {
        const mountpoint = where;
        document.getElementById(mountpoint).innerHTML = /* HTML */ `
          <div class="modal-background"></div>
          <div class="modal-card" id="desk-modal-card">
            <header class="modal-card-head">
              <p class="modal-card-title">
                Ajout/modification d'un bureau
              </p>
              <button
                class="delete"
                aria-label="close"
                id="desk-modal-close"
              ></button>
            </header>
            <section class="modal-card-body">
              <div class="field">
                <label>Id</label>
                <div class="control">
                  <input class="input" type="number" id="desk-modal-id" disabled />
                </div>
              </div>
              <div class="field">
                <label>Nom</label>
                <div class="control">
                  <input class="input" type="text" id="desk-modal-name" />
                </div>
              </div>
              <div class="field">
                <label>Subscribed</label>
                <div class="control">
                  <input class="input" type="number" id="desk-modal-subscribed" />
                </div>
              </div>
              <div class="field">
                <div class="control">
                  <label class="checkbox">
                    <input class="input" type="checkbox" id="desk-modal-witness" />
                    Bureau témoin
                  </label>
                </div>
              </div>
            </section>
            <footer class="modal-card-foot">
              <button id="desk-modal-save" class="button is-success">
                Sauvegarder
              </button>
              <button id="desk-modal-save-desk" class="button is-success">
                Sauvegarder (ajouter bureau)
              </button>
              <button id="desk-modal-cancel" class="button">Annuler</button>
            </footer>
          </div>
        `;
      }
    
      async displayDesks() {
        let deskHandler = this;
        let desks = await this.updateDesks();
        const markup = desks.map((section) => this.deskTemplate(section)).join("");
        document.getElementById("desk-list").innerHTML = markup;
        document.getElementById("desk-new").removeAttribute("disabled");
        document.getElementById("desk-title").innerHTML =
          "Bureaux de&nbsp;<em> " + this.section.Name + "</em>";
    
        desks.map((desk) => {
          document
            .getElementById(`desks-desk-edit-${desk.ID}`)
            .addEventListener("click", function () {
              deskHandler.editSection(desk);
            });
          document
            .getElementById(`desks-desk-delete-${desk.ID}`)
            .addEventListener("click", function () {
              new Delete(() => {
                deskHandler.deleteDesk(desk);
              });
            });
        });
      }
    
      emptyDesks() {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        document.getElementById("desk-title").innerHTML = "Bureaux";
    
        this.section = null;
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        document.getElementById("desk-list").innerHTML =
          "Veuillez sélectionner une section";
    
      }
    
      deskTemplate(desk) {
        return /* HTML */ `<div class="card card-list">
          <div class="card-content">
            <div class="content">
              <nav class="level">
                <div class="level-left">
                  ${desk.Name} (Inscrits : ${desk.Subscribed})
                </div>
                <div class="level-right">
                  <a
                    id="desks-desk-edit-${desk.ID}"
                    class="button is-link is-small"
                    title="Modifier"
                  >
                    <span class="icon is-small">
                      <i class="fas fa-pen"></i>
                    </span>
                  </a>
                  <a
                    id="desks-desk-delete-${desk.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>`;
      }
    
      newDesk(section) {
        this.method = "POST";
        this.section = section;
        document.getElementById("desk-modal-id").value = null;
        document.getElementById("desk-modal-name").value = section.Name;
        document.getElementById("desk-modal-subscribed").value = section.Subscribed;
        document.getElementById("desk-modal-witness").checked = section.WitnessDesk;
        Common.toggleModal("desk-modal", "desk-modal-card");
      }
    
      editSection(section) {
        this.method = "PUT";
        document.getElementById("desk-modal-id").value = section.ID;
        document.getElementById("desk-modal-name").value = section.Name;
        document.getElementById("desk-modal-subscribed").value = section.Subscribed;
        document.getElementById("desk-modal-witness").checked = section.WitnessDesk;
        Common.toggleModal("desk-modal", "desk-modal-card");
      }
    
      async saveDesk() {
        if (this.method == "POST")
          document.getElementById("desk-modal-id").value = null;
    
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        await this.DeskModel.saveDesk(
          this.method,
          parseInt(document.getElementById("desk-modal-id").value),
          this.section.ID,
          document.getElementById("desk-modal-name").value,
          parseInt(document.getElementById("desk-modal-subscribed").value),
          document.getElementById("desk-modal-witness").checked
        );
        await this.displayDesks();
    
        Common.toggleModal("desk-modal", "desk-modal-card");
      }
    
      async updateDesks() {
        let deskHandler = this;
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        let desks = await this.DeskModel.getDesks();
        return desks.filter(function (desk) {
          return desk.SectionID == deskHandler.section.ID;
        });
    
      }
    
      async deleteDesk(desk) {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        await this.DeskModel.deleteDesk(desk.ID);
    
        this.displayDesks();
      }
    
    
      async cloneDesks(sectionCloned, sectionToClone) {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        let deskHandler = this;
    
        await sectionToClone.Desks.forEach(async (deskToClone) => {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
          await deskHandler.DeskModel.saveDesk(
            "POST",
            null,
            sectionCloned.ID,
            deskToClone.Name,
            deskToClone.Subscribed,
            deskToClone.WitnessDesk
          );