// let contentScrollPosition = 0; Init_UI(); function Init_UI() { renderPosts(); $('#createPost').on("click", async function () { saveContentScrollPosition(); renderCreatePostForm(); }); $('#abort').on("click", async function () { renderPosts(); }); $('#aboutCmd').on("click", function () { renderAbout(); }); } function renderAbout() { saveContentScrollPosition(); eraseContent(); $("#createPost").hide(); $("#abort").show(); $("#actionTitle").text("À propos..."); $("#content").append( $(`

Gestionnaire de nouvelles


Petite application de gestion de nouvelles à titre de démonstration d'interface utilisateur monopage réactive.

Auteur: Nicolas Chourot, Mathieu Dubois, Rosalie Boyer

Collège Lionel-Groulx, automne 2024

`)) } async function renderPosts() { showWaitingGif(); $("#actionTitle").text("Liste des nouvelle"); $("#createPost").show(); $("#abort").hide(); let posts = await API_GetPosts(); eraseContent(); if (posts !== null) { posts.forEach(post => { $("#content").append(renderPost(post)); }); restoreContentScrollPosition(); // Attached click events on command icons $(".editCmd").on("click", function () { saveContentScrollPosition(); renderEditPostForm($(this).attr("editPostId")); }); $(".deleteCmd").on("click", function () { saveContentScrollPosition(); renderDeletePostForm($(this).attr("deletePostId")); }); $(".postRow").on("click", function (e) { e.preventDefault(); }) } else { renderError("Service introuvable"); } } function showWaitingGif() { eraseContent(); $("#content").append($("
'")); } function eraseContent() { $("#content").empty(); } function saveContentScrollPosition() { contentScrollPosition = $("#content")[0].scrollTop; } function restoreContentScrollPosition() { $("#content")[0].scrollTop = contentScrollPosition; } function renderError(message) { eraseContent(); $("#content").append( $(`
${message}
`) ); } function renderCreatePostForm() { renderPostForm(); } async function renderEditPostForm(id) { showWaitingGif(); let post = await API_GetPost(id); if (post !== null) renderPostForm(post); else renderError("Post introuvable!"); } async function renderDeletePostForm(id) { showWaitingGif(); $("#createPost").hide(); $("#abort").show(); $("#actionTitle").text("Retrait"); let post = await API_GetPost(id); eraseContent(); if (post !== null) { $("#content").append(`

Effacer la nouvelle suivante?


${post.Title}
${post.Image}
${post.Creation}
${post.Text}

`); $('#deletePost').on("click", async function () { showWaitingGif(); let result = await API_DeletePost(post.Id); if (result) renderPosts(); else renderError("Une erreur est survenue!"); }); $('#cancel').on("click", function () { renderPosts(); }); } else { renderError("Post introuvable!"); } } function newPost() { post = {}; post.Id = 0; post.Title = ""; post.Text = ""; post.Category = ""; post.Image = ""; post.Creation = ""; return post; } function renderPostForm(post = null) { $("#createPost").hide(); $("#abort").show(); eraseContent(); let create = post == null; if (create) { post = newPost(); post.Image = "images/no-avatar.png"; } $("#actionTitle").text(create ? "Création" : "Modification"); $("#content").append(`

`); initImageUploaders(); initFormValidation(); // important do to after all html injection! $('#postForm').on("submit", async function (event) { event.preventDefault(); let post = getFormData($("#postForm")); showWaitingGif(); let result = await API_SavePost(post, create); if (result) renderPosts(); else renderError("Une erreur est survenue! " + API_getcurrentHttpError()); }); $('#cancel').on("click", function () { renderPosts(); }); } function getFormData($form) { const removeTag = new RegExp("(<[a-zA-Z0-9]+>)|()", "g"); var jsonObject = {}; $.each($form.serializeArray(), (index, control) => { jsonObject[control.name] = control.value.replace(removeTag, ""); }); return jsonObject; } function renderPost(post) { return $(`
${post.Text}
`); }