|
| 1 | +import { getPluralWord } from './utils.js'; |
| 2 | + |
| 3 | +const MAX_COMMENTS_VIEW = 5; |
| 4 | + |
| 5 | +const bigPicture = document.querySelector('.big-picture'); |
| 6 | +const commentsLoader = bigPicture.querySelector('.comments-loader'); |
| 7 | +const socialCommentsContainer = bigPicture.querySelector('.social__comments'); |
| 8 | +const socialComment = bigPicture.querySelector('.social__comment'); |
| 9 | +const commentCountContainer = bigPicture.querySelector('.social__comment-count'); |
| 10 | +const commentShownCount = bigPicture.querySelector('.social__comment-shown-count'); |
| 11 | +const commentTotalCount = bigPicture.querySelector('.social__comment-total-count'); |
| 12 | + |
| 13 | +let currentListComments = []; |
| 14 | +let currentCommentsCount = 0; |
| 15 | + |
| 16 | +const renderOneComment = ({ avatar, name, message }) => { |
| 17 | + const newComment = socialComment.cloneNode(true); |
| 18 | + const authorsImage = newComment.querySelector('.social__picture'); |
| 19 | + |
| 20 | + authorsImage.src = avatar; |
| 21 | + authorsImage.alt = name; |
| 22 | + newComment.querySelector('.social__text').textContent = message; |
| 23 | + return newComment; |
| 24 | +}; |
| 25 | + |
| 26 | +const renderComments = (comments) => { |
| 27 | + comments.forEach((comment) => { |
| 28 | + socialCommentsContainer.append(renderOneComment(comment)); |
| 29 | + }); |
| 30 | +}; |
| 31 | + |
| 32 | +export const initComments = (comments) => { |
| 33 | + const showCommentText = commentCountContainer.childNodes[4]; |
| 34 | + const word = getPluralWord(comments.length, ['комментарий', 'комментария', 'комментариев']); |
| 35 | + |
| 36 | + if (comments.length <= MAX_COMMENTS_VIEW) { |
| 37 | + commentsLoader.classList.add('hidden'); |
| 38 | + } else { |
| 39 | + commentsLoader.classList.remove('hidden'); |
| 40 | + } |
| 41 | + |
| 42 | + commentTotalCount.textContent = comments.length; |
| 43 | + showCommentText.textContent = `${word.padStart(word.length + 1)}`; |
| 44 | + socialCommentsContainer.innerHTML = ''; |
| 45 | + |
| 46 | + currentListComments = comments; |
| 47 | + currentCommentsCount = Math.min(comments.length, MAX_COMMENTS_VIEW); |
| 48 | + commentShownCount.textContent = currentCommentsCount; |
| 49 | + |
| 50 | + renderComments(currentListComments.slice(0, currentCommentsCount)); |
| 51 | +}; |
| 52 | + |
| 53 | +commentsLoader.addEventListener('click', () => { |
| 54 | + let previousCommentsCount = currentCommentsCount; |
| 55 | + |
| 56 | + currentCommentsCount = Math.min(currentListComments.length, currentCommentsCount + MAX_COMMENTS_VIEW); |
| 57 | + |
| 58 | + const newCommentsBatch = currentListComments.slice(previousCommentsCount, currentCommentsCount); |
| 59 | + |
| 60 | + renderComments(newCommentsBatch); |
| 61 | + commentShownCount.textContent = currentCommentsCount; |
| 62 | + |
| 63 | + if (currentCommentsCount >= currentListComments.length) { |
| 64 | + commentsLoader.classList.add('hidden'); |
| 65 | + } |
| 66 | +}); |
0 commit comments