|
|
| Zeile 222: |
Zeile 222: |
| /* Whisky-Ratings – eigenes Frontend für RatePage (Variante B, robust + Stats) */ | | /* Whisky-Ratings – eigenes Frontend für RatePage (Variante B, robust + Stats) */ |
| mw.loader.using(['mediawiki.api', 'mediawiki.user']).then(function () { | | mw.loader.using(['mediawiki.api', 'mediawiki.user']).then(function () { |
|
| |
| /* --- Kompakte Ø-Übersichtstabelle für alle drei Kategorien --- */
| |
| function initSummaryBlocks() {
| |
| document.querySelectorAll('[data-ratepage-summary="true"]').forEach(renderSummary);
| |
| }
| |
|
| |
| function renderSummary(container) {
| |
| const pageId = mw.config.get('wgArticleId');
| |
| const contests = (container.dataset.ratepageContests || 'NASE,GESCHMACK,ABGANG')
| |
| .split(',').map(s => s.trim()).filter(Boolean);
| |
|
| |
| const labels = {
| |
| NASE: 'Nase',
| |
| GESCHMACK: 'Geschmack',
| |
| ABGANG: 'Abgang'
| |
| };
| |
|
| |
| container.textContent = 'Lade Bewertungen …';
| |
|
| |
| function fetchContest(contest) {
| |
| return new mw.Api().get({
| |
| action: 'query',
| |
| prop: 'pagerating',
| |
| pageids: pageId,
| |
| prcontest: contest,
| |
| format: 'json'
| |
| }).then(function (data) {
| |
| const pr = data?.query?.pages?.[pageId]?.pagerating;
| |
| if (!pr || (typeof pr.canSee !== 'undefined' && pr.canSee === 0)) {
| |
| return { contest, total: 0, avg: null, label: labels[contest] || contest };
| |
| }
| |
| const hist = pr.pageRating || {};
| |
| let total = 0, sum = 0;
| |
| Object.keys(hist).forEach(k => {
| |
| const s = parseInt(k, 10), c = parseInt(hist[k], 10);
| |
| if (!isNaN(s) && !isNaN(c)) { total += c; sum += s * c; }
| |
| });
| |
| const avg = total ? Math.round((sum / total) * 10) / 10 : null;
| |
| return { contest, total, avg, label: labels[contest] || contest };
| |
| });
| |
| }
| |
|
| |
| Promise.all(contests.map(fetchContest)).then(function (rows) {
| |
| const table = document.createElement('table');
| |
| table.className = 'whisky-summary__table';
| |
|
| |
| const thead = document.createElement('thead');
| |
| thead.innerHTML = '<tr><th>Kategorie</th><th>Ø</th><th>Stimmen</th></tr>';
| |
| table.appendChild(thead);
| |
|
| |
| const tbody = document.createElement('tbody');
| |
| rows.forEach(r => {
| |
| const avgText = (r.avg !== null) ? r.avg.toFixed(1) : '–';
| |
| const totalText = r.total ? String(r.total) : '0';
| |
| const tr = document.createElement('tr');
| |
| tr.innerHTML = `<td>${r.label}</td><td>${avgText}</td><td>${totalText}</td>`;
| |
| tbody.appendChild(tr);
| |
| });
| |
| table.appendChild(tbody);
| |
|
| |
| container.replaceChildren(table);
| |
| });
| |
| }
| |
|
| |
| // Beim Laden ausführen
| |
| if (document.readyState === 'loading') {
| |
| document.addEventListener('DOMContentLoaded', initSummaryBlocks);
| |
| } else {
| |
| initSummaryBlocks();
| |
| }
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| function init() { | | function init() { |