Skip to content

Commit

Permalink
✔️ Сборка #7
Browse files Browse the repository at this point in the history
  • Loading branch information
Keks committed Dec 11, 2024
1 parent 20310e6 commit 07852b5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 22 deletions.
29 changes: 7 additions & 22 deletions 7/js/open-full-picture.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {isEscapeKey} from './util.js';
import {picturesContainer} from './render-thumbnails.js';
import {createComment} from './data.js';
import { clearComments, renderComments } from './render-comments.js';

const fullPicture = document.querySelector('.big-picture');
const closeButton = document.querySelector('.big-picture__cancel');
const commentsList = document.querySelector('.social__comments'); //socialCommentsNode
const commentTemplate = commentsList.querySelector('.social__comment');

const onDocumentKeydown = (evt) => {
if (isEscapeKey(evt)) {
Expand All @@ -21,38 +20,24 @@ const onClickButtonClose = (evt) => {

const openFullPicture = (pictureId, data) => {
const currentPhoto = data.find((photo) => photo.id === Number(pictureId));
const commentsFragment = document.createDocumentFragment();

document.querySelector('body').classList.add('modal-open');
document.querySelector('.big-picture').classList.remove('hidden');
fullPicture.classList.remove('hidden');

document.querySelector('.big-picture .big-picture__img img').src = currentPhoto.url;
document.querySelector('.likes-count').textContent = currentPhoto.likes;
document.querySelector('.social__comment-shown-count').textContent = currentPhoto.comments.length;
document.querySelector('.social__caption').textContent = currentPhoto.description;

commentsList.innerHTML = '';

currentPhoto.comments.forEach((comment) => {
const pictureComment = commentTemplate.cloneNode(true);

pictureComment.querySelector('.social__picture').src = comment.avatar;
pictureComment.querySelector('.social__picture').alt = comment.name;
pictureComment.querySelector('.social__text').textContent = comment.message;

commentsFragment.appendChild(pictureComment);
});

commentsList.appendChild(commentsFragment);
renderComments(currentPhoto.comments);

document.addEventListener('keydown', onDocumentKeydown);
closeButton.addEventListener('click', onClickButtonClose);
};


function closeFullPicture() {
document.querySelector('.big-picture').classList.add('hidden');
document.querySelector('.social__comment-count').classList.add('hidden');
document.querySelector('.comments-loader').classList.add('hidden');
clearComments();
fullPicture.classList.add('hidden');
document.querySelector('body').classList.remove('modal-open');
document.removeEventListener('keydown', onDocumentKeydown);
}
Expand Down
53 changes: 53 additions & 0 deletions 7/js/render-comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const commentsList = document.querySelector('.social__comments');
const commentTemplate = commentsList.querySelector('.social__comment');
const commentCount = document.querySelector('.social__comment-count');
const commentsLoader = document.querySelector('.social__comment-loader');

const VISIBLE_COMMENTS = 5;
let shownComments = 0;
let comments = [];

commentsList.innerHTML = '';

const renderNextComments = () => {
const commentsFragment = document.createDocumentFragment();
const renderedComments = comments.slice(shownComments, shownComments + VISIBLE_COMMENTS);
const renderedCommentsLength = renderedComments.length + shownComments;

renderedComments.forEach ((comment) => {
const pictureComment = commentTemplate.cloneNode(true);

pictureComment.querySelector('.social__picture').src = comment.avatar;
pictureComment.querySelector('.social__picture').alt = comment.name;
pictureComment.querySelector('.social__text').textContent = comment.message;

commentsFragment.appendChild(pictureComment);
});

commentsList.appendChild(commentsFragment);

commentCount.firstChild.textContent = `${renderedCommentsLength} из `;
commentCount.querySelector('.comments-count').textContent = comments.length;

if (renderedCommentsLength >= comments.length) {
commentsLoader.classList.add('hidden');
}

shownComments += VISIBLE_COMMENTS;
};

const clearComments = () => {
shownComments = 0;
commentsList.innerHTML = '';
commentsLoader.classList.remove('hidden');
commentsLoader.removeEventListener('click', renderNextComments);
};

const renderComments = (currentPhotoComments) => {
comments = currentPhotoComments;
renderNextComments();

commentsLoader.addEventListener('click', renderNextComments);
};

export {clearComments, renderComments};

0 comments on commit 07852b5

Please sign in to comment.