Files
ygg-service/frontend/templates/search_results.html
2025-11-11 01:48:45 +01:00

87 lines
2.7 KiB
HTML

{% extends 'base.html' %}
{% block content %}
<h2>Résultats pour "{{ query }}"</h2>
<table id="results-table">
<thead>
<tr>
<th onclick="sortTable(0)">Nom &#x21C5;</th>
<th onclick="sortTable(1)">Taille &#x21C5;</th>
<th onclick="sortTable(2)">Âge &#x21C5;</th>
<th>Lien</th>
</tr>
</thead>
<tbody>
{% for item in results %}
<tr>
<td>{{ item.title }}</td>
<td>{{ item.size }}</td>
<td data-age="{{ item.age_days }}">{{ item.age_human }}</td>
<td>
<button onclick="getTorrentLink('{{ item.id }}', this)">Obtenir le lien</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<script>
function parseSize(sizeStr) {
// Convertit "6.2 Go", "700 Mo" en octets
const units = { "o": 1, "Ko": 1024, "Mo": 1024**2, "Go": 1024**3, "To": 1024**4 };
const match = sizeStr.match(/([\d\.]+)\s*(o|Ko|Mo|Go|To)/);
if (!match) return 0;
return parseFloat(match[1]) * (units[match[2]] || 1);
}
function sortTable(colIndex) {
const table = document.getElementById("results-table");
const tbody = table.tBodies[0];
const rows = Array.from(tbody.rows);
const asc = table.asc = !table.asc;
rows.sort((a, b) => {
let aText = a.cells[colIndex].innerText.trim();
let bText = b.cells[colIndex].innerText.trim();
if (colIndex === 1) { // Taille
return asc ? parseSize(aText) - parseSize(bText) : parseSize(bText) - parseSize(aText);
} else if (colIndex === 2) { // Âge
const aVal = parseInt(a.cells[colIndex].dataset.age || 0);
const bVal = parseInt(b.cells[colIndex].dataset.age || 0);
return asc ? aVal - bVal : bVal - aVal;
} else { // Nom
return asc ? aText.localeCompare(bText) : bText.localeCompare(aText);
}
});
rows.forEach(row => tbody.appendChild(row));
}
async function getTorrentLink(torrentId, btn) {
btn.disabled = true;
btn.innerText = "Chargement...";
try {
const response = await fetch(`/torrent/${torrentId}`);
const links = await response.json(); // <-- lire comme JSON
if (links && links.length > 0) {
// Construire une liste HTML
const listHTML = links.map(link =>
`<li><a href="${link.link}" target="_blank">${link.name} (${(link.size / 1024 / 1024).toFixed(2)} Mo)</a></li>`
).join("");
btn.innerHTML = `<ul>${listHTML}</ul>`;
} else {
btn.innerText = "Aucun fichier trouvé";
}
} catch (err) {
btn.innerText = "Erreur";
console.error(err);
}
}
</script>
{% endblock %}