[Client] Improve places admin page + fix its JavaScript
- Use proper HTML; - Add specific CSS of this page in a dedicated file; - Add common header; - Move places' JavaScript in a dedicated file, improve its code and fix its access to DOM elements.
This commit is contained in:
parent
4d46c62aae
commit
2b20185ff5
16
truthinquiry/static/css/admin_ui_places.css
Normal file
16
truthinquiry/static/css/admin_ui_places.css
Normal file
@ -0,0 +1,16 @@
|
||||
.place {
|
||||
align-content: center;
|
||||
align-items: center;
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.place_input {
|
||||
flex-grow: 1;
|
||||
}
|
||||
|
||||
#add_place, #save_changes {
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
}
|
95
truthinquiry/static/js/admin_places.js
Normal file
95
truthinquiry/static/js/admin_places.js
Normal file
@ -0,0 +1,95 @@
|
||||
function addPlace() {
|
||||
const placeElement = document.createElement("div");
|
||||
placeElement.classList.add("place");
|
||||
|
||||
const inputElement = document.createElement("input");
|
||||
inputElement.classList.add("place_input");
|
||||
inputElement.setAttribute("type", "text");
|
||||
inputElement.setAttribute("id", "");
|
||||
|
||||
placeElement.appendChild(inputElement);
|
||||
|
||||
const buttonElement = document.createElement("button");
|
||||
buttonElement.classList.add("delete_place_btn", "action_button", "short_color_transition");
|
||||
buttonElement.setAttribute("title", "Cliquez ici pour supprimer ce lieu");
|
||||
|
||||
const svgElement = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
||||
svgElement.classList.add("action_icon", "short_color_transition");
|
||||
svgElement.setAttribute("viewBox", "0 0 48 48");
|
||||
|
||||
const pathElement = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
||||
pathElement.setAttribute("d",
|
||||
"M12.45 38.7 9.3 35.55 20.85 24 9.3 12.5l3.15-3.2L24 20.8 35.55 9.3l3.15 3.2L27.2 24l11.5 11.55-3.15 3.15L24 27.2Z");
|
||||
|
||||
svgElement.appendChild(pathElement);
|
||||
|
||||
buttonElement.appendChild(svgElement);
|
||||
buttonElement.appendChild(document.createTextNode("Supprimer le lieu"));
|
||||
buttonElement.addEventListener("click", () => deletePlace(buttonElement));
|
||||
|
||||
placeElement.appendChild(buttonElement);
|
||||
|
||||
const placesElement = document.getElementById("places");
|
||||
if (placesElement === null) {
|
||||
// No places element, this should never happen
|
||||
// Do nothing in this case
|
||||
return;
|
||||
}
|
||||
|
||||
placesElement.appendChild(placeElement);
|
||||
}
|
||||
|
||||
function deletePlace(placeRemoveButton) {
|
||||
if (!confirm("Voulez-vous vraiement supprimer ce lieu ?")) {
|
||||
return;
|
||||
}
|
||||
|
||||
const placeElement = placeRemoveButton.parentNode;
|
||||
placeElement.parentNode.removeChild(placeElement);
|
||||
}
|
||||
|
||||
function saveChanges() {
|
||||
const data = [];
|
||||
for (const section of document.getElementsByClassName("place")) {
|
||||
const place = {};
|
||||
place["id"] = section.id;
|
||||
place["name"] = section.querySelector("input").value;
|
||||
data.push(place);
|
||||
}
|
||||
|
||||
makeAPIRequest("admin/setPlaces", {"places": data, "lang": "FR"}, {"content": "json"}).then(() => {
|
||||
alert("Opération effectuée avec succès");
|
||||
});
|
||||
}
|
||||
|
||||
function setListenersToPlaceAdditionButton() {
|
||||
const addPlaceButton = document.getElementById("add_place");
|
||||
if (addPlaceButton === null) {
|
||||
// There is no add_place button, this should never happen
|
||||
// Do nothing in this case
|
||||
return;
|
||||
}
|
||||
|
||||
addPlaceButton.addEventListener("click", addPlace);
|
||||
}
|
||||
|
||||
function setListenersToPlaceDeletionButtons() {
|
||||
for (const deletePlaceButton of document.getElementsByClassName("delete_place_btn")) {
|
||||
deletePlaceButton.addEventListener("click", () => deletePlace(deletePlaceButton));
|
||||
};
|
||||
}
|
||||
|
||||
function setListenersToSaveChangesButton() {
|
||||
const saveChangesButton = document.getElementById("save_changes");
|
||||
if (saveChangesButton === null) {
|
||||
// There is no save_changes button, this should never happen
|
||||
// Do nothing in this case
|
||||
return;
|
||||
}
|
||||
|
||||
saveChangesButton.addEventListener("click", saveChanges);
|
||||
}
|
||||
|
||||
setListenersToPlaceDeletionButtons();
|
||||
setListenersToPlaceAdditionButton();
|
||||
setListenersToSaveChangesButton();
|
@ -1,26 +1,64 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<title>Places</title>
|
||||
<meta charset="UTF-8">
|
||||
<link rel="stylesheet" href="admin_ui.css">
|
||||
<script src="/static/js/admin.js"></script>
|
||||
<script src="/static/js/api.js"></script>
|
||||
<title>Truth Inquiry - Gestion des lieux</title>
|
||||
<link rel="stylesheet" href="/static/css/admin_ui.css">
|
||||
<link rel="stylesheet" href="/static/css/admin_ui_places.css">
|
||||
<link rel="icon" href="/static/images/favicon/favicon_32.png" type="image/png" sizes="32x32">
|
||||
<link rel="icon" href="/static/images/favicon/favicon_64.png" type="image/png" sizes="64x64">
|
||||
<link rel="icon" href="/static/images/favicon/favicon_96.png" type="image/png" sizes="96x96">
|
||||
<link rel="icon" href="/static/images/favicon/favicon_128.png" type="image/png" sizes="128x128">
|
||||
<link rel="icon" href="/static/images/favicon/favicon_192.png" type="image/png" sizes="192x192">
|
||||
<link rel="icon" href="/static/images/favicon/favicon_256.png" type="image/png" sizes="256x256">
|
||||
<link rel="icon" href="/static/images/favicon/favicon_256.png" type="image/png" sizes="512x512">
|
||||
<meta name="color-scheme" content="dark light">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
</head>
|
||||
<body>
|
||||
<a href="/admin"> go Back </a> <br>
|
||||
|
||||
|
||||
<section id="places">
|
||||
<header>
|
||||
<a class="short_color_transition" href="/admin" title="Cliquez ici pour revenir à l'accueil de l'interface d'administration du jeu">Accueil</a>
|
||||
<a class="short_color_transition" href="/admin/questions" title="Cliquez ici pour gérer les questions du jeu">Gestion des questions</a>
|
||||
<a class="short_color_transition" href="/admin/places" title="Cliquez ici pour gérer les lieux du jeu">Gestion des lieux</a>
|
||||
<a class="short_color_transition" href="/admin/traits" title="Cliquez ici pour gérer les réactions du jeu">Gestion des réactions</a>
|
||||
<a class="short_color_transition" href="/api/v1/admin/logout" title="Cliquez ici pour vous déconnecter de l'interface d'administration du jeu">Déconnexion</a>
|
||||
</header>
|
||||
<h1 class="page_title">Truth Inquiry - Interface d'administration</h1>
|
||||
<h2 class="page_category">Gestion des lieux</h2>
|
||||
<p class="page_description">Cliquez sur les champs pour éditer les lieux.</p>
|
||||
<div id="places">
|
||||
{%for place in places%}
|
||||
<section id="{{place['id']}}">
|
||||
<input value="{{place['name']}}">
|
||||
<button onclick="deleteInputPlaces(this)">Delete place</button>
|
||||
</section>
|
||||
<div id="{{place['id']}}" class="place">
|
||||
<input class="place_input" type="text" value="{{place['name']}}">
|
||||
<button class="delete_place_btn action_button short_color_transition" title="Cliquez ici pour supprimer ce lieu">
|
||||
<svg class="action_icon short_color_transition" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 48 48">
|
||||
<path d="M12.45 38.7 9.3 35.55 20.85 24 9.3 12.5l3.15-3.2L24 20.8 35.55 9.3l3.15 3.2L27.2 24l11.5 11.55-3.15 3.15L24 27.2Z"/>
|
||||
</svg>
|
||||
Supprimer le lieu
|
||||
</button>
|
||||
</div>
|
||||
{%endfor%}
|
||||
</section>
|
||||
<button onclick="addInputPlaces()">Add new</button>
|
||||
<button onclick="saveFormPlaces()">Save changes</button>
|
||||
|
||||
</div>
|
||||
<button id="add_place" class="action_button short_color_transition" title="Cliquez ici pour ajouter un lieu">
|
||||
<svg class="action_icon short_color_transition" xmlns="http://www.w3.org/2000/svg" viewBox="0 96 960 960">
|
||||
<path d="M435 871V622H185v-91h250V281h91v250h250v91H526v249h-91Z"/>
|
||||
</svg>
|
||||
Ajouter un lieu
|
||||
</button>
|
||||
<button id="save_changes" class="action_button short_color_transition" title="Cliquez ici pour enregistrer vos changements">
|
||||
<svg class="action_icon short_color_transition" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 48 48">
|
||||
<path d="M18.9 36.75 6.65 24.5l3.3-3.3 8.95 9L38 11.1l3.3 3.25Z"/>
|
||||
</svg>
|
||||
Sauvegarder les changements
|
||||
</button>
|
||||
<noscript>
|
||||
<div class="alert_dialog_background"></div>
|
||||
<dialog>
|
||||
<h3 class="alert_dialog_title">JavaScript nécessaire</h3>
|
||||
<p class="alert_dialog_msg unsupported_browser_msg">Désolé, mais JavaScript est nécessaire pour faire fonctionner cette page. Veuillez l'activer dans votre navigateur ou en utiliser un qui le supporte afin de pouvoir gérer les lieux.</p>
|
||||
</dialog>
|
||||
</noscript>
|
||||
<script src="/static/js/api.js"></script>
|
||||
<script src="/static/js/admin_places.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
Loading…
Reference in New Issue
Block a user