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 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
13 changes: 12 additions & 1 deletion js/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
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};
57 changes: 57 additions & 0 deletions js/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { escKeypress } from './utils';

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


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) => {
postModal.classList.remove('hidden');
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 postData = data.find((photo) => photo.id === photoId);
openPostModal(postData);
};

document.addEventListener('keydown', (evt) => {
escKeypress(evt, closePostModal);
});

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};
8 changes: 7 additions & 1 deletion js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ const createRandomId = (min, max) => {
};
};

export {createRandomId, getRandomInteger};
const escKeypress = (evt, fn) => {
if (evt.key === 'Escape') {
fn();
}
};

export {createRandomId, getRandomInteger, escKeypress};
Loading