-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Перламутровые пуговицы #13
Merged
keksobot
merged 5 commits into
htmlacademy-javascript:master
from
NikitaKir98:module12-task1
Dec 20, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ca85635
Добавил функцию устраняющую дребезг и функцию перемешивания массива
NikitaKir98 159a6f4
убрал магическое число
NikitaKir98 439cd52
убрал мусор
NikitaKir98 f9293eb
Добавил функцию очистки миниатюр, и открытия фильтров
NikitaKir98 8326c85
создал модуль для отработки фильтров миниатюр
NikitaKir98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. и тут |
||
|
||
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}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
где .js из-за этого не собирается