-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Keks
committed
Dec 11, 2024
1 parent
20310e6
commit 07852b5
Showing
2 changed files
with
60 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const commentsList = document.querySelector('.social__comments'); | ||
const commentTemplate = commentsList.querySelector('.social__comment'); | ||
const commentCount = document.querySelector('.social__comment-count'); | ||
const commentsLoader = document.querySelector('.social__comment-loader'); | ||
|
||
const VISIBLE_COMMENTS = 5; | ||
let shownComments = 0; | ||
let comments = []; | ||
|
||
commentsList.innerHTML = ''; | ||
|
||
const renderNextComments = () => { | ||
const commentsFragment = document.createDocumentFragment(); | ||
const renderedComments = comments.slice(shownComments, shownComments + VISIBLE_COMMENTS); | ||
const renderedCommentsLength = renderedComments.length + shownComments; | ||
|
||
renderedComments.forEach ((comment) => { | ||
const pictureComment = commentTemplate.cloneNode(true); | ||
|
||
pictureComment.querySelector('.social__picture').src = comment.avatar; | ||
pictureComment.querySelector('.social__picture').alt = comment.name; | ||
pictureComment.querySelector('.social__text').textContent = comment.message; | ||
|
||
commentsFragment.appendChild(pictureComment); | ||
}); | ||
|
||
commentsList.appendChild(commentsFragment); | ||
|
||
commentCount.firstChild.textContent = `${renderedCommentsLength} из `; | ||
commentCount.querySelector('.comments-count').textContent = comments.length; | ||
|
||
if (renderedCommentsLength >= comments.length) { | ||
commentsLoader.classList.add('hidden'); | ||
} | ||
|
||
shownComments += VISIBLE_COMMENTS; | ||
}; | ||
|
||
const clearComments = () => { | ||
shownComments = 0; | ||
commentsList.innerHTML = ''; | ||
commentsLoader.classList.remove('hidden'); | ||
commentsLoader.removeEventListener('click', renderNextComments); | ||
}; | ||
|
||
const renderComments = (currentPhotoComments) => { | ||
comments = currentPhotoComments; | ||
renderNextComments(); | ||
|
||
commentsLoader.addEventListener('click', renderNextComments); | ||
}; | ||
|
||
export {clearComments, renderComments}; |