Skip to content
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
merged 5 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions js/form-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const formImgUploadOpen = () => {
document.addEventListener('keydown', onDocumentKeydown);
};


//Закрытие формы
const formImgUploadClose = () => {
imgOverlay.classList.add('hidden');
Expand All @@ -60,17 +61,14 @@ const formImgUploadClose = () => {
imgUploadCancel.removeEventListener('click', onCancelClick);
document.removeEventListener('keydown', onDocumentKeydown);

imgUploadInput.value = '';
inputHashtags.value = '';
inputDescription.value = '';
previewImg.src = '';
pristine.reset();
imgUploadForm.reset();
imgUploadSubmit.disabled = false;
resetEffect();
resetZoom();
};


//обработчик клика на zoom
scaleControlSmaller.addEventListener('click', () => changeZoom(-1));
scaleControlBigger.addEventListener('click', () => changeZoom());
Expand Down
16 changes: 15 additions & 1 deletion js/insert-thumbnails.js
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};
51 changes: 51 additions & 0 deletions js/photo-filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { insertThumbnails } from './insert-thumbnails';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

где .js из-за этого не собирается

import { debounce, shuffleArray } from './utils';
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 };
4 changes: 3 additions & 1 deletion js/popup-error.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const DELAY_TIME = 5000;

const popupErroreTemplate = document.querySelector('#data-error').content;

const showPopup = (message) => {
Expand All @@ -8,7 +10,7 @@ const showPopup = (message) => {

document.body.append(popupError);

setTimeout(removePopup, 5000);
setTimeout(removePopup, DELAY_TIME);
};

function removePopup(){
Expand Down
18 changes: 17 additions & 1 deletion js/utils.js
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};
Loading