-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (109 loc) · 4.14 KB
/
script.js
File metadata and controls
133 lines (109 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const URL_IMAGE_SERVER = 'https://pixabay.com/api/?key=37954840-02a2d9419214ddd959c05fe70&orientation=vertical&image_type=photo&per_page=21'
const input = document.querySelector('.filter_input');
const main = document.querySelector('.photo_container');
const btnClearInput = document.querySelector('.btn_clear_input');
const btnPrevPage = document.querySelector('.btn_prev');
const btnNextPage = document.querySelector('.btn_next');
const pageNumber = document.querySelector('.number_page');
const modal = document.querySelector('.modal');
const modalImg = document.querySelector('.modal-img');
const btnCloseModal = document.querySelector('.modal-close');
let photos = [];
let currentPage = 1;
let timeoutId;
let numberOfPage;
// Загрузка данных из API
const getData = (url, page = 1, filterValue) => fetch(`${url}&page=${page}&q=${filterValue || ''}`)
.then(res => res.json())
.then(({totalHits, hits, total}) => {
photos = [...hits];
numberOfPage = total / 21;
if (totalHits === 0) {
main.innerHTML = '<h2>Ничего не найдено</h2>';
btnNextPage.disabled = true;
return;
} else if (btnNextPage.disabled){
btnNextPage.disabled = false;
}
if(currentPage >= numberOfPage) btnNextPage.disabled = true;
showPhoto(photos);
})
.catch(error => console.error('Ошибка:', error))
getData(URL_IMAGE_SERVER, currentPage);
// Функция очистки фильтра тегов
function clearInput() {
input.value = '';
btnClearInput.setAttribute('disabled', true);
currentPage = 1;
paginationControl(1);
}
// Функция создающая элемент для вывода фото
function createElementShowPhoto({user, largeImageURL}){
const photoItem = document.createElement('div');
photoItem.classList.add('photo_item');
photoItem.innerHTML = `
<h2>${user}</h2>
<img src="${largeImageURL}" alt="${user}"></img>
`
// Вариант 1 Открытие модального окна
// photoItem.addEventListener('click', () => {
// showModal(largeImageURL);
// })
main.appendChild(photoItem);
}
// Функция вывода полученных фотографий на страницу
function showPhoto (photos) {
main.innerHTML = ``;
photos.map(createElementShowPhoto);
}
// Функция фильтрации фотографий по тегу
function filterPhotos() {
btnClearInput.disabled = !input.value.trim();
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
currentPage = 1;
paginationControl(1);
}, 500);
}
// Функция откытия модального окна
function showModal(url){
modal.classList.remove('hidden');
modal.classList.add('active');
modalImg.src = url;
}
// Функция закрытия модальнго окна
function closeModal() {
modal.classList.remove('active');
modal.classList.add('hidden');
}
// Инпут для ввода тегов
input.addEventListener('input', filterPhotos);
// Вызов функции очистки фильтра
btnClearInput.addEventListener('click', clearInput);
// Разбиение на страницы
function paginationControl(page) {
getData(URL_IMAGE_SERVER, page, input.value.toUpperCase().trim());
pageNumber.textContent = page;
btnPrevPage.disabled = page < 2;
}
// Вызов предыдущей страницы
btnPrevPage.addEventListener('click', () => {
paginationControl(--currentPage);
});
// Вызов следующей страницы
btnNextPage.addEventListener('click', () => {
paginationControl(++currentPage);
});
// Вариант 2 Открытие модального окна
main.addEventListener('click', (e) => {
let url = e.target.src;
if(e.target.tagName === 'IMG') showModal(url);
})
// Закрытие модального окна
btnCloseModal.addEventListener('click', closeModal);
modal.addEventListener('click', (e) => {
if(e.target === modal) closeModal();
});
window.addEventListener('keydown', (e) => {
if(!modal.classList.contains('hidden') && e.key === 'Escape') closeModal();
});