Skip to content

Commit 8584677

Browse files
committed
implementation of task 12.12 (photo filtering)
1 parent 3762d0e commit 8584677

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

js/filters.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { renderGallery } from './gallery.js';
2+
import { debounce } from './util.js';
3+
4+
const filtersContainer = document.querySelector('.img-filters');
5+
const filtersForm = filtersContainer.querySelector('.img-filters__form');
6+
7+
const RANDOM_PHOTOS_COUNT = 10;
8+
9+
let photos = [];
10+
11+
const clearGallery = () => {
12+
document.querySelectorAll('.picture').forEach((picture) => {
13+
picture.remove();
14+
});
15+
};
16+
17+
const getDefaultPhotos = () => photos.slice();
18+
19+
const getRandomPhotos = () => {
20+
const shuffled = photos.slice().sort(() => Math.random() - 0.5);
21+
return shuffled.slice(0, RANDOM_PHOTOS_COUNT);
22+
};
23+
24+
const getDiscussedPhotos = () =>
25+
photos
26+
.slice()
27+
.sort((a, b) => b.comments.length - a.comments.length);
28+
29+
const filterHandlers = {
30+
default: getDefaultPhotos,
31+
random: getRandomPhotos,
32+
discussed: getDiscussedPhotos,
33+
};
34+
35+
const applyFilter = (filter) => {
36+
clearGallery();
37+
renderGallery((filterHandlers[filter] || filterHandlers.default)());
38+
};
39+
40+
const debouncedApplyFilter = debounce(applyFilter);
41+
42+
const onFilterClick = (evt) => {
43+
if (!evt.target.matches('button')) {
44+
return;
45+
}
46+
47+
const activeButton = filtersForm.querySelector('.img-filters__button--active');
48+
activeButton.classList.remove('img-filters__button--active');
49+
evt.target.classList.add('img-filters__button--active');
50+
51+
const filter = evt.target.id.replace('filter-', '');
52+
debouncedApplyFilter(filter);
53+
};
54+
55+
const initFilters = (loadedPhotos) => {
56+
photos = loadedPhotos.slice();
57+
58+
filtersContainer.classList.remove('img-filters--inactive');
59+
filtersForm.addEventListener('click', onFilterClick);
60+
};
61+
62+
export { initFilters };

js/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { renderGallery } from './gallery.js';
22
import { initPhotoUploadForm } from './photo-upload-form.js';
33
import { getData } from './api.js';
4+
import { initFilters } from './filters.js';
45
import { showDataError } from './messages.js';
56

67
getData()
78
.then((photos) => {
89
renderGallery(photos);
10+
initFilters(photos);
911
})
1012
.catch(() => {
1113
showDataError();

js/util.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const TIMEOUT_DELAY = 500;
12
const body = document.body;
23

34
const isEscapeKey = (evt) => evt.key === 'Escape';
@@ -13,4 +14,12 @@ const showModal = (modal, isShown = true) => {
1314
body.classList.remove('modal-open');
1415
};
1516

16-
export { isEscapeKey, showModal};
17+
const debounce = (callback, timeoutDelay = TIMEOUT_DELAY) => {
18+
let timeoutId;
19+
return (...rest) => {
20+
clearTimeout(timeoutId);
21+
timeoutId = setTimeout(() => callback.apply(this, rest), timeoutDelay);
22+
};
23+
};
24+
25+
export { isEscapeKey, showModal, debounce};

0 commit comments

Comments
 (0)