Skip to content
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) #13

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 44 additions & 23 deletions js/big-picture-popup.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,69 @@
import { COMMENTS_STEP } from './consts.js';
import { getPhotos } from './get-photos.js';
import { hide, show } from './utils.js';
const photos = getPhotos();
const bigPhotoPopup = document.querySelector('.big-picture');
const popupCancelElement = document.querySelector('.big-picture__cancel');
const commentsLoadBtn = bigPhotoPopup.querySelector('.comments-loader');

let currentComments = null;
let currentCommentsCount = 0;

function onEscapeKeydown(e) {
if (e.key === 'Escape') {
hidePhotoPopup(e);
}
}

function renderComments(comments) {
const commentFragmentList = document.createDocumentFragment();
comments.forEach((comment) => {
const commEl = document.createElement('li');
commEl.className = 'social__comment';
function onLoadBtnClick(e) {
e.preventDefault();
loadComments();
}

function loadComments() {
if (currentComments.length > currentCommentsCount) {
currentComments.slice(currentCommentsCount, currentCommentsCount + COMMENTS_STEP).forEach((comment) => {
const commEl = document.createElement('li');
commEl.className = 'social__comment';

const imgEl = document.createElement('img');
imgEl.className = 'social__picture';
imgEl.src = comment.avatar;
imgEl.alt = comment.name;
imgEl.width = 35;
imgEl.height = 35;
const imgEl = document.createElement('img');
imgEl.className = 'social__picture';
imgEl.src = comment.avatar;
imgEl.alt = comment.name;
imgEl.width = 35;
imgEl.height = 35;

const pEl = document.createElement('p');
pEl.className = 'social__text';
pEl.textContent = comment.message;
const pEl = document.createElement('p');
pEl.className = 'social__text';
pEl.textContent = comment.message;

commEl.append(imgEl);
commEl.append(pEl);
commentFragmentList.append(commEl);
commEl.append(imgEl);
commEl.append(pEl);
bigPhotoPopup.querySelector('.social__comments').append(commEl);
currentCommentsCount++;
});

});
bigPhotoPopup.querySelector('.social__comment-shown-count').textContent = bigPhotoPopup.querySelectorAll('.social__comment').length;
}
}

function clearComments() {
bigPhotoPopup.querySelector('.social__comments').innerHTML = '';
bigPhotoPopup.querySelector('.social__comments').append(commentFragmentList);
currentCommentsCount = 0;
}

function renderComments(comments) {
clearComments();
currentComments = comments;
loadComments();
}
function hidePhotoPopup(e) {
e.preventDefault();
clearComments();
hide(bigPhotoPopup);
document.removeEventListener('keydown', onEscapeKeydown);
document.body.classList.remove('modal-open');

}
function showPhotoPopup() {
document.body.classList.add('modal-open');
Expand All @@ -56,14 +78,13 @@ function openBigPhotoPopup(photoId) {
renderComments(currentPhoto.comments);
bigPhotoPopup.querySelector('.big-picture__img img').src = currentPhoto.url;
bigPhotoPopup.querySelector('.likes-count').textContent = currentPhoto.likes.length;
bigPhotoPopup.querySelector('.social__comment-shown-count').textContent = currentPhoto.comments.length;
bigPhotoPopup.querySelector('.social__comment-total-count').textContent = currentPhoto.comments.length;
bigPhotoPopup.querySelector('.social__caption').textContent = currentPhoto.description;
hide(bigPhotoPopup.querySelector('.social__comment-shown-count'));
hide(bigPhotoPopup.querySelector('.comments-loader'));
bigPhotoPopup.querySelector('.likes-count').textContent = currentPhoto.likes;

}
}

popupCancelElement.addEventListener('click', hidePhotoPopup);

commentsLoadBtn.addEventListener('click', onLoadBtnClick);
export { openBigPhotoPopup };
3 changes: 2 additions & 1 deletion js/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ const MESSAGES = [
'Лица у людей на фотке перекошены, как будто их избивают.Как можно было поймать такой неудачный момент ? !',
];
const PHOTOS_COUNT = 25;
const COMMENTS_STEP = 5;

export { NAMES, DESCRIPTIONS, MESSAGES, PHOTOS_COUNT };
export { NAMES, DESCRIPTIONS, MESSAGES, PHOTOS_COUNT, COMMENTS_STEP };
Loading