|
| 1 | +import { createPhotos } from './generate-data'; |
| 2 | +import { closeModal, openModal } from './modal.js'; |
| 3 | + |
| 4 | +const fullPhotoModal = document.querySelector('.big-picture'); |
| 5 | +const comments = document.querySelector('.social__comments'); |
| 6 | +const body = document.querySelector('body'); |
| 7 | +const closeModalButton = document.querySelector('.big-picture__cancel'); |
| 8 | + |
| 9 | +const renderComments = (mockedComments) => { |
| 10 | + mockedComments.forEach((commentData) => { |
| 11 | + const comment = document.createElement('li'); |
| 12 | + const avatar = document.createElement('img'); |
| 13 | + const commentText = document.createElement('p'); |
| 14 | + |
| 15 | + comment.classList.add('social__comment'); |
| 16 | + avatar.classList.add('social__picture'); |
| 17 | + avatar.alt = commentData.name; |
| 18 | + avatar.src = commentData.avatar; |
| 19 | + avatar.width = 35; |
| 20 | + avatar.height = 35; |
| 21 | + commentText.classList.add('social__text'); |
| 22 | + commentText.textContent = commentData.message; |
| 23 | + |
| 24 | + comment.append(avatar); |
| 25 | + comment.append(commentText); |
| 26 | + |
| 27 | + comments.append(comment); |
| 28 | + }); |
| 29 | +}; |
| 30 | + |
| 31 | +const fillPhotoData = (currentPhoto) => { |
| 32 | + const fullPhotoImage = document.querySelector('.big-picture__img img'); |
| 33 | + const fullPhotoDescription = document.querySelector('.social__caption'); |
| 34 | + const fullPhotoLikes = document.querySelector('.likes-count'); |
| 35 | + const shownCommentsCount = document.querySelector('.social__comment-shown-count'); |
| 36 | + fullPhotoImage.src = currentPhoto.url; |
| 37 | + fullPhotoImage.alt = currentPhoto.description; |
| 38 | + fullPhotoLikes.textContent = currentPhoto.likes; |
| 39 | + fullPhotoDescription.textContent = currentPhoto.description; |
| 40 | + shownCommentsCount.textContent = currentPhoto.comments.length; |
| 41 | + comments.innerHTML = ''; |
| 42 | + renderComments(currentPhoto.comments); |
| 43 | +}; |
| 44 | + |
| 45 | +export const showFullPhoto = () => { |
| 46 | + const photos = createPhotos(); |
| 47 | + const thumbnails = document.querySelectorAll('.picture'); |
| 48 | + const commentsCount = document.querySelector('.social__comment-count'); |
| 49 | + const commentsLoaderBtn = document.querySelector('.comments-loader'); |
| 50 | + |
| 51 | + thumbnails.forEach((thumbnail) => { |
| 52 | + thumbnail.addEventListener('click', () => { |
| 53 | + openModal(fullPhotoModal, body); |
| 54 | + commentsCount.classList.add('hidden'); |
| 55 | + commentsLoaderBtn.classList.add('hidden'); |
| 56 | + const photoId = Number(thumbnail.dataset.id); |
| 57 | + const currentPhoto = photos.find((photo) => photo.id === photoId); |
| 58 | + fillPhotoData(currentPhoto); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + closeModalButton.addEventListener('click', () => closeModal(fullPhotoModal, body)); |
| 63 | +}; |
0 commit comments