-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Открывается и закрывается (часть 2) #9
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,72 @@ | ||
const socialCommentsElement = document.querySelector('.social__comments'); | ||
const commentTemplateElement = socialCommentsElement.querySelector('.social__comment'); | ||
const bigPictureElement = document.querySelector('.big-picture'); | ||
const loadCommentElement = bigPictureElement.querySelector('.social__comment-count'); | ||
const currentCommentsCountElement = bigPictureElement.querySelector('.social__comment-shown-count'); | ||
const totalCommentsCountElement = bigPictureElement.querySelector('.social__comment-total-count'); | ||
const loadCommentsButtonElement = bigPictureElement.querySelector('.social__comments-loader'); | ||
const loadingStep = 5; | ||
let commentsArray = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. У массива не let, а const |
||
let totalComments; | ||
let currentCommentsLoaded = 0; | ||
|
||
function fillCommentsList(comments) { | ||
socialCommentsElement.innerHTML = ''; | ||
comments.forEach((comment) => { | ||
const {avatar, name, commentsList} = comment; | ||
const newCommentElement = commentTemplateElement.cloneNode(true); | ||
const commentPictureElement = newCommentElement.querySelector('.social__picture'); | ||
|
||
commentPictureElement.setAttribute('src', avatar); | ||
commentPictureElement.setAttribute('alt', name); | ||
newCommentElement.querySelector('.social__text').textContent = commentsList; | ||
socialCommentsElement.appendChild(newCommentElement); | ||
}); | ||
|
||
if (comments.length > 0) { | ||
commentsArray = comments; | ||
currentCommentsLoaded = 0; | ||
totalComments = comments.length; | ||
|
||
const commentsToLoad = Math.min(loadingStep, totalComments); | ||
for (let i = 0; i < commentsToLoad; i++) { | ||
addCommentAndUpdateCounter(commentsArray[i]); | ||
} | ||
updateLoadCommentsButtonVisibility(); | ||
} else { | ||
loadCommentElement.innerHTML = ''; | ||
loadCommentElement.textContent = 'Нет комментариев'; | ||
updateLoadCommentsButtonVisibility(); | ||
} | ||
} | ||
|
||
function loadMoreComments() { | ||
const commentsToLoad = Math.min(loadingStep, totalComments - currentCommentsLoaded); | ||
|
||
for (let i = 0; i < commentsToLoad; i++) { | ||
addCommentAndUpdateCounter(commentsArray[currentCommentsLoaded]); | ||
} | ||
updateLoadCommentsButtonVisibility(); | ||
} | ||
|
||
function addComment(comment) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const {avatar, name, commentsList} = comment; | ||
const newCommentElement = commentTemplateElement.cloneNode(true); | ||
const commentPictureElement = newCommentElement.querySelector('.social__picture'); | ||
|
||
commentPictureElement.setAttribute('src', avatar); | ||
commentPictureElement.setAttribute('alt', name); | ||
newCommentElement.querySelector('.social__text').textContent = commentsList; | ||
socialCommentsElement.appendChild(newCommentElement); | ||
} | ||
|
||
function updateCommentCounters() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Удали функцию и вынеси две строчки внутрь |
||
currentCommentsCountElement.textContent = currentCommentsLoaded; | ||
totalCommentsCountElement.textContent = totalComments; | ||
} | ||
|
||
function addCommentAndUpdateCounter(comment) { | ||
addComment(comment); | ||
currentCommentsLoaded++; | ||
updateCommentCounters(); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Удали функцию и применяй вместо нее одну строку с classList.toggle Подробнее можешь почитать в статье https://gomakethings.com/the-mysterious-second-argument-on-the-vanilla-js-classlist.toggle-method/ |
||
function updateLoadCommentsButtonVisibility() { | ||
if (currentCommentsLoaded >= totalComments) { | ||
loadCommentsButtonElement.classList.add('hidden'); | ||
} else { | ||
loadCommentsButtonElement.classList.remove('hidden'); | ||
} | ||
} | ||
|
||
export {fillCommentsList}; | ||
export {fillCommentsList, loadMoreComments}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Оформи как константу плиз