Skip to content

Commit 4b62eaf

Browse files
authored
Merge pull request #7 from zarram89/module8-task1
2 parents b0eb0fe + a2f4ff0 commit 4b62eaf

5 files changed

Lines changed: 176 additions & 23 deletions

File tree

js/main.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { getPictures } from './data/pictures.js';
2-
import { renderPictures } from './render/gallery.js';
2+
import { renderGallery } from './render/gallery.js';
3+
import { initBigPicture } from './render/big-picture.js';
34

45
const container = document.querySelector('.pictures');
56
const template = document.querySelector('#picture')?.content?.querySelector('.picture');
67

8+
initBigPicture();
9+
710
if (container && template) {
8-
renderPictures(getPictures(), container, template);
11+
renderGallery(getPictures(), container, template);
912
}

js/render/big-picture.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { isEscapeKey } from '../utils/common.js';
2+
3+
const bigPicture = document.querySelector('.big-picture');
4+
const body = document.querySelector('body');
5+
const cancelButton = bigPicture.querySelector('.big-picture__cancel');
6+
7+
const bigPictureImage = bigPicture.querySelector('.big-picture__img img');
8+
const likesCount = bigPicture.querySelector('.likes-count');
9+
const caption = bigPicture.querySelector('.social__caption');
10+
11+
const commentsList = bigPicture.querySelector('.social__comments');
12+
const commentCountBlock = bigPicture.querySelector('.social__comment-count');
13+
const commentsLoader = bigPicture.querySelector('.comments-loader');
14+
15+
const shownCount = bigPicture.querySelector('.social__comment-shown-count');
16+
const totalCount = bigPicture.querySelector('.social__comment-total-count');
17+
18+
let isOpen = false;
19+
let isInitialized = false;
20+
21+
const createComment = ({ avatar, name, message }) => {
22+
const li = document.createElement('li');
23+
li.className = 'social__comment';
24+
25+
const img = document.createElement('img');
26+
img.className = 'social__picture';
27+
img.src = avatar;
28+
img.alt = name;
29+
img.width = 35;
30+
img.height = 35;
31+
32+
const text = document.createElement('p');
33+
text.className = 'social__text';
34+
text.textContent = message;
35+
36+
li.append(img, text);
37+
38+
return li;
39+
};
40+
41+
const renderComments = (comments = []) => {
42+
commentsList.innerHTML = '';
43+
44+
const fragment = document.createDocumentFragment();
45+
46+
comments.forEach((comment) => {
47+
const commentElement = createComment(comment);
48+
fragment.append(commentElement);
49+
});
50+
51+
commentsList.append(fragment);
52+
53+
shownCount.textContent = String(comments.length);
54+
totalCount.textContent = String(comments.length);
55+
};
56+
57+
const close = () => {
58+
if (!isOpen) {
59+
return;
60+
}
61+
62+
isOpen = false;
63+
64+
bigPicture.classList.add('hidden');
65+
body.classList.remove('modal-open');
66+
67+
document.removeEventListener('keydown', onDocumentKeydown);
68+
};
69+
70+
const renderMeta = ({ url, likes, description }) => {
71+
bigPictureImage.src = url;
72+
bigPictureImage.alt = description;
73+
74+
likesCount.textContent = String(likes);
75+
caption.textContent = description;
76+
};
77+
78+
const renderBigPicture = ({ comments, ...rest }) => {
79+
renderMeta(rest);
80+
renderComments(comments);
81+
};
82+
83+
const open = (data) => {
84+
if (!data || isOpen) {
85+
return;
86+
}
87+
88+
isOpen = true;
89+
90+
bigPicture.classList.remove('hidden');
91+
body.classList.add('modal-open');
92+
93+
commentCountBlock.classList.add('hidden');
94+
commentsLoader.classList.add('hidden');
95+
96+
document.addEventListener('keydown', onDocumentKeydown);
97+
98+
renderBigPicture(data);
99+
};
100+
101+
const initBigPicture = () => {
102+
if (isInitialized) {
103+
return;
104+
}
105+
isInitialized = true;
106+
107+
cancelButton.addEventListener('click', onCancelButtonClick);
108+
};
109+
110+
function onDocumentKeydown(evt) {
111+
if (isEscapeKey(evt)) {
112+
evt.preventDefault();
113+
close();
114+
}
115+
}
116+
117+
function onCancelButtonClick() {
118+
close();
119+
}
120+
121+
export {
122+
initBigPicture,
123+
open as showBigPicture,
124+
close as hideBigPicture,
125+
};

js/render/gallery.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
1-
const createPictureElement = ({ url, description, likes, comments }, template) => {
2-
const element = template.cloneNode(true);
1+
import { renderThumbnails } from './thumbnail.js';
2+
import { showBigPicture } from './big-picture.js';
33

4-
const elementImage = element.querySelector('.picture__img');
5-
elementImage.src = url;
6-
elementImage.alt = description;
4+
const renderGallery = (pictures, container, template) => {
5+
const picturesMap = new Map(pictures.map((item) => [item.id, item]));
76

8-
element.querySelector('.picture__likes').textContent = String(likes);
9-
element.querySelector('.picture__comments').textContent = String(comments?.length ?? 0);
7+
container.addEventListener('click', (evt) => {
8+
const thumbnail = evt.target.closest('[data-thumbnail-id]');
9+
if (!thumbnail) {
10+
return;
11+
}
1012

11-
return element;
12-
};
13-
14-
const renderPictures = (pictures, container, template) => {
15-
if (!Array.isArray(pictures)) {
16-
throw new TypeError('Pictures must be an array');
17-
}
18-
19-
const fragment = document.createDocumentFragment();
20-
21-
pictures.forEach((picture) => {
22-
fragment.append(createPictureElement(picture, template));
13+
evt.preventDefault();
14+
const picture = picturesMap.get(Number(thumbnail.dataset.thumbnailId));
15+
showBigPicture(picture);
2316
});
2417

25-
container.append(fragment);
18+
renderThumbnails(pictures, container, template);
2619
};
2720

28-
export { renderPictures };
21+
export { renderGallery };

js/render/thumbnail.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const createThumbnail = ({ id, url, description, likes, comments }, template) => {
2+
const thumbnail = template.cloneNode(true);
3+
4+
const thumbnailImage = thumbnail.querySelector('.picture__img');
5+
thumbnailImage.src = url;
6+
thumbnailImage.alt = description;
7+
8+
thumbnail.querySelector('.picture__likes').textContent = String(likes);
9+
thumbnail.querySelector('.picture__comments').textContent = String(comments?.length ?? 0);
10+
thumbnail.dataset.thumbnailId = id;
11+
12+
return thumbnail;
13+
};
14+
15+
const renderThumbnails = (pictures, container, template) => {
16+
if (!Array.isArray(pictures)) {
17+
throw new TypeError('Pictures must be an array');
18+
}
19+
20+
const fragment = document.createDocumentFragment();
21+
22+
pictures.forEach((picture) => {
23+
fragment.append(createThumbnail(picture, template));
24+
});
25+
26+
container.append(fragment);
27+
};
28+
29+
export { renderThumbnails };

js/utils/common.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const isEscapeKey = (evt) => evt.key === 'Escape';
2+
3+
export { isEscapeKey };

0 commit comments

Comments
 (0)