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

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

Merged
merged 4 commits into from
Mar 25, 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
96 changes: 96 additions & 0 deletions js/bigPicture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const openModal = () => {
const bigPicture = document.querySelector('.big-picture');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Давай в эту функцию добавим обработчики событий для закрытия модалки

const body = document.querySelector('body');

bigPicture.classList.remove('hidden');
body.classList.add('modal-open');
};

const renderImageAndDescription = (post) => {
const bigPictureImage = document.querySelector('.big-picture__img img');
bigPictureImage.src = post.url;

const bigPictureLikes = document.querySelector('.likes-count');
bigPictureLikes.textContent = post.likes;

const bigPictureCaption = document.querySelector('.social__caption');
bigPictureCaption.textContent = post.description;
};

const renderComments = (post) => {
const commentsContainer = document.querySelector('.social__comments');

while (commentsContainer.firstChild) {
commentsContainer.removeChild(commentsContainer.firstChild);
}

post.comments.forEach((comment) => {
const commentItem = document.createElement('li');
commentItem.classList.add('social__comment');

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

const commentText = document.createElement('p');
commentText.classList.add('social__text');
commentText.textContent = comment.message;

commentItem.appendChild(avatar);
commentItem.appendChild(commentText);
commentsContainer.appendChild(commentItem);
});

const bigPictureCommentsSize = document.querySelector('.social__comment-shown-count');
bigPictureCommentsSize.textContent = post.comments.length.toString();

const bigPictureCommentsTotal = document.querySelector('.social__comment-total-count');
bigPictureCommentsTotal.textContent = post.comments.length.toString();

const commentCount = document.querySelector('.social__comment-count');
const commentsLoader = document.querySelector('.comments-loader');
commentCount.classList.add('hidden');
commentsLoader.classList.add('hidden');
};

const close = () => {
const body = document.querySelector('body');
const bigPicture = document.querySelector('.big-picture');

body.classList.remove('modal-open');
bigPicture.classList.add('hidden');
};

const clickHandler = () => {
close();
const closeButton = document.getElementById('picture-cancel');
closeButton.removeEventListener('click', clickHandler);
};

const setupCloseHandlers = () => {
const closeButton = document.getElementById('picture-cancel');

const closeHandler = (event) => {
if (event.key === 'Escape' || event.code === 'Escape') {
close();
document.removeEventListener('keydown', closeHandler);
}
};

document.addEventListener('keydown', closeHandler);
closeButton.addEventListener('click', clickHandler);
};

export const showBigPicture = (post) => {
openModal();
renderImageAndDescription(post);
renderComments(post);

const closeButton = document.getElementById('picture-cancel');
closeButton.removeEventListener('click', clickHandler);

setupCloseHandlers(clickHandler);
};
9 changes: 7 additions & 2 deletions js/thumbnails.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { generateImagePosts } from './util.js';
import { showBigPicture } from './bigPicture.js';

const pictureTemplate = document.querySelector('#picture')
.content.querySelector('.picture');
Expand All @@ -14,9 +15,15 @@ const postParser = (post) => {
likes.textContent = post.likes;
comments.textContent = post.comments.length;


pictureElement.addEventListener('click', () => {
showBigPicture(post);
});

return pictureElement;
};


export const createThumbnails = () => {
const posts = generateImagePosts(25, 30);
const fragment = document.createDocumentFragment();
Expand All @@ -29,5 +36,3 @@ export const createThumbnails = () => {

pictures.appendChild(fragment);
};


Loading