Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
185 lines (154 loc) · 6.21 KB
/
Copy pathscript.js
File metadata and controls
185 lines (154 loc) · 6.21 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const API_KEY = "6d3f033d-09d2-415a-a165-910896b21c91";
const API_URL_POPULAR = "https://kinopoiskapiunofficial.tech/api/v2.2/films/collections?type=TOP_250_MOVIES";
const API_URL_SEARCH = "https://kinopoiskapiunofficial.tech/api/v2.1/films/search-by-keyword?keyword=";
const API_TRAILER = "https://kinopoiskapiunofficial.tech/api/v2.2/films/premieres?year=2027&month=JULY";
let currentPage = 1;
let totalPages = 1;
let currentApiUrl = "";
function getMovies(url, page = 1) {
currentApiUrl = url;
currentPage = page;
const urlWithPage = page > 1 ? `${url}&page=${page}` : url;
fetch(urlWithPage, {
method: 'GET',
headers: {
'X-API-KEY': API_KEY,
'Content-Type': 'application/json',
},
})
.then(res => res.json())
.then(json => {
console.log(json);
if (json.totalPages) {
totalPages = json.totalPages;
}
showMovies(json);
updatePagination();
})
.catch(err => console.log(err));
}
getMovies(API_URL_POPULAR);
// Add pagination to the page after initial load
document.addEventListener('DOMContentLoaded', () => {
const rowSection = document.querySelector(".row");
if (rowSection) {
const pagination = createPagination();
rowSection.appendChild(pagination);
setupPaginationEvents();
}
});
function showMovies(data) {
const movieblock = document.querySelector(".movie-grid");
if (!movieblock) return;
document.querySelector(".movie-grid").innerHTML = "";
const movies = data.items || data.films;
movies.forEach((card) => {
const movieEL = document.createElement("div");
movieEL.classList.add("card");
movieEL.innerHTML = `
<img src="${card.posterUrlPreview || card.posterUrl} ">
<p>${card.nameRu || card.nameEn || "-"}</p>
`;
movieblock.appendChild(movieEL);
});
}
const form = document.querySelector("form");
const search = document.querySelector(".header_search");
const hero = document.querySelector(".hero");
const films = document.querySelector(".row h2");
const marfun = document.querySelector(".movie-grid");
// Pagination functions
function createPagination() {
const paginationContainer = document.createElement("div");
paginationContainer.className = "pagination";
paginationContainer.innerHTML = `
<button class="pagination-prev" disabled>←</button>
<div class="pagination-pages"></div>
<button class="pagination-next">→</button>
`;
return paginationContainer;
}
function updatePagination() {
const prevButton = document.querySelector(".pagination-prev");
const nextButton = document.querySelector(".pagination-next");
const pagesContainer = document.querySelector(".pagination-pages");
if (prevButton && nextButton && pagesContainer) {
prevButton.disabled = currentPage <= 1;
nextButton.disabled = currentPage >= totalPages;
// Clear existing page buttons
pagesContainer.innerHTML = '';
// Determine range of pages to show
let startPage = Math.max(1, currentPage - 1);
let endPage = Math.min(totalPages, currentPage + 1);
// Always show first page
if (startPage > 1) {
const firstPageButton = document.createElement("button");
firstPageButton.textContent = "1";
firstPageButton.className = "pagination-page" + (currentPage === 1 ? " active" : "");
firstPageButton.addEventListener("click", () => getMovies(currentApiUrl, 1));
pagesContainer.appendChild(firstPageButton);
if (startPage > 2) {
const ellipsis = document.createElement("span");
ellipsis.textContent = "...";
ellipsis.className = "pagination-ellipsis";
pagesContainer.appendChild(ellipsis);
}
}
// Show pages in range
for (let i = startPage; i <= endPage; i++) {
const pageButton = document.createElement("button");
pageButton.textContent = i;
pageButton.className = "pagination-page" + (currentPage === i ? " active" : "");
pageButton.addEventListener("click", () => getMovies(currentApiUrl, i));
pagesContainer.appendChild(pageButton);
}
// Always show last page
if (endPage < totalPages) {
if (endPage < totalPages - 1) {
const ellipsis = document.createElement("span");
ellipsis.textContent = "...";
ellipsis.className = "pagination-ellipsis";
pagesContainer.appendChild(ellipsis);
}
const lastPageButton = document.createElement("button");
lastPageButton.textContent = totalPages;
lastPageButton.className = "pagination-page" + (currentPage === totalPages ? " active" : "");
lastPageButton.addEventListener("click", () => getMovies(currentApiUrl, totalPages));
pagesContainer.appendChild(lastPageButton);
}
}
}
function setupPaginationEvents() {
const prevButton = document.querySelector(".pagination-prev");
const nextButton = document.querySelector(".pagination-next");
if (prevButton && nextButton) {
prevButton.addEventListener("click", () => {
if (currentPage > 1) {
getMovies(currentApiUrl, currentPage - 1);
}
});
nextButton.addEventListener("click", () => {
if (currentPage < totalPages) {
getMovies(currentApiUrl, currentPage + 1);
}
});
}
}
form.addEventListener("submit", (e) => {
e.preventDefault();
const apiSearchUrl = `${API_URL_SEARCH}${search.value}`;
getMovies(apiSearchUrl);
if (hero) {
hero.innerHTML="";
hero.classList.remove("hero");
films.innerHTML="";
marfun.classList.add("mar")
}
search.value = "";
});
//trailer and classic code
const trailer = document.querySelector(".trailer");
function trlr() {
window.location.href = API_TRAILER;
}
trailer.addEventListener("click", trlr);