diff --git a/js/main.js b/js/main.js index 48373a3..7541cc9 100644 --- a/js/main.js +++ b/js/main.js @@ -1,4 +1,6 @@ import { listPhotos } from './create-photos.js'; import {renderThumbnails} from './render-photos.js'; +import './open-full-photo.js'; renderThumbnails(listPhotos); + diff --git a/js/open-full-photo.js b/js/open-full-photo.js new file mode 100644 index 0000000..268f483 --- /dev/null +++ b/js/open-full-photo.js @@ -0,0 +1,41 @@ +import { picturesContainer } from './render-photos.js'; +import {isEscapeKey} from './utils.js'; +import {renderFullPhoto} from './render-full-photo.js'; +import { listPhotos } from './create-photos.js'; + +const bigPicture = document.querySelector('.big-picture'); +const bigPictureClose = bigPicture.querySelector('.big-picture__cancel'); +const body = document.body; + + +const onDocumentKeydown = (evt) => { + if (isEscapeKey(evt)) { + evt.preventDefault(); + closeFullPhoto(); + } +}; + +function openFullPhoto() { + bigPicture.classList.remove('hidden'); + body.classList.add('modal-open'); + document.addEventListener('keydown', onDocumentKeydown); +} + +function closeFullPhoto() { + bigPicture.classList.add('hidden'); + body.classList.remove('modal-open'); + document.removeEventListener('keydown', onDocumentKeydown); +} + +picturesContainer.addEventListener('click', (evt) => { + const chosenPhoto = evt.target.closest('.picture'); + if (chosenPhoto) { + evt.preventDefault(); + const chosenPhotoID = chosenPhoto.getAttribute('data-id'); + renderFullPhoto(chosenPhotoID, listPhotos); + openFullPhoto(); + } +}); + +bigPictureClose.addEventListener('click', closeFullPhoto); + diff --git a/js/render-full-photo.js b/js/render-full-photo.js new file mode 100644 index 0000000..4b76d4d --- /dev/null +++ b/js/render-full-photo.js @@ -0,0 +1,39 @@ +const bigPicture = document.querySelector('.big-picture'); +const bigPictureImg = bigPicture.querySelector('.big-picture__img img'); +const likesCount = bigPicture.querySelector('.likes-count'); +const commentsCountFullPhoto = bigPicture.querySelector('.social__comment-count'); +const shownComments = commentsCountFullPhoto.querySelector('.social__comment-shown-count'); +const totalComments = commentsCountFullPhoto.querySelector('.social__comment-total-count'); +const commentsList = bigPicture.querySelector('.social__comments'); +const bigPictureDescription = bigPicture.querySelector('.social__caption'); +const buttonMoreComments = bigPicture.querySelector('.comments-loader'); + +const renderCommentsFullPhoto = (comments) =>{ + commentsCountFullPhoto.classList.add('hidden'); + buttonMoreComments.classList.add('hidden'); + comments.forEach((comment) => { + commentsList.insertAdjacentHTML( + 'afterbegin', + `