-
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.
Merge pull request #13 from NikitaKir98/module12-task1
- Loading branch information
Showing
5 changed files
with
88 additions
and
7 deletions.
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
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 |
---|---|---|
@@ -1,17 +1,31 @@ | ||
import {createThumbnail} from './rendering-pictures.js'; | ||
import {getData} from './api.js'; | ||
import {showPopup} from './popup-error.js'; | ||
import {openFilter} from './photo-filter.js'; | ||
|
||
const picturesContainer = document.querySelector('.pictures'); | ||
const fragment = document.createDocumentFragment(); | ||
|
||
const clearThumbnails = () => { | ||
const allPictures = picturesContainer.querySelectorAll('.picture'); | ||
allPictures.forEach((picture) => picture.remove()); | ||
}; | ||
|
||
|
||
const insertThumbnails = (pictures) => { | ||
clearThumbnails(); | ||
pictures.forEach((photo) => { | ||
fragment.append(createThumbnail(photo)); | ||
picturesContainer.append(fragment); | ||
}); | ||
}; | ||
|
||
getData() | ||
.then((pictures)=> insertThumbnails(pictures)) | ||
.then((pictures)=> { | ||
insertThumbnails(pictures); | ||
openFilter(pictures); | ||
}) | ||
.catch((err) => showPopup(err.message)); | ||
|
||
|
||
export {insertThumbnails}; |
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,51 @@ | ||
import { insertThumbnails } from './insert-thumbnails'; | ||
import { debounce, shuffleArray } from './utils'; | ||
|
||
const RANDOM_PICTURES_COUNT = 10; | ||
let picturesFromServer = []; | ||
|
||
const imgFilters = document.querySelector('.img-filters'); | ||
const imgFilterButtons = imgFilters.querySelectorAll('.img-filters__button'); | ||
|
||
const Filters = { | ||
default: (pictures) => pictures, | ||
random: (pictures) => shuffleArray(pictures).slice(0, RANDOM_PICTURES_COUNT), | ||
discussed: (pictures) => pictures.sort((A, B) => B.comments.length - A.comments.length) | ||
}; | ||
|
||
const openFilter = (pictures) => { | ||
picturesFromServer = pictures; | ||
if (pictures) { | ||
imgFilters.classList.remove('img-filters--inactive'); | ||
} | ||
}; | ||
|
||
const setCurrentFilterButton = (currentFilterButton) => { | ||
imgFilterButtons.forEach((button) => button.classList.remove('img-filters__button--active')); | ||
currentFilterButton.classList.add('img-filters__button--active'); | ||
}; | ||
|
||
const filtersClick = (cb) => { | ||
imgFilters.addEventListener('click', (evt) => { | ||
if (evt.target.type === 'button') { | ||
const currentFilter = evt.target.id.replace('filter-', ''); | ||
|
||
setCurrentFilterButton(evt.target); | ||
|
||
cb(currentFilter); | ||
} | ||
}); | ||
}; | ||
|
||
const filterPictures = (filter, pictures) => { | ||
let filteredPictures = pictures.slice(); | ||
filteredPictures = Filters[filter](filteredPictures); | ||
|
||
insertThumbnails(filteredPictures); | ||
}; | ||
|
||
filtersClick( | ||
debounce((currentFilter) => filterPictures(currentFilter, picturesFromServer)) | ||
); | ||
|
||
export { openFilter }; |
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 |
---|---|---|
@@ -1,4 +1,20 @@ | ||
//Проверка на escape | ||
const isEscapeKey = (evt) => evt.key === 'Escape'; | ||
|
||
export {isEscapeKey}; | ||
//Устранение дребезга | ||
const debounce = (callback, timeoutDelay = 500) => { | ||
let timeoutId; | ||
|
||
return (...rest) => { | ||
clearTimeout(timeoutId); | ||
timeoutId = setTimeout(() => callback.apply(this, rest), timeoutDelay); | ||
}; | ||
}; | ||
|
||
//Перемешивает массив | ||
const shuffleArray = (array) => { | ||
array.sort(() => 0.5 - Math.random()); | ||
return array; | ||
}; | ||
|
||
export {isEscapeKey, debounce, shuffleArray}; |