peertubeclient / index.html
sldshr's picture
Update index.html
a25a91f verified
Raw History Blame Contribute Delete
35 kB
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8">
<title>STube</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
<style>
body { background:#f5f5f5; padding-top:60px; }
.hero-unit { background:#fff; border:1px solid #e5e5e5; box-shadow:0 1px 2px rgba(0,0,0,.05); padding:40px; }
.video-thumbnail { background:#fff; padding:8px; border:1px solid #ddd; border-radius:4px; box-shadow:0 1px 3px rgba(0,0,0,0.1); margin-bottom:20px; cursor:pointer; transition:border-color 0.2s; }
.video-thumbnail:hover { border-color:#08c; }
.video-thumbnail .mock-poster { width:100%; height:120px; background:#222; display:flex; align-items:center; justify-content:center; color:#fff; margin-bottom:10px; }
.video-title { font-size:14px; font-weight:bold; line-height:1.2; height:34px; overflow:hidden; color:#333; }
.video-desc-thumb { font-size:11px; color:#777; margin-top:3px; overflow:hidden; white-space:nowrap; text-overflow:ellipsis; }
.video-author { font-size:11px; color:#999; margin-top:5px; }
.video-author a,.clickable-author { color:#08c; cursor:pointer; text-decoration:none; }
.video-author a:hover,.clickable-author:hover { text-decoration:underline; }
.custom-player-wrapper { position:relative; width:100%; max-width:800px; margin:0 auto; background:#000; border:1px solid #333; border-radius:4px; overflow:hidden; box-shadow:0 4px 10px rgba(0,0,0,0.5); }
.custom-player-wrapper video { width:100%; display:block; }
.player-controls { position:absolute; bottom:0; left:0; right:0; background:linear-gradient(to bottom,#333,#111); border-top:1px solid #444; display:flex; align-items:center; padding:5px 10px; height:35px; opacity:0.9; transition:opacity 0.3s; z-index:5; }
.custom-player-wrapper:hover .player-controls { opacity:1; }
.btn-player { background:transparent; border:none; color:#ccc; cursor:pointer; outline:none; padding:0 8px; display:flex; align-items:center; justify-content:center; }
.btn-player:hover { color:#fff; }
.btn-player svg { pointer-events:none; }
.progress-container { flex-grow:1; height:8px; background:#444; margin:0 10px; border-radius:4px; position:relative; cursor:pointer; box-shadow:inset 0 1px 2px rgba(0,0,0,0.5); }
.progress-bar-filled { height:100%; background:#d14; border-radius:4px; width:0%; pointer-events:none; }
.progress-container:hover .progress-bar-filled { background:#e04; }
.time-display { color:#aaa; font-size:12px; font-family:monospace; white-space:nowrap; }
.video-loading-overlay { position:absolute; top:0; left:0; right:0; bottom:0; background:#000; display:flex; flex-direction:column; align-items:center; justify-content:center; z-index:10; }
.video-loading-overlay.hidden { display:none; }
.spinner { width:50px; height:50px; border:4px solid #333; border-top:4px solid #d14; border-radius:50%; animation:spin 1s linear infinite; margin-bottom:15px; }
@keyframes spin { 0%{transform:rotate(0deg)} 100%{transform:rotate(360deg)} }
.video-loading-overlay .loading-text { color:#aaa; font-size:13px; }
.custom-player-wrapper:fullscreen,.custom-player-wrapper:-webkit-full-screen { width:100vw; height:100vh; max-width:none; border:none; border-radius:0; box-shadow:none; display:flex; flex-direction:column; background:#000; }
.custom-player-wrapper:fullscreen video,.custom-player-wrapper:-webkit-full-screen video { flex:1 1 0; width:100%; min-height:0; object-fit:contain; }
.custom-player-wrapper:fullscreen .player-controls,.custom-player-wrapper:-webkit-full-screen .player-controls { position:static; flex-shrink:0; height:42px; padding:8px 15px; z-index:20; }
.vote-bar { display:flex; align-items:center; gap:8px; margin:10px 0; }
.vote-bar .vote-btn { background:#f5f5f5; border:1px solid #ddd; border-radius:4px; padding:5px 12px; cursor:pointer; display:flex; align-items:center; gap:5px; color:#555; font-size:13px; transition:all 0.2s; }
.vote-bar .vote-btn:hover { border-color:#08c; color:#08c; }
.vote-bar .vote-btn.active-up { background:#dff0d8; border-color:#468847; color:#468847; }
.vote-bar .vote-btn.active-down { background:#f2dede; border-color:#b94a48; color:#b94a48; }
.vote-count { font-weight:bold; font-size:14px; color:#333; min-width:20px; text-align:center; }
.video-description { color:#555; font-size:13px; line-height:1.5; padding:10px; background:#fafafa; border:1px solid #eee; border-radius:4px; margin-top:8px; white-space:pre-wrap; }
.author-card { text-align:center; margin-bottom:20px; }
.author-card .author-avatar { width:80px; height:80px; background:#555; border-radius:50%; display:flex; align-items:center; justify-content:center; color:#fff; margin:0 auto 10px; }
.author-card .author-name { font-size:20px; font-weight:bold; color:#333; }
.author-card .author-bio { color:#666; font-size:13px; margin-top:5px; }
.profile-avatar { width:80px; height:80px; background:#333; border-radius:50%; display:flex; align-items:center; justify-content:center; color:#fff; margin:0 auto 15px; }
.profile-field-group label { font-weight:bold; color:#555; }
textarea,input[type="text"],input[type="password"] { resize:none !important; }
.page-section { display:none; }
.page-section.active { display:block; }
</style>
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#" onclick="showSection('home')" style="display:flex;align-items:center;gap:6px;">
<svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor"><path d="M18 4l2 4h-3l-2-4h-2l2 4h-3l-2-4H8l2 4H7L5 4H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V4h-4z"/></svg>
STube
</a>
<ul class="nav">
<li><a href="#" onclick="showSection('home')">Home</a></li>
<li><a href="#" onclick="showSection('upload')">Upload video</a></li>
</ul>
<ul class="nav pull-right">
<li id="nav-user-info" style="display:none;padding:10px 15px;color:#777;">Hello, <b id="current-username"></b></li>
<li><a href="#" id="nav-profile-btn" style="display:none;" onclick="showSection('profile')">Profile</a></li>
<li><a href="#" id="nav-login-btn" onclick="showSection('auth')">Login / Register</a></li>
<li><a href="#" id="nav-logout-btn" style="display:none;" onclick="logout()">Logout</a></li>
</ul>
</div>
</div>
</div>
<div class="container">
<div id="home" class="page-section active">
<div class="hero-unit">
<h1>Welcome to STube!</h1>
<p>Video hosting on HTML and Python</p>
</div>
<div class="row-fluid">
<div class="span12">
<h3>Recent videos</h3><hr>
<div class="row-fluid" id="video-grid"><p id="loading-text">Loading videos...</p></div>
</div>
</div>
</div>
<div id="video-view" class="page-section">
<div class="row-fluid">
<div class="span8">
<div class="custom-player-wrapper" id="player-wrapper">
<div class="video-loading-overlay" id="video-loading-overlay">
<div class="spinner"></div>
<div class="loading-text">Loading video...</div>
</div>
<video id="main-video" src="" preload="metadata"></video>
<div class="player-controls" id="player-controls">
<button class="btn-player" id="play-pause-btn"></button>
<div class="time-display" id="time-display">0:00 / 0:00</div>
<div class="progress-container" id="progress-container">
<div class="progress-bar-filled" id="progress-bar"></div>
</div>
<button class="btn-player" id="mute-btn"></button>
<button class="btn-player" id="fullscreen-btn"></button>
</div>
</div>
<h3 id="view-title" style="margin-top:15px;">Video title</h3>
<div id="view-description" class="video-description" style="display:none;"></div>
<p class="muted" style="margin-top:6px;">By <span id="view-author" class="clickable-author"></span></p>
<div class="vote-bar">
<button class="vote-btn" id="vote-up-btn" onclick="voteVideo(1)">
<svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor"><path d="M1 21h4V9H1v12zm22-11c0-1.1-.9-2-2-2h-6.31l.95-4.57.03-.32c0-.41-.17-.79-.44-1.06L14.17 1 7.59 7.59C7.22 7.95 7 8.45 7 9v10c0 1.1.9 2 2 2h9c.83 0 1.54-.5 1.84-1.22l3.02-7.05c.09-.23.14-.47.14-.73v-2z"/></svg>
Like
</button>
<span class="vote-count" id="vote-score">0</span>
<button class="vote-btn" id="vote-down-btn" onclick="voteVideo(-1)">
<svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor"><path d="M15 3H6c-.83 0-1.54.5-1.84 1.22l-3.02 7.05c-.09.23-.14.47-.14.73v2c0 1.1.9 2 2 2h6.31l-.95 4.57-.03.32c0 .41.17.79.44 1.06L9.83 23l6.59-6.59c.36-.36.58-.86.58-1.41V5c0-1.1-.9-2-2-2zm4 0v12h4V3h-4z"/></svg>
Dislike
</button>
</div>
<hr>
</div>
<div class="span4">
<div class="well">
<h4>Comments</h4>
<div id="comments-list" style="max-height:300px;overflow-y:auto;margin-bottom:15px;"></div>
<form id="comment-form" onsubmit="postComment(event)">
<textarea id="comment-text" class="span12" rows="3" placeholder="Leave a comment..." required></textarea>
<button type="submit" class="btn btn-primary btn-small">Send</button>
</form>
</div>
</div>
</div>
</div>
<div id="upload" class="page-section">
<div class="row-fluid">
<div class="span6 offset3">
<div class="well">
<legend>Upload new video</legend>
<div id="upload-alert" class="alert" style="display:none;"></div>
<form id="upload-form" onsubmit="handleUpload(event)">
<label>Video title</label>
<input type="text" id="upload-title" class="span12" required placeholder="Cool video">
<label>Description (optional)</label>
<textarea id="upload-description" class="span12" rows="3" placeholder="Video description..."></textarea>
<label>Video file (.mp4, .webm)</label>
<input type="file" id="upload-file" class="span12" accept="video/mp4,video/webm" required>
<hr>
<button type="submit" class="btn btn-success btn-large" id="upload-submit-btn">
<svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" style="vertical-align:middle;margin-right:4px;"><path d="M9 16h6v-6h4l-7-7-7 7h4v6zm-4 2h14v2H5v-2z"/></svg>
Upload
</button>
</form>
</div>
</div>
</div>
</div>
<div id="auth" class="page-section">
<div class="row-fluid">
<div class="span5 offset1">
<div class="well">
<legend>Login</legend>
<form onsubmit="handleAuth(event, 'login')">
<label>Username</label>
<input type="text" id="login-user" class="span12" required>
<label>Password</label>
<input type="password" id="login-pass" class="span12" required>
<button type="submit" class="btn btn-primary">Login</button>
</form>
</div>
</div>
<div class="span5">
<div class="well">
<legend>Register</legend>
<form onsubmit="handleAuth(event, 'register')">
<label>Username</label>
<input type="text" id="reg-user" class="span12" required>
<label>Password</label>
<input type="password" id="reg-pass" class="span12" required>
<button type="submit" class="btn btn-success">Create account</button>
</form>
</div>
</div>
</div>
</div>
<div id="profile" class="page-section">
<div class="row-fluid">
<div class="span6 offset3">
<div class="well">
<div class="profile-avatar">
<svg viewBox="0 0 24 24" width="40" height="40" fill="currentColor"><path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/></svg>
</div>
<legend style="text-align:center;">My Profile</legend>
<div id="profile-alert" class="alert" style="display:none;"></div>
<form id="profile-form" onsubmit="handleProfileUpdate(event)">
<div class="profile-field-group">
<label>Username</label>
<input type="text" id="profile-username" class="span12" readonly style="background:#f5f5f5;cursor:not-allowed;">
</div>
<div class="profile-field-group">
<label>About me</label>
<textarea id="profile-bio" class="span12" rows="3" placeholder="Tell about yourself..."></textarea>
</div>
<div class="profile-field-group" style="margin-top:10px;">
<label>New password (leave empty to keep)</label>
<input type="password" id="profile-new-pass" class="span12" placeholder="New password">
</div>
<div class="profile-field-group" style="margin-top:10px;">
<label>Current password (to confirm)</label>
<input type="password" id="profile-cur-pass" class="span12" placeholder="Current password" required>
</div>
<hr>
<button type="submit" class="btn btn-primary btn-large" id="profile-save-btn">
<svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" style="vertical-align:middle;margin-right:4px;"><path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/></svg>
Save changes
</button>
</form>
</div>
</div>
</div>
</div>
<div id="author-profile" class="page-section">
<div class="row-fluid">
<div class="span8 offset2">
<div class="author-card">
<div class="author-avatar">
<svg viewBox="0 0 24 24" width="40" height="40" fill="currentColor"><path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/></svg>
</div>
<div class="author-name" id="author-profile-name"></div>
<div class="author-bio" id="author-profile-bio"></div>
</div>
<h4>Videos by this user</h4><hr>
<div class="row-fluid" id="author-videos-grid"></div>
</div>
</div>
</div>
</div> <!-- /container -->
<script>
// --- SVG ICONS ---
const IC = {
play: '<svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor"><path d="M8 5v14l11-7z"/></svg>',
pause: '<svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor"><path d="M6 4h4v16H6zm8 0h4v16h-4z"/></svg>',
volUp: '<svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor"><path d="M3 9v6h4l5 5V4L7 9H3zm13.5 3A4.5 4.5 0 0014 8.5v7a4.49 4.49 0 002.5-3.5zM14 3.23v2.06a7 7 0 010 13.42v2.06A9 9 0 0014 3.23z"/></svg>',
volOff: '<svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor"><path d="M3 9v6h4l5 5V4L7 9H3zm13.5 3A4.5 4.5 0 0014 8.5v7a4.49 4.49 0 002.5-3.5z"/><line x1="23" y1="9" x2="17" y2="15" stroke="currentColor" stroke-width="2"/><line x1="17" y1="9" x2="23" y2="15" stroke="currentColor" stroke-width="2"/></svg>',
fullscreen: '<svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor"><path d="M7 14H5v5h5v-2H7v-3zm-2-4h2V7h3V5H5v5zm12 7h-3v2h5v-5h-2v3zM14 5v2h3v3h2V5h-5z"/></svg>',
playCircle: '<svg viewBox="0 0 24 24" width="48" height="48" fill="currentColor" opacity="0.8"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 14.5v-9l6 4.5-6 4.5z"/></svg>',
upload: '<svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor"><path d="M9 16h6v-6h4l-7-7-7 7h4v6zm-4 2h14v2H5v-2z"/></svg>'
};
const API_URL = 'https://pyser.onrunxbuild.com/api';
let currentUser = localStorage.getItem('retrotube_user') || null;
let currentVideoId = null;
let isSeeking = false;
let seekBeforePlay = -1;
function init() {
setPlayerIcons();
updateAuthUI();
loadVideos();
initPlayerControls();
}
function setPlayerIcons() {
document.getElementById('play-pause-btn').innerHTML = IC.play;
document.getElementById('mute-btn').innerHTML = IC.volUp;
document.getElementById('fullscreen-btn').innerHTML = IC.fullscreen;
}
function showSection(id) {
document.querySelectorAll('.page-section').forEach(s => s.classList.remove('active'));
document.getElementById(id).classList.add('active');
if (id !== 'video-view') document.getElementById('main-video').pause();
if (id === 'home') loadVideos();
if (id === 'upload' && !currentUser) { alert('Please login to upload.'); showSection('auth'); }
if (id === 'profile' && currentUser) loadProfile();
window.scrollTo(0, 0);
}
function updateAuthUI() {
const show = !!currentUser;
document.getElementById('nav-user-info').style.display = show ? 'block' : 'none';
document.getElementById('nav-logout-btn').style.display = show ? 'block' : 'none';
document.getElementById('nav-login-btn').style.display = show ? 'none' : 'block';
document.getElementById('nav-profile-btn').style.display = show ? 'block' : 'none';
if (show) document.getElementById('current-username').innerText = currentUser;
}
async function handleAuth(e, type) {
e.preventDefault();
const user = document.getElementById((type === 'login' ? 'login' : 'reg') + '-user').value;
const pass = document.getElementById((type === 'login' ? 'login' : 'reg') + '-pass').value;
try {
const res = await fetch(API_URL + '/auth/' + type, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({username: user, password: pass}) });
const data = await res.json();
if (!res.ok) throw new Error(data.detail || 'Auth error');
currentUser = type === 'login' ? data.username : user;
localStorage.setItem('retrotube_user', currentUser);
updateAuthUI(); showSection('home'); alert(data.message);
} catch (err) {
alert(err.message);
if (err.message === 'Failed to fetch') { currentUser = user; localStorage.setItem('retrotube_user', currentUser); updateAuthUI(); showSection('home'); }
}
}
function logout() { currentUser = null; localStorage.removeItem('retrotube_user'); updateAuthUI(); showSection('home'); }
function escHtml(s) { const d = document.createElement('div'); d.textContent = s; return d.innerHTML; }
function escAttr(s) { return s.replace(/'/g, "\\'").replace(/"/g, '&quot;'); }
async function loadVideos() {
const grid = document.getElementById('video-grid');
grid.innerHTML = '<p>Loading...</p>';
try {
const res = await fetch(API_URL + '/videos');
if (!res.ok) throw new Error();
renderVideos(await res.json());
} catch (err) {
renderVideos([
{id:"1",title:"Big Buck Bunny (Demo)",description:"Big Buck Bunny tells the story of a giant, gentle rabbit.",hf_url:"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",username:"admin"},
{id:"2",title:"Elephant Dream",description:"The first Blender Open Movie from 2006.",hf_url:"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4",username:"demo_user"}
]);
}
}
function renderVideos(videos) {
const grid = document.getElementById('video-grid');
if (!videos || videos.length === 0) { grid.innerHTML = '<p>No videos yet.</p>'; return; }
let html = '';
videos.forEach((v, i) => {
if (i % 3 === 0) html += '<div class="row-fluid">';
const desc = v.description ? '<div class="video-desc-thumb">' + escHtml(v.description) + '</div>' : '';
const vj = JSON.stringify(v).replace(/'/g, "&#39;");
html += '<div class="span4"><div class="video-thumbnail" onclick=\'playVideo(' + vj + ')\'>' +
'<div class="mock-poster">' + IC.playCircle + '</div>' +
'<div class="video-title">' + escHtml(v.title) + '</div>' + desc +
'<div class="video-author">By <a href="#" onclick="event.stopPropagation();openAuthorProfile(\'' + escAttr(v.username) + '\')">' + escHtml(v.username) + '</a></div>' +
'</div></div>';
if (i % 3 === 2 || i === videos.length - 1) html += '</div>';
});
grid.innerHTML = html;
}
function playVideo(v) {
currentVideoId = v.id;
document.getElementById('view-title').innerText = v.title;
const authorEl = document.getElementById('view-author');
authorEl.innerText = v.username;
authorEl.onclick = function() { openAuthorProfile(v.username); };
authorEl.style.cursor = 'pointer';
const descEl = document.getElementById('view-description');
if (v.description) { descEl.style.display = 'block'; descEl.innerText = v.description; } else { descEl.style.display = 'none'; }
const video = document.getElementById('main-video');
const overlay = document.getElementById('video-loading-overlay');
overlay.classList.remove('hidden');
video.src = v.hf_url;
video.load();
showSection('video-view');
document.getElementById('play-pause-btn').innerHTML = IC.play;
const onOk = function() { video.removeEventListener('canplay', onOk); video.removeEventListener('error', onErr); overlay.classList.add('hidden'); video.play().then(() => { document.getElementById('play-pause-btn').innerHTML = IC.pause; }).catch(() => {}); };
const onErr = function() { video.removeEventListener('canplay', onOk); video.removeEventListener('error', onErr); overlay.classList.add('hidden'); };
video.addEventListener('canplay', onOk);
video.addEventListener('error', onErr);
loadComments(v.id);
loadVoteInfo(v.id);
}
async function handleUpload(e) {
e.preventDefault();
const title = document.getElementById('upload-title').value;
const description = document.getElementById('upload-description').value;
const fileInput = document.getElementById('upload-file');
const alertBox = document.getElementById('upload-alert');
const btn = document.getElementById('upload-submit-btn');
if (!fileInput.files[0]) return;
const fd = new FormData();
fd.append('title', title); fd.append('description', description); fd.append('username', currentUser); fd.append('file', fileInput.files[0]);
btn.disabled = true; btn.innerHTML = 'Uploading...';
alertBox.style.display = 'block'; alertBox.className = 'alert alert-info'; alertBox.innerText = 'Uploading...';
try {
const res = await fetch(API_URL + '/videos/upload', { method: 'POST', body: fd });
const data = await res.json();
if (!res.ok) throw new Error(data.detail || 'Upload failed');
alertBox.className = 'alert alert-success'; alertBox.innerText = data.message;
document.getElementById('upload-title').value = ''; document.getElementById('upload-description').value = ''; fileInput.value = '';
} catch (err) { alertBox.className = 'alert alert-error'; alertBox.innerText = err.message; }
finally { btn.disabled = false; btn.innerHTML = IC.upload + ' Upload'; }
}
async function loadComments(videoId) {
const list = document.getElementById('comments-list');
list.innerHTML = '<p class="muted">Loading...</p>';
try {
const res = await fetch(API_URL + '/videos/' + videoId + '/comments');
const comments = await res.json();
if (!comments.length) { list.innerHTML = '<p class="muted">No comments yet.</p>'; return; }
list.innerHTML = comments.map(c => '<div style="border-bottom:1px solid #eee;padding:6px 0;"><b>' + escHtml(c.username) + '</b> <small class="muted">' + (c.created_at ? new Date(c.created_at).toLocaleDateString() : '') + '</small><br>' + escHtml(c.text) + '</div>').join('');
} catch { list.innerHTML = '<p class="muted">Comments unavailable.</p>'; }
}
async function postComment(e) {
e.preventDefault();
if (!currentUser) { alert('Please login to comment.'); return; }
const text = document.getElementById('comment-text').value.trim();
if (!text) return;
try {
await fetch(API_URL + '/comments', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({video_id: currentVideoId, text, username: currentUser}) });
document.getElementById('comment-text').value = '';
loadComments(currentVideoId);
} catch { alert('Server unavailable.'); }
}
async function loadVoteInfo(videoId) {
document.getElementById('vote-score').innerText = '0';
document.getElementById('vote-up-btn').className = 'vote-btn';
document.getElementById('vote-down-btn').className = 'vote-btn';
try {
const url = API_URL + '/videos/' + videoId + '/votes' + (currentUser ? '?username=' + encodeURIComponent(currentUser) : '');
const res = await fetch(url);
if (!res.ok) return;
const data = await res.json();
document.getElementById('vote-score').innerText = data.score || 0;
if (data.my_vote === 1) document.getElementById('vote-up-btn').classList.add('active-up');
if (data.my_vote === -1) document.getElementById('vote-down-btn').classList.add('active-down');
} catch {}
}
async function voteVideo(dir) {
if (!currentUser) { alert('Please login to vote.'); return; }
try {
const res = await fetch(API_URL + '/videos/' + currentVideoId + '/vote', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({username: currentUser, vote_type: dir}) });
if (res.ok) loadVoteInfo(currentVideoId);
} catch { alert('Server unavailable.'); }
}
async function loadProfile() {
const ab = document.getElementById('profile-alert'); ab.style.display = 'none';
document.getElementById('profile-username').value = currentUser || '';
document.getElementById('profile-bio').value = '';
if (!currentUser) return;
try { const r = await fetch(API_URL + '/profile/' + encodeURIComponent(currentUser)); if (r.ok) { const d = await r.json(); document.getElementById('profile-bio').value = d.bio || ''; } } catch {}
}
async function handleProfileUpdate(e) {
e.preventDefault();
const ab = document.getElementById('profile-alert');
if (!currentUser) { ab.style.display='block'; ab.className='alert alert-error'; ab.innerText='Please login.'; return; }
const bio = document.getElementById('profile-bio').value;
const np = document.getElementById('profile-new-pass').value;
const cp = document.getElementById('profile-cur-pass').value;
const btn = document.getElementById('profile-save-btn');
btn.disabled = true; btn.innerHTML = 'Saving...';
try {
const res = await fetch(API_URL + '/profile', { method: 'PUT', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({username: currentUser, password: cp, bio, new_password: np || null}) });
const data = await res.json();
if (!res.ok) throw new Error(data.detail || 'Error');
ab.style.display='block'; ab.className='alert alert-success'; ab.innerText=data.message;
document.getElementById('profile-new-pass').value = ''; document.getElementById('profile-cur-pass').value = '';
} catch (err) { ab.style.display='block'; ab.className='alert alert-error'; ab.innerText=err.message; }
finally { btn.disabled = false; btn.innerHTML = '<svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" style="vertical-align:middle;margin-right:4px;"><path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/></svg> Save changes'; }
}
async function openAuthorProfile(username) {
document.querySelectorAll('.page-section').forEach(s => s.classList.remove('active'));
document.getElementById('author-profile').classList.add('active');
document.getElementById('author-profile-name').innerText = username;
document.getElementById('author-profile-bio').innerText = 'Loading...';
document.getElementById('author-videos-grid').innerHTML = '<p>Loading videos...</p>';
window.scrollTo(0, 0);
try { const r = await fetch(API_URL + '/profile/' + encodeURIComponent(username)); if (r.ok) { const d = await r.json(); document.getElementById('author-profile-bio').innerText = d.bio || 'No bio yet.'; } else document.getElementById('author-profile-bio').innerText = ''; } catch { document.getElementById('author-profile-bio').innerText = ''; }
try {
const res = await fetch(API_URL + '/videos');
if (!res.ok) throw new Error();
const all = await res.json();
const mine = all.filter(v => v.username === username);
if (!mine.length) { document.getElementById('author-videos-grid').innerHTML = '<p>No videos yet.</p>'; return; }
let html = '';
mine.forEach((v, i) => {
if (i % 3 === 0) html += '<div class="row-fluid">';
const vj = JSON.stringify(v).replace(/'/g, "&#39;");
html += '<div class="span4"><div class="video-thumbnail" onclick=\'playVideo(' + vj + ')\'>' +
'<div class="mock-poster">' + IC.playCircle + '</div>' +
'<div class="video-title">' + escHtml(v.title) + '</div></div></div>';
if (i % 3 === 2 || i === mine.length - 1) html += '</div>';
});
document.getElementById('author-videos-grid').innerHTML = html;
} catch { document.getElementById('author-videos-grid').innerHTML = '<p>Could not load videos.</p>'; }
}
function initPlayerControls() {
const video = document.getElementById('main-video');
const playBtn = document.getElementById('play-pause-btn');
const muteBtn = document.getElementById('mute-btn');
const fullBtn = document.getElementById('fullscreen-btn');
const pc = document.getElementById('progress-container');
const pb = document.getElementById('progress-bar');
const td = document.getElementById('time-display');
const pw = document.getElementById('player-wrapper');
function updateIcon() { playBtn.innerHTML = video.paused ? IC.play : IC.pause; }
function doPlay() { if (seekBeforePlay >= 0) { video.currentTime = seekBeforePlay; seekBeforePlay = -1; } video.play().catch(() => {}); }
playBtn.addEventListener('click', (e) => { e.stopPropagation(); if (video.paused) doPlay(); else video.pause(); });
pw.addEventListener('click', (e) => { if (e.target === video || e.target === pw) { if (video.paused) doPlay(); else video.pause(); } });
muteBtn.addEventListener('click', (e) => { e.stopPropagation(); video.muted = !video.muted; muteBtn.innerHTML = video.muted ? IC.volOff : IC.volUp; });
fullBtn.addEventListener('click', (e) => { e.stopPropagation(); if (pw.requestFullscreen) pw.requestFullscreen(); else if (pw.webkitRequestFullscreen) pw.webkitRequestFullscreen(); });
video.addEventListener('timeupdate', () => {
if (isSeeking) return;
if (!video.duration || isNaN(video.duration)) return;
pb.style.width = ((video.currentTime / video.duration) * 100) + '%';
updateTime();
});
video.addEventListener('play', updateIcon);
video.addEventListener('pause', updateIcon);
video.addEventListener('seeked', () => { if (!video.paused) seekBeforePlay = -1; });
video.addEventListener('ended', () => { playBtn.innerHTML = IC.play; seekBeforePlay = -1; });
function updateTime() {
const c = video.currentTime || 0, d = video.duration || 0;
td.innerText = Math.floor(c/60) + ':' + String(Math.floor(c%60)).padStart(2,'0') + ' / ' + Math.floor(d/60) + ':' + String(Math.floor(d%60)).padStart(2,'0');
}
function seekTo(e) {
if (!video.duration || isNaN(video.duration)) return;
const r = pc.getBoundingClientRect();
const p = Math.max(0, Math.min(1, (e.clientX - r.left) / r.width));
video.currentTime = p * video.duration;
pb.style.width = (p * 100) + '%';
seekBeforePlay = p * video.duration;
}
pc.addEventListener('mousedown', (e) => {
e.preventDefault(); e.stopPropagation(); isSeeking = true;
const r = pc.getBoundingClientRect();
const onMove = (ev) => {
const p = Math.max(0, Math.min(1, (ev.clientX - r.left) / r.width));
pb.style.width = (p * 100) + '%';
const cur = p * video.duration;
const dur = video.duration || 0;
td.innerText = Math.floor(cur/60) + ':' + String(Math.floor(cur%60)).padStart(2,'0') + ' / ' + Math.floor(dur/60) + ':' + String(Math.floor(dur%60)).padStart(2,'0');
};
const onUp = (ev) => {
isSeeking = false; seekTo(ev);
document.removeEventListener('mousemove', onMove);
document.removeEventListener('mouseup', onUp);
};
document.addEventListener('mousemove', onMove);
document.addEventListener('mouseup', onUp);
});
pc.addEventListener('click', (e) => { e.stopPropagation(); seekTo(e); });
}
window.onload = init;
</script>
</body>
</html>