Skip to content

Commit 1c569fa

Browse files
authored
Merge pull request #11 from khepriKreator/module8-task1
2 parents 8317589 + 70a8a23 commit 1c569fa

4 files changed

Lines changed: 74 additions & 7 deletions

File tree

js/fake-data.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import {
1111
const generateFakeCommentsArray = () => Array.from({length: getRandomNumber(CommentsCount.MIN, CommentsCount.MAX)}, (_, id) => ({
1212
id: id + 26,
1313
avatar: `img/avatar-${getRandomNumber(1, 6)}.svg`,
14-
message: MessagesArray[getRandomNumber(0, MessagesArray.length)],
14+
message: MessagesArray[getRandomNumber(0, MessagesArray.length - 1)],
1515
name: Names[getRandomNumber(0, Names.length)],
1616
}));
1717

1818
//Генерирует массив данных
1919
const generateFakeDataArray = (photosCount) => Array.from({length: photosCount}, (_, i) => ({
2020
id: i + 1,
2121
url: `photos/${i + 1}.jpg`,
22-
description: DescriptionArray[getRandomNumber(0, DescriptionArray.length)],
22+
description: DescriptionArray[getRandomNumber(0, DescriptionArray.length - 1)],
2323
likes: getRandomNumber(Likes.MIN, Likes.MAX),
2424
comments: generateFakeCommentsArray(),
2525
}));

js/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import './upload-modal.js';
55
import './functions.js';
66
import {renderPics} from './render-pics.js';
77

8-
const photos = generateFakeDataArray(PHOTOS_COUNT);
9-
renderPics(photos);
8+
const data = generateFakeDataArray(PHOTOS_COUNT);
9+
renderPics(data);

js/render-pics.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1+
import { onOpenModal } from './view-modal';
2+
13
const template = document.querySelector('#picture').content;
24

35
const renderPics = (photos) => {
46
const picturesContainer = document.querySelector('.pictures');
57
photos.forEach((item) => {
6-
const {url, description, likes: likesCount, comments} = item;
8+
const {url, description, likes, comments} = item;
79
const picture = template.cloneNode(true);
810
const image = picture.querySelector('.picture__img');
9-
const likes = picture.querySelector('.picture__likes');
11+
const likesCount = picture.querySelector('.picture__likes');
1012
const commentsCount = picture.querySelector('.picture__comments');
13+
const link = picture.querySelector('.picture');
14+
link.addEventListener('click', (evt) => {
15+
evt.preventDefault();
16+
onOpenModal(item);
17+
});
1118
image.src = url;
1219
image.alt = description;
13-
likes.textContent = likesCount;
20+
likesCount.textContent = likes;
1421
commentsCount.textContent = comments.length;
1522
picturesContainer.appendChild(picture);
1623
});

js/view-modal.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Задание:
3+
1) По клику на миниатюру должно открываться модальное окно .big-picture,
4+
в котором будет отрисовываться информация кликнутой миниатюры. Отрисовка реализована через удаление класса hidden у .big-picture.
5+
2) Комментарии вставляются в блок .social__comments.
6+
3) .social__caption - описание фотографии (description)
7+
4) после открытия модалки, скрываем через добавление класса hidden счетчик комментариев .social__comment-count и загрузку новых .comments-loader.
8+
5) после открытия окна добавляем боди класс modal-open. При закрытии, соответственно, удаляем.
9+
6) закрытие по клику на кнопку и по нажатию на ESC
10+
*/
11+
12+
const modalWindow = document.querySelector('.big-picture');
13+
const bigPictureContainer = modalWindow.querySelector('.big-picture__img');
14+
const bigPictureImg = bigPictureContainer.querySelector('img');
15+
const userAvatar = modalWindow.querySelector('.social__picture');
16+
const caption = modalWindow.querySelector('.social__caption');
17+
const likesCount = modalWindow.querySelector('.likes-count');
18+
const closeButton = modalWindow.querySelector('.big-picture__cancel');
19+
const commentsContainer = modalWindow.querySelector('.social__comments');
20+
const commentItem = modalWindow.querySelector('.social__comment');
21+
const commentsCount = modalWindow.querySelector('.social__comment-count');
22+
const commentsShownCount = commentsCount.querySelector('.social__comment-shown-count');
23+
const commentsTotalCount = commentsCount.querySelector('.social__comment-total-count');
24+
const commentsLoaderButton = modalWindow.querySelector('.social__comments-loader');
25+
26+
const createComment = (commentData) => {
27+
const {avatar, message, name} = commentData;
28+
const commentItemClone = commentItem.cloneNode(true);
29+
const commentAvatar = commentItemClone.querySelector('.social__picture');
30+
const commentText = commentItemClone.querySelector('.social__text');
31+
commentAvatar.src = avatar;
32+
commentAvatar.alt = name;
33+
commentText.textContent = message;
34+
commentsContainer.appendChild(commentItemClone);
35+
};
36+
37+
const onCloseModal = () => {
38+
modalWindow.classList.add('hidden');
39+
document.body.classList.remove('modal-open');
40+
closeButton.removeEventListener('click', onCloseModal);
41+
document.removeEventListener('keydown', onCloseModal);
42+
};
43+
const onOpenModal = (data) => {
44+
const {url, description, likes, comments} = data;
45+
commentsCount.classList.add('hidden');
46+
commentsLoaderButton.classList.add('hidden');
47+
commentsContainer.innerHTML = '';
48+
comments.forEach(createComment);
49+
bigPictureImg.src = url;
50+
caption.textContent = description;
51+
likesCount.textContent = likes;
52+
modalWindow.classList.remove('hidden');
53+
document.body.classList.add('modal-open');
54+
closeButton.addEventListener('click', onCloseModal);
55+
document.addEventListener('keydown', onCloseModal);
56+
};
57+
58+
export {
59+
onOpenModal,
60+
};

0 commit comments

Comments
 (0)