-
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) #10
Changes from all commits
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,13 +1,16 @@ | ||
import { isEscapeKey } from './util.js'; | ||
|
||
const COMMENTS_STEP = 5; | ||
const bigPictureElement = document.querySelector('.big-picture'); | ||
const bodyElement = document.querySelector('body'); | ||
const commentCountElement = bigPictureElement.querySelector('.social__comment-count'); | ||
const commentListElement = bigPictureElement.querySelector('.social__comments'); | ||
const commentsLoaderElement = bigPictureElement.querySelector('.comments-loader'); | ||
const commentsLoader = document.querySelector('.comments-loader'); | ||
const cancelButtonElement = bigPictureElement.querySelector('.big-picture__cancel'); | ||
const liElement = commentListElement.querySelector('li'); | ||
|
||
let currentCount = 0; | ||
let comments = []; | ||
|
||
const createComment = ({ avatar, message, name}) => { | ||
const comment = liElement.cloneNode(true); | ||
|
||
|
@@ -18,16 +21,30 @@ const createComment = ({ avatar, message, name}) => { | |
return comment; | ||
}; | ||
|
||
const showComments = (comments) => { | ||
commentListElement.innerHTML = ''; | ||
const showComments = () => { | ||
currentCount += COMMENTS_STEP; | ||
if (currentCount >= comments.length) { | ||
commentsLoader.classList.add('hidden'); | ||
currentCount = comments.length; | ||
} else { | ||
commentsLoader.classList.remove('hidden'); | ||
} | ||
|
||
const fragment = document.createDocumentFragment(); | ||
comments.forEach ((item) => { | ||
const comment = createComment(item); | ||
for (let i = 0; i < currentCount; i++) { | ||
const comment = createComment(comments[i]); | ||
fragment.append(comment); | ||
}); | ||
} | ||
|
||
commentListElement.innerHTML = ''; | ||
commentListElement.append(fragment); | ||
bigPictureElement.querySelector('.social__comment-count').innerHTML = | ||
`${currentCount} из <span class="comments-count">${comments.length}</span> комментариев`; | ||
|
||
}; | ||
|
||
const onCommentsLoaderClick = () => { | ||
showComments(comments); | ||
}; | ||
|
||
const onDocumentKeydown = (evt) => { | ||
|
@@ -41,6 +58,7 @@ function hideBigPicture () { | |
bigPictureElement.classList.add('hidden'); | ||
bodyElement.classList.remove('modal-open'); | ||
document.removeEventListener('keydown', onDocumentKeydown); | ||
currentCount = 0; | ||
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. вот тут удалять bigPictureElement.classList.add('hidden');
bodyElement.classList.remove('modal-open');
document.removeEventListener('keydown', onDocumentKeydown);
commentsLoader.removeEventListener('click', onCommentsLoaderClick);
currentCount = 0; |
||
} | ||
|
||
const onCancelButtonClick = () => { | ||
|
@@ -56,14 +74,17 @@ const showPictureDetails = ({ url, likes, description }) => { | |
const showBigPicture = (data) => { | ||
bigPictureElement.classList.remove('hidden'); | ||
bodyElement.classList.add('modal-open'); | ||
commentsLoaderElement.classList.add('hidden'); | ||
commentCountElement.classList.add('hidden'); | ||
commentsLoader.classList.add('hidden'); | ||
document.addEventListener('keydown', onDocumentKeydown); | ||
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. тут можно перенести внутрь addEventListener document.addEventListener('keydown', onDocumentKeydown);
commentsLoader.addEventListener('click', onCommentsLoaderClick); |
||
|
||
showPictureDetails(data); | ||
showComments(data.comments); | ||
comments = data.comments; | ||
if (comments.length >= 0) { | ||
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. тут всегда true будет с такой проверкой if (comments?.length > 0) { // сразу проверяем что comments существует и дальше уже от него length делаем
showComments(comments);
} |
||
showComments(comments); | ||
} | ||
}; | ||
|
||
cancelButtonElement.addEventListener('click', onCancelButtonClick); | ||
commentsLoader.addEventListener('click', onCommentsLoaderClick); | ||
|
||
export { showBigPicture }; |
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.
тут можно использовать replaceChildren()