-
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.
Merge pull request #9 from lenapokrovskaya/module8-task2
- Loading branch information
Showing
3 changed files
with
47 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,57 @@ | ||
const сommentsListElement = document.querySelector('.social__comments'); | ||
const socialCommentElement = сommentsListElement.querySelector('.social__comment'); | ||
const fragmentElement = document.createDocumentFragment(); | ||
const STEP_COMMENTS = 5; | ||
let currentCommentCount = 0; | ||
let currentPostComments = []; | ||
|
||
const bigPictureElement = document.querySelector('.big-picture'); | ||
const socialCommentsElement = bigPictureElement.querySelector('.social__comments'); | ||
const socialCommentElement = socialCommentsElement.querySelector('.social__comment'); | ||
const commentTotalCountElement = bigPictureElement.querySelector('.social__comment-total-count'); | ||
const commentShownCountElement = bigPictureElement.querySelector('.social__comment-shown-count'); | ||
const commentsLoaderElement = bigPictureElement.querySelector('.social__comments-loader'); | ||
socialCommentsElement.innerHTML = ''; | ||
|
||
//Функция создания комментария | ||
const renderComment = ({avatar, name, message}) => { | ||
const clonedCommentElement = socialCommentElement.cloneNode(true); | ||
const commentPictureElement = clonedCommentElement.querySelector('.social__picture'); | ||
const commentText = clonedCommentElement.querySelector('.social__text'); | ||
const commentTextElement = clonedCommentElement.querySelector('.social__text'); | ||
commentPictureElement.src = avatar; | ||
commentPictureElement.alt = name; | ||
commentText.textContent = message; | ||
commentTextElement.textContent = message; | ||
return clonedCommentElement; | ||
}; | ||
|
||
//Отрисуем массив комментариев для каждого поста | ||
const renderComments = (comments) => { | ||
const postСomments = comments; | ||
postСomments.forEach((comment) => { | ||
//Функция отрисовки показываемых комментариев частями | ||
const renderShownComments = () => { | ||
const fragmentElement = document.createDocumentFragment(); | ||
const shownComments = currentPostComments.slice(currentCommentCount, currentCommentCount + STEP_COMMENTS); | ||
const shownCommentsLength = shownComments.length + currentCommentCount; | ||
|
||
shownComments.forEach((comment) => { | ||
const commentElement = renderComment(comment); | ||
сommentsListElement.appendChild(commentElement); | ||
fragmentElement.appendChild(commentElement); | ||
}); | ||
|
||
сommentsListElement.innerHTML = ''; | ||
сommentsListElement.appendChild(fragmentElement); | ||
socialCommentsElement.appendChild(fragmentElement); | ||
commentShownCountElement.textContent = shownCommentsLength; | ||
commentTotalCountElement.textContent = currentPostComments.length; | ||
commentsLoaderElement.classList.toggle('hidden', shownCommentsLength >= currentPostComments.length); | ||
currentCommentCount += STEP_COMMENTS; | ||
}; | ||
|
||
//Функция отрисовки и обновления по клику количества комментариев | ||
const renderComments = (comments) => { | ||
currentPostComments = comments; | ||
renderShownComments(); | ||
commentsLoaderElement.addEventListener('click', renderShownComments); | ||
}; | ||
|
||
export {renderComments}; | ||
//Функция очистки комментариев | ||
const clearComments = () => { | ||
currentCommentCount = 0; | ||
socialCommentsElement.innerHTML = ''; | ||
commentsLoaderElement.classList.remove('hidden'); | ||
commentsLoaderElement.removeEventListener('click', renderShownComments); | ||
}; | ||
|
||
export {renderComments, clearComments}; |
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