|
| 1 | +import { toggleClass, isEscapeKey } from './util.js'; |
| 2 | + |
| 3 | +const fullSizePhotoContainer = document.querySelector('.big-picture'); |
| 4 | +const fullSizePhoto = fullSizePhotoContainer.querySelector('.big-picture__img img'); |
| 5 | +const likesCount = document.querySelector('.likes-count'); |
| 6 | +const commentCount = document.querySelector('.social__comment-count'); |
| 7 | +const userCommentsContainer = document.querySelector('.social__comments'); |
| 8 | +const userComment = userCommentsContainer.querySelector('.social__comment'); |
| 9 | +const commentsLoader = document.querySelector('.comments-loader'); |
| 10 | +const caption = document.querySelector('.social__caption'); |
| 11 | +const closeBtn = document.querySelector('#picture-cancel'); |
| 12 | + |
| 13 | +const commentFragment = document.createDocumentFragment(); |
| 14 | +let currentComments = []; |
| 15 | + |
| 16 | +const toggleModal = () => { |
| 17 | + toggleClass(fullSizePhotoContainer, 'hidden'); |
| 18 | + toggleClass(document.body, 'modal-open'); |
| 19 | +}; |
| 20 | + |
| 21 | +const show = (photo) => { |
| 22 | + const { url, likes, description } = photo; |
| 23 | + fullSizePhoto.src = url; |
| 24 | + likesCount.textContent = likes; |
| 25 | + caption.textContent = description; |
| 26 | +}; |
| 27 | + |
| 28 | +const renderComment = (comment) => { |
| 29 | + const newComment = userComment.cloneNode(true); |
| 30 | + const avatar = newComment.querySelector('.social__picture'); |
| 31 | + |
| 32 | + avatar.src = comment.avatar; |
| 33 | + avatar.alt = comment.name; |
| 34 | + newComment.querySelector('.social__text').textContent = comment.message; |
| 35 | + |
| 36 | + return newComment; |
| 37 | +}; |
| 38 | + |
| 39 | +const renderComments = () => { |
| 40 | + userCommentsContainer.innerHTML = ''; |
| 41 | + commentCount.innerHTML = ''; |
| 42 | + |
| 43 | + commentCount.innerHTML = `${currentComments.length} из <span class="social__comment-total-count">${currentComments.length}</span> комментариев`; |
| 44 | + |
| 45 | + currentComments.forEach((comment) => { |
| 46 | + commentFragment.appendChild(renderComment(comment)); |
| 47 | + }); |
| 48 | + |
| 49 | + userCommentsContainer.appendChild(commentFragment); |
| 50 | +}; |
| 51 | + |
| 52 | +function closeFullSizePhoto() { |
| 53 | + document.removeEventListener('keydown', onFullSizePhotoEscKey); |
| 54 | + toggleModal(); |
| 55 | +} |
| 56 | + |
| 57 | +const onCloseFullSizePhoto = () => { |
| 58 | + closeFullSizePhoto(); |
| 59 | +}; |
| 60 | + |
| 61 | +function onFullSizePhotoEscKey(evt) { |
| 62 | + if (isEscapeKey(evt)) { |
| 63 | + evt.preventDefault(); |
| 64 | + closeFullSizePhoto(); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +const renderFullSizePhoto = (photo) => { |
| 69 | + currentComments = photo.comments.slice(); |
| 70 | + show(photo); |
| 71 | + renderComments(); |
| 72 | + document.addEventListener('keydown', onFullSizePhotoEscKey); |
| 73 | + toggleModal(); |
| 74 | + commentsLoader.classList.add('hidden'); |
| 75 | +}; |
| 76 | + |
| 77 | +closeBtn.addEventListener('click', onCloseFullSizePhoto); |
| 78 | + |
| 79 | +export { renderFullSizePhoto }; |
0 commit comments