Spaces:
Running
Running
File size: 10,258 Bytes
0ac72be 2ea1081 0ac72be bbb6ff7 0ac72be bbb6ff7 2ea1081 bbb6ff7 2ea1081 0ac72be bbb6ff7 0ac72be bbb6ff7 0ac72be bbb6ff7 0ac72be bbb6ff7 0ac72be bbb6ff7 0ac72be bbb6ff7 2ea1081 bbb6ff7 0ac72be bbb6ff7 0ac72be bbb6ff7 2ea1081 bbb6ff7 2ea1081 bbb6ff7 0ac72be bbb6ff7 2ea1081 bbb6ff7 2ea1081 bbb6ff7 2ea1081 bbb6ff7 2ea1081 bbb6ff7 2ea1081 bbb6ff7 2ea1081 bbb6ff7 2ea1081 bbb6ff7 2ea1081 bbb6ff7 2ea1081 bbb6ff7 2ea1081 bbb6ff7 2ea1081 bbb6ff7 2ea1081 bbb6ff7 2ea1081 bbb6ff7 0ac72be bbb6ff7 0ac72be bbb6ff7 0ac72be bbb6ff7 d2873cc bbb6ff7 2ea1081 0ac72be 2ea1081 bbb6ff7 2ea1081 0ac72be 2ea1081 bbb6ff7 d2873cc bbb6ff7 0ac72be bbb6ff7 0ac72be bbb6ff7 0ac72be bbb6ff7 | 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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | // Global variables
let videos = [];
let currentVideoId = null;
let currentPage = 1;
const videosPerPage = 12;
let filteredVideos = [];
// DOM elements
const videoGrid = document.getElementById('videoGrid');
const suggestionsContainer = document.getElementById('suggestions');
const videoTitle = document.getElementById('videoTitle');
const videoDescription = document.getElementById('videoDescription');
const searchInput = document.getElementById('searchInput');
const searchBtn = document.getElementById('searchBtn');
const paginationContainer = document.getElementById('pagination');
// Initialize the app based on current page
document.addEventListener('DOMContentLoaded', () => {
loadVideos();
if (window.location.pathname.includes('player.html')) {
initPlayer();
} else {
// Setup search functionality for index page
searchInput.addEventListener('input', handleSearch);
searchBtn.addEventListener('click', handleSearch);
}
});
// Load videos from database.json
async function loadVideos() {
try {
const response = await fetch('database.json');
videos = await response.json();
filteredVideos = [...videos];
if (window.location.pathname.includes('player.html')) {
// Player page - load current video and suggestions
const urlParams = new URLSearchParams(window.location.search);
currentVideoId = urlParams.get('v');
if (currentVideoId) {
loadVideo(currentVideoId);
renderSuggestions();
} else {
// Redirect to home if no video specified
window.location.href = 'index.html';
}
} else {
// Home page - render video grid
renderVideoGrid();
renderPagination();
}
} catch (error) {
console.error('Error loading videos:', error);
alert('Failed to load video data. Please try again later.');
}
}
// Handle search functionality
function handleSearch() {
const searchTerm = searchInput.value.toLowerCase();
if (searchTerm.trim() === '') {
filteredVideos = [...videos];
} else {
filteredVideos = videos.filter(video =>
video.title.toLowerCase().includes(searchTerm) ||
video.description.toLowerCase().includes(searchTerm)
);
}
currentPage = 1;
renderVideoGrid();
renderPagination();
}
// Render video grid on home page with pagination
function renderVideoGrid() {
if (!videoGrid) return;
videoGrid.innerHTML = '';
const startIndex = (currentPage - 1) * videosPerPage;
const endIndex = startIndex + videosPerPage;
const videosToShow = filteredVideos.slice(startIndex, endIndex);
if (videosToShow.length === 0) {
videoGrid.innerHTML = '<p class="no-results">No videos found matching your search.</p>';
return;
}
videosToShow.forEach(video => {
const videoCard = document.createElement('div');
videoCard.className = 'video-card';
videoCard.addEventListener('click', () => {
window.location.href = `player.html?v=${video.id}`;
});
videoCard.innerHTML = `
<div class="thumbnail">
<img src="${video.thumbnailPath}" alt="${video.title}" loading="lazy">
${video.duration ? `<span class="duration">${video.duration}</span>` : ''}
</div>
<div class="video-info">
<h3>${video.title}</h3>
<p>${video.description.substring(0, 100)}...</p>
</div>
`;
videoGrid.appendChild(videoCard);
});
}
// Render pagination controls
function renderPagination() {
if (!paginationContainer) return;
paginationContainer.innerHTML = '';
const totalPages = Math.ceil(filteredVideos.length / videosPerPage);
if (totalPages <= 1) return;
// Previous button
const prevBtn = document.createElement('button');
prevBtn.className = 'page-btn';
prevBtn.textContent = 'Previous';
prevBtn.disabled = currentPage === 1;
prevBtn.addEventListener('click', () => {
if (currentPage > 1) {
currentPage--;
renderVideoGrid();
renderPagination();
window.scrollTo({ top: 0, behavior: 'smooth' });
}
});
paginationContainer.appendChild(prevBtn);
// Page buttons
const maxVisiblePages = 5;
let startPage = Math.max(1, currentPage - Math.floor(maxVisiblePages / 2));
let endPage = Math.min(totalPages, startPage + maxVisiblePages - 1);
if (endPage - startPage + 1 < maxVisiblePages) {
startPage = Math.max(1, endPage - maxVisiblePages + 1);
}
if (startPage > 1) {
const firstBtn = document.createElement('button');
firstBtn.className = 'page-btn';
firstBtn.textContent = '1';
firstBtn.addEventListener('click', () => {
currentPage = 1;
renderVideoGrid();
renderPagination();
window.scrollTo({ top: 0, behavior: 'smooth' });
});
paginationContainer.appendChild(firstBtn);
if (startPage > 2) {
const ellipsis = document.createElement('span');
ellipsis.textContent = '...';
paginationContainer.appendChild(ellipsis);
}
}
for (let i = startPage; i <= endPage; i++) {
const pageBtn = document.createElement('button');
pageBtn.className = `page-btn ${i === currentPage ? 'active' : ''}`;
pageBtn.textContent = i;
pageBtn.addEventListener('click', () => {
currentPage = i;
renderVideoGrid();
renderPagination();
window.scrollTo({ top: 0, behavior: 'smooth' });
});
paginationContainer.appendChild(pageBtn);
}
if (endPage < totalPages) {
if (endPage < totalPages - 1) {
const ellipsis = document.createElement('span');
ellipsis.textContent = '...';
paginationContainer.appendChild(ellipsis);
}
const lastBtn = document.createElement('button');
lastBtn.className = 'page-btn';
lastBtn.textContent = totalPages;
lastBtn.addEventListener('click', () => {
currentPage = totalPages;
renderVideoGrid();
renderPagination();
window.scrollTo({ top: 0, behavior: 'smooth' });
});
paginationContainer.appendChild(lastBtn);
}
// Next button
const nextBtn = document.createElement('button');
nextBtn.className = 'page-btn';
nextBtn.textContent = 'Next';
nextBtn.disabled = currentPage === totalPages;
nextBtn.addEventListener('click', () => {
if (currentPage < totalPages) {
currentPage++;
renderVideoGrid();
renderPagination();
window.scrollTo({ top: 0, behavior: 'smooth' });
}
});
paginationContainer.appendChild(nextBtn);
}
// Load a specific video on player page
function loadVideo(videoId) {
const video = videos.find(v => v.id === videoId);
if (!video) {
alert('Video not found');
window.location.href = 'index.html';
return;
}
currentVideoId = videoId;
videoTitle.textContent = video.title;
videoDescription.textContent = video.description;
// Update page title
document.title = `${video.title} - Video Browser`;
// Initialize Video.js player
const player = videojs('mainVideo', {
controls: true,
autoplay: false,
preload: 'auto',
responsive: true,
fluid: true,
sources: [{
src: video.videoPath,
type: 'video/mp4' // Adjust based on your video format
}]
});
// Add subtitle tracks if available
if (video.subtitleTracks) {
video.subtitleTracks.forEach(track => {
player.addRemoteTextTrack({
kind: 'subtitles',
src: track.path,
srclang: track.srclang,
label: track.label,
default: track.srclang === 'en' // Default to English if available
}, false);
});
}
// Handle player errors
player.on('error', () => {
const errorDisplay = player.errorDisplay;
errorDisplay.contentEl().innerHTML = `
<div class="vjs-error-display">
<div class="vjs-modal-dialog-content">
<h1>Error loading video</h1>
<p>The video format may not be supported by your browser.</p>
<p>Try using a different browser or check the video file.</p>
</div>
</div>
`;
errorDisplay.show();
});
}
// Render suggested videos
function renderSuggestions() {
if (!suggestionsContainer) return;
// Filter out current video
const suggestedVideos = videos.filter(v => v.id !== currentVideoId);
suggestionsContainer.innerHTML = '<h2>Suggested Videos</h2>';
suggestedVideos.forEach(video => {
const suggestionCard = document.createElement('div');
suggestionCard.className = 'suggestion-card';
suggestionCard.addEventListener('click', () => {
// Update URL without reloading the page
window.history.pushState({}, '', `player.html?v=${video.id}`);
loadVideo(video.id);
renderSuggestions();
});
suggestionCard.innerHTML = `
<div class="suggestion-thumbnail">
<img src="${video.thumbnailPath}" alt="${video.title}" loading="lazy">
</div>
<div class="suggestion-info">
<h3>${video.title}</h3>
${video.duration ? `<p>${video.duration}</p>` : ''}
</div>
`;
suggestionsContainer.appendChild(suggestionCard);
});
}
// Initialize video player (Video.js handles most of this now)
function initPlayer() {
// Video.js is initialized in loadVideo function
} |