Files
offlineu/templates/lesson_view.html
T

579 lines
22 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>{{ lesson.title }} - {{ course.name }}</title>
<style>
:root {
--bg-primary: #1a1a1a;
--bg-secondary: #2d2d2d;
--bg-tertiary: #3d3d3d;
--bg-tertiary-hover: #404040;
--text-primary: #e0e0e0;
--text-muted: #999;
--border-color: #555;
--accent: #007acc;
--accent-hover: #005a9e;
--font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
--font-size-base: 16px;
--container-max-width: 1600px;
--radius: 8px;
}
[data-theme="light"] {
--bg-primary: #f2f2f2;
--bg-secondary: #ffffff;
--bg-tertiary: #eeeeee;
--bg-tertiary-hover: #e2e2e2;
--text-primary: #222222;
--text-muted: #666666;
--border-color: #cccccc;
}
[data-density="compact"] .container { padding: 10px; }
[data-density="compact"] .text-content { padding: 10px; margin: 10px 0; }
[data-density="compact"] .navigation,
[data-density="compact"] .nav-buttons { gap: 5px; margin: 10px 0; }
[data-card-style="elevated"] .container,
[data-card-style="elevated"] .text-content,
[data-card-style="elevated"] .file-link {
box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
}
[data-card-style="bordered"] .container,
[data-card-style="bordered"] .text-content,
[data-card-style="bordered"] .file-link {
border: 1px solid var(--border-color);
}
html, body {
height: 100%;
}
body {
font-family: var(--font-family);
font-size: var(--font-size-base);
margin: 0;
background: var(--bg-primary);
color: var(--text-primary);
display: flex;
flex-direction: column;
min-height: 100vh;
}
.page-content {
flex: 1;
padding: 20px;
}
.app-header {
background: linear-gradient(135deg, var(--bg-secondary), var(--bg-tertiary));
padding: 18px 0;
border-bottom: 3px solid var(--accent);
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.25);
}
.app-header .container {
max-width: var(--container-max-width);
width: 95%;
margin: 0 auto;
display: flex;
align-items: center;
gap: 12px;
}
.app-header .brand-icon {
font-size: 1.6em;
}
.app-header a.brand-link {
color: var(--accent);
text-decoration: none;
font-size: 1.3em;
font-weight: 600;
letter-spacing: 0.5px;
}
.app-footer {
background: var(--bg-secondary);
border-top: 1px solid var(--border-color);
padding: 18px 0;
}
.app-footer .container-inner {
max-width: var(--container-max-width);
width: 95%;
margin: 0 auto;
display: flex;
align-items: center;
justify-content: space-between;
gap: 15px;
flex-wrap: wrap;
}
.footer-brand {
color: var(--text-muted);
font-size: 0.9em;
display: flex;
align-items: center;
gap: 8px;
}
.footer-links {
display: flex;
gap: 20px;
}
.footer-links a {
color: var(--text-muted);
text-decoration: none;
font-size: 0.9em;
display: flex;
align-items: center;
gap: 6px;
transition: color 0.2s;
}
.footer-links a:hover {
color: var(--accent);
}
.container {
max-width: var(--container-max-width);
width: 95%;
margin: 20px auto;
background: var(--bg-secondary);
padding: 20px;
border-radius: var(--radius);
}
video, audio {
width: 100%;
max-width: 100%;
border-radius: var(--radius);
display: block;
margin: 0 auto;
}
video {
resize: both;
overflow: hidden;
object-fit: contain;
background: #000;
min-width: 320px;
min-height: 200px;
}
.resize-hint {
text-align: center;
font-size: 0.8em;
color: var(--text-muted);
margin-top: 6px;
}
.content {
margin: 20px 0;
}
.navigation {
margin: 20px 0;
display: flex;
gap: 10px;
flex-wrap: wrap;
align-items: center;
}
button {
padding: 10px 20px;
margin: 5px;
cursor: pointer;
background: var(--accent);
color: white;
border: none;
border-radius: var(--radius);
transition: background 0.3s;
}
button:hover {
background: var(--accent-hover);
}
button:disabled {
background: #666;
cursor: not-allowed;
}
.file-link {
display: block;
margin: 5px 0;
padding: 10px;
background: var(--bg-tertiary);
text-decoration: none;
color: var(--text-primary);
border-radius: var(--radius);
transition: background 0.3s;
}
.file-link:hover {
background: var(--bg-tertiary-hover);
}
.lesson-path {
background: var(--bg-tertiary);
padding: 10px;
border-radius: var(--radius);
margin-bottom: 20px;
font-family: monospace;
color: var(--text-muted);
}
.lesson-title {
color: var(--accent);
margin-bottom: 20px;
}
.text-content {
background: var(--bg-tertiary);
padding: 20px;
border-radius: var(--radius);
margin: 20px 0;
}
.text-content.plain-text {
max-height: 600px;
overflow-y: auto;
white-space: pre-wrap;
font-family: 'Courier New', monospace;
line-height: 1.6;
}
.text-content iframe {
background: white;
border-radius: var(--radius);
}
.nav-buttons {
display: flex;
gap: 10px;
margin-top: 20px;
}
.nav-buttons a {
text-decoration: none;
color: inherit;
}
</style>
</head>
<body>
<div class="app-header">
<div class="container">
<span class="brand-icon">📚</span>
<a href="/reset_course" class="brand-link">OfflineU</a>
</div>
</div>
<div class="page-content">
<div class="container">
<h1 class="lesson-title">{{ lesson.title }}</h1>
<div class="lesson-path">
<strong>Path:</strong> {{ lesson_path }}
</div>
<div class="navigation">
<a href="/" style="text-decoration: none; color: var(--accent);">← Back to Course</a>
<button onclick="markCompleted()">Mark as Completed</button>
</div>
<div class="content">
{% if lesson.video_file %}
<h3>Video</h3>
<video controls preload="metadata" id="video-player">
<source src="/files/{{ lesson.video_file }}" type="video/mp4">
{% if lesson.subtitle_file %}
<track kind="subtitles" src="/files/{{ lesson.subtitle_file }}" srclang="en" label="English">
{% endif %}
Your browser does not support the video tag.
</video>
<p class="resize-hint">↘ Drag the bottom-right corner to resize the player</p>
{% endif %}
{% if lesson.audio_file %}
<h3>Audio</h3>
<audio controls preload="metadata" id="audio-player">
<source src="/files/{{ lesson.audio_file }}" type="audio/mp3">
Your browser does not support the audio tag.
</audio>
{% endif %}
{% if lesson.text_files %}
<h3>Content</h3>
{% for text_file in lesson.text_files %}
<div style="margin-bottom: 20px;">
<h4>{{ text_file.split('/')[-1] }}</h4>
<div class="text-content" id="content-{{ loop.index0 }}">
<div style="text-align: center; color: #666;">Loading content...</div>
</div>
<a href="/files/{{ text_file|replace('\\', '/') }}" class="file-link" target="_blank">
📄 Open {{ text_file.split('/')[-1] }} in new tab
</a>
</div>
{% endfor %}
{% endif %}
</div>
<div class="nav-buttons">
{% if prev_lesson %}
<a href="/lesson/{{ prev_lesson }}">
<button>← Previous</button>
</a>
{% else %}
<button disabled>← Previous</button>
{% endif %}
{% if next_lesson %}
<a href="/lesson/{{ next_lesson }}">
<button>Next →</button>
</a>
{% else %}
<button disabled>Next →</button>
{% endif %}
</div>
<script>
// Load text content
{% for text_file in lesson.text_files %}
console.log('Loading file: {{ text_file }}');
const filePath = '{{ text_file|replace("\\", "/") }}';
console.log('Encoded file path:', filePath);
// Check if this is an HTML file
const isHtmlFile = filePath.toLowerCase().endsWith('.html') || filePath.toLowerCase().endsWith('.htm');
const isPdfFile = filePath.toLowerCase().endsWith('.pdf');
const isBinaryDoc = ['.docx', '.doc', '.rtf'].some(ext => filePath.toLowerCase().endsWith(ext));
const contentDiv = document.getElementById('content-{{ loop.index0 }}');
if (isHtmlFile) {
// For HTML files, create an iframe
console.log('Creating iframe for HTML file');
contentDiv.innerHTML = `
<iframe
src="/files/${encodeURIComponent(filePath)}"
style="width: 100%; height: 600px; border: none; border-radius: 5px;"
title="${filePath.split('/').pop()}"
></iframe>
`;
} else if (isPdfFile) {
// For PDFs, embed via iframe rather than fetching as text -
// browsers render PDFs natively in an iframe, but fetching
// the raw bytes as text (like below) produces garbage.
console.log('Embedding PDF in iframe');
contentDiv.innerHTML = `
<iframe
src="/files/${encodeURIComponent(filePath)}"
style="width: 100%; height: 85vh; border: none; border-radius: 5px; background: #fff;"
title="${filePath.split('/').pop()}"
></iframe>
`;
} else if (isBinaryDoc) {
// .docx/.doc/.rtf can't be rendered inline by the browser;
// fetching them as text would show garbled binary content,
// so just point at the download/open link instead.
contentDiv.innerHTML =
'<div style="color: #999;">Preview isn\'t available for this file type. ' +
'Use the "Open in new tab" link below to view or download it.</div>';
} else {
// For text files, fetch and display content
fetch('/files/' + encodeURIComponent(filePath))
.then(response => {
console.log('Response status:', response.status);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
return response.text();
})
.then(content => {
console.log('Content loaded successfully, length:', content.length);
if (contentDiv) {
contentDiv.classList.add('plain-text');
// Check if it's HTML content
if (content.trim().startsWith('<') && content.includes('</')) {
contentDiv.innerHTML = content;
} else {
// Treat as plain text
contentDiv.textContent = content;
}
}
})
.catch(error => {
console.error('Error loading content:', error);
if (contentDiv) {
contentDiv.innerHTML =
'<div style="color: #ff6b6b;">Error loading content: ' + error.message + '</div>' +
'<div style="color: #999; font-size: 0.9em; margin-top: 10px;">File path: ' + filePath + '</div>';
}
});
}
{% endfor %}
// Media progress tracking
const video = document.getElementById('video-player');
const audio = document.getElementById('audio-player');
const activeMedia = video || audio;
let isCompleted = false;
// Restore last-used video player size, and persist future resizes
// (dragging the native corner handle) so it sticks next time.
if (video) {
let applyingStoredSize = true;
let resizeSaveTimeout = null;
fetch('/api/settings')
.then(r => r.json())
.then(data => {
const s = data.settings || {};
if (s.video_width && s.video_height) {
video.style.width = s.video_width + 'px';
video.style.height = s.video_height + 'px';
}
setTimeout(() => { applyingStoredSize = false; }, 300);
})
.catch(() => { applyingStoredSize = false; });
if (window.ResizeObserver) {
const observer = new ResizeObserver(() => {
if (applyingStoredSize) return;
clearTimeout(resizeSaveTimeout);
resizeSaveTimeout = setTimeout(() => {
const rect = video.getBoundingClientRect();
fetch('/api/settings', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
video_width: Math.round(rect.width),
video_height: Math.round(rect.height)
})
}).catch(() => {});
}, 500);
});
observer.observe(video);
}
}
if (activeMedia) {
let lastSaveTime = 0;
activeMedia.addEventListener('timeupdate', function() {
const currentTime = activeMedia.currentTime;
// Save progress every 15 seconds
if (currentTime - lastSaveTime > 15) {
saveProgress(currentTime);
lastSaveTime = currentTime;
}
});
activeMedia.addEventListener('ended', function() {
saveProgress(activeMedia.currentTime, true);
markAsCompleted();
});
// Resume from saved position
const savedProgress = {{ lesson.progress_seconds|default(0) }};
if (savedProgress > 0 && savedProgress < activeMedia.duration - 30) {
activeMedia.currentTime = savedProgress;
showNotification(`Resumed from ${Math.floor(savedProgress / 60)}:${String(Math.floor(savedProgress % 60)).padStart(2, '0')}`);
}
}
function saveProgress(progressSeconds, completed = false) {
const duration = activeMedia && isFinite(activeMedia.duration) ? Math.floor(activeMedia.duration) : null;
fetch('/api/progress', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
lesson_path: '{{ lesson_path }}',
completed: completed,
progress_seconds: Math.floor(progressSeconds),
duration_seconds: duration
})
}).catch(err => console.error('Failed to save progress:', err));
}
function markCompleted() {
if (isCompleted) return;
const currentTime = activeMedia ? activeMedia.currentTime : 0;
saveProgress(currentTime, true);
markAsCompleted();
}
function markAsCompleted() {
const btn = document.querySelector('button[onclick="markCompleted()"]');
if (btn) {
btn.textContent = 'Completed ✓';
btn.style.background = '#28a745';
btn.disabled = true;
}
isCompleted = true;
showNotification('Lesson marked as completed!', 'success');
}
function showNotification(message, type = 'info') {
const notification = document.createElement('div');
notification.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
background: ${type === 'success' ? '#28a745' : '#007acc'};
color: white;
padding: 15px 20px;
border-radius: 5px;
z-index: 10000;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
opacity: 0;
transform: translateY(-20px);
transition: all 0.3s ease;
`;
notification.textContent = message;
document.body.appendChild(notification);
setTimeout(() => {
notification.style.opacity = '1';
notification.style.transform = 'translateY(0)';
}, 100);
setTimeout(() => {
notification.style.opacity = '0';
notification.style.transform = 'translateY(-20px)';
setTimeout(() => {
if (document.body.contains(notification)) {
document.body.removeChild(notification);
}
}, 300);
}, 3000);
}
// Keyboard shortcuts
document.addEventListener('keydown', function(e) {
if (activeMedia && !e.ctrlKey && !e.altKey && !e.metaKey) {
switch(e.key) {
case ' ':
e.preventDefault();
if (activeMedia.paused) {
activeMedia.play();
} else {
activeMedia.pause();
}
break;
case 'ArrowLeft':
e.preventDefault();
activeMedia.currentTime = Math.max(0, activeMedia.currentTime - 10);
break;
case 'ArrowRight':
e.preventDefault();
activeMedia.currentTime = Math.min(activeMedia.duration, activeMedia.currentTime + 10);
break;
case 'ArrowUp':
e.preventDefault();
activeMedia.volume = Math.min(1, activeMedia.volume + 0.1);
showNotification(`Volume: ${Math.round(activeMedia.volume * 100)}%`);
break;
case 'ArrowDown':
e.preventDefault();
activeMedia.volume = Math.max(0, activeMedia.volume - 0.1);
showNotification(`Volume: ${Math.round(activeMedia.volume * 100)}%`);
break;
}
}
});
// Set completed state if already completed
if ({{ 'true' if lesson.completed else 'false' }}) {
markAsCompleted();
}
</script>
</div>
</div>
<footer class="app-footer">
<div class="container-inner">
<div class="footer-brand">
📚 <a href="/reset_course" style="color: inherit; text-decoration: none;">OfflineU</a>
</div>
<div class="footer-links">
<a href="/help">❓ Help</a>
<a href="/settings">⚙ Settings</a>
</div>
</div>
</footer>
<script src="/static/theme.js"></script>
</body>
</html>