|
1 | | -import { createPhotos } from './generate-data'; |
| 1 | +import { photos } from './generate-data'; |
2 | 2 | import { closeModal, openModal } from './modal.js'; |
| 3 | +import { paginateComments } from './comments.js'; |
3 | 4 |
|
4 | 5 | const fullPhotoModal = document.querySelector('.big-picture'); |
5 | | -const comments = document.querySelector('.social__comments'); |
6 | 6 | const body = document.querySelector('body'); |
7 | 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 | | - |
| 8 | +const comments = document.querySelector('.social__comments'); |
| 9 | +const pictureContainer = document.querySelector('.pictures'); |
| 10 | +let currentLoadMoreHandler = null; |
31 | 11 | const fillPhotoData = (currentPhoto) => { |
32 | 12 | const fullPhotoImage = document.querySelector('.big-picture__img img'); |
33 | 13 | const fullPhotoDescription = document.querySelector('.social__caption'); |
34 | 14 | const fullPhotoLikes = document.querySelector('.likes-count'); |
35 | | - const shownCommentsCount = document.querySelector('.social__comment-shown-count'); |
| 15 | + const commentsCount = document.querySelector('.social__comment-total-count'); |
| 16 | + |
36 | 17 | fullPhotoImage.src = currentPhoto.url; |
37 | 18 | fullPhotoImage.alt = currentPhoto.description; |
38 | 19 | fullPhotoLikes.textContent = currentPhoto.likes; |
39 | 20 | fullPhotoDescription.textContent = currentPhoto.description; |
40 | | - shownCommentsCount.textContent = currentPhoto.comments.length; |
| 21 | + commentsCount.textContent = currentPhoto.comments.length; |
41 | 22 | comments.innerHTML = ''; |
42 | | - renderComments(currentPhoto.comments); |
43 | 23 | }; |
44 | 24 |
|
45 | 25 | export const showFullPhoto = () => { |
46 | | - const photos = createPhotos(); |
47 | | - const thumbnails = document.querySelectorAll('.picture'); |
48 | | - const commentsCount = document.querySelector('.social__comment-count'); |
49 | 26 | const commentsLoaderBtn = document.querySelector('.comments-loader'); |
| 27 | + const shownCommentsCount = document.querySelector('.social__comment-shown-count'); |
| 28 | + pictureContainer.addEventListener('click', (evt) => { |
| 29 | + const thumbnail = evt.target.closest('.picture'); |
| 30 | + if (!thumbnail) { |
| 31 | + return; |
| 32 | + } |
| 33 | + evt.preventDefault(); |
| 34 | + openModal(fullPhotoModal, body); |
| 35 | + const photoId = Number(thumbnail.dataset.id); |
| 36 | + const currentPhoto = photos.find((photo) => photo.id === photoId); |
| 37 | + fillPhotoData(currentPhoto); |
| 38 | + |
| 39 | + commentsLoaderBtn.classList.toggle('hidden', currentPhoto.comments.length <= 5); |
| 40 | + const pagination = paginateComments(currentPhoto.comments, comments); |
| 41 | + shownCommentsCount.textContent = pagination.getShownCount(); |
| 42 | + const handleLoadMore = () => { |
| 43 | + pagination.loadMore(); |
| 44 | + shownCommentsCount.textContent = pagination.getShownCount(); |
| 45 | + commentsLoaderBtn.classList.toggle('hidden', pagination.getShownCount() >= currentPhoto.comments.length); |
| 46 | + }; |
| 47 | + if (currentLoadMoreHandler) { |
| 48 | + commentsLoaderBtn.removeEventListener('click', currentLoadMoreHandler); |
| 49 | + currentLoadMoreHandler = null; |
| 50 | + } |
| 51 | + currentLoadMoreHandler = handleLoadMore; |
| 52 | + commentsLoaderBtn.addEventListener('click', handleLoadMore); |
50 | 53 |
|
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 | 54 | }); |
61 | | - |
62 | 55 | closeModalButton.addEventListener('click', () => closeModal(fullPhotoModal, body)); |
63 | 56 | }; |
| 57 | + |
| 58 | + |
0 commit comments