/* Admin stats : groupes AS + chargement pages via flux RSS XML /trending?period=14d */ // ── Groupes de réseaux ──────────────────────────────────────────────────────── (function () { var addBtn = document.getElementById('as-group-add'); if (!addBtn) { return; } addBtn.addEventListener('click', function () { var tpl = document.getElementById('as-group-tpl').content.cloneNode(true); document.getElementById('as-groups-list').appendChild(tpl); }); document.getElementById('as-groups-list').addEventListener('click', function (e) { if (e.target.classList.contains('as-group-delete')) { e.target.closest('.as-group-row').remove(); } }); }()); // ── Pages les plus visitées (RSS XML + sparklines) ─────────────────────────── (function () { var container = document.getElementById('stats-pages-container'); var badge = document.getElementById('stats-pages-count'); var pagesByDay = (typeof FOLIO_PAGES_BY_DAY !== 'undefined') ? FOLIO_PAGES_BY_DAY : {}; if (!container) { return; } function esc(s) { return String(s).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); } function sparkline(data) { var W = 120, H = 28, padX = 2, padY = 3; var max = Math.max.apply(null, data) || 1; var n = data.length; var pts = data.map(function (v, i) { var x = padX + i * (W - 2 * padX) / (n - 1); var y = H - padY - (v / max) * (H - 2 * padY); return x.toFixed(1) + ',' + y.toFixed(1); }).join(' '); // Zone remplie sous la courbe var first = padX.toFixed(1) + ',' + (H - padY).toFixed(1); var last = (W - padX).toFixed(1) + ',' + (H - padY).toFixed(1); return '' + '' + '' + '' + '' + '' + '' + ''; } fetch('/trending?period=14d') .then(function (r) { return r.ok ? r.text() : Promise.reject(); }) .then(function (xml) { var doc = new DOMParser().parseFromString(xml, 'application/xml'); var items = Array.from(doc.querySelectorAll('item')); if (!items.length) { container.innerHTML = '

Aucune donnée.

'; return; } var rows = items.map(function (item) { var raw = (item.querySelector('title') || { textContent: '' }).textContent; var link = ((item.querySelector('link') || {}).textContent || '').trim(); var m = raw.match(/\((\d+)\s+visiteurs?\)$/); var vis = m ? parseInt(m[1], 10) : 0; var title = raw.replace(/\s*\(\d+\s+visiteurs?\)$/, ''); var slug = decodeURIComponent(link.replace(/.*\/post\//, '')); var pm = link.match(/\/post\/[^?#]*/); var daily = pm ? (pagesByDay[pm[0]] || null) : null; return { title: title, link: link, slug: slug, vis: vis, daily: daily }; }); var maxV = Math.max.apply(null, rows.map(function (r) { return r.vis; })) || 1; var html = '
'; rows.forEach(function (row, i) { var vis = row.vis.toLocaleString('fr-FR'); var spk = row.daily ? sparkline(row.daily) : ''; html += '' + '' + '' + '' + '' + ''; }); html += '
' + (i + 1) + '' + esc(row.title || row.slug) + '' + spk + '' + vis + ' vis.
'; if (badge) { badge.textContent = rows.length + ' URLs'; } container.innerHTML = html; }) .catch(function () { container.innerHTML = '

Impossible de charger le flux.

'; }); }());