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 all commits
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
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ <h2 class="img-upload__title visually-hidden">Загрузка фотограф
</fieldset>

<!-- Форма редактирования изображения -->
<div class="img-upload__overlay hidden">
<div class="img-upload__overlay hidden">
<div class="img-upload__wrapper">
<div class="img-upload__preview-container">

Expand Down
76 changes: 53 additions & 23 deletions js/big-picture-popup.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,70 @@
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 currentShownCommentsCount = 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();
}

const imgEl = document.createElement('img');
imgEl.className = 'social__picture';
imgEl.src = comment.avatar;
imgEl.alt = comment.name;
imgEl.width = 35;
imgEl.height = 35;
function loadComments() {
if (currentComments.length > currentShownCommentsCount) {
currentComments.slice(currentShownCommentsCount, currentShownCommentsCount + COMMENTS_STEP).forEach((comment) => {
const commEl = document.createElement('li');
commEl.className = 'social__comment';

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

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

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

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

function clearComments() {
bigPhotoPopup.querySelector('.social__comments').innerHTML = '';
bigPhotoPopup.querySelector('.social__comments').append(commentFragmentList);
currentShownCommentsCount = 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 +79,21 @@ 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);
function checkShownCommentsCount() {
if (currentComments.length <= currentShownCommentsCount) {
hide(commentsLoadBtn);
} else {
show(commentsLoadBtn);
}
}

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