|
| 1 | +import { isEscapeKey } from './util.js'; |
| 2 | + |
| 3 | +const COMMENTS_PER_PAGE = 5; |
| 4 | +let currentCommentsShown = 0; |
| 5 | +let escapeHandler = null; |
| 6 | + |
| 7 | +// Закрытие модального окна |
| 8 | +const closeFullScreen = (bigPicture, body, closeButton) => { |
| 9 | + if (bigPicture) { |
| 10 | + bigPicture.classList.add('hidden'); |
| 11 | + } |
| 12 | + if (body) { |
| 13 | + body.classList.remove('modal-open'); |
| 14 | + } |
| 15 | + if (closeButton) { |
| 16 | + closeButton.removeEventListener('click', closeFullScreen); |
| 17 | + } |
| 18 | + if (escapeHandler) { |
| 19 | + document.removeEventListener('keydown', escapeHandler); |
| 20 | + escapeHandler = null; |
| 21 | + } |
| 22 | + currentCommentsShown = 0; |
| 23 | +}; |
| 24 | + |
| 25 | +// Настройка обработчиков закрытия |
| 26 | +const setupCloseHandlers = (bigPicture, body) => { |
| 27 | + const closeButton = bigPicture?.querySelector('.big-picture__cancel'); |
| 28 | + if (!closeButton) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + escapeHandler = (evt) => { |
| 33 | + if (isEscapeKey(evt)) { |
| 34 | + closeFullScreen(bigPicture, body, closeButton); |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + closeButton.addEventListener('click', () => closeFullScreen(bigPicture, body, closeButton)); |
| 39 | + document.addEventListener('keydown', escapeHandler); |
| 40 | +}; |
| 41 | + |
| 42 | +// Обновление счётчика комментариев |
| 43 | +const updateCommentCount = (shown, total, bigPicture) => { |
| 44 | + const el = bigPicture?.querySelector('.social__comment-count'); |
| 45 | + if (el) { |
| 46 | + el.textContent = `${shown} из ${total} комментариев`; |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +// Создаём элемент комментария |
| 51 | +const createCommentElement = (comment) => { |
| 52 | + const commentEl = document.createElement('li'); |
| 53 | + commentEl.className = 'social__comment'; |
| 54 | + |
| 55 | + const img = document.createElement('img'); |
| 56 | + img.className = 'social__picture'; |
| 57 | + img.src = comment.avatar; |
| 58 | + img.alt = comment.name; |
| 59 | + |
| 60 | + const textEl = document.createElement('p'); |
| 61 | + textEl.className = 'social__text'; |
| 62 | + textEl.textContent = comment.message; |
| 63 | + |
| 64 | + commentEl.appendChild(img); |
| 65 | + commentEl.appendChild(textEl); |
| 66 | + |
| 67 | + return commentEl; |
| 68 | +}; |
| 69 | + |
| 70 | +const renderComments = (comments, bigPicture) => { |
| 71 | + const commentsContainer = bigPicture.querySelector('.social__comments'); |
| 72 | + const totalComments = comments.length; |
| 73 | + |
| 74 | + // Определяем диапазон комментариев для текущей порции |
| 75 | + const endIndex = Math.min(currentCommentsShown + COMMENTS_PER_PAGE, totalComments); |
| 76 | + const commentsToShow = comments.slice(currentCommentsShown, endIndex); |
| 77 | + |
| 78 | + // Отрисовываем новые комментарии |
| 79 | + commentsToShow.forEach((comment) => { |
| 80 | + const commentElement = createCommentElement(comment); |
| 81 | + commentsContainer.appendChild(commentElement); |
| 82 | + }); |
| 83 | + |
| 84 | + // Обновляем счётчик |
| 85 | + currentCommentsShown = endIndex; |
| 86 | + updateCommentCount(currentCommentsShown, totalComments, bigPicture); |
| 87 | + |
| 88 | + // Скрываем кнопку, если все комментарии загружены |
| 89 | + const commentsLoaderEl = bigPicture.querySelector('.comments-loader'); |
| 90 | + if (currentCommentsShown >= totalComments && commentsLoaderEl) { |
| 91 | + commentsLoaderEl.classList.add('hidden'); |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | +const openFullScreen = (photoData) => { |
| 96 | + const bigPicture = document.querySelector('.big-picture'); |
| 97 | + const body = document.body; |
| 98 | + |
| 99 | + // Проверка существования bigPicture-без проверки выдавало ошибку в консоли |
| 100 | + if (!bigPicture) { |
| 101 | + throw new Error('Элемент .big-picture не найден в DOM'); |
| 102 | + } |
| 103 | + |
| 104 | + //заполнение данных |
| 105 | + const setTextContent = (selector, value) => { |
| 106 | + const el = bigPicture.querySelector(selector); |
| 107 | + if (el) { |
| 108 | + el.textContent = value; |
| 109 | + } |
| 110 | + }; |
| 111 | + const setAttribute = (selector, attr, value) => { |
| 112 | + const el = bigPicture.querySelector(selector); |
| 113 | + if (el) { |
| 114 | + el[attr] = value; |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + // Заполняем данные |
| 119 | + setAttribute('.big-picture__img img', 'src', photoData.url); |
| 120 | + setTextContent('.likes-count', photoData.likes); |
| 121 | + setTextContent('.social__comment-total-count', photoData.comments.length); |
| 122 | + setTextContent('.social__caption', photoData.descriptions || ''); |
| 123 | + |
| 124 | + // Показываем элементы интерфейса |
| 125 | + const commentCountEl = bigPicture.querySelector('.social__comment-count'); |
| 126 | + const commentsLoaderEl = bigPicture.querySelector('.comments-loader'); |
| 127 | + |
| 128 | + if (commentCountEl) { |
| 129 | + commentCountEl.classList.remove('hidden'); |
| 130 | + } |
| 131 | + if (commentsLoaderEl) { |
| 132 | + commentsLoaderEl.classList.remove('hidden'); |
| 133 | + } |
| 134 | + |
| 135 | + // Очищаем комментарии и сбрасываем счётчик |
| 136 | + const commentsContainer = bigPicture.querySelector('.social__comments'); |
| 137 | + if (commentsContainer) { |
| 138 | + commentsContainer.innerHTML = ''; |
| 139 | + } |
| 140 | + currentCommentsShown = 0; |
| 141 | + |
| 142 | + // Отрисовываем первые комментарии |
| 143 | + renderComments(photoData.comments, bigPicture); |
| 144 | + |
| 145 | + // Настраиваем обработчик кнопки «Загрузить ещё» с удалением старого |
| 146 | + if (commentsLoaderEl) { |
| 147 | + // Удаляем старый обработчик, если есть |
| 148 | + commentsLoaderEl.replaceWith(commentsLoaderEl.cloneNode(true)); |
| 149 | + // Добавляем новый |
| 150 | + const newLoader = bigPicture.querySelector('.comments-loader'); |
| 151 | + newLoader.addEventListener('click', () => { |
| 152 | + renderComments(photoData.comments, bigPicture); |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + // Добавляем классы |
| 157 | + bigPicture.classList.remove('hidden'); |
| 158 | + body.classList.add('modal-open'); |
| 159 | + |
| 160 | + // Навешиваем обработчики закрытия |
| 161 | + setupCloseHandlers(bigPicture, body); |
| 162 | +}; |
| 163 | + |
| 164 | +export { openFullScreen }; |
0 commit comments