Skip to content

Commit 5ea8239

Browse files
authored
Merge pull request #15 from AnastasiiaXX/master
2 parents 4f813c9 + da08f72 commit 5ea8239

4 files changed

Lines changed: 44 additions & 42 deletions

File tree

js/comments.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
export const COMMENTS_PER_PAGE = 5;
2-
export const renderComments = (commentsData, container) => {
1+
const COMMENTS_PER_PAGE = 5;
2+
3+
const renderComments = (commentsData, container) => {
34
commentsData.forEach((commentData) => {
45
const comment = document.createElement('li');
56
const avatar = document.createElement('img');
@@ -14,24 +15,20 @@ export const renderComments = (commentsData, container) => {
1415
commentText.classList.add('social__text');
1516
commentText.textContent = commentData.message;
1617

17-
comment.append(avatar);
18-
comment.append(commentText);
19-
18+
comment.append(avatar, commentText);
2019
container.append(comment);
2120
});
2221
};
2322

24-
export const paginateComments = (comments, container) => {
25-
const firstComments = comments.slice(0, COMMENTS_PER_PAGE);
26-
let currentIndex = COMMENTS_PER_PAGE;
27-
renderComments(firstComments, container);
28-
const loadMoreComments = () => {
29-
const nextCommentsChunk = comments.slice(currentIndex, currentIndex + COMMENTS_PER_PAGE);
30-
renderComments(nextCommentsChunk, container);
31-
currentIndex += COMMENTS_PER_PAGE;
32-
};
33-
return {
34-
loadMore: loadMoreComments,
35-
getShownCount: () => container.children.length
36-
};
23+
const initCommentsPagination = (comments, container) => {
24+
renderComments(comments.slice(0, COMMENTS_PER_PAGE), container);
25+
return COMMENTS_PER_PAGE;
3726
};
27+
28+
const loadMoreComments = (comments, container, currentIndex) => {
29+
const nextChunk = comments.slice(currentIndex, currentIndex + COMMENTS_PER_PAGE);
30+
renderComments(nextChunk, container);
31+
return currentIndex + COMMENTS_PER_PAGE;
32+
};
33+
34+
export { COMMENTS_PER_PAGE, loadMoreComments, initCommentsPagination };

js/notifications.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const ERROR_SHOW_TIME = 5000;
44

55
const dataErrorTemplate = document.querySelector('#data-error');
66
const body = document.querySelector('body');
7+
78
const showDataError = () => {
89
const template = dataErrorTemplate.content.cloneNode(true);
910
const errorDiv = template.firstElementChild;
@@ -12,36 +13,36 @@ const showDataError = () => {
1213
errorDiv.remove();
1314
}, ERROR_SHOW_TIME);
1415
};
15-
const showMessage = (templateId, buttonClass) => {
16-
let onDocumentKeydown = null;
17-
let onMessageDivClick = null;
1816

17+
const showMessage = (templateId, buttonClass) => {
1918
const template = document.querySelector(templateId).content.cloneNode(true);
2019
const messageDiv = template.firstElementChild;
2120
const messageButton = messageDiv.querySelector(buttonClass);
2221
body.append(template);
2322

24-
const onMessageButtonClick = () => {
23+
const closeMessage = () => {
2524
messageDiv.remove();
26-
document.removeEventListener('keydown', onDocumentKeydown, true);
27-
messageDiv.removeEventListener('click', onMessageDivClick);
25+
document.removeEventListener('keydown', handleKeydown, true);
26+
messageButton.removeEventListener('click', closeMessage);
27+
messageDiv.removeEventListener('click', handleOverlayClick);
2828
};
2929

30-
onDocumentKeydown = (evt) => {
30+
function handleKeydown(evt) {
3131
evt.stopPropagation();
3232
if (isEscapeKey(evt)) {
33-
onMessageButtonClick();
33+
closeMessage();
3434
}
35-
};
35+
}
3636

37-
onMessageDivClick = (evt) => {
37+
function handleOverlayClick(evt) {
3838
if (evt.target === messageDiv) {
39-
onMessageButtonClick();
39+
closeMessage();
4040
}
41-
};
42-
messageButton.addEventListener('click', onMessageButtonClick);
43-
document.addEventListener('keydown', onDocumentKeydown, true);
44-
messageDiv.addEventListener('click', onMessageDivClick);
41+
}
42+
43+
messageButton.addEventListener('click', closeMessage);
44+
document.addEventListener('keydown', handleKeydown, true);
45+
messageDiv.addEventListener('click', handleOverlayClick);
4546
};
4647

4748
const showSuccessMessage = () => showMessage('#success', '.success__button');

js/photos-filters.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const ACTIVE_FILTER_CLASS = 'img-filters__button--active';
2+
13
const filtersBlock = document.querySelector('.img-filters');
24
const filterDefault = document.querySelector('#filter-default');
35
const filterRandom = document.querySelector('#filter-random');
@@ -17,18 +19,18 @@ const toggleActiveBtn = (btnClicked, activeClass) => {
1719
};
1820
const initPhotosFiltering = (photos, onRender) => {
1921
const onFilterDefaultClick = () => {
20-
toggleActiveBtn(filterDefault, 'img-filters__button--active');
22+
toggleActiveBtn(filterDefault, ACTIVE_FILTER_CLASS);
2123
onRender(photos);
2224
};
2325

2426
const onFilterRandomClick = () => {
25-
toggleActiveBtn(filterRandom, 'img-filters__button--active');
27+
toggleActiveBtn(filterRandom, ACTIVE_FILTER_CLASS);
2628
const randomPhotos = photos.slice().sort(() => 0.5 - Math.random()).slice(0, 10);
2729
onRender(randomPhotos);
2830
};
2931

3032
const onFilterDiscussedClick = () => {
31-
toggleActiveBtn(filterDiscussed, 'img-filters__button--active');
33+
toggleActiveBtn(filterDiscussed, ACTIVE_FILTER_CLASS);
3234
const discussedPhotos = photos.slice().sort((a, b) => b.comments.length - a.comments.length);
3335
onRender(discussedPhotos);
3436
};

js/show-full-photo.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { closeModal, openModal } from './modal.js';
2-
import { COMMENTS_PER_PAGE, paginateComments } from './comments.js';
2+
import { COMMENTS_PER_PAGE, initCommentsPagination, loadMoreComments } from './comments.js';
33

44
const fullPhotoModal = document.querySelector('.big-picture');
55
const body = document.querySelector('body');
@@ -37,14 +37,16 @@ export const showFullPhoto = (photos) => {
3737
fillPhotoData(currentPhoto);
3838

3939
commentsLoader.classList.toggle('hidden', currentPhoto.comments.length <= COMMENTS_PER_PAGE);
40-
const pagination = paginateComments(currentPhoto.comments, comments);
41-
shownCommentsCount.textContent = pagination.getShownCount();
40+
41+
let currentIndex = initCommentsPagination(currentPhoto.comments, comments);
42+
shownCommentsCount.textContent = comments.children.length;
4243

4344
const onCommentsLoaderClick = () => {
44-
pagination.loadMore();
45-
shownCommentsCount.textContent = pagination.getShownCount();
46-
commentsLoader.classList.toggle('hidden', pagination.getShownCount() >= currentPhoto.comments.length);
45+
currentIndex = loadMoreComments(currentPhoto.comments, comments, currentIndex);
46+
shownCommentsCount.textContent = comments.children.length;
47+
commentsLoader.classList.toggle('hidden', comments.children.length >= currentPhoto.comments.length);
4748
};
49+
4850
if (currentCommentsHandler) {
4951
commentsLoader.removeEventListener('click', currentCommentsHandler);
5052
currentCommentsHandler = null;

0 commit comments

Comments
 (0)