|
| 1 | +/* |
| 2 | + Задание: |
| 3 | + 1) По клику на миниатюру должно открываться модальное окно .big-picture, |
| 4 | + в котором будет отрисовываться информация кликнутой миниатюры. Отрисовка реализована через удаление класса hidden у .big-picture. |
| 5 | + 2) Комментарии вставляются в блок .social__comments. |
| 6 | + 3) .social__caption - описание фотографии (description) |
| 7 | + 4) после открытия модалки, скрываем через добавление класса hidden счетчик комментариев .social__comment-count и загрузку новых .comments-loader. |
| 8 | + 5) после открытия окна добавляем боди класс modal-open. При закрытии, соответственно, удаляем. |
| 9 | + 6) закрытие по клику на кнопку и по нажатию на ESC |
| 10 | +*/ |
| 11 | + |
| 12 | +const modalWindow = document.querySelector('.big-picture'); |
| 13 | +const bigPictureContainer = modalWindow.querySelector('.big-picture__img'); |
| 14 | +const bigPictureImg = bigPictureContainer.querySelector('img'); |
| 15 | +const userAvatar = modalWindow.querySelector('.social__picture'); |
| 16 | +const caption = modalWindow.querySelector('.social__caption'); |
| 17 | +const likesCount = modalWindow.querySelector('.likes-count'); |
| 18 | +const closeButton = modalWindow.querySelector('.big-picture__cancel'); |
| 19 | +const commentsContainer = modalWindow.querySelector('.social__comments'); |
| 20 | +const commentItem = modalWindow.querySelector('.social__comment'); |
| 21 | +const commentsCount = modalWindow.querySelector('.social__comment-count'); |
| 22 | +const commentsShownCount = commentsCount.querySelector('.social__comment-shown-count'); |
| 23 | +const commentsTotalCount = commentsCount.querySelector('.social__comment-total-count'); |
| 24 | +const commentsLoaderButton = modalWindow.querySelector('.social__comments-loader'); |
| 25 | + |
| 26 | +const createComment = (commentData) => { |
| 27 | + const {avatar, message, name} = commentData; |
| 28 | + const commentItemClone = commentItem.cloneNode(true); |
| 29 | + const commentAvatar = commentItemClone.querySelector('.social__picture'); |
| 30 | + const commentText = commentItemClone.querySelector('.social__text'); |
| 31 | + commentAvatar.src = avatar; |
| 32 | + commentAvatar.alt = name; |
| 33 | + commentText.textContent = message; |
| 34 | + commentsContainer.appendChild(commentItemClone); |
| 35 | +}; |
| 36 | + |
| 37 | +const onCloseModal = () => { |
| 38 | + modalWindow.classList.add('hidden'); |
| 39 | + document.body.classList.remove('modal-open'); |
| 40 | + closeButton.removeEventListener('click', onCloseModal); |
| 41 | + document.removeEventListener('keydown', onCloseModal); |
| 42 | +}; |
| 43 | +const onOpenModal = (data) => { |
| 44 | + const {url, description, likes, comments} = data; |
| 45 | + commentsCount.classList.add('hidden'); |
| 46 | + commentsLoaderButton.classList.add('hidden'); |
| 47 | + commentsContainer.innerHTML = ''; |
| 48 | + comments.forEach(createComment); |
| 49 | + bigPictureImg.src = url; |
| 50 | + caption.textContent = description; |
| 51 | + likesCount.textContent = likes; |
| 52 | + modalWindow.classList.remove('hidden'); |
| 53 | + document.body.classList.add('modal-open'); |
| 54 | + closeButton.addEventListener('click', onCloseModal); |
| 55 | + document.addEventListener('keydown', onCloseModal); |
| 56 | +}; |
| 57 | + |
| 58 | +export { |
| 59 | + onOpenModal, |
| 60 | +}; |
0 commit comments