Skip to content

Commit a136872

Browse files
author
Юлия Перелыгина
committed
добавляет обработчики изменения фильтров
1 parent f141c71 commit a136872

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

js/debounce.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const DEBOUNCE_DELAY = 500;
2+
3+
const debounce = (callback, timeoutDelay = DEBOUNCE_DELAY) => {
4+
let timeoutId;
5+
6+
return (...rest) => {
7+
clearTimeout(timeoutId);
8+
timeoutId = setTimeout(() => callback.apply(this, rest), timeoutDelay);
9+
};
10+
};
11+
12+
export { debounce };

js/filters.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { renderPictures } from './pictures.js';
2+
import { debounce } from './debounce.js';
3+
4+
const RANDOM_PICTURES_COUNT = 10;
5+
6+
const filtersElement = document.querySelector('.img-filters');
7+
const filtersForm = document.querySelector('.img-filters__form');
8+
9+
const FilterType = {
10+
DEFAULT: 'filter-default',
11+
RANDOM: 'filter-random',
12+
DISCUSSED: 'filter-discussed',
13+
};
14+
15+
let currentFilter = FilterType.DEFAULT;
16+
let pictures = [];
17+
18+
const getRandomPictures = (data) => {
19+
const shuffled = [...data].sort(() => Math.random() - 0.5);
20+
return shuffled.slice(0, RANDOM_PICTURES_COUNT);
21+
};
22+
23+
const getDiscussedPictures = (data) =>
24+
[...data].sort((a, b) => b.comments.length - a.comments.length);
25+
26+
const filterPictures = () => {
27+
switch (currentFilter) {
28+
case FilterType.RANDOM:
29+
return getRandomPictures(pictures);
30+
case FilterType.DISCUSSED:
31+
return getDiscussedPictures(pictures);
32+
default:
33+
return [...pictures];
34+
}
35+
};
36+
37+
const onFilterChange = debounce(() => {
38+
renderPictures(filterPictures());
39+
});
40+
41+
const onFiltersFormClick = (evt) => {
42+
const target = evt.target;
43+
44+
if (!target.classList.contains('img-filters__button')) {
45+
return;
46+
}
47+
48+
if (target.id === currentFilter) {
49+
return;
50+
}
51+
52+
filtersForm.querySelector('.img-filters__button--active').classList.remove('img-filters__button--active');
53+
target.classList.add('img-filters__button--active');
54+
currentFilter = target.id;
55+
onFilterChange();
56+
};
57+
58+
const initFilters = (data) => {
59+
pictures = data;
60+
filtersElement.classList.remove('img-filters--inactive');
61+
filtersForm.addEventListener('click', onFiltersFormClick);
62+
};
63+
64+
export { initFilters };

js/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { initBigPictureHandlers } from './big-picture.js';
55
import { initFormUpload } from './form-upload.js';
66
import { getData } from './api.js';
77
import { showDataError } from './messages.js';
8+
import { initFilters } from './filters.js';
89

910
getData()
1011
.then((pictures) => {
1112
renderPictures(pictures);
13+
initFilters(pictures);
1214
})
1315
.catch(() => {
1416
showDataError();

0 commit comments

Comments
 (0)