|
1 | | -import {debounce} from './util.js'; |
| 1 | +import { debounce } from './util.js'; |
2 | 2 |
|
3 | | -import { renderPhoto } from './render-photo.js'; |
4 | | - |
5 | | -const FILTER = { |
6 | | - default:'filter-default', |
7 | | - random: 'filter-random', |
8 | | - discussed: 'filter-discussed' |
9 | | -}; |
10 | | - |
11 | | -let currentFilter = FILTER.default; |
| 3 | +const ACTIVE_BUTTON_CLASS = 'img-filters__button--active'; |
| 4 | +const DEFAULT_FILTER = 'filter-default'; |
| 5 | +const RANDOM_FILTER = 'filter-random'; |
| 6 | +const DISCUSSED_FILTER = 'filter-discussed'; |
| 7 | +const RANDOM_FILTER_LIMIT = 10; |
12 | 8 |
|
13 | 9 | const filterPanel = document.querySelector('.img-filters'); |
14 | | -const ACTIVE_BUTTON_CLASS = 'img-filters__button--active'; |
15 | 10 |
|
16 | | -const debounceRender = debounce(renderPhoto); |
| 11 | +const filterDefault = (photos) => photos; |
| 12 | +const filterDiscused = (photos) => |
| 13 | + photos.toSorted((a, b) => b.comments.length - a.comments.length); |
| 14 | +const filterRandom = (photos) => |
| 15 | + [...photos].sort(() => Math.random() - 0.5).slice(0, RANDOM_FILTER_LIMIT); |
17 | 16 |
|
18 | | -const applyFilter = (photos) => { |
19 | | - let filteredPictures = []; |
20 | | - if(currentFilter === FILTER.default) { |
21 | | - filteredPictures = photos; |
22 | | - } |
23 | | - if(currentFilter === FILTER.random) { |
24 | | - filteredPictures = photos.toSorted(() => 0.5 - Math.random()).slice(0, 10); |
25 | | - } |
26 | | - if(currentFilter === FILTER.discussed) { |
27 | | - filteredPictures = photos.toSorted((a,b) => b.comments.length - a.comments.length); |
| 17 | +const Filters = { |
| 18 | + [DEFAULT_FILTER]: filterDefault, |
| 19 | + [RANDOM_FILTER]: filterRandom, |
| 20 | + [DISCUSSED_FILTER]: filterDiscused, |
| 21 | +}; |
| 22 | + |
| 23 | +const filter = (photos, type) => { |
| 24 | + if (!photos.length) { |
| 25 | + return []; |
28 | 26 | } |
29 | | - debounceRender(filteredPictures); |
| 27 | + const filterByType = Filters[type] || Filters.DEFAULT_FILTER; |
| 28 | + return filterByType(photos); |
30 | 29 | }; |
31 | 30 |
|
32 | | -const getHandleFilter = (photos) => (evt) => { |
| 31 | +const state = { |
| 32 | + type: DEFAULT_FILTER, |
| 33 | + photos: [], |
| 34 | + debounceRender: null, |
| 35 | + render: null |
| 36 | +}; |
| 37 | + |
| 38 | +const handleFilter = (evt) => { |
33 | 39 | const targetButton = evt.target; |
34 | 40 | const activeButton = document.querySelector(`.${ACTIVE_BUTTON_CLASS}`); |
| 41 | + |
35 | 42 | if (!targetButton.matches('button')) { |
36 | 43 | return; |
37 | 44 | } |
| 45 | + |
38 | 46 | if (activeButton === targetButton) { |
39 | 47 | return; |
40 | 48 | } |
| 49 | + |
41 | 50 | activeButton.classList.toggle(ACTIVE_BUTTON_CLASS); |
42 | 51 | targetButton.classList.toggle(ACTIVE_BUTTON_CLASS); |
43 | | - currentFilter = targetButton.getAttribute('id'); |
44 | | - applyFilter(photos); |
| 52 | + state.type = targetButton.getAttribute('id'); |
| 53 | + |
| 54 | + if (!state.photos.length) { |
| 55 | + return; |
| 56 | + } |
| 57 | + const filteredPictures = filter(state.photos, state.type); |
| 58 | + state.debounceRender(filteredPictures); |
| 59 | +}; |
| 60 | + |
| 61 | +export const initFilter = (render) => { |
| 62 | + state.debounceRender = debounce(render); |
| 63 | + state.render = render; |
| 64 | + filterPanel.addEventListener('click', handleFilter); |
45 | 65 | }; |
46 | 66 |
|
47 | | -export const initFilter = (photos) => { |
| 67 | +export const setPhotos = (data) => { |
| 68 | + state.photos = [...data]; |
| 69 | + state.type = DEFAULT_FILTER; |
| 70 | + state.render(filter(state.photos, state.type)); |
48 | 71 | filterPanel.classList.remove('img-filters--inactive'); |
49 | | - filterPanel.addEventListener('click', getHandleFilter(photos)); |
50 | 72 | }; |
0 commit comments