Skip to content

Commit beb7117

Browse files
authored
Merge pull request #7 from Tikud11/module8-task1
2 parents 1dc051f + dfb214f commit beb7117

5 files changed

Lines changed: 150 additions & 16 deletions

File tree

js/big-picture.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// import { photosData } from './mock.js';
2+
import { initComments } from './comments.js';
3+
4+
const body = document.querySelector('body');
5+
const bigPicture = document.querySelector('.big-picture');
6+
const image = bigPicture.querySelector('.big-picture__preview img');
7+
const likesCount = bigPicture.querySelector('.likes-count');
8+
const socialCaption = bigPicture.querySelector('.social__caption');
9+
const cancelBtn = bigPicture.querySelector('.big-picture__cancel');
10+
const isEscapeKey = (evt) => evt.key === 'Escape';
11+
12+
const closeBigPicture = () => {
13+
bigPicture.classList.add('hidden');
14+
body.classList.remove('modal-open');
15+
document.removeEventListener('keydown', onDocumentKeydown);
16+
};
17+
18+
function onDocumentKeydown(evt) {
19+
if (isEscapeKey(evt)) {
20+
evt.preventDefault();
21+
closeBigPicture();
22+
}
23+
}
24+
25+
cancelBtn.addEventListener('click', closeBigPicture);
26+
27+
export const renderBigPicture = (photo) => {
28+
29+
image.src = photo.url;
30+
likesCount.textContent = photo.likes;
31+
socialCaption.textContent = photo.description;
32+
33+
initComments(photo.comments);
34+
35+
bigPicture.classList.remove('hidden');
36+
body.classList.add('modal-open');
37+
document.addEventListener('keydown', onDocumentKeydown);
38+
};

js/comments.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { getPluralWord } from './utils.js';
2+
3+
const MAX_COMMENTS_VIEW = 5;
4+
5+
const bigPicture = document.querySelector('.big-picture');
6+
const commentsLoader = bigPicture.querySelector('.comments-loader');
7+
const socialCommentsContainer = bigPicture.querySelector('.social__comments');
8+
const socialComment = bigPicture.querySelector('.social__comment');
9+
const commentCountContainer = bigPicture.querySelector('.social__comment-count');
10+
const commentShownCount = bigPicture.querySelector('.social__comment-shown-count');
11+
const commentTotalCount = bigPicture.querySelector('.social__comment-total-count');
12+
13+
let currentListComments = [];
14+
let currentCommentsCount = 0;
15+
16+
const renderOneComment = ({ avatar, name, message }) => {
17+
const newComment = socialComment.cloneNode(true);
18+
const authorsImage = newComment.querySelector('.social__picture');
19+
20+
authorsImage.src = avatar;
21+
authorsImage.alt = name;
22+
newComment.querySelector('.social__text').textContent = message;
23+
return newComment;
24+
};
25+
26+
const renderComments = (comments) => {
27+
comments.forEach((comment) => {
28+
socialCommentsContainer.append(renderOneComment(comment));
29+
});
30+
};
31+
32+
export const initComments = (comments) => {
33+
const showCommentText = commentCountContainer.childNodes[4];
34+
const word = getPluralWord(comments.length, ['комментарий', 'комментария', 'комментариев']);
35+
36+
if (comments.length <= MAX_COMMENTS_VIEW) {
37+
commentsLoader.classList.add('hidden');
38+
} else {
39+
commentsLoader.classList.remove('hidden');
40+
}
41+
42+
commentTotalCount.textContent = comments.length;
43+
showCommentText.textContent = `${word.padStart(word.length + 1)}`;
44+
socialCommentsContainer.innerHTML = '';
45+
46+
currentListComments = comments;
47+
currentCommentsCount = Math.min(comments.length, MAX_COMMENTS_VIEW);
48+
commentShownCount.textContent = currentCommentsCount;
49+
50+
renderComments(currentListComments.slice(0, currentCommentsCount));
51+
};
52+
53+
commentsLoader.addEventListener('click', () => {
54+
let previousCommentsCount = currentCommentsCount;
55+
56+
currentCommentsCount = Math.min(currentListComments.length, currentCommentsCount + MAX_COMMENTS_VIEW);
57+
58+
const newCommentsBatch = currentListComments.slice(previousCommentsCount, currentCommentsCount);
59+
60+
renderComments(newCommentsBatch);
61+
commentShownCount.textContent = currentCommentsCount;
62+
63+
if (currentCommentsCount >= currentListComments.length) {
64+
commentsLoader.classList.add('hidden');
65+
}
66+
});

js/main.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
import { photosData } from './mock';
2-
import { setThumbnails } from './photos';
2+
import { renderThumbnails } from './photos';
3+
import { renderBigPicture } from './big-picture';
34

4-
const container = document.querySelector('.pictures');
5+
renderThumbnails(photosData);
56

6-
setThumbnails(photosData, container);
7+
document.body.addEventListener('click', (evt) => {
8+
const pictureNode = evt.target.closest('.picture');
9+
10+
if (!pictureNode) {
11+
return;
12+
}
13+
14+
evt.preventDefault();
15+
16+
const photoId = pictureNode.dataset.id;
17+
const currentPhoto = photosData.find((photo) => photo.id === Number(photoId));
18+
19+
if (currentPhoto) {
20+
renderBigPicture(currentPhoto);
21+
}
22+
23+
});

js/photos.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1-
const template = document.querySelector('#picture').content;
1+
const template = document.querySelector('#picture').content.querySelector('.picture');
2+
const container = document.querySelector('.pictures');
23

3-
const createThumbnail = ({ id, comments, likes, url, description }) => {
4+
const createThumbnail = ({ id, url, description, comments, likes }) => {
45
const thumbnail = template.cloneNode(true);
5-
6-
const commentsContainer = thumbnail.querySelector('.picture__comments');
7-
const likesContainer = thumbnail.querySelector('.picture__likes');
86
const image = thumbnail.querySelector('.picture__img');
97

10-
thumbnail.children[0].dataset.id = id;
11-
commentsContainer.textContent = comments.length;
12-
likesContainer.textContent = likes;
138
image.src = url;
149
image.alt = description;
10+
thumbnail.dataset.id = id;
11+
thumbnail.href = url;
12+
thumbnail.querySelector('.picture__comments').textContent = comments.length;
13+
thumbnail.querySelector('.picture__likes').textContent = likes;
1514

1615
return thumbnail;
1716
};
1817

19-
export const setThumbnails = (photos, container) => {
18+
export const renderThumbnails = (photos) => {
2019
const fragment = document.createDocumentFragment();
21-
photos.forEach((photoItem) => {
22-
const thumbnail = createThumbnail(photoItem);
23-
fragment.appendChild(thumbnail);
20+
21+
photos.forEach((photo) => {
22+
const thumbnail = createThumbnail(photo);
23+
fragment.append(thumbnail);
2424
});
2525

26-
container.appendChild(fragment);
26+
container.append(fragment);
2727
};
28+
29+

js/utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,16 @@ export const getRandomNumber = (max, min = 0) => {
1616
return Math.floor(Math.random() * (max - min + 1) + min);
1717
};
1818

19+
export const getPluralWord = (count, [one, two, five]) => {
20+
const someNum = Math.abs(count) % 100;
21+
const oneNum = someNum % 10;
22+
23+
if (someNum > 10 && someNum < 20) { return five };
24+
if (oneNum > 1 && oneNum < 5) { return two };
25+
if (oneNum == 1) { return one };
26+
27+
return five;
28+
};
29+
1930
export const getRandomArrayElement = (arr) =>
2031
arr[getRandomNumber(arr.length - 1)];

0 commit comments

Comments
 (0)