Skip to content

Commit 74c5f05

Browse files
committed
add photos filtering
1 parent 67d8034 commit 74c5f05

3 files changed

Lines changed: 62 additions & 55 deletions

File tree

js/helpers.js

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,11 @@
1-
const checkStringLength = (string = 'str', maxLength = 1) => string.length <= maxLength;
1+
const debounce = (callback, timeoutDelay = 500) => {
2+
let timeoutId;
23

3-
checkStringLength('string', 3);
4-
5-
const isPalindrome = (string = '') => {
6-
const newString = string.toLowerCase().replaceAll(' ', '');
7-
let result = '';
8-
9-
for(let i = newString.length - 1; i >= 0; i -= 1) {
10-
const char = newString[i];
11-
result += char;
12-
}
13-
14-
return newString === result;
15-
};
16-
17-
isPalindrome('А роза упала на лапу азора');
18-
19-
const parseStringToInt = (string) => {
20-
string = string.toString();
21-
let res = '';
22-
for (let i = 0; i < string.length; i += 1) {
23-
const parsed = parseInt(string[i], 10);
24-
const isDigit = (!isNaN(parsed));
25-
if (isDigit) {
26-
res += parsed;
27-
}
28-
}
29-
30-
return res ? +res : NaN;
31-
};
32-
33-
parseStringToInt('78 dogs');
34-
35-
export const getRandomInteger = (num1, num2) => {
36-
const lower = Math.ceil(Math.min(num1, num2));
37-
const upper = Math.floor(Math.max(num1, num2));
38-
const result = Math.random() * (upper - lower + 1) + lower;
39-
return Math.floor(result);
40-
};
41-
42-
export const getRandomElement = (arr) => arr[getRandomInteger(0, arr.length - 1)];
43-
44-
const stringToMinutes = (string) => {
45-
const [hours, minutes] = string.split(':').map((el) => parseInt(el, 10));
46-
return hours * 60 + minutes;
47-
};
48-
49-
export const checkWorkingTime = (workDayStart, workDayEnd, meetingStartTime, meetingDuration) => {
50-
const workStart = stringToMinutes(workDayStart);
51-
const workEnd = stringToMinutes(workDayEnd);
52-
const meetingStart = stringToMinutes(meetingStartTime);
53-
const meetingEnd = meetingStart + meetingDuration;
54-
55-
return meetingStart >= workStart && meetingEnd <= workEnd;
4+
return (...rest) => {
5+
clearTimeout(timeoutId);
6+
timeoutId = setTimeout(() => callback.apply(this, rest), timeoutDelay);
7+
};
568
};
9+
const isEscapeKey = (evt) => evt.key === 'Escape';
5710

58-
export const isEscapeKey = (evt) => evt.key === 'Escape';
11+
export { debounce, isEscapeKey };

js/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import { initUploadForm, resetUploadForm } from './upload-form.js';
44
import { getData } from './api';
55
import { showDataError, showSuccessMessage, showErrorMessage } from './notifications.js';
66
import { setUploadFormSubmit } from './upload-form.js';
7+
import { showFilters, initPhotosFiltering } from './photos-filters.js';
78

89
initUploadForm();
910
getData()
1011
.then((photos) => {
1112
renderThumbnails(photos);
13+
showFilters();
14+
initPhotosFiltering(photos);
1215
showFullPhoto(photos);
1316
})
1417
.catch((e)=> {

js/photos-filters.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { renderThumbnails } from './thumbnails.js';
2+
import { debounce } from './helpers.js';
3+
4+
const RERENDER_TIME = 500;
5+
6+
const filtersBlock = document.querySelector('.img-filters');
7+
const filterDefaultBtn = document.querySelector('#filter-default');
8+
const filterRandomBtn = document.querySelector('#filter-random');
9+
const filterDiscussedBtn = document.querySelector('#filter-discussed');
10+
11+
const showFilters = () => {
12+
filtersBlock.classList.remove('img-filters--inactive');
13+
};
14+
15+
const clearThumbnails = () => {
16+
const thumbnails = document.querySelectorAll('.picture');
17+
thumbnails.forEach((thumbnail) => {
18+
thumbnail.remove();
19+
});
20+
};
21+
22+
const toggleActiveBtn = (btnClicked, activeclass) => {
23+
const activeBtn = document.querySelector(`.${activeclass}`);
24+
activeBtn.classList.remove(activeclass);
25+
btnClicked.classList.add(activeclass);
26+
};
27+
28+
const rerenderThumbnails = (btn, photos, cb = null) => {
29+
toggleActiveBtn(btn, 'img-filters__button--active');
30+
const photosToRender = cb ? cb(photos) : photos;
31+
clearThumbnails();
32+
renderThumbnails(photosToRender);
33+
};
34+
35+
const debouncedRender = debounce(rerenderThumbnails, RERENDER_TIME);
36+
37+
const initPhotosFiltering = (photos) => {
38+
filterDefaultBtn.addEventListener('click', () => {
39+
debouncedRender(filterDefaultBtn, photos);
40+
});
41+
42+
filterRandomBtn.addEventListener('click', () => {
43+
debouncedRender(filterRandomBtn, photos, () => photos.slice().sort(() => 0.5 - Math.random()).slice(0, 10));
44+
});
45+
46+
filterDiscussedBtn.addEventListener('click', () => {
47+
debouncedRender(filterDiscussedBtn, photos, () => photos.slice().sort((a, b) => b.comments.length - a.comments.length));
48+
});
49+
};
50+
51+
export { showFilters, initPhotosFiltering };

0 commit comments

Comments
 (0)