Skip to content

Commit 4a044ec

Browse files
authored
Merge pull request #7 from natalya87324/module8-task1
2 parents 5565186 + 7673c46 commit 4a044ec

File tree

4 files changed

+111
-12
lines changed

4 files changed

+111
-12
lines changed

js/full-size-photo.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { toggleClass, isEscapeKey } from './util.js';
2+
3+
const fullSizePhotoContainer = document.querySelector('.big-picture');
4+
const fullSizePhoto = fullSizePhotoContainer.querySelector('.big-picture__img img');
5+
const likesCount = document.querySelector('.likes-count');
6+
const commentCount = document.querySelector('.social__comment-count');
7+
const userCommentsContainer = document.querySelector('.social__comments');
8+
const userComment = userCommentsContainer.querySelector('.social__comment');
9+
const commentsLoader = document.querySelector('.comments-loader');
10+
const caption = document.querySelector('.social__caption');
11+
const closeBtn = document.querySelector('#picture-cancel');
12+
13+
const commentFragment = document.createDocumentFragment();
14+
let currentComments = [];
15+
16+
const toggleModal = () => {
17+
toggleClass(fullSizePhotoContainer, 'hidden');
18+
toggleClass(document.body, 'modal-open');
19+
};
20+
21+
const show = (photo) => {
22+
const { url, likes, description } = photo;
23+
fullSizePhoto.src = url;
24+
likesCount.textContent = likes;
25+
caption.textContent = description;
26+
};
27+
28+
const renderComment = (comment) => {
29+
const newComment = userComment.cloneNode(true);
30+
const avatar = newComment.querySelector('.social__picture');
31+
32+
avatar.src = comment.avatar;
33+
avatar.alt = comment.name;
34+
newComment.querySelector('.social__text').textContent = comment.message;
35+
36+
return newComment;
37+
};
38+
39+
const renderComments = () => {
40+
userCommentsContainer.innerHTML = '';
41+
commentCount.innerHTML = '';
42+
43+
commentCount.innerHTML = `${currentComments.length} из <span class="social__comment-total-count">${currentComments.length}</span> комментариев`;
44+
45+
currentComments.forEach((comment) => {
46+
commentFragment.appendChild(renderComment(comment));
47+
});
48+
49+
userCommentsContainer.appendChild(commentFragment);
50+
};
51+
52+
function closeFullSizePhoto() {
53+
document.removeEventListener('keydown', onFullSizePhotoEscKey);
54+
toggleModal();
55+
}
56+
57+
const onCloseFullSizePhoto = () => {
58+
closeFullSizePhoto();
59+
};
60+
61+
function onFullSizePhotoEscKey(evt) {
62+
if (isEscapeKey(evt)) {
63+
evt.preventDefault();
64+
closeFullSizePhoto();
65+
}
66+
}
67+
68+
const renderFullSizePhoto = (photo) => {
69+
currentComments = photo.comments.slice();
70+
show(photo);
71+
renderComments();
72+
document.addEventListener('keydown', onFullSizePhotoEscKey);
73+
toggleModal();
74+
commentsLoader.classList.add('hidden');
75+
};
76+
77+
closeBtn.addEventListener('click', onCloseFullSizePhoto);
78+
79+
export { renderFullSizePhoto };

js/main.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
import './display-photos.js';
2-
1+
import { addPhotos } from './data.js';
2+
import { renderPhotos } from './photos.js';
33

4+
renderPhotos(addPhotos());
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
import {addPhotos} from './data.js';
1+
import { renderFullSizePhoto } from './full-size-photo.js';
22

33
const userPhotosContainer = document.querySelector('.pictures');
44
const userPhotoTemplate = document.querySelector('#picture').content.querySelector('.picture');
5-
6-
const userPhotos = addPhotos();
7-
85
const userPhotosFragment = document.createDocumentFragment();
96

10-
userPhotos.forEach((photo) => {
7+
const renderPhoto = (photo) => {
118
const photoElement = userPhotoTemplate.cloneNode(true);
129
photoElement.querySelector('.picture__img').src = photo.url;
1310
photoElement.querySelector('.picture__img').alt = photo.description;
1411
photoElement.querySelector('.picture__likes').textContent = photo.likes;
1512
photoElement.querySelector('.picture__comments').textContent = photo.comments.length;
1613

17-
userPhotosFragment.appendChild(photoElement);
18-
});
14+
photoElement.addEventListener('click', (evt) => {
15+
evt.preventDefault();
16+
renderFullSizePhoto(photo);
17+
});
18+
19+
return photoElement;
20+
};
21+
22+
const renderPhotos = (photos) => {
23+
photos?.forEach((photo) => {
24+
userPhotosFragment.appendChild(renderPhoto(photo));
25+
});
26+
userPhotosContainer.appendChild(userPhotosFragment);
27+
};
28+
1929

20-
userPhotosContainer.appendChild(userPhotosFragment);
30+
export { renderPhotos };

js/util.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
const getRandomNumber = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);
22

3-
const getRandomArrayItem = (items) => items[getRandomNumber(0 , items.length - 1)];
3+
const getRandomArrayItem = (items) => items[getRandomNumber(0, items.length - 1)];
44

5-
export {getRandomNumber, getRandomArrayItem};
5+
const toggleClass = (element, className = '') => {
6+
if (element) {
7+
element.classList.toggle(className);
8+
}
9+
};
10+
11+
const isEscapeKey = (evt) => evt.key === 'Escape';
12+
13+
14+
export { getRandomNumber, getRandomArrayItem, toggleClass, isEscapeKey };

0 commit comments

Comments
 (0)