Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Открывается и закрывается #8

Merged
merged 12 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ const isInWorkingTime = function (workStart, workEnd, meetingStart, meetingTime)

return workStartInMinute < meetingStartInMinute && meetingEnd <= workEndInMinute;
};
isInWorkingTime();
isInWorkingTime('1:1', '1:1', '1:1', 90);
63 changes: 63 additions & 0 deletions js/gallery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { openPreview } from './preview.js';

const template = document.querySelector('#picture').content.querySelector('.picture');
const container = document.querySelector('.pictures');

// создает миниатюру

const createThumbnail = ({url, description, likes, comments, id}) => {
const thumbnail = template.cloneNode(true);

const thumbnailImage = thumbnail.querySelector('.picture__img');

thumbnailImage.src = url;
thumbnailImage.alt = description;
thumbnail.querySelector('.picture__likes').textContent = likes;
thumbnail.querySelector('.picture__comments').textContent = comments.length ?? 0;
thumbnail.dataset.id = id;

return thumbnail;
};

// создает блок миниатюр

const renderThumbnails = (photosArray) => {
photosArray.forEach((photo) => {
container.append(createThumbnail(photo));
});
};

// находит в массиве данные нажатой миниатюры

const findPhotoById = (selectedThumbnail, photosArray) => {
const thumbnailId = Number(selectedThumbnail.dataset.id);

return photosArray.find((photo) => photo.id === thumbnailId);
};

// передает данные нажатой миниатюры

const getSelectedThumbnailData = (photosArray) => {
container.addEventListener('click', (evt) => {
const selectedThumbnail = evt.target.closest('.picture');
if (!selectedThumbnail) {
return;
}

evt.preventDefault();

const selectedThumbnailData = findPhotoById(selectedThumbnail, photosArray);
openPreview(selectedThumbnailData);
});

};

// передает исходные данные

const initGallery = (photosArray) => {

renderThumbnails(photosArray);
getSelectedThumbnailData(photosArray);
};

export { initGallery };
4 changes: 2 additions & 2 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPhotos } from './photo-description.js';
import { createThumbnails } from './thumbnails.js';
import { initGallery } from './gallery.js';

createThumbnails(getPhotos());
initGallery(getPhotos());
70 changes: 70 additions & 0 deletions js/preview-comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const COMMENTS_NUMBER = 5;

const previewShownComments = document.querySelector('.social__comment-shown-count');
const previewMoreButton = document.querySelector('.comments-loader');
const previewCommentsList = document.querySelector('.social__comments');

let comments = [];
let commentsShown = 0;

// создает элемент комментария

const getPreviewComments = (commentData) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

функция возвращает один коммент getPreviewComment

const previewComment = document.createElement('li');
previewComment.classList.add('social__comment');

previewComment.innerHTML = `
<img
class="social__picture"
src="${commentData.avatar}"
alt="${commentData.name}"
width="35" height="35">
<p class="social__text">${commentData.message}</p>`;

return previewComment;
};

// добавляет комментарии в список

const addComment = (commentsBlock) => {
commentsBlock.forEach((comment) => {
previewCommentsList.append(getPreviewComments(comment));
});
};

// скрывает кнопку превью "загрузить еще" если комментариев больше нет

const checkButtonClass = () => {
previewMoreButton.classList.toggle('hidden',(commentsShown >= comments.length));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(commentsShown >= comments.length) - скобки не обязательны

};

// создает список комментариев для превью

const renderNextComment = () => {

const nextComments = comments.slice(commentsShown, commentsShown + COMMENTS_NUMBER);
addComment(nextComments);
commentsShown += nextComments.length;

checkButtonClass();
previewShownComments.textContent = commentsShown;
};

// очищает комментарии и удаляет обработчик

const clearComments = () => {
commentsShown = 0;
previewCommentsList.innerHTML = '';
previewMoreButton.removeEventListener('click', renderNextComment);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

именование обработчика

};

// добавляет комментарии по клику

const createCommentsList = (newComments) => {
clearComments();
comments = newComments;
previewMoreButton.addEventListener('click', renderNextComment);
renderNextComment();
};

export { createCommentsList, clearComments };
58 changes: 58 additions & 0 deletions js/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { isEscapeKey } from './util.js';
import { createCommentsList, clearComments } from './preview-comments.js';


const preview = document.querySelector('.big-picture');
const previewImg = preview.querySelector('.big-picture__img img');
const previewLikesCount = preview.querySelector('.likes-count');
const previewCommentsCount = preview.querySelector('.social__comment-total-count');
const previewDescription = preview.querySelector('.social__caption');
const previewCloseButton = document.querySelector('.big-picture__cancel');


// создает превью по клику на миниатюру

const createPreview = ({url, likes, comments, description}) => {
previewImg.src = url;
previewLikesCount.textContent = likes;
previewCommentsCount.textContent = comments.length;
previewDescription.textContent = description;

createCommentsList(comments);
};

// Логика открытия превью

const openPreview = (pictureData) => {
preview.classList.remove('hidden');
createPreview(pictureData);
document.addEventListener('keydown', onDocumentKeydown);

document.body.classList.add('modal-open');

};

// Логика закрытия превью

const closePreview = () => {
preview.classList.add('hidden');
document.removeEventListener('keydown', onDocumentKeydown);
document.body.classList.remove('modal-open');
clearComments();
};

// функция закрытия превью по нажатию escape

function onDocumentKeydown(evt) {
if (isEscapeKey(evt)) {
evt.preventDefault();
closePreview();
}
}

// обработчик клика на кнопку закрытия превью

previewCloseButton.addEventListener('click', closePreview);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

именование



export { openPreview };
24 changes: 0 additions & 24 deletions js/thumbnails.js

This file was deleted.

6 changes: 5 additions & 1 deletion js/util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const ESCAPE = 'Escape';

const getRandomInteger = (min, max) => {
const lower = Math.ceil(Math.min(min, max));
Expand All @@ -13,6 +14,9 @@ const getId = () => {
const getCommentId = getId();
const getPhotoId = getId();

export { getRandomInteger, getCommentId, getPhotoId };
// Проверка нажатой клавиши

const isEscapeKey = (evt) => evt.key === ESCAPE;

export { getRandomInteger, getCommentId, getPhotoId, isEscapeKey };