MediaWiki:Common.js: Unterschied zwischen den Versionen
Erscheinungsbild
Admin (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung Markierung: Zurückgesetzt |
Admin (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung Markierung: Zurückgesetzt |
||
| Zeile 219: | Zeile 219: | ||
/* ADOS Whisky-Ratings – RatePage Frontend (robust + Stats + Summary) */ | /* ADOS Whisky-Ratings – RatePage Frontend (robust + Stats + Summary, ohne Optional Chaining) */ | ||
mw.loader.using(['mediawiki.api', 'mediawiki.user']).then(function () { | mw.loader.using(['mediawiki.api', 'mediawiki.user']).then(function () { | ||
// ---------- Hilfsfunktionen ---------- | |||
function get(obj, pathArray) { | |||
var cur = obj; | |||
for (var i = 0; i < pathArray.length; i++) { | |||
if (!cur || typeof cur !== 'object') return undefined; | |||
cur = cur[pathArray[i]]; | |||
} | |||
return cur; | |||
} | |||
// ---------- Initialisierung ---------- | // ---------- Initialisierung ---------- | ||
function boot( | function boot(root) { | ||
var scope = root || document; | |||
var items = scope.querySelectorAll('.whisky-rating__item'); | |||
for (var i = 0; i < items.length; i++) setupWidget(items[i]); | |||
var summaries = scope.querySelectorAll('[data-ratepage-summary="true"]'); | |||
for (i = 0; i < summaries.length; i++) renderSummary(summaries[i]); | |||
var metaOnly = scope.querySelectorAll('.whisky-rating__meta-only'); | |||
for (i = 0; i < metaOnly.length; i++) renderMetaOnly(metaOnly[i]); | |||
} | } | ||
if (document.readyState === 'loading') { | if (document.readyState === 'loading') { | ||
document.addEventListener('DOMContentLoaded', function(){ boot(document); }); | document.addEventListener('DOMContentLoaded', function(){ boot(document); }); | ||
| Zeile 238: | Zeile 250: | ||
boot(document); | boot(document); | ||
} | } | ||
mw.hook('wikipage.content').add(function($content) { | mw.hook('wikipage.content').add(function($content) { | ||
if ($content && $content[0]) boot($content[0]); | if ($content && $content[0]) boot($content[0]); | ||
}); | }); | ||
| Zeile 246: | Zeile 257: | ||
// ---------- Widget (Gläser) ---------- | // ---------- Widget (Gläser) ---------- | ||
function setupWidget(box) { | function setupWidget(box) { | ||
var pageId = mw.config.get('wgArticleId'); | |||
var contest = box.dataset.ratepageContest || undefined; // "NASE" | "GESCHMACK" | "ABGANG" | |||
var scale = parseInt(box.dataset.ratepageScale || '10', 10); | |||
var widget = box.querySelector('.whisky-rating__widget'); | |||
var meta = box.querySelector('.whisky-rating__meta'); | |||
var isAnon = mw.user.isAnon(); | |||
if (isAnon) { | if (isAnon) { | ||
box.classList.add('whisky-rating--disabled'); | box.classList.add('whisky-rating--disabled'); | ||
| Zeile 260: | Zeile 271: | ||
// Buttons bauen | // Buttons bauen | ||
var buttons = []; | |||
for ( | for (var i = 1; i <= scale; i++) { | ||
(function(iVal){ | |||
var btn = document.createElement('button'); | |||
btn.type = 'button'; | |||
btn.className = 'whisky-glass'; | |||
btn.setAttribute('aria-label', iVal + ' von ' + scale); | |||
btn.setAttribute('aria-pressed', 'false'); | |||
if (isAnon) { | |||
btn.disabled = true; | |||
btn.title = 'Nur für registrierte Benutzer'; | |||
} else { | |||
btn.title = iVal + ' / ' + scale; | |||
btn.addEventListener('mouseenter', function(){ highlight(iVal); }); | |||
btn.addEventListener('mouseleave', function(){ highlight(current); }); | |||
btn.addEventListener('click', function(){ vote(iVal); }); | |||
btn.addEventListener('keydown', function(e){ | |||
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); vote(iVal); } | |||
if (e.key === 'ArrowRight' && iVal < scale) buttons[iVal].focus(); | |||
if (e.key === 'ArrowLeft' && iVal > 1) buttons[iVal-2].focus(); | |||
}); | |||
} | |||
widget.appendChild(btn); | |||
buttons.push(btn); | |||
})(i); | |||
} | } | ||
var current = 0; | |||
highlight(current); | highlight(current); | ||
function highlight(n) { | function highlight(n) { | ||
buttons. | for (var j = 0; j < buttons.length; j++) { | ||
var active = (j < n); | |||
buttons[j].classList.toggle('is-active', active); | |||
buttons[j].setAttribute('aria-pressed', active ? 'true' : 'false'); | |||
} | } | ||
} | } | ||
// Stats laden (Ø & Stimmen, eigene Stimme, canVote/see) | // Stats laden (Ø & Stimmen, eigene Stimme, canVote/see) | ||
function updateStats() { | function updateStats() { | ||
var api = new mw.Api(); | |||
api.get({ | api.get({ | ||
action: 'query', | action: 'query', | ||
| Zeile 308: | Zeile 321: | ||
}).done(function (data) { | }).done(function (data) { | ||
try { | try { | ||
var pages = get(data, ['query','pages']) || {}; | |||
var pid = Object.keys(pages)[0]; | |||
var page = pages[pid] || {}; | |||
var pr = page.pagerating; | |||
if (!meta) return; | if (!meta) return; | ||
| Zeile 323: | Zeile 336: | ||
meta.textContent = 'Bewertungen sind verborgen.'; | meta.textContent = 'Bewertungen sind verborgen.'; | ||
} else { | } else { | ||
var hist = pr.pageRating || {}; | |||
var total = 0, sum = 0; | |||
Object.keys(hist).forEach(k | Object.keys(hist).forEach(function(k){ | ||
var s = parseInt(k, 10), c = parseInt(hist[k], 10); | |||
if (!isNaN(s) && !isNaN(c)) { total += c; sum += s * c; } | if (!isNaN(s) && !isNaN(c)) { total += c; sum += s * c; } | ||
}); | }); | ||
| Zeile 341: | Zeile 354: | ||
if (typeof pr.canVote !== 'undefined' && pr.canVote === 0) { | if (typeof pr.canVote !== 'undefined' && pr.canVote === 0) { | ||
box.classList.add('whisky-rating--disabled'); | box.classList.add('whisky-rating--disabled'); | ||
widget.querySelectorAll('.whisky-glass') | var gls = widget.querySelectorAll('.whisky-glass'); | ||
for (var k = 0; k < gls.length; k++) gls[k].disabled = true; | |||
if (!/nicht abstimmen/.test(meta.textContent)) { | if (!/nicht abstimmen/.test(meta.textContent)) { | ||
meta.textContent += (meta.textContent ? ' • ' : '') + 'Du darfst hier nicht abstimmen.'; | meta.textContent += (meta.textContent ? ' • ' : '') + 'Du darfst hier nicht abstimmen.'; | ||
| Zeile 358: | Zeile 372: | ||
// Vote senden | // Vote senden | ||
function vote(value) { | function vote(value) { | ||
var api = new mw.Api(); | |||
if (meta) meta.textContent = 'Wird gespeichert …'; | if (meta) meta.textContent = 'Wird gespeichert …'; | ||
var saved = false; | |||
var failTimer = setTimeout(function () { | |||
if (!saved && meta) meta.textContent = 'Speichern dauert ungewöhnlich lange … bitte Seite neu laden.'; | if (!saved && meta) meta.textContent = 'Speichern dauert ungewöhnlich lange … bitte Seite neu laden.'; | ||
}, 8000); | }, 8000); | ||
| Zeile 381: | Zeile 395: | ||
}).fail(function (xhr) { | }).fail(function (xhr) { | ||
clearTimeout(failTimer); | clearTimeout(failTimer); | ||
var msg = 'Unbekannter Fehler'; | |||
try { | try { | ||
var j = xhr && xhr.responseJSON ? xhr.responseJSON : xhr; | |||
if (j && j.error) { | if (j && j.error) { | ||
msg = (j.error.code ? j.error.code + ': ' : '') + (j.error.info || ''); | msg = (j.error.code ? j.error.code + ': ' : '') + (j.error.info || ''); | ||
| Zeile 398: | Zeile 412: | ||
// ---------- Meta-only Ø für einzelne Kategorien ---------- | // ---------- Meta-only Ø für einzelne Kategorien ---------- | ||
function renderMetaOnly(box) { | function renderMetaOnly(box) { | ||
var pageId = parseInt(box.dataset.ratepagePageid || mw.config.get('wgArticleId'), 10); | |||
var contest = box.dataset.ratepageContest || undefined; | |||
new mw.Api().get({ | new mw.Api().get({ | ||
action: 'query', | action: 'query', | ||
| Zeile 407: | Zeile 421: | ||
format: 'json' | format: 'json' | ||
}).done(function (data) { | }).done(function (data) { | ||
var pages = get(data, ['query','pages']) || {}; | |||
var pid = Object.keys(pages)[0]; | |||
var pr = pages[pid] && pages[pid].pagerating; | |||
if (!pr) { box.textContent = ''; return; } | if (!pr) { box.textContent = ''; return; } | ||
if (typeof pr.canSee !== 'undefined' && pr.canSee === 0) { box.textContent = 'Bewertung verborgen'; return; } | if (typeof pr.canSee !== 'undefined' && pr.canSee === 0) { box.textContent = 'Bewertung verborgen'; return; } | ||
var hist = pr.pageRating || {}; | |||
var total = 0, sum = 0; | |||
Object.keys(hist).forEach(k | Object.keys(hist).forEach(function(k){ | ||
var s = +k, c = +hist[k]; | |||
if (s && c) { total += c; sum += s * c; } | |||
}); | |||
box.textContent = total ? ('Ø ' + (Math.round((sum/total)*10)/10) + ' (' + total + ' Stimmen)') : 'Noch keine Bewertungen'; | box.textContent = total ? ('Ø ' + (Math.round((sum/total)*10)/10) + ' (' + total + ' Stimmen)') : 'Noch keine Bewertungen'; | ||
}); | }); | ||
| Zeile 421: | Zeile 438: | ||
// ---------- Kompakte Ø-Tabelle oben ---------- | // ---------- Kompakte Ø-Tabelle oben ---------- | ||
function renderSummary(container) { | function renderSummary(container) { | ||
var pageId = mw.config.get('wgArticleId'); | |||
var contests = (container.dataset.ratepageContests || 'NASE,GESCHMACK,ABGANG') | |||
.split(','). | .split(','); | ||
for (var i = 0; i < contests.length; i++) contests[i] = contests[i].trim(); | |||
contests = contests.filter(function(s){ return !!s; }); | |||
var labels = { NASE: 'Nase', GESCHMACK: 'Geschmack', ABGANG: 'Abgang' }; | |||
container.textContent = 'Lade Bewertungen …'; | container.textContent = 'Lade Bewertungen …'; | ||
| Zeile 436: | Zeile 455: | ||
format: 'json' | format: 'json' | ||
}).then(function (data) { | }).then(function (data) { | ||
var pages = get(data, ['query','pages']) || {}; | |||
var pid = Object.keys(pages)[0]; | |||
var pr = pages[pid] && pages[pid].pagerating; | |||
if (!pr || (typeof pr.canSee !== 'undefined' && pr.canSee === 0)) { | if (!pr || (typeof pr.canSee !== 'undefined' && pr.canSee === 0)) { | ||
return { contest, total: 0, avg: null, label: labels[contest] || contest }; | return { contest: contest, total: 0, avg: null, label: labels[contest] || contest }; | ||
} | } | ||
var hist = pr.pageRating || {}; | |||
var total = 0, sum = 0; | |||
Object.keys(hist).forEach(k | Object.keys(hist).forEach(function(k){ | ||
var s = +k, c = +hist[k]; | |||
return { contest, total, avg, label: labels[contest] || contest }; | if (s && c) { total += c; sum += s * c; } | ||
}); | |||
var avg = total ? Math.round((sum / total) * 10) / 10 : null; | |||
return { contest: contest, total: total, avg: avg, label: labels[contest] || contest }; | |||
}); | }); | ||
} | } | ||
Promise.all(contests.map(fetchContest)).then(function (rows) { | Promise.all(contests.map(fetchContest)).then(function (rows) { | ||
var table = document.createElement('table'); | |||
table.className = 'whisky-summary__table'; | table.className = 'whisky-summary__table'; | ||
var thead = document.createElement('thead'); | |||
thead.innerHTML = '<tr><th>Kategorie</th><th>Ø</th><th>Stimmen</th></tr>'; | thead.innerHTML = '<tr><th>Kategorie</th><th>Ø</th><th>Stimmen</th></tr>'; | ||
table.appendChild(thead); | table.appendChild(thead); | ||
var tbody = document.createElement('tbody'); | |||
rows. | for (var r = 0; r < rows.length; r++) { | ||
var row = rows[r]; | |||
var avgText = (row.avg !== null) ? row.avg.toFixed(1) : '–'; | |||
var totalText = row.total ? String(row.total) : '0'; | |||
tr.innerHTML = | var tr = document.createElement('tr'); | ||
tr.innerHTML = '<td>' + row.label + '</td><td>' + avgText + '</td><td>' + totalText + '</td>'; | |||
tbody.appendChild(tr); | tbody.appendChild(tr); | ||
} | } | ||
table.appendChild(tbody); | table.appendChild(tbody); | ||
Version vom 5. September 2025, 21:33 Uhr
/* Das folgende JavaScript wird für alle Benutzer geladen. */
// === Skript 1 ===
/* Charity-Popup (v9) – mit zwei Buttons nebeneinander/untereinander je nach Platz */
mw.loader.using(['mediawiki.util', 'jquery']).then(function () {
(function ($, mw) {
'use strict';
var CONFIG = {
enabled: true,
id: 'charity_notice_v9', // Version hochsetzen, damit alle es sehen
title: 'Charity für kinderherzen e.V',
logoUrl: 'https://www.kinderherzen.de/wp-content/uploads/2019/03/kinderherzen.de_orange_RGB.jpg',
html:
'<p>Liebe Whisky, ADoS Heads,</p>' +
'<p>wir wurden jetzt schon gefragt, ob ihr dieses Wiki-Projekt unterstützen könnt.</p>' +
'<p>Wir freuen uns sehr, dass dieses Projekt so gut angenommen wurde.</p>' +
'<p>Wir würden uns daher sehr freuen, wenn ihr anstatt uns, lieber das Projekt vom Whiskywaiter unterstützen würdet.</p>' +
'<p><strong>Jede Spende zählt!</strong></p>' +
'<p><a href="https://www.kinderherzen.de/whisky-fuer-den-guten-zweck/" target="_blank" rel="noopener" class="mw-ui-button">Mehr erfahren</a></p>',
wikiButton: {
text: 'Mehr auf dem Wiki',
url: 'https://ados-wiki.de/index.php?title=Charity-Event_%E2%80%9EWhisky_f%C3%BCr_den_guten_Zweck%E2%80%9C_%E2%80%93_The_Whisky_Waiter'
},
showOnNamespaces: 'all',
dailyLimit: 1,
escToClose: true,
clickBackdropToClose: true
};
if (!CONFIG.enabled) return;
var ns = mw.config.get('wgNamespaceNumber');
if (CONFIG.showOnNamespaces !== 'all' &&
$.isArray(CONFIG.showOnNamespaces) &&
$.inArray(ns, CONFIG.showOnNamespaces) === -1) {
return;
}
var isAnon = mw.config.get('wgUserName') === null;
function storageGet(k){ try { return window.localStorage.getItem(k); } catch (e) { return null; } }
function storageSet(k,v){ try { window.localStorage.setItem(k, v); } catch (e) {} }
var key = 'popup_' + CONFIG.id + (isAnon ? ':anon' : ':user');
var today = (function(d){ return d.getFullYear() + '-' + ('0'+(d.getMonth()+1)).slice(-2) + '-' + ('0'+d.getDate()).slice(-2); })(new Date());
if (storageGet(key) === today) return;
function markSeen(){ storageSet(key, today); }
$(function () {
var $overlay = $('<div>', { 'class': 'mw-popup-overlay' });
var $modal = $('<div>', {
'class': 'mw-popup-modal',
'role': 'dialog',
'aria-modal': 'true',
'aria-labelledby': 'mw-popup-title'
});
var $logoWrap = $('<div>', { 'class': 'mw-popup-logo' })
.append($('<div>', { 'class': 'mw-popup-heart' }))
.append($('<img>', { src: CONFIG.logoUrl, alt: 'Kinderherzen Logo' }));
var $title = $('<h2>', { id: 'mw-popup-title' }).text(CONFIG.title);
var $content = $('<div>', { 'class': 'mw-popup-content' }).html(CONFIG.html);
var $btnOk = $('<button>', { 'class': 'mw-popup-close', type: 'button' }).text('OK');
var $btnWiki = $('<a>', {
'class': 'mw-popup-wiki-button',
'href': CONFIG.wikiButton.url,
'target': '_blank',
'rel': 'noopener'
}).text(CONFIG.wikiButton.text);
var $btnWrapper = $('<div>', { 'class': 'mw-popup-button-row' }).append($btnOk, $btnWiki);
$modal.append($logoWrap, $title, $content, $btnWrapper);
$('body').append($overlay, $modal);
// Fokus setzen
setTimeout(function(){ try { $modal.attr('tabindex','-1').focus(); } catch(e) {} }, 0);
function close() {
markSeen();
$overlay.remove();
$modal.remove();
$(document).off('keydown.mwpopup');
}
$btnOk.on('click', close);
if (CONFIG.clickBackdropToClose) $overlay.on('click', close);
if (CONFIG.escToClose) {
$(document).on('keydown.mwpopup', function (e) {
var key = e.key || e.keyCode;
if (key === 'Escape' || key === 'Esc' || key === 27) {
e.preventDefault();
close();
}
});
}
markSeen();
});
})(jQuery, mw);
});
// === Skript 2 ===
/* Sticky Charity Banner (v1) – MediaWiki
- Fix am oberen Rand
- Schließen mit Erinnerung (session/day/forever)
- Automatisches Ende via endISO
- Links: Extern + interne Wiki-Seite
*/
mw.loader.using(['mediawiki.util', 'jquery']).then(function () {
(function ($, mw) {
'use strict';
var CONFIG = {
enabled: true,
id: 'charity_banner_v1', // Bei inhaltlichen Änderungen erhöhen
text: '❤️ Unterstütze die Kinderherzen-Charity – Jede Spende zählt!',
primary: { // Externer Link
label: 'Mehr erfahren',
href: 'https://www.kinderherzen.de/whisky-fuer-den-guten-zweck/'
},
secondary: { // Interner Wiki-Link
label: 'Mehr auf dem Wiki',
href: 'https://ados-wiki.de/index.php?title=Charity-Event_%E2%80%9EWhisky_f%C3%BCr_den_guten_Zweck%E2%80%9C_%E2%80%93_The_Whisky_Waiter'
},
showOnNamespaces: 'all', // 'all' oder Array z. B. [0,4]
endISO: null, // z. B. '2025-10-06T23:59:59Z' (danach ausgeblendet)
dismiss: { mode: 'day' }, // 'session' | 'day' | 'forever'
zIndex: 10050 // über normalen Headern
};
if (!CONFIG.enabled) return;
// Zeitfenster prüfen
if (CONFIG.endISO && new Date() > new Date(CONFIG.endISO)) return;
// Namespace-Filter
var ns = mw.config.get('wgNamespaceNumber');
if (CONFIG.showOnNamespaces !== 'all' &&
$.isArray(CONFIG.showOnNamespaces) &&
$.inArray(ns, CONFIG.showOnNamespaces) === -1) {
return;
}
// Dismiss-Speicher
function getLS(k){ try { return localStorage.getItem(k); } catch(e){ return null; } }
function setLS(k,v){ try { localStorage.setItem(k,v); } catch(e){} }
var key = 'sticky_banner:' + CONFIG.id;
var today = (function(d){ return d.getFullYear()+'-'+('0'+(d.getMonth()+1)).slice(-2)+'-'+('0'+d.getDate()).slice(-2); })(new Date());
var dismissed = getLS(key);
if (CONFIG.dismiss.mode === 'forever' && dismissed) return;
if (CONFIG.dismiss.mode === 'day' && dismissed === today) return;
// session: nicht in localStorage speichern → immer wieder pro Session möglich
$(function () {
// Banner HTML
var $bar = $('<div>', {
'class': 'mw-sticky-banner',
'role': 'region',
'aria-label': 'Charity-Hinweis',
'style': 'z-index:' + CONFIG.zIndex + ';'
});
var $inner = $('<div>', { 'class': 'mw-sticky-banner__inner' });
var $text = $('<div>', { 'class': 'mw-sticky-banner__text', 'html': mw.html.escape(CONFIG.text) });
// Buttons
var $btns = $('<div>', { 'class': 'mw-sticky-banner__btns' });
if (CONFIG.primary && CONFIG.primary.href) {
$btns.append($('<a>', {
'class': 'mw-sticky-banner__btn mw-sticky-banner__btn--primary',
'href': CONFIG.primary.href,
'target': '_blank',
'rel': 'noopener'
}).text(CONFIG.primary.label || 'Mehr erfahren'));
}
if (CONFIG.secondary && CONFIG.secondary.href) {
$btns.append($('<a>', {
'class': 'mw-sticky-banner__btn mw-sticky-banner__btn--secondary',
'href': CONFIG.secondary.href
}).text(CONFIG.secondary.label || 'Mehr auf dem Wiki'));
}
var $close = $('<button>', {
'class': 'mw-sticky-banner__close',
'type': 'button',
'aria-label': 'Banner schließen'
}).text('×');
$inner.append($text, $btns, $close);
$bar.append($inner);
$('body').prepend($bar);
$('body').addClass('mw-sticky-banner-active');
function rememberDismiss() {
if (CONFIG.dismiss.mode === 'forever') { setLS(key, '1'); }
else if (CONFIG.dismiss.mode === 'day') { setLS(key, today); }
// session → nichts speichern
}
function close() {
rememberDismiss();
$bar.remove();
$('body').removeClass('mw-sticky-banner-active');
}
$close.on('click', close);
});
})(jQuery, mw);
});
/* ADOS Whisky-Ratings – RatePage Frontend (robust + Stats + Summary, ohne Optional Chaining) */
mw.loader.using(['mediawiki.api', 'mediawiki.user']).then(function () {
// ---------- Hilfsfunktionen ----------
function get(obj, pathArray) {
var cur = obj;
for (var i = 0; i < pathArray.length; i++) {
if (!cur || typeof cur !== 'object') return undefined;
cur = cur[pathArray[i]];
}
return cur;
}
// ---------- Initialisierung ----------
function boot(root) {
var scope = root || document;
var items = scope.querySelectorAll('.whisky-rating__item');
for (var i = 0; i < items.length; i++) setupWidget(items[i]);
var summaries = scope.querySelectorAll('[data-ratepage-summary="true"]');
for (i = 0; i < summaries.length; i++) renderSummary(summaries[i]);
var metaOnly = scope.querySelectorAll('.whisky-rating__meta-only');
for (i = 0; i < metaOnly.length; i++) renderMetaOnly(metaOnly[i]);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function(){ boot(document); });
} else {
boot(document);
}
mw.hook('wikipage.content').add(function($content) {
if ($content && $content[0]) boot($content[0]);
});
// ---------- Widget (Gläser) ----------
function setupWidget(box) {
var pageId = mw.config.get('wgArticleId');
var contest = box.dataset.ratepageContest || undefined; // "NASE" | "GESCHMACK" | "ABGANG"
var scale = parseInt(box.dataset.ratepageScale || '10', 10);
var widget = box.querySelector('.whisky-rating__widget');
var meta = box.querySelector('.whisky-rating__meta');
var isAnon = mw.user.isAnon();
if (isAnon) {
box.classList.add('whisky-rating--disabled');
if (meta) meta.textContent = 'Bitte einloggen, um zu bewerten.';
}
// Buttons bauen
var buttons = [];
for (var i = 1; i <= scale; i++) {
(function(iVal){
var btn = document.createElement('button');
btn.type = 'button';
btn.className = 'whisky-glass';
btn.setAttribute('aria-label', iVal + ' von ' + scale);
btn.setAttribute('aria-pressed', 'false');
if (isAnon) {
btn.disabled = true;
btn.title = 'Nur für registrierte Benutzer';
} else {
btn.title = iVal + ' / ' + scale;
btn.addEventListener('mouseenter', function(){ highlight(iVal); });
btn.addEventListener('mouseleave', function(){ highlight(current); });
btn.addEventListener('click', function(){ vote(iVal); });
btn.addEventListener('keydown', function(e){
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); vote(iVal); }
if (e.key === 'ArrowRight' && iVal < scale) buttons[iVal].focus();
if (e.key === 'ArrowLeft' && iVal > 1) buttons[iVal-2].focus();
});
}
widget.appendChild(btn);
buttons.push(btn);
})(i);
}
var current = 0;
highlight(current);
function highlight(n) {
for (var j = 0; j < buttons.length; j++) {
var active = (j < n);
buttons[j].classList.toggle('is-active', active);
buttons[j].setAttribute('aria-pressed', active ? 'true' : 'false');
}
}
// Stats laden (Ø & Stimmen, eigene Stimme, canVote/see)
function updateStats() {
var api = new mw.Api();
api.get({
action: 'query',
prop: 'pagerating',
pageids: pageId,
prcontest: contest || undefined,
format: 'json'
}).done(function (data) {
try {
var pages = get(data, ['query','pages']) || {};
var pid = Object.keys(pages)[0];
var page = pages[pid] || {};
var pr = page.pagerating;
if (!meta) return;
if (!pr) {
if (!meta.textContent) meta.textContent = 'Noch keine Bewertungen';
return;
}
if (typeof pr.canSee !== 'undefined' && pr.canSee === 0) {
meta.textContent = 'Bewertungen sind verborgen.';
} else {
var hist = pr.pageRating || {};
var total = 0, sum = 0;
Object.keys(hist).forEach(function(k){
var s = parseInt(k, 10), c = parseInt(hist[k], 10);
if (!isNaN(s) && !isNaN(c)) { total += c; sum += s * c; }
});
meta.textContent = total
? ('Ø ' + (Math.round((sum/total)*10)/10) + ' (' + total + ' Stimmen)')
: 'Noch keine Bewertungen';
}
if (pr.userVote) {
current = pr.userVote;
highlight(current);
}
if (typeof pr.canVote !== 'undefined' && pr.canVote === 0) {
box.classList.add('whisky-rating--disabled');
var gls = widget.querySelectorAll('.whisky-glass');
for (var k = 0; k < gls.length; k++) gls[k].disabled = true;
if (!/nicht abstimmen/.test(meta.textContent)) {
meta.textContent += (meta.textContent ? ' • ' : '') + 'Du darfst hier nicht abstimmen.';
}
}
} catch (e) {
console.error(e);
if (meta && !meta.textContent) meta.textContent = 'Bewertungen konnten nicht geladen werden.';
}
}).fail(function (xhr) {
console.error('Pagerating-Load-Error', xhr);
if (meta && !meta.textContent) meta.textContent = 'Bewertungen konnten nicht geladen werden.';
});
}
// Vote senden
function vote(value) {
var api = new mw.Api();
if (meta) meta.textContent = 'Wird gespeichert …';
var saved = false;
var failTimer = setTimeout(function () {
if (!saved && meta) meta.textContent = 'Speichern dauert ungewöhnlich lange … bitte Seite neu laden.';
}, 8000);
api.postWithToken('csrf', {
action: 'ratepage',
pageid: pageId,
answer: value,
contest: contest || undefined,
format: 'json'
}).done(function () {
saved = true;
clearTimeout(failTimer);
current = value;
highlight(current);
if (meta) meta.textContent = 'Danke! Deine Bewertung: ' + value + ' / ' + scale;
updateStats();
}).fail(function (xhr) {
clearTimeout(failTimer);
var msg = 'Unbekannter Fehler';
try {
var j = xhr && xhr.responseJSON ? xhr.responseJSON : xhr;
if (j && j.error) {
msg = (j.error.code ? j.error.code + ': ' : '') + (j.error.info || '');
}
} catch(e){}
console.error('RatePage-API-Fehler:', xhr);
if (meta) meta.textContent = 'Speichern fehlgeschlagen: ' + msg;
});
}
updateStats();
}
// ---------- Meta-only Ø für einzelne Kategorien ----------
function renderMetaOnly(box) {
var pageId = parseInt(box.dataset.ratepagePageid || mw.config.get('wgArticleId'), 10);
var contest = box.dataset.ratepageContest || undefined;
new mw.Api().get({
action: 'query',
prop: 'pagerating',
pageids: pageId,
prcontest: contest || undefined,
format: 'json'
}).done(function (data) {
var pages = get(data, ['query','pages']) || {};
var pid = Object.keys(pages)[0];
var pr = pages[pid] && pages[pid].pagerating;
if (!pr) { box.textContent = ''; return; }
if (typeof pr.canSee !== 'undefined' && pr.canSee === 0) { box.textContent = 'Bewertung verborgen'; return; }
var hist = pr.pageRating || {};
var total = 0, sum = 0;
Object.keys(hist).forEach(function(k){
var s = +k, c = +hist[k];
if (s && c) { total += c; sum += s * c; }
});
box.textContent = total ? ('Ø ' + (Math.round((sum/total)*10)/10) + ' (' + total + ' Stimmen)') : 'Noch keine Bewertungen';
});
}
// ---------- Kompakte Ø-Tabelle oben ----------
function renderSummary(container) {
var pageId = mw.config.get('wgArticleId');
var contests = (container.dataset.ratepageContests || 'NASE,GESCHMACK,ABGANG')
.split(',');
for (var i = 0; i < contests.length; i++) contests[i] = contests[i].trim();
contests = contests.filter(function(s){ return !!s; });
var 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) {
var pages = get(data, ['query','pages']) || {};
var pid = Object.keys(pages)[0];
var pr = pages[pid] && pages[pid].pagerating;
if (!pr || (typeof pr.canSee !== 'undefined' && pr.canSee === 0)) {
return { contest: contest, total: 0, avg: null, label: labels[contest] || contest };
}
var hist = pr.pageRating || {};
var total = 0, sum = 0;
Object.keys(hist).forEach(function(k){
var s = +k, c = +hist[k];
if (s && c) { total += c; sum += s * c; }
});
var avg = total ? Math.round((sum / total) * 10) / 10 : null;
return { contest: contest, total: total, avg: avg, label: labels[contest] || contest };
});
}
Promise.all(contests.map(fetchContest)).then(function (rows) {
var table = document.createElement('table');
table.className = 'whisky-summary__table';
var thead = document.createElement('thead');
thead.innerHTML = '<tr><th>Kategorie</th><th>Ø</th><th>Stimmen</th></tr>';
table.appendChild(thead);
var tbody = document.createElement('tbody');
for (var r = 0; r < rows.length; r++) {
var row = rows[r];
var avgText = (row.avg !== null) ? row.avg.toFixed(1) : '–';
var totalText = row.total ? String(row.total) : '0';
var tr = document.createElement('tr');
tr.innerHTML = '<td>' + row.label + '</td><td>' + avgText + '</td><td>' + totalText + '</td>';
tbody.appendChild(tr);
}
table.appendChild(tbody);
container.replaceChildren(table);
});
}
});