245 lines
11 KiB
HTML
245 lines
11 KiB
HTML
{{ define "index" }}
|
|
<div class="container mt-4">
|
|
<div class="card">
|
|
<div class="card-header d-flex justify-content-between align-items-center gap-4">
|
|
<h4 class="mb-0 text-nowrap"><i class="bi bi-table me-2"></i>Active Torrents</h4>
|
|
<div class="d-flex align-items-center overflow-auto" style="flex-wrap: nowrap; gap: 0.5rem;">
|
|
<button class="btn btn-outline-danger btn-sm" id="batchDeleteBtn" style="display: none; flex-shrink: 0;">
|
|
<i class="bi bi-trash me-1"></i>Delete Selected
|
|
</button>
|
|
<button class="btn btn-outline-secondary btn-sm me-2" id="refreshBtn" style="flex-shrink: 0;">
|
|
<i class="bi bi-arrow-clockwise me-1"></i>Refresh
|
|
</button>
|
|
<select class="form-select form-select-sm d-inline-block w-auto me-2" id="stateFilter" style="flex-shrink: 0;">
|
|
<option value="">All States</option>
|
|
<option value="downloading">Downloading</option>
|
|
<option value="pausedup">Paused</option>
|
|
<option value="error">Error</option>
|
|
</select>
|
|
<select class="form-select form-select-sm d-inline-block w-auto" id="categoryFilter">
|
|
<option value="">All Categories</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="card-body p-0">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover mb-0">
|
|
<thead>
|
|
<tr>
|
|
<th>
|
|
<input type="checkbox" class="form-check-input" id="selectAll">
|
|
</th>
|
|
<th>Name</th>
|
|
<th>Size</th>
|
|
<th>Progress</th>
|
|
<th>Speed</th>
|
|
<th>Category</th>
|
|
<th>Debrid</th>
|
|
<th>State</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody id="torrentsList">
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<script>
|
|
let refs = {
|
|
torrentsList: document.getElementById('torrentsList'),
|
|
categoryFilter: document.getElementById('categoryFilter'),
|
|
stateFilter: document.getElementById('stateFilter'),
|
|
selectAll: document.getElementById('selectAll'),
|
|
batchDeleteBtn: document.getElementById('batchDeleteBtn'),
|
|
refreshBtn: document.getElementById('refreshBtn'),
|
|
};
|
|
let state = {
|
|
torrents: [],
|
|
selectedTorrents: new Set(),
|
|
categories: new Set(),
|
|
states: new Set('downloading', 'pausedup', 'error'),
|
|
selectedCategory: refs.categoryFilter?.value || '',
|
|
selectedState: refs.stateFilter?.value || '',
|
|
};
|
|
|
|
const torrentRowTemplate = (torrent) => `
|
|
<tr data-hash="${torrent.hash}">
|
|
<td>
|
|
<input type="checkbox" class="form-check-input torrent-select" data-hash="${torrent.hash}" ${state.selectedTorrents.has(torrent.hash) ? 'checked' : ''}>
|
|
</td>
|
|
<td class="text-nowrap text-truncate overflow-hidden" style="max-width: 350px;" title="${torrent.name}">${torrent.name}</td>
|
|
<td class="text-nowrap">${formatBytes(torrent.size)}</td>
|
|
<td style="min-width: 150px;">
|
|
<div class="progress" style="height: 8px;">
|
|
<div class="progress-bar" role="progressbar"
|
|
style="width: ${(torrent.progress * 100).toFixed(1)}%"
|
|
aria-valuenow="${(torrent.progress * 100).toFixed(1)}"
|
|
aria-valuemin="0"
|
|
aria-valuemax="100"></div>
|
|
</div>
|
|
<small class="text-muted">${(torrent.progress * 100).toFixed(1)}%</small>
|
|
</td>
|
|
<td>${formatSpeed(torrent.dlspeed)}</td>
|
|
<td><span class="badge bg-secondary">${torrent.category || 'None'}</span></td>
|
|
<td>${torrent.debrid || 'None'}</td>
|
|
<td><span class="badge ${getStateColor(torrent.state)}">${torrent.state}</span></td>
|
|
<td>
|
|
<button class="btn btn-sm btn-outline-danger" onclick="deleteTorrent('${torrent.hash}')">
|
|
<i class="bi bi-trash"></i>
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
`;
|
|
|
|
function formatBytes(bytes) {
|
|
if (!bytes) return '0 B';
|
|
const k = 1024;
|
|
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
|
|
}
|
|
|
|
function formatSpeed(speed) {
|
|
return `${formatBytes(speed)}/s`;
|
|
}
|
|
|
|
function getStateColor(state) {
|
|
const stateColors = {
|
|
'downloading': 'bg-primary',
|
|
'pausedup': 'bg-success',
|
|
'error': 'bg-danger',
|
|
};
|
|
return stateColors[state?.toLowerCase()] || 'bg-secondary';
|
|
}
|
|
|
|
function updateUI() {
|
|
// Filter torrents by selected category and state
|
|
let filteredTorrents = state.torrents;
|
|
if (state.selectedCategory) {
|
|
filteredTorrents = filteredTorrents.filter(t => t.category === state.selectedCategory);
|
|
}
|
|
if (state.selectedState) {
|
|
filteredTorrents = filteredTorrents.filter(t => t.state === state.selectedState);
|
|
}
|
|
|
|
// Update the torrents list table
|
|
refs.torrentsList.innerHTML = filteredTorrents.map(torrent => torrentRowTemplate(torrent)).join('');
|
|
|
|
// Update the category filter dropdown
|
|
const currentCategories = Array.from(state.categories).sort();
|
|
const categoryOptions = ['<option value="">All Categories</option>']
|
|
.concat(currentCategories.map(cat =>
|
|
`<option value="${cat}" ${cat === state.selectedCategory ? 'selected' : ''}>${cat}</option>`
|
|
));
|
|
refs.categoryFilter.innerHTML = categoryOptions.join('');
|
|
|
|
// Clean up selected torrents that no longer exist
|
|
state.selectedTorrents = new Set(
|
|
Array.from(state.selectedTorrents)
|
|
.filter(hash => filteredTorrents.some(t => t.hash === hash))
|
|
);
|
|
|
|
// Update batch delete button visibility
|
|
refs.batchDeleteBtn.style.display = state.selectedTorrents.size > 0 ? '' : 'none';
|
|
|
|
// Update the select all checkbox state
|
|
refs.selectAll.checked = filteredTorrents.length > 0 && filteredTorrents.every(torrent => state.selectedTorrents.has(torrent.hash));
|
|
}
|
|
|
|
async function loadTorrents() {
|
|
try {
|
|
const response = await fetch('/internal/torrents');
|
|
const torrents = await response.json();
|
|
|
|
state.torrents = torrents;
|
|
state.categories = new Set(torrents.map(t => t.category).filter(Boolean));
|
|
|
|
updateUI();
|
|
} catch (error) {
|
|
console.error('Error loading torrents:', error);
|
|
}
|
|
}
|
|
|
|
async function deleteTorrent(hash) {
|
|
if (!confirm('Are you sure you want to delete this torrent?')) return;
|
|
|
|
try {
|
|
await fetch(`/internal/torrents/${hash}`, {
|
|
method: 'DELETE'
|
|
});
|
|
await loadTorrents();
|
|
createToast('Torrent deleted successfully');
|
|
} catch (error) {
|
|
console.error('Error deleting torrent:', error);
|
|
createToast('Failed to delete torrent', 'error');
|
|
}
|
|
}
|
|
|
|
async function deleteSelectedTorrents() {
|
|
if (!confirm(`Are you sure you want to delete ${state.selectedTorrents.size} selected torrents?`)) return;
|
|
|
|
try {
|
|
const deletePromises = Array.from(state.selectedTorrents).map(hash =>
|
|
fetch(`/internal/torrents/${hash}`, { method: 'DELETE' })
|
|
);
|
|
await Promise.all(deletePromises);
|
|
await loadTorrents();
|
|
createToast('Selected torrents deleted successfully');
|
|
} catch (error) {
|
|
console.error('Error deleting torrents:', error);
|
|
createToast('Failed to delete some torrents' , 'error');
|
|
}
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
loadTorrents();
|
|
const refreshInterval = setInterval(loadTorrents, 5000);
|
|
|
|
refs.refreshBtn.addEventListener('click', loadTorrents);
|
|
refs.batchDeleteBtn.addEventListener('click', deleteSelectedTorrents);
|
|
|
|
refs.selectAll.addEventListener('change', (e) => {
|
|
const filteredTorrents = state.torrents.filter(t => {
|
|
if (state.selectedCategory && t.category !== state.selectedCategory) return false;
|
|
if (state.selectedState && t.state?.toLowerCase() !== state.selectedState.toLowerCase()) return false;
|
|
return true;
|
|
});
|
|
|
|
if (e.target.checked) {
|
|
filteredTorrents.forEach(torrent => state.selectedTorrents.add(torrent.hash));
|
|
} else {
|
|
filteredTorrents.forEach(torrent => state.selectedTorrents.delete(torrent.hash));
|
|
}
|
|
updateUI();
|
|
});
|
|
|
|
refs.torrentsList.addEventListener('change', (e) => {
|
|
if (e.target.classList.contains('torrent-select')) {
|
|
const hash = e.target.dataset.hash;
|
|
if (e.target.checked) {
|
|
state.selectedTorrents.add(hash);
|
|
} else {
|
|
state.selectedTorrents.delete(hash);
|
|
}
|
|
updateUI();
|
|
}
|
|
});
|
|
|
|
refs.categoryFilter.addEventListener('change', (e) => {
|
|
state.selectedCategory = e.target.value;
|
|
updateUI();
|
|
});
|
|
|
|
refs.stateFilter.addEventListener('change', (e) => {
|
|
state.selectedState = e.target.value;
|
|
updateUI();
|
|
});
|
|
|
|
window.addEventListener('beforeunload', () => {
|
|
clearInterval(refreshInterval);
|
|
});
|
|
});
|
|
</script>
|
|
{{ end }} |