JavaScript Korrektur

This commit is contained in:
2026-07-18 17:20:14 +02:00
parent 7b7208ad6b
commit b564306fb3
2 changed files with 44 additions and 23 deletions
+1
View File
@@ -383,3 +383,4 @@ if ($replyAuthor === null) {
<?php endif; ?> <?php endif; ?>
</section> </section>
</main> </main>
<script src="js/comments.js"></script>
+37 -17
View File
@@ -11,7 +11,7 @@ document.addEventListener("DOMContentLoaded", function () {
const parentCommentInput = document.getElementById("parent-comment-id"); const parentCommentInput = document.getElementById("parent-comment-id");
const replyInfo = document.getElementById("reply-info"); const replyInfo = document.getElementById("reply-info");
if (!form || !commentsList || !commentContent || !parentCommentInput || !replyInfo) { if (!form || !commentsList || !commentContent || !parentCommentInput) {
return; return;
} }
@@ -29,8 +29,13 @@ document.addEventListener("DOMContentLoaded", function () {
event.preventDefault(); event.preventDefault();
parentCommentInput.value = replyLink.dataset.commentId; parentCommentInput.value = replyLink.dataset.commentId;
replyInfo.textContent = "Antwort auf " + replyLink.dataset.author;
if (replyInfo) {
replyInfo.textContent =
"Antwort auf " + replyLink.dataset.author;
replyInfo.style.display = "block"; replyInfo.style.display = "block";
}
commentContent.focus(); commentContent.focus();
}); });
} }
@@ -58,14 +63,22 @@ document.addEventListener("DOMContentLoaded", function () {
"X-Requested-With": "XMLHttpRequest" "X-Requested-With": "XMLHttpRequest"
} }
}) })
.then(response => response.json()) .then(function (response) {
.then(data => { if (!response.ok) {
throw new Error("Fehlerhafte Serverantwort.");
}
return response.json();
})
.then(function (data) {
if (!data.success) { if (!data.success) {
alert(data.message); alert(data.message);
return; return;
} }
const emptyMessage = commentsList.querySelector(".no-comments-message"); const emptyMessage = commentsList.querySelector(
".no-comments-message"
);
if (emptyMessage) { if (emptyMessage) {
emptyMessage.remove(); emptyMessage.remove();
@@ -73,8 +86,9 @@ document.addEventListener("DOMContentLoaded", function () {
const commentElement = document.createElement("div"); const commentElement = document.createElement("div");
commentElement.classList.add("comment-item"); commentElement.classList.add("comment-item");
commentElement.dataset.commentId = data.commentId;
if (parentCommentId) { if (parentCommentId !== "") {
commentElement.classList.add("comment-reply"); commentElement.classList.add("comment-reply");
commentElement.innerHTML = ` commentElement.innerHTML = `
@@ -85,17 +99,16 @@ document.addEventListener("DOMContentLoaded", function () {
<p>${escapeHtml(data.content).replace(/\n/g, "<br>")}</p> <p>${escapeHtml(data.content).replace(/\n/g, "<br>")}</p>
`; `;
const parentReplies = document.querySelector( const parentReplies = commentsList.querySelector(
`.comment-item[data-comment-id="${parentCommentId}"] .comment-replies` `.comment-item[data-comment-id="${parentCommentId}"] .comment-replies`
); );
if (parentReplies) { if (parentReplies) {
parentReplies.appendChild(commentElement); parentReplies.appendChild(commentElement);
}
} else { } else {
commentElement.dataset.commentId = data.commentId; commentsList.prepend(commentElement);
}
} else {
commentElement.innerHTML = ` commentElement.innerHTML = `
<p> <p>
<strong>${escapeHtml(data.author)}</strong> <strong>${escapeHtml(data.author)}</strong>
@@ -103,10 +116,12 @@ document.addEventListener("DOMContentLoaded", function () {
</p> </p>
<p>${escapeHtml(data.content).replace(/\n/g, "<br>")}</p> <p>${escapeHtml(data.content).replace(/\n/g, "<br>")}</p>
<a href="#comment-form" <a
href="#comment-form"
class="reply-button" class="reply-button"
data-comment-id="${escapeHtml(data.commentId)}" data-comment-id="${escapeHtml(data.commentId)}"
data-author="${escapeHtml(data.author)}"> data-author="${escapeHtml(data.author)}"
>
Antworten Antworten
</a> </a>
@@ -115,7 +130,8 @@ document.addEventListener("DOMContentLoaded", function () {
commentsList.prepend(commentElement); commentsList.prepend(commentElement);
const newReplyButton = commentElement.querySelector(".reply-button"); const newReplyButton =
commentElement.querySelector(".reply-button");
if (newReplyButton) { if (newReplyButton) {
registerReplyButton(newReplyButton); registerReplyButton(newReplyButton);
@@ -124,10 +140,14 @@ document.addEventListener("DOMContentLoaded", function () {
commentContent.value = ""; commentContent.value = "";
parentCommentInput.value = ""; parentCommentInput.value = "";
if (replyInfo) {
replyInfo.textContent = ""; replyInfo.textContent = "";
replyInfo.style.display = "none"; replyInfo.style.display = "none";
}
}) })
.catch(() => { .catch(function (error) {
console.error(error);
alert("Kommentar konnte nicht gesendet werden."); alert("Kommentar konnte nicht gesendet werden.");
}); });
}); });
@@ -135,12 +155,12 @@ document.addEventListener("DOMContentLoaded", function () {
/** /**
* Entfernt HTML-Sonderzeichen aus Nutzereingaben. * Entfernt HTML-Sonderzeichen aus Nutzereingaben.
* *
* @param {string} text Zu bereinigender Text * @param {*} text Zu bereinigender Text
* @returns {string} Sicherer Text * @returns {string} Sicherer Text
*/ */
function escapeHtml(text) { function escapeHtml(text) {
const div = document.createElement("div"); const div = document.createElement("div");
div.textContent = text; div.textContent = String(text ?? "");
return div.innerHTML; return div.innerHTML;
} }
}); });