Skip to content

Commit

Permalink
Merge pull request #7 from KateSolodchuk/module8-task1
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored Dec 11, 2024
2 parents ad5b007 + 7f216e3 commit b7a5d6c
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 27 deletions.
4 changes: 2 additions & 2 deletions js/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@ const createPhoto = () => {
};
};

const CreatePhotoList = () => Array.from({length: PHOTO_LIST_LENGTH}, createPhoto());
const createPhotoList = () => Array.from({length: PHOTO_LIST_LENGTH}, createPhoto());

export {CreatePhotoList};
export {createPhotoList, createComment};
13 changes: 10 additions & 3 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import './util.js';
import {CreatePhotoList} from './data.js';
import './thumbnails.js';
import {createPhotoList} from './data.js';
import {renderThumbnails, picturesContainer} from './render-thumbnails.js';

Check failure on line 3 in js/main.js

View workflow job for this annotation

GitHub Actions / Check

'picturesContainer' is defined but never used
import {initClickListener} from './open-full-picture.js';

const data = createPhotoList();

renderThumbnails(data);
initClickListener(data);



Check failure on line 12 in js/main.js

View workflow job for this annotation

GitHub Actions / Check

Too many blank lines at the end of file. Max of 2 allowed
console.log(CreatePhotoList());
56 changes: 56 additions & 0 deletions js/open-full-picture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {isEscapeKey} from './util.js';
import {picturesContainer} from './render-thumbnails.js';
import { clearComments, renderComments } from './render-comments.js';

const fullPicture = document.querySelector('.big-picture');
const closeButton = document.querySelector('.big-picture__cancel');

const onDocumentKeydown = (evt) => {
if (isEscapeKey(evt)) {
evt.preventDefault();
closeFullPicture();
}
};

const onClickButtonClose = (evt) => {
evt.preventDefault();
closeFullPicture();
};


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

document.querySelector('body').classList.add('modal-open');
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;

renderComments(currentPhoto.comments);

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

function closeFullPicture() {
clearComments();
fullPicture.classList.add('hidden');
document.querySelector('body').classList.remove('modal-open');
document.removeEventListener('keydown', onDocumentKeydown);
}

const initClickListener = (data) => {
picturesContainer.addEventListener('click', (evt) => {
const currentPicture = evt.target.closest('.picture');

if (currentPicture) {
openFullPicture(currentPicture.dataset.pictureId, data);
}
});
};

export {openFullPicture, closeFullPicture, initClickListener};

53 changes: 53 additions & 0 deletions 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};
27 changes: 27 additions & 0 deletions js/render-thumbnails.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {createPhotoList} from './data.js';

const picturesContainer = document.querySelector('.pictures');
const thumbnailTemplate = document.querySelector('#picture')
.content
.querySelector('.picture');

const usersThumbnails = createPhotoList ();

const renderThumbnails = () => {
const usersThumbnailsFragment = document.createDocumentFragment();

usersThumbnails.forEach((photo) => {
const usersElement = thumbnailTemplate.cloneNode(true);
usersElement.dataset.pictureId = photo.id;
usersElement.querySelector('.picture__img').src = photo.url;
usersElement.querySelector('.picture__img').alt = photo.description;
usersElement.querySelector('.picture__likes').textContent = photo.likes;
usersElement.querySelector('.picture__comments').textContent = photo.comments.length;
usersThumbnailsFragment.appendChild(usersElement);
});

picturesContainer.appendChild(usersThumbnailsFragment);

};

export {renderThumbnails, picturesContainer};
22 changes: 0 additions & 22 deletions js/thumbnails.js

This file was deleted.

3 changes: 3 additions & 0 deletions js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ const getRandomInteger = (a, b) => {
};
};

const isEscapeKey = (evt) => evt.key === 'Escape';

export {getRandomInteger};
export {isEscapeKey};

0 comments on commit b7a5d6c

Please sign in to comment.