Kommentarfunktion für Nutzer einschränken
This commit is contained in:
+13
-60
@@ -1,13 +1,10 @@
|
||||
/**
|
||||
* Initialisiert die Kommentarfunktion.
|
||||
*
|
||||
* Ermöglicht:
|
||||
* - Erstellen neuer Kommentare
|
||||
* - Antworten auf bestehende Kommentare
|
||||
* - AJAX-Kommunikation ohne Seitenneuladen
|
||||
* Kommentare werden per AJAX gespeichert,
|
||||
* ohne dass die Seite neu geladen werden muss.
|
||||
*/
|
||||
document.addEventListener("DOMContentLoaded", function () {
|
||||
|
||||
const form = document.getElementById("comment-form");
|
||||
const commentsList = document.getElementById("comments-list");
|
||||
const commentContent = document.getElementById("comment-content");
|
||||
@@ -19,65 +16,44 @@ document.addEventListener("DOMContentLoaded", function () {
|
||||
}
|
||||
|
||||
/**
|
||||
* Registriert die Antwort-Buttons.
|
||||
*
|
||||
* Beim Klick wird die ID des Eltern-Kommentars gespeichert,
|
||||
* damit die neue Nachricht als Antwort angelegt werden kann.
|
||||
* Registriert alle Antwort-Buttons.
|
||||
*/
|
||||
document.querySelectorAll(".reply-button").forEach(function (button) {
|
||||
|
||||
button.addEventListener("click", function () {
|
||||
|
||||
parentCommentInput.value = button.dataset.commentId;
|
||||
|
||||
replyInfo.textContent =
|
||||
"Antwort auf " + button.dataset.author;
|
||||
|
||||
replyInfo.textContent = "Antwort auf " + button.dataset.author;
|
||||
replyInfo.style.display = "block";
|
||||
|
||||
commentContent.focus();
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Verarbeitet das Absenden eines Kommentars.
|
||||
*
|
||||
* Die Daten werden per AJAX an den Server gesendet,
|
||||
* sodass die Seite nicht neu geladen werden muss.
|
||||
* Sendet Kommentare per AJAX an den Server.
|
||||
*/
|
||||
form.addEventListener("submit", function (event) {
|
||||
|
||||
event.preventDefault();
|
||||
|
||||
const formData = new FormData(form);
|
||||
const parentCommentId = parentCommentInput.value;
|
||||
|
||||
/*
|
||||
* Sendet den Kommentar an den Server
|
||||
* und speichert ihn in der Datenbank.
|
||||
*/
|
||||
fetch("php/ajax/add-comment.php", {
|
||||
method: "POST",
|
||||
body: formData
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
|
||||
if (!data.success) {
|
||||
alert(data.message);
|
||||
return;
|
||||
}
|
||||
|
||||
const emptyMessage =
|
||||
commentsList.querySelector(".no-comments-message");
|
||||
const emptyMessage = commentsList.querySelector(".no-comments-message");
|
||||
|
||||
if (emptyMessage) {
|
||||
emptyMessage.remove();
|
||||
}
|
||||
|
||||
const commentElement =
|
||||
document.createElement("div");
|
||||
|
||||
const commentElement = document.createElement("div");
|
||||
commentElement.classList.add("comment-item");
|
||||
|
||||
if (parentCommentId) {
|
||||
@@ -92,60 +68,37 @@ document.addEventListener("DOMContentLoaded", function () {
|
||||
<p>${escapeHtml(data.content).replace(/\n/g, "<br>")}</p>
|
||||
`;
|
||||
|
||||
/*
|
||||
* Antworten werden unter dem
|
||||
* zugehörigen Kommentar angezeigt.
|
||||
*/
|
||||
if (parentCommentId) {
|
||||
|
||||
const parentComment = document.querySelector(
|
||||
const parentReplies = document.querySelector(
|
||||
`.comment-item[data-comment-id="${parentCommentId}"] .comment-replies`
|
||||
);
|
||||
|
||||
if (parentComment) {
|
||||
parentComment.appendChild(commentElement);
|
||||
if (parentReplies) {
|
||||
parentReplies.appendChild(commentElement);
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
/*
|
||||
* Normale Kommentare werden
|
||||
* oben in die Liste eingefügt.
|
||||
*/
|
||||
commentElement.dataset.commentId = "";
|
||||
|
||||
commentsList.prepend(commentElement);
|
||||
}
|
||||
|
||||
/*
|
||||
* Formular zurücksetzen.
|
||||
*/
|
||||
commentContent.value = "";
|
||||
parentCommentInput.value = "";
|
||||
replyInfo.textContent = "";
|
||||
replyInfo.style.display = "none";
|
||||
})
|
||||
.catch(() => {
|
||||
|
||||
alert(
|
||||
"Kommentar konnte nicht gesendet werden."
|
||||
);
|
||||
alert("Kommentar konnte nicht gesendet werden.");
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Escaped HTML-Sonderzeichen zur Vermeidung
|
||||
* von XSS-Angriffen.
|
||||
* Entfernt HTML-Sonderzeichen aus Nutzereingaben.
|
||||
*
|
||||
* @param {string} text Zu bereinigender Text
|
||||
* @returns {string} HTML-sicherer Text
|
||||
* @returns {string} Sicherer Text
|
||||
*/
|
||||
function escapeHtml(text) {
|
||||
|
||||
const div = document.createElement("div");
|
||||
|
||||
div.textContent = text;
|
||||
|
||||
return div.innerHTML;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user