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

Открывается и закрывается #12

Merged
merged 5 commits into from
Dec 13, 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
69 changes: 69 additions & 0 deletions js/big-picture-popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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');

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';

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;

commEl.append(imgEl);
commEl.append(pEl);
commentFragmentList.append(commEl);

});

bigPhotoPopup.querySelector('.social__comments').innerHTML = '';
bigPhotoPopup.querySelector('.social__comments').append(commentFragmentList);

}
function hidePhotoPopup(e) {
e.preventDefault();
hide(bigPhotoPopup);
document.removeEventListener('keydown', onEscapeKeydown);
document.body.classList.remove('modal-open');
}
function showPhotoPopup() {
document.body.classList.add('modal-open');
show(bigPhotoPopup);

document.addEventListener('keydown', onEscapeKeydown);
}
function openBigPhotoPopup(photoId) {
showPhotoPopup();
const currentPhoto = photos.find((photo) => photo.id === Number(photoId));
if (currentPhoto) {
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'));
}
}

popupCancelElement.addEventListener('click', hidePhotoPopup);

export { openBigPhotoPopup };
9 changes: 9 additions & 0 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { renderPictures } from './render-pictures.js';
import { openBigPhotoPopup } from './big-picture-popup.js';

renderPictures();

document.addEventListener('click', (e) => {
const currentPicture = e.target.closest('.picture');

if (currentPicture) {
e.preventDefault();
openBigPhotoPopup(currentPicture.dataset.photoId);
}
});
3 changes: 2 additions & 1 deletion js/render-pictures.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getPhotos } from './get-photos.js';
const photos = getPhotos();

function renderPictures() {
const photos = getPhotos();
const pictureTemplate = document.querySelector('#picture').content.querySelector('.picture');
const picturesList = document.querySelector('.pictures');

Expand All @@ -12,6 +12,7 @@ function renderPictures() {
const photoImg = photoElement.querySelector('img');
photoImg.src = photo.url;
photoImg.alt = photo.description;
photoElement.dataset.photoId = photo.id;
photoElement.querySelector('.picture__comments').textContent = photo.comments.length;
photoElement.querySelector('.picture__likes').textContent = photo.likes;

Expand Down
9 changes: 8 additions & 1 deletion js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ function getRandomInt(min, max) {
const randomInt = Math.floor(Math.random() * (max - min + 1)) + min;
return randomInt;
}

function hide(elem) {
elem.classList.add('hidden');
}
function show(elem) {
elem.classList.remove('hidden');
}
function getUniqueId(min, max) {
const receivedId = [];

Expand All @@ -23,4 +30,4 @@ function getRandomElement(elements) {
return elements[getRandomInt(0, elements.length - 1)];
}

export { getRandomInt, getUniqueId, getRandomElement };
export { getRandomInt, getUniqueId, getRandomElement, hide, show };
Loading