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

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

Merged
merged 4 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 13 additions & 1 deletion js/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import './post.js';
import { createMockPosts } from './mock.js';
import { printThumbnails } from './thumbnails.js';
import { picturesContainer } from './thumbnails.js';
import { findPostContent } from './post.js';

createMockPosts();
const initApp = () => {
const data = createMockPosts();
printThumbnails(data);
picturesContainer.addEventListener('click', (evt) => {
findPostContent(evt, data);
});
};

document.addEventListener('DOMContentLoaded', initApp);

14 changes: 4 additions & 10 deletions js/mock.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { createRandomId, getRandomInteger } from './utils';
import { printThumbnails } from './thumbnails';
import { POSTS_DATA } from './data';

import { createRandomId, getRandomInteger } from './utils.js';
import { POSTS_DATA } from './data.js';
const { POSTS_COUNT, NAMES, DESCRIPTIONS, COMMENTS } = POSTS_DATA;

const generatePhotoId = createRandomId(1, POSTS_COUNT);
Expand All @@ -27,11 +25,7 @@ const createPhotoPost = () => {
};
};

const createMockPosts = () => {
const data = Array.from({length: POSTS_COUNT}, createPhotoPost);

printThumbnails(data);
};

// const createMockPosts = () => Array.from({length: POSTS_COUNT}, createPhotoPost);
const createMockPosts = () => Array.from({length: POSTS_COUNT}, createPhotoPost);

export {createMockPosts};
56 changes: 56 additions & 0 deletions js/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const postModal = document.querySelector('.big-picture');
const closeModalBtn = document.querySelector('.big-picture__cancel');
const postComments = document.querySelector('.social__comments');


const closePostModal = () => {
postModal.classList.add('hidden');
closeModalBtn.removeEventListener('click', closePostModal);
document.body.classList.remove('modal-open');
};

document.addEventListener('keydown', (evt) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Обернуть код в функцию, для того чтобы удалить импорт модуля внутри main. Не забыть удалить обработчик после закрытия модального окна

if (evt.key === 'Escape') {
closePostModal();
}
});

const insertPostComments = (commentsArr) => {
const commentsFragment = document.createDocumentFragment();
commentsArr.forEach((comment) => {
const commentNode = document.createElement('li');
commentNode.classList.add('social__comment');
commentNode.innerHTML = `
<img
class="social__picture"
src="${comment.avatar}"
alt="${comment.name}"
width="35" height="35">
<p class="social__text">${comment.message}</p>`;
commentsFragment.append(commentNode);
});
postComments.append(commentsFragment);
};

const openPostModal = (content) => {
document.body.classList.add('modal-open');
postModal.querySelector('img').src = content.url;
postModal.querySelector('.likes-count').textContent = content.likes;
postModal.querySelector('.social__caption').textContent = content.description;
postModal.querySelector('.social__comment-total-count').textContent = content.comments.length;
postModal.querySelector('.social__comment-count').classList.add('hidden');
postModal.querySelector('.comments-loader').classList.add('hidden');
postComments.innerHTML = '';
insertPostComments(content.comments);
closeModalBtn.addEventListener('click', closePostModal);
};

const findPostContent = (evt, data) => {
const url = evt.target.closest('.picture').querySelector('img').src;
const photoId = Number(url.split('/').pop().split('.')[0]);
const res = data.find((photo) => photo.id === photoId);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Нужно дать более полноценное название для переменной

postModal.classList.remove('hidden');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Эту строчку уместнее использовать внутри openPostModal

openPostModal(res);
};

export {findPostContent};
2 changes: 1 addition & 1 deletion js/thumbnails.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ const printThumbnails = (data) => {
picturesContainer.append(postsFragment);
};

export {printThumbnails};
export {printThumbnails, picturesContainer};