-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Решить проблему с удалением
- Loading branch information
Showing
3 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { debounce } from '../utils/debounce.js'; | ||
|
||
const PICTURES_COUNT = 10; | ||
const Filter = { | ||
DEFAULT: 'filter-default', | ||
RANDOM: 'filter-random', | ||
DISCUSSED: 'filter-discussed', | ||
}; | ||
|
||
const filtersElement = document.querySelector('.img-filters'); | ||
|
||
let currentFilter = ''; | ||
let pictures = []; | ||
|
||
const randomSort = () => Math.random() - 0.5; | ||
|
||
const discussedSort = (pictureA, pictureB) => | ||
pictureB.comments.length - pictureA.comments.length; | ||
|
||
const filterPictures = () => { | ||
switch (currentFilter) { | ||
case Filter.RANDOM: | ||
return [...pictures].sort(randomSort).slice(0, PICTURES_COUNT); | ||
case Filter.DISCUSSED: | ||
return [...pictures].sort(discussedSort); | ||
default: | ||
return [...pictures]; | ||
} | ||
}; | ||
|
||
const turnFilterOn = (loadedPictures) => { | ||
filtersElement.classList.remove('img-filters--inactive'); | ||
pictures = [...loadedPictures]; | ||
currentFilter = Filter.DEFAULT = ''; | ||
}; | ||
|
||
const setOnFilterClick = (cb) => { | ||
const debouncedCallback = debounce(cb); | ||
|
||
filtersElement.addEventListener('click', (evt) => { | ||
if (!evt.target.classList.contains('img-filters__button')) { | ||
return; | ||
} | ||
|
||
const clickedButton = evt.target; | ||
if (clickedButton.id === currentFilter) { | ||
return; | ||
} | ||
|
||
filtersElement | ||
.querySelector('.img-filters__button--active') | ||
.classList.remove('img-filters__button--active'); | ||
clickedButton.classList.add('img-filters__button--active'); | ||
currentFilter = clickedButton.id; | ||
debouncedCallback(filterPictures()); | ||
}); | ||
}; | ||
|
||
export { setOnFilterClick, turnFilterOn, filterPictures }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const debounce = (callback, timeoutDelay = 500) => { | ||
let timeoutId; | ||
return (...rest) => { | ||
clearTimeout(timeoutId); | ||
timeoutId = setTimeout(() => callback.apply(this, rest), timeoutDelay); | ||
}; | ||
}; | ||
|
||
export { debounce }; |