Newer
Older
// Imports
import * as Auth from "/services/auth/auth.js";
import * as Common from "/services/common/common.js";
import * as ElectionModel from "/services/model/election-model.js";
import * as RoundModel from "/services/model/round-model.js";
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;
this.ElectionModel = ElectionModel.getElectionModel();
this.RoundModel = RoundModel.getRoundModel();
this.ElectionModel.current_user = await Auth.GetUser();
this.RoundModel.current_user = await Auth.GetUser();
const mountpoint = where;
document.getElementById(mountpoint).innerHTML = /* HTML */ `
<header class="card-header">
<p class="card-header-title">
Tours
</p>
<button id="round-new" class="button is-success">
<span class="icon is-small">
<i class="fas fa-plus"></i>
</span>
</button>
</header>
<div class="card-content">
<div id="round-list" class="content">Liste des tours</div>
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
this.mountModal("round-modal");
this.handleDom();
this.displayRounds();
this.refreshElections();
}
handleDom() {
let roundHandler = this;
document.getElementById(`round-new`).addEventListener("click", function () {
roundHandler.newRound();
});
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");
let elections = await this.ElectionModel.getElections();
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
elections.forEach((election) => {});
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() {
const markup = rounds.map((round) => this.roundTemplate(round)).join("");
document.getElementById("round-list").innerHTML = markup;
let roundHandler = this;
rounds.map(async (round) => {
document
.getElementById(`rounds-round-edit-${round.ID}`)
.addEventListener("click", function () {
roundHandler.editRound(round);
});
let election = await this.ElectionModel.getElection(round.ElectionID);
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);
roundHandler.parent.deskRoundsHandler.round = round;
await roundHandler.parent.deskRoundsHandler.displayDesks();
document
.getElementById("candidate-list-new")
.setAttribute("disabled", "true");
roundHandler.parent.candidateListHandler.round = round;
roundHandler.parent.candidateListHandler.displayAreas();
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
});
});
}
roundTemplate(round) {
return /* HTML */ `<div class="card card-list">
<div id="rounds-round-${round.ID}" class="card-content">
<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) {
.getElementById(`rounds-round-${round.ID}`)
.classList.remove("active-card");
.getElementById(`rounds-round-${roundToActivate.ID}`)
.classList.add("active-card");
}
newRound() {
this.method = "POST";
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");
}
editRound(round) {
this.method = "PUT";
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];
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(
this.method,
parseInt(document.getElementById("round-modal-id").value),
parseInt(document.getElementById("round-modal-election-id").value),
document.getElementById("round-modal-date").value,
parseInt(document.getElementById("round-modal-round").value)
);
await this.displayRounds();
Common.toggleModal("round-modal", "round-modal-card");
this.activateRound(round);
// TODO open desks
// TODO open candidateLists
return round;
}
async deleteRound(round) {
await this.RoundModel.deleteRound(round.ID);
// TODO empty desks
// TODO empty candidateLists
document
.getElementById("candidate-list-new")
.setAttribute("disabled", "true");