diff --git a/js/main.js b/js/main.js index fefa8a1..66df91a 100644 --- a/js/main.js +++ b/js/main.js @@ -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); diff --git a/js/mock.js b/js/mock.js index 72c7595..3c04413 100644 --- a/js/mock.js +++ b/js/mock.js @@ -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); @@ -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}; diff --git a/js/post.js b/js/post.js new file mode 100644 index 0000000..93cb1e9 --- /dev/null +++ b/js/post.js @@ -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 = ` + +
`; + 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}; diff --git a/js/thumbnails.js b/js/thumbnails.js index 3bbd441..07ee962 100644 --- a/js/thumbnails.js +++ b/js/thumbnails.js @@ -15,4 +15,4 @@ const printThumbnails = (data) => { picturesContainer.append(postsFragment); }; -export {printThumbnails}; +export {printThumbnails, picturesContainer}; diff --git a/js/utils.js b/js/utils.js index c4b1eb7..a327d53 100644 --- a/js/utils.js +++ b/js/utils.js @@ -22,4 +22,10 @@ const createRandomId = (min, max) => { }; }; -export {createRandomId, getRandomInteger}; +const escKeypress = (evt, fn) => { + if (evt.key === 'Escape') { + fn(); + } +}; + +export {createRandomId, getRandomInteger, escKeypress};