MediaWiki:Common.js: Unterschied zwischen den Versionen
Admin (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung Markierung: Zurückgesetzt |
Admin (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
||
| (167 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) | |||
| Zeile 1: | Zeile 1: | ||
/* Das folgende JavaScript wird für alle Benutzer geladen. */ | /* Das folgende JavaScript wird für alle Benutzer geladen. */ | ||
/* ADOS Whisky-Ratings – RatePage Frontend (ES5, Widgets + Stats + Summary, Doppel-Init-Schutz; ANON VOTING ERLAUBT) */ | |||
/* | mw.loader.using(['mediawiki.api', 'mediawiki.user']).then(function () { | ||
mw.loader.using(['mediawiki. | |||
// ---------- kleine Hilfsfunktion ---------- | |||
function get(obj, path) { | |||
var cur = obj, i; | |||
for (i = 0; i < path.length; i++) { | |||
if (!cur || typeof cur !== 'object') return undefined; | |||
cur = cur[path[i]]; | |||
} | |||
return cur; | |||
} | |||
// ---------- Bootstrapping ---------- | |||
function boot(root) { | |||
var scope = root || document; | |||
var i, nodes; | |||
nodes = scope.querySelectorAll('.whisky-rating__item'); | |||
for (i = 0; i < nodes.length; i++) setupWidget(nodes[i]); | |||
initMetaOnly(scope); | |||
nodes = scope.querySelectorAll('[data-ratepage-summary="true"]'); | |||
for (i = 0; i < nodes.length; i++) renderSummary(nodes[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]); | |||
}); | |||
// ---------- Interaktives Widget ---------- | |||
function setupWidget(box) { | |||
if (box.getAttribute('data-rating-init') === '1') return; | |||
box.setAttribute('data-rating-init', '1'); | |||
var pageId = mw.config.get('wgArticleId'); | |||
var contest = box.dataset.ratepageContest || undefined; | |||
var scale = parseInt(box.dataset.ratepageScale || '10', 10); | |||
var widget = box.querySelector('.whisky-rating__widget'); | |||
var meta = box.querySelector('.whisky-rating__meta'); | |||
while (widget.firstChild) widget.removeChild(widget.firstChild); | |||
// Anonyme NICHT blockieren – nur optionaler Hinweis | |||
var isAnon = mw.user.isAnon(); | |||
if (isAnon && meta && !meta.textContent) { | |||
meta.textContent = 'Bewerte diesen Whisky!'; | |||
} | |||
function | var buttons = []; | ||
var i; | |||
for (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'); | |||
// Immer klickbar – egal ob anonym oder eingeloggt | |||
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) { | |||
var j; | |||
for (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'); | |||
} | } | ||
} | |||
function updateStats() { | |||
var api = new mw.Api(); | |||
api.get({ | |||
} | action: 'query', | ||
prop: 'pagerating', | |||
pageids: pageId, | |||
prcontest: contest || undefined, | |||
format: 'json', | |||
errorformat: 'plaintext' | |||
}).done(function (data) { | |||
try { | |||
var pages = get(data, ['query','pages']) || {}; | |||
var keys = []; for (var k in pages) if (Object.prototype.hasOwnProperty.call(pages, k)) keys.push(k); | |||
var pid = keys.length ? keys[0] : String(pageId); | |||
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; | |||
for (var key in hist) { | |||
if (Object.prototype.hasOwnProperty.call(hist, key)) { | |||
var s = parseInt(key, 10), c = parseInt(hist[key], 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) { | |||
// Serverseitig verboten → hier deaktivieren | |||
box.classList.add('whisky-rating--disabled'); | |||
var gls = widget.querySelectorAll('.whisky-glass'); | |||
for (var i2 = 0; i2 < gls.length; i2++) gls[i2].disabled = true; | |||
if (meta.textContent.indexOf('nicht abstimmen') === -1) { | |||
meta.textContent += (meta.textContent ? ' • ' : '') + 'Du darfst hier nicht abstimmen.'; | |||
} | |||
} | |||
} catch (e) { | |||
if (window.console && console.error) console.error(e); | |||
} | if (meta && !meta.textContent) meta.textContent = 'Bewertungen konnten nicht geladen werden.'; | ||
} | |||
}).fail(function (xhr) { | |||
if (window.console && console.error) console.error('Pagerating-Load-Error', xhr); | |||
if (meta && !meta.textContent) meta.textContent = 'Bewertungen konnten nicht geladen werden.'; | |||
}); | |||
} | |||
if ( | 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){} | |||
if (window.console && console.error) console.error('RatePage-API-Fehler:', xhr); | |||
if (meta) meta.textContent = 'Speichern fehlgeschlagen: ' + msg; | |||
}); | |||
} | } | ||
// | updateStats(); | ||
} | |||
var | // ---------- Meta-only ---------- | ||
function initMetaOnly(scope) { | |||
var root = scope || document; | |||
var nodes = root.querySelectorAll('.whisky-rating__meta-only'); | |||
var i; | |||
for (i = 0; i < nodes.length; i++) (function(box){ | |||
if (box.getAttribute('data-meta-init') === '1') return; | |||
box.setAttribute('data-meta-init', '1'); | |||
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 keys = []; for (var k in pages) if (Object.prototype.hasOwnProperty.call(pages,k)) keys.push(k); | |||
var pid = keys.length ? keys[0] : String(pageId); | |||
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; | |||
for (var key in hist) { | |||
if (Object.prototype.hasOwnProperty.call(hist, key)) { | |||
var s = Number(key), c = Number(hist[key]); | |||
if (s && c) { total += c; sum += s * c; } | |||
} | |||
} | |||
box.textContent = total ? ('Ø ' + (Math.round((sum/total)*10)/10) + ' (' + total + ' Stimmen)') : 'Noch keine Bewertungen'; | |||
}); | }); | ||
})(nodes[i]); | |||
} | |||
// ---------- Summary inkl. Gesamt + Balken ---------- | |||
function renderSummary(container) { | |||
if (container.getAttribute('data-summary-init') === '1') return; | |||
container.setAttribute('data-summary-init', '1'); | |||
var pageId = mw.config.get('wgArticleId'); | |||
var raw = container.dataset.ratepageContests || 'NASE,GESCHMACK,ABGANG'; | |||
var parts = raw.split(','); | |||
var i; | |||
for (i = 0; i < parts.length; i++) parts[i] = parts[i].replace(/^\s+|\s+$/g, ''); | |||
var nameToId = { 'nase':'NASE', 'geschmack':'GESCHMACK', 'abgang':'ABGANG', 'gesamteindruck':'GESAMTEINDRUCK' }; | |||
var contests = []; | |||
var seen = {}; | |||
for (i = 0; i < parts.length; i++) { | |||
var key = parts[i]; if (!key) continue; | |||
var norm = key.toLowerCase(); | |||
var id = nameToId[norm] ? nameToId[norm] : key; | |||
if (!seen[id]) { contests.push(id); seen[id] = true; } | |||
} | |||
var labels = { NASE: 'Nase', GESCHMACK: 'Geschmack', ABGANG: 'Abgang', GESAMTEINDRUCK: 'Gesamteindruck' }; | |||
container.textContent = 'Lade Bewertungen …'; | |||
function fetchContest(contest) { | |||
return new mw.Api().get({ | |||
action: 'query', | |||
prop: 'pagerating', | |||
pageids: pageId, | |||
prcontest: contest, | |||
format: 'json', | |||
errorformat: 'plaintext' | |||
}).then(function (data) { | |||
var pages = get(data, ['query','pages']) || {}; | |||
var keys = []; for (var k in pages) if (Object.prototype.hasOwnProperty.call(pages,k)) keys.push(k); | |||
var pid = keys.length ? keys[0] : String(pageId); | |||
var pr = pages[pid] && pages[pid].pagerating; | |||
if (!pr || (typeof pr.canSee !== 'undefined' && pr.canSee === 0)) { | |||
return { contest: contest, label: (labels[contest] || contest), avg: null, total: 0 }; | |||
} | |||
var hist = pr.pageRating || {}; | |||
var total = 0, sum = 0; | |||
for (var key in hist) { | |||
if (Object.prototype.hasOwnProperty.call(hist, key)) { | |||
var s = Number(key), c = Number(hist[key]); | |||
if (s && c) { total += c; sum += s * c; } | |||
} | |||
} | |||
var avg = total ? Math.round((sum / total) * 10) / 10 : null; | |||
return { contest: contest, label: (labels[contest] || contest), avg: avg, total: total }; | |||
}, function () { | |||
return { contest: contest, label: (labels[contest] || contest), avg: null, total: 0, _error: true }; | |||
}); | |||
} | |||
var promises = []; | |||
for (i = 0; i < contests.length; i++) promises.push(fetchContest(contests[i])); | |||
Promise.all(promises).then(function (rows) { | |||
if (!rows || !rows.length) { | |||
container.textContent = 'Konnte Bewertungen nicht laden.'; | |||
return; | |||
} | } | ||
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'); | |||
// Zeilen mit Balken | |||
var r; | |||
for (r = 0; r < rows.length; r++) { | |||
var row = rows[r]; | |||
var totalText = row.total ? String(row.total) : '0'; | |||
var tr = document.createElement('tr'); | |||
var tdLabel = document.createElement('td'); | |||
tdLabel.textContent = row.label; | |||
tr.appendChild(tdLabel); | |||
var tdAvg = document.createElement('td'); | |||
if (row.avg !== null) { | |||
var wrap = document.createElement('div'); wrap.className = 'whisky-bar'; | |||
var track = document.createElement('div'); track.className = 'whisky-bar__track'; | |||
var fill = document.createElement('div'); fill.className = 'whisky-bar__fill'; | |||
fill.style.width = Math.max(0, Math.min(100, (row.avg/10)*100)) + '%'; | |||
var val = document.createElement('span'); val.className = 'whisky-bar__value'; | |||
val.textContent = (row.avg.toFixed ? row.avg.toFixed(1) : (Math.round(row.avg*10)/10)); | |||
track.appendChild(fill); wrap.appendChild(track); wrap.appendChild(val); tdAvg.appendChild(wrap); | |||
} else { | |||
tdAvg.textContent = '–'; | |||
} | |||
tr.appendChild(tdAvg); | |||
var tdCnt = document.createElement('td'); | |||
tdCnt.textContent = totalText; | |||
tr.appendChild(tdCnt); | |||
tbody.appendChild(tr); | |||
} | } | ||
var | // Gesamt | ||
var present = 0, sumAvg = 0, totalVotes = 0; | |||
for (r = 0; r < rows.length; r++) { | |||
if (rows[r].avg !== null) { present++; sumAvg += rows[r].avg; } | |||
if (rows[r].total) totalVotes += rows[r].total; | |||
} | |||
var overall = (present > 0) ? Math.round((sumAvg / present) * 10) / 10 : null; | |||
var overallText = (overall !== null) | |||
? (overall.toFixed ? overall.toFixed(1) : (Math.round(overall*10)/10)) | |||
: '–'; | |||
var trG = document.createElement('tr'); | |||
var tdGL = document.createElement('td'); | |||
tdGL.innerHTML = '<strong>Gesamt</strong>'; | |||
trG.appendChild(tdGL); | |||
/ | |||
var tdGA = document.createElement('td'); | |||
if (overall !== null) { | |||
var w = document.createElement('div'); w.className = 'whisky-bar'; | |||
var t = document.createElement('div'); t.className = 'whisky-bar__track'; | |||
var f = document.createElement('div'); f.className = 'whisky-bar__fill'; | |||
f.style.width = Math.max(0, Math.min(100, (overall/10)*100)) + '%'; | |||
var v = document.createElement('span'); v.className = 'whisky-bar__value'; | |||
v.innerHTML = '<strong>' + overallText + '</strong>'; | |||
t.appendChild(f); w.appendChild(t); w.appendChild(v); tdGA.appendChild(w); | |||
} else { | |||
tdGA.innerHTML = '<strong>–</strong>'; | |||
} | } | ||
trG.appendChild(tdGA); | |||
var tdGD = document.createElement('td'); | |||
tdGD.textContent = totalVotes; | |||
trG.appendChild(tdGD); | |||
tbody.appendChild(trG); | |||
table.appendChild(tbody); | |||
while (container.firstChild) container.removeChild(container.firstChild); | |||
container.appendChild(table); | |||
var badge = document.getElementById('whisky-overall-badge'); | |||
if (badge && overall !== null) { | |||
badge.textContent = overallText; | |||
} | } | ||
}).catch(function(){ | |||
container.textContent = 'Konnte Bewertungen nicht geladen werden.'; | |||
}); | |||
} | |||
}); | }); | ||
/* --- Whisky Top-5 (multi-root, recursive, robust, namespaces, ES5) ------- */ | |||
mw.loader.using(['mediawiki.api']).then(function () { | |||
/* ========== Utils ========== */ | |||
function get(obj, path) { | |||
var cur = obj, i; | |||
for (i = 0; i < path.length; i++) { if (!cur || typeof cur !== 'object') return; cur = cur[path[i]]; } | |||
return cur; | |||
} | |||
function parseWeights(raw, contests) { | |||
var map = {}, parts = (raw || '').split(','), i; | |||
for (i = 0; i < parts.length; i++) { | |||
var kv = parts[i].split(':'); if (kv.length !== 2) continue; | |||
var k = kv[0].replace(/^\s+|\s+$/g, ''), v = parseFloat(kv[1]); | |||
if (!isNaN(v)) map[k] = v; | |||
} | |||
for (i = 0; i < contests.length; i++) if (typeof map[contests[i]] !== 'number') map[contests[i]] = 1; | |||
return map; | |||
} | |||
/* ========== Statusbox im Widget ========== */ | |||
function makeStatus(container, keep){ | |||
var box = container.querySelector('.whisky-top5__status'); | |||
if (!box) { | |||
box = document.createElement('div'); | |||
box.className = 'whisky-top5__status'; | |||
container.insertBefore(box, container.firstChild || null); | |||
} | |||
function line(txt){ var row = document.createElement('div'); row.textContent = txt; box.appendChild(row); box.scrollTop = box.scrollHeight; } | |||
function status(msg, append){ if (!append && !keep) box.innerHTML = ''; line(msg); /* console.log('[Top5]', msg); */ } | |||
status.done = function(delayMs){ if (keep) return; setTimeout(function(){ if (box && box.parentNode) box.parentNode.removeChild(box); }, typeof delayMs==='number'?delayMs:3000); }; | |||
return status; | |||
} | |||
/* ========== Robuste Kategorie-Auflösung ========== */ | |||
function categoryCandidates(name){ | |||
var n = (name || '').replace(/^\s+|\s+$/g,''); | |||
var v = {}, add=function(s){ if (s && !v[s]) v[s]=1; }; | |||
add(n); | |||
add(n.replace(/\u2026/g,'...')); // … -> ... | |||
add(n.replace(/\.{3}/g,'…')); // ... -> … | |||
add(n.replace(/\u2013/g,'-')); // – -> - | |||
add(n.replace(/-/g,'–')); // - -> – | |||
add(n.replace(/\s+/g,' ')); // Mehrfach-Spaces | |||
return Object.keys(v); | |||
} | |||
function resolveCategoryTitle(api, rawName){ | |||
var cands = categoryCandidates(rawName).map(function(n){ return 'Kategorie:' + n; }); | |||
return api.get({ action:'query', titles: cands.join('|'), format:'json' }).then(function(d){ | |||
var pages = get(d,['query','pages']) || {}, pid, p; | |||
for (pid in pages) if (Object.prototype.hasOwnProperty.call(pages,pid)) { p = pages[pid]; if (p && p.pageid && p.ns === 14) return p.title; } | |||
return null; | |||
}); | |||
} | |||
/* | /* ========== Kategorien rekursiv einsammeln (inkl. Subkats, Namespaces) ========== */ | ||
function fetchCategoryMembersRecursiveSingleResolved(api, catTitle, limit, outSet, pages, nsStr){ | |||
var visited = {}, queue = [catTitle]; | |||
var cmNS = (nsStr && nsStr.trim()) ? nsStr.trim() : '0|14'; // default: Artikel+Kategorie | |||
function fetchOne(title, cont){ | |||
var params = { | |||
action: 'query', | |||
list: 'categorymembers', | |||
cmtitle: title, | |||
cmtype: 'page|subcat', | |||
cmlimit: Math.min(200, limit), | |||
format: 'json' | |||
}; | |||
if (cmNS !== '*' && cmNS !== '') { | |||
params.cmnamespace = cmNS; // z.B. "0|102|14" | |||
} | |||
if (cont) params.cmcontinue = cont; | |||
return api.get(params).then(function(d){ | |||
var cms = (d.query && d.query.categorymembers) || [], i, it; | |||
for (i = 0; i < cms.length; i++) { | |||
it = cms[i]; | |||
if (it.ns !== 14) { // alles außer Kategorien zählt als Seite | |||
var pid = String(it.pageid); | |||
if (!outSet[pid] && pages.length < limit) { | |||
outSet[pid] = 1; | |||
pages.push({ pageid: pid, title: it.title }); | |||
} | |||
} else { | |||
var sub = it.title; | |||
if (!visited[sub]) { visited[sub] = 1; queue.push(sub); } | |||
} | |||
} | |||
var next = d.continue && d.continue.cmcontinue; | |||
if (next && pages.length < limit) return fetchOne(title, next); | |||
}); | |||
} | |||
function | function loop(){ | ||
if (pages.length >= limit || !queue.length) return Promise.resolve(); | |||
var next = queue.shift(); | |||
if (visited[next]) return loop(); | |||
visited[next] = 1; | |||
return fetchOne(next).then(loop); | |||
} | } | ||
return loop(); | |||
} | |||
function fetchCategoryMembersRecursiveMulti(rootCats, limit, status, nsStr){ | |||
var api = new mw.Api(); | |||
var pages = [], outSet = {}; | |||
var idx = 0; | |||
function next(){ | |||
if (idx >= rootCats.length || pages.length >= limit) return Promise.resolve(pages); | |||
var raw = rootCats[idx++]; if (!raw) return next(); | |||
return resolveCategoryTitle(api, raw).then(function(resolved){ | |||
if (!resolved) { status('Kategorie nicht gefunden: "' + raw + '"', true); return next(); } | |||
status('Kategorie erkannt: ' + resolved + ' – sammle …', true); | |||
var before = pages.length; | |||
return fetchCategoryMembersRecursiveSingleResolved(api, resolved, limit, outSet, pages, nsStr).then(function(){ | |||
var added = pages.length - before; | |||
status('→ gefunden in "' + resolved + '": ' + added + ' Seiten (kumuliert: ' + pages.length + ')', true); | |||
return next(); | |||
}); | |||
}); | |||
} | |||
return next(); | |||
} | } | ||
// | /* ========== Ratings laden / auswerten ========== */ | ||
function fetchRatingsForContest(pageIds, contest, includeHidden) { | |||
var api = new mw.Api(), res = {}, i, chunk = 50, chunks = []; | |||
for (i = 0; i < pageIds.length; i += chunk) chunks.push(pageIds.slice(i, i + chunk)); | |||
function step(idx) { | |||
if (idx >= chunks.length) return Promise.resolve(res); | |||
var ids = chunks[idx]; | |||
return api.get({ | |||
action:'query', prop:'pagerating', pageids: ids.join('|'), | |||
prcontest: contest, format:'json' | |||
}).then(function(d){ | |||
var pages = get(d,['query','pages']) || {}, pid, pr, hist, k, total, sum, s, c; | |||
for (pid in pages) if (Object.prototype.hasOwnProperty.call(pages,pid)) { | |||
if (!res[pid]) res[pid] = { avg:null, total:0 }; | |||
pr = pages[pid].pagerating; | |||
if (pr && (includeHidden || !('canSee' in pr) || pr.canSee !== 0)) { | |||
hist = pr.pageRating || {}; total=0; sum=0; | |||
for (k in hist) if (Object.prototype.hasOwnProperty.call(hist,k)) { | |||
s = parseInt(k,10); c = parseInt(hist[k],10); | |||
if (!isNaN(s) && !isNaN(c)) { total += c; sum += s*c; } | |||
} | |||
if (total > 0) res[pid] = { avg: Math.round((sum/total)*10)/10, total: total }; | |||
} | |||
} | |||
return step(idx + 1); | |||
}, function(){ return step(idx + 1); }); | |||
} | |||
return step(0); | |||
} | |||
function computeOverall(entry, contests, weights) { | |||
var wSum=0, wAvgSum=0, present=0, totalVotes=0, i, sc, w; | |||
for (i=0;i<contests.length;i++){ | |||
sc = entry.scores[contests[i]]; | |||
if (sc && sc.avg !== null) { w = (typeof weights[contests[i]]==='number') ? weights[contests[i]] : 1; wSum+=w; wAvgSum+=sc.avg*w; present++; } | |||
if (sc && sc.total) totalVotes += sc.total; | |||
} | } | ||
entry.totalVotes = totalVotes; | |||
entry.overall = (present>0 && wSum>0) ? Math.round((wAvgSum/wSum)*10)/10 : null; | |||
} | } | ||
/* ========== Render ========== */ | |||
function renderTopN(container, rows, N, minVotes) { | |||
var keep = (container.getAttribute && container.getAttribute('data-keep-status') === 'true'); | |||
var statusBox = keep ? container.querySelector('.whisky-top5__status') : null; | |||
rows = rows.filter(function(r){ return (r.overall !== null) && (r.totalVotes >= minVotes); }); | |||
rows.sort(function(a,b){ | |||
if (a.overall===null && b.overall!==null) return 1; | |||
b. | if (a.overall!==null && b.overall===null) return -1; | ||
b. | if (b.overall!==a.overall) return b.overall - a.overall; | ||
if (b.totalVotes!==a.totalVotes) return b.totalVotes - a.totalVotes; | |||
return a.title.localeCompare(b.title); | |||
}); | }); | ||
rows = rows.slice(0, N); | |||
while (container.firstChild) container.removeChild(container.firstChild); | |||
if (statusBox) container.appendChild(statusBox); | |||
if (!rows.length) { container.appendChild(document.createTextNode('Noch keine Bewertungen vorhanden.')); return; } | |||
var frag = document.createDocumentFragment(), i, r, item, rank, name, a, right, mini, track, fill, val, votes; | |||
for (i=0;i<rows.length;i++){ | |||
r = rows[i]; | |||
item = document.createElement('div'); item.className = 'whisky-top5__item'; | |||
rank = document.createElement('div'); rank.className = 'whisky-top5__rank'; rank.textContent = (i+1); | |||
name = document.createElement('div'); name.className = 'whisky-top5__name'; | |||
a = document.createElement('a'); a.href = mw.util.getUrl(r.title); a.textContent = r.title; name.appendChild(a); | |||
right = document.createElement('div'); right.style.minWidth = '160px'; | |||
mini = document.createElement('div'); mini.className = 'whisky-mini'; | |||
track = document.createElement('div'); track.className = 'whisky-mini__track'; | |||
fill = document.createElement('div'); fill.className = 'whisky-mini__fill'; | |||
fill.style.width = Math.max(0, Math.min(100, (r.overall/10)*100)) + '%'; | |||
val = document.createElement('span'); val.className = 'whisky-mini__val'; | |||
val.textContent = (r.overall.toFixed ? r.overall.toFixed(1) : (Math.round(r.overall*10)/10)); | |||
track.appendChild(fill); mini.appendChild(track); mini.appendChild(val); | |||
votes = document.createElement('div'); votes.className = 'whisky-top5__votes'; | |||
votes.textContent = r.totalVotes + ' Stimmen'; | |||
right.appendChild(mini); right.appendChild(votes); | |||
item.appendChild(rank); item.appendChild(name); item.appendChild(right); | |||
frag.appendChild(item); | |||
} | |||
container.appendChild(frag); | |||
} | } | ||
// | /* ========== Boot ========== */ | ||
function | function bootTop5(root) { | ||
var nodes = (root || document).querySelectorAll('.whisky-top5'); | |||
if (!nodes.length) return; | |||
for (var n=0;n<nodes.length;n++) (function(container){ | |||
if (container.getAttribute('data-top5-init')==='1') return; | |||
container.setAttribute('data-top5-init','1'); | |||
var rawCats = container.getAttribute('data-categories') || container.getAttribute('data-category') || ''; | |||
var parts = rawCats.split(/\n|;/), rootCats = [], i; | |||
for (i=0;i<parts.length;i++){ var nm = parts[i].replace(/^\s+|\s+$/g,''); if (nm) rootCats.push(nm); } | |||
if (!rootCats.length) { container.textContent = 'Keine Kategorien angegeben.'; return; } | |||
if (! | |||
var lim = parseInt(container.getAttribute('data-limit') || '2000', 10); | |||
var cnt = parseInt(container.getAttribute('data-count') || '5', 10); | |||
var minVotes = parseInt(container.getAttribute('data-min-votes') || '1', 10); | |||
var includeHidden = (container.getAttribute('data-include-hidden') === 'true'); | |||
var nsStr = container.getAttribute('data-namespaces') || '0|14'; | |||
var rawC = container.getAttribute('data-contests') || 'NASE,GESCHMACK,ABGANG,GESAMTEINDRUCK'; | |||
var cParts = rawC.split(','), contests = [], seen = {}; | |||
for (i=0;i<cParts.length;i++){ var c = cParts[i].replace(/^\s+|\s+$/g,''); if (c && !seen[c]){ contests.push(c); seen[c]=1; } } | |||
var weights = parseWeights(container.getAttribute('data-weights') || '', contests); | |||
var keep = (container.getAttribute('data-keep-status') === 'true'); | |||
function status(){ /* Debug-Ausgabe deaktiviert */ } | |||
status('Sammle Seiten …'); | |||
fetchCategoryMembersRecursiveMulti(rootCats, lim, status, nsStr).then(function(members){ | |||
status('Gefundene Seiten gesamt: ' + (members ? members.length : 0) + ' – lade Bewertungen …', true); | |||
if (!members || !members.length) { status('Keine passenden Seiten gefunden.', true); return; } | |||
var pageIds = [], byId = {}, i; | |||
for (i=0;i<members.length;i++){ pageIds.push(members[i].pageid); byId[members[i].pageid] = { pageid: members[i].pageid, title: members[i].title, scores: {} }; } | |||
function loopContest(idx){ | |||
if (idx >= contests.length) return Promise.resolve(); | |||
var contest = contests[idx]; | |||
return fetchRatingsForContest(pageIds, contest, includeHidden).then(function(map){ | |||
var pid; for (pid in map) if (Object.prototype.hasOwnProperty.call(map,pid)) byId[pid].scores[contest] = map[pid]; | |||
return loopContest(idx + 1); | |||
}); | |||
} | } | ||
function buildAndRender(){ | |||
var rows = [], pid, e, withVotes = 0; | |||
for (pid in byId) if (Object.prototype.hasOwnProperty.call(byId,pid)) { | |||
e = byId[pid]; computeOverall(e, contests, weights); rows.push(e); | |||
if (e.totalVotes > 0 && e.overall !== null) withVotes++; | |||
} | } | ||
if (withVotes === 0 && !includeHidden) { | |||
status('Keine sichtbaren Stimmen – zweiter Versuch (versteckte Ergebnisse mitzählen) …', true); | |||
includeHidden = true; | |||
return loopContest(0).then(buildAndRender); | |||
} | |||
renderTopN(container, rows, cnt, minVotes); | |||
status.done(keep ? 0 : 3000); | |||
} | |||
loopContest(0).then(buildAndRender).catch(function(){ status('Topliste konnte nicht geladen werden.', true); }); | |||
}).catch(function(){ status('Topliste konnte nicht geladen werden.'); }); | |||
})(nodes[n]); | |||
} | |||
if (document.readyState === 'loading') { | |||
document.addEventListener('DOMContentLoaded', function(){ bootTop5(document); }); | |||
} else { bootTop5(document); } | |||
mw.hook('wikipage.content').add(function($c){ if($c && $c[0]) bootTop5($c[0]); }); | |||
}); | |||
/* Render star ratings from data-rating on .rating elements (0..5, step .5) */ | |||
mw.hook('wikipage.content').add(function($content){ | |||
$content.find('.rating').each(function(){ | |||
var el = this, val = parseFloat(el.getAttribute('data-rating') || '0'); | |||
if (isNaN(val)) val = 0; | |||
val = Math.max(0, Math.min(5, val)); | |||
el.style.setProperty('--stars', (val).toString()); | |||
el.setAttribute('aria-label', val + ' von 5 Sternen'); | |||
el.setAttribute('title', val + ' von 5 Sternen'); | |||
}); | |||
}); | |||
// Force light color-scheme at document level (helps Mobile Safari) | |||
mw.loader.using('mediawiki.util').then(function () { | |||
var m = document.querySelector('meta[name="color-scheme"]'); | |||
if (!m) { | |||
m = document.createElement('meta'); | |||
m.name = 'color-scheme'; | |||
m.content = 'light'; | |||
document.head.appendChild(m); | |||
} else { | |||
m.content = 'light'; | |||
} | |||
}); | |||
function isFlagTrue(v){ | |||
if (v == null) return false; | |||
v = String(v).trim().toLowerCase(); | |||
return v === 'true' || v === '1' || v === 'yes'; | |||
} | |||
// Gesamtzahl unter der Legende einfügen (im Diagramm-Block, nicht im Canvas!) | |||
function addTotalBelowLegend(chart, block) { | |||
try { | |||
if (!chart || !block) return; | |||
const total = chart.data.datasets.reduce((sum, ds) => | |||
sum + (ds.data || []).reduce((a, b) => a + (parseFloat(b) || 0), 0) | |||
, 0); | |||
const oldInfo = block.querySelector(':scope > .chart-total-info'); | |||
if (oldInfo) oldInfo.remove(); | |||
const info = document.createElement('div'); | |||
info.className = 'chart-total-info'; | |||
info.textContent = 'Gesamte Anzahl aller eigenen Abfüllungen: ' + total; | |||
info.style.textAlign = 'center'; | |||
info.style.fontWeight = 'bold'; | |||
info.style.fontSize = '1.05em'; | |||
info.style.marginTop = '0.5rem'; | |||
info.style.marginBottom = '0.5rem'; | |||
info.style.color = '#444'; | |||
block.appendChild(info); | |||
} catch(e) { console.warn('[addTotalBelowLegend]', e); } | |||
} | |||
/* === ADOS Multi-Serien-Chart (Chart.js) ============================= * | |||
* Lädt Chart.js sicher (asynchron) und baut Diagramme aus Cargo-Tabellen. | |||
* Benötigt im Artikel: <div class="ados-chart-multi"> + Cargo-Query als |format=table | |||
* ==================================================================== */ | |||
(function () { | |||
var _chartReady = null; | |||
function ensureChartJS() { | |||
if (_chartReady) return _chartReady; | |||
_chartReady = new Promise(function (resolve, reject) { | |||
if (window.Chart) return resolve(); | |||
var s = document.createElement('script'); | |||
s.src = 'https://cdn.jsdelivr.net/npm/chart.js'; | |||
s.async = true; | |||
s.onload = function(){ resolve(); }; | |||
s.onerror = function(){ console.error('Chart.js konnte nicht geladen werden'); reject(); }; | |||
document.head.appendChild(s); | |||
}); | |||
return _chartReady; | |||
} | |||
var ADOS_COLORS = { | |||
'A Dream of Scotland': '#C2410C', | |||
'A Dream of Ireland': '#15803D', | |||
'A Dream of... – Der Rest der Welt': '#1D4ED8', | |||
'Friendly Mr. Z': '#9333EA', | |||
'Die Whisky Elfen': '#0891B2', | |||
'The Fine Art of Whisky': '#CA8A04' | |||
}; | |||
var COLOR_CYCLE = ['#2563eb','#16a34a','#f97316','#dc2626','#a855f7','#0ea5e9','#f59e0b','#10b981']; | |||
function toYear(x){ | |||
var n = parseInt(String(x).replace(/[^\d]/g,''),10); | |||
return isFinite(n) ? n : null; | |||
} | |||
function getColor(name, used){ | |||
if (ADOS_COLORS[name]) return ADOS_COLORS[name]; | |||
var i = used.size % COLOR_CYCLE.length; | |||
used.add(name); | |||
return COLOR_CYCLE[i]; | |||
} | |||
function buildDatasetsFromTable(tbl){ | |||
var rows = Array.from(tbl.querySelectorAll('tr')); | |||
if (rows.length < 2) return { labels:[], datasets:[] }; | |||
var yearsSet = new Set(); | |||
var bySeries = new Map(); | |||
rows.slice(1).forEach(function(tr){ | |||
var tds = tr.querySelectorAll('td,th'); | |||
if (tds.length < 3) return; | |||
var y = toYear(tds[0].textContent.trim()); | |||
var s = tds[1].textContent.trim(); | |||
var v = parseFloat(tds[2].textContent.replace(',','.')) || 0; | |||
if (y == null) return; | |||
yearsSet.add(y); | |||
if (!bySeries.has(s)) bySeries.set(s, new Map()); | |||
bySeries.get(s).set(y, v); | |||
}); | |||
var years = Array.from(yearsSet).sort(function(a,b){return a-b;}); | |||
var used = new Set(); | |||
var labels = years.map(String); | |||
var datasets = Array.from(bySeries.entries()).map(function(entry){ | |||
var name = entry[0], yearMap = entry[1]; | |||
var data = years.map(function(y){ return yearMap.get(y) || 0; }); | |||
var color = getColor(name, used); | |||
return { | |||
label: name, | |||
data: data, | |||
borderColor: color, | |||
backgroundColor: color + '80', | |||
tension: 0.25, | |||
pointRadius: 3 | |||
}; | |||
}); | |||
return { labels: labels, datasets: datasets }; | |||
} | |||
function renderOne(block){ | |||
if (block.dataset.rendered === '1') return; | |||
var el = block.nextElementSibling, tbl = null, wrapToHide = null; | |||
while (el) { | |||
if (/^H[1-6]$/.test(el.tagName) || (el.classList && el.classList.contains('ados-chart-multi'))) break; | |||
if (el.tagName === 'TABLE') { | |||
tbl = el; | |||
} else if (el.querySelector) { | |||
var t = el.querySelector('table'); | |||
if (t) tbl = t; | |||
} | |||
if (tbl) { | |||
wrapToHide = tbl.parentElement; | |||
break; | |||
} | |||
el = el.nextElementSibling; | |||
} | |||
if (!tbl) return; | |||
var out = buildDatasetsFromTable(tbl); | |||
if (!out.labels.length || !out.datasets.length) return; | |||
var hide = (block.dataset.hideTable || '').toLowerCase() === 'true'; | |||
if (hide) { | |||
var onlyTable = wrapToHide && wrapToHide.children.length === 1 && wrapToHide.firstElementChild === tbl; | |||
if (onlyTable) { | |||
wrapToHide.setAttribute('aria-hidden','true'); | |||
wrapToHide.style.display = 'none'; | |||
} else { | |||
tbl.setAttribute('aria-hidden','true'); | |||
tbl.style.display = 'none'; | |||
} | |||
} | |||
var wrap = document.createElement('div'); | |||
wrap.style.position = 'relative'; | |||
wrap.style.width = '100%'; | |||
wrap.style.height = block.dataset.height || (window.matchMedia('(min-width:768px)').matches ? '450px' : '300px'); | |||
var canvas = document.createElement('canvas'); | |||
wrap.appendChild(canvas); | |||
block.innerHTML = ''; | |||
block.appendChild(wrap); | |||
var type = (block.dataset.type || 'line').toLowerCase(); | |||
var title = block.dataset.title || ''; | |||
var cumulative = (block.dataset.cumulative || '').toLowerCase() === 'true'; | |||
if (cumulative) { | |||
out.datasets = out.datasets.map(function(ds){ | |||
var acc = 0; | |||
return Object.assign({}, ds, { | |||
data: ds.data.map(function(v){ acc += v; return acc; }) | |||
}); | }); | ||
}); | |||
} | |||
ensureChartJS().then(function(){ | |||
const chart = new Chart(canvas.getContext('2d'), { | |||
type: type, | |||
data: { labels: out.labels, datasets: out.datasets }, | |||
options: { | |||
responsive: true, | |||
maintainAspectRatio: false, | |||
interaction: { mode: 'nearest', intersect: false }, | |||
plugins: { | |||
legend: { position: 'bottom', labels: { font: { size: 13 }, boxWidth: 20 } }, | |||
title: { display: !!title, text: title, font: { size: 16 } }, | |||
tooltip:{ backgroundColor: 'rgba(0,0,0,0.8)', titleFont: {size:14}, bodyFont: {size:13} } | |||
}, | |||
scales: { | |||
x: { ticks: { font: { size: 12 } } }, | |||
y: { beginAtZero: true, ticks: { precision: 0, font: { size: 12 } } } | |||
} | |||
} | } | ||
}); | |||
const hideTotal = (block.dataset.hideTotal || '').toLowerCase() === 'true'; | |||
if ( | const oldInfo = block.querySelector(':scope > .chart-total-info'); | ||
if (oldInfo) oldInfo.remove(); | |||
if (!hideTotal) { | |||
addTotalBelowLegend(chart, block); | |||
if (window.ResizeObserver) { | |||
const obs = new ResizeObserver(() => addTotalBelowLegend(chart, block)); | |||
obs.observe(chart.canvas); | |||
chart.$adosTotalObserver = obs; | |||
} | } | ||
} | } | ||
block.dataset.rendered = '1'; | |||
}); | }); | ||
} | } | ||
function boot($scope){ | |||
var blocks = ($scope && $scope[0] ? $scope[0] : document).querySelectorAll('.ados-chart-multi'); | |||
if (!blocks.length) return; | |||
ensureChartJS().then(function(){ blocks.forEach(renderOne); }); | |||
} | |||
if (window.mw && mw.hook) { | |||
mw.hook('wikipage.content').add(boot); | |||
} else { | |||
(document.readyState === 'loading') | |||
? document.addEventListener('DOMContentLoaded', function(){ boot(); }) | |||
: boot(); | |||
} | |||
})(); | |||
// ==========================Scan================================== | |||
mw.loader.using('mediawiki.util').then(function () { | |||
if (mw.config.get('wgPageName') !== 'LabelScan') return; | |||
mw.loader.load('/index.php?title=MediaWiki:Gadget-LabelScan.js&action=raw&ctype=text/javascript'); | |||
mw.loader.load('/index.php?title=MediaWiki:Gadget-LabelScan.css&action=raw&ctype=text/css', 'text/css'); | |||
}); | |||
// ==========================ScanApp================================== | |||
/* ==== PWA: Manifest + Service Worker + Install-Button (ES5) ==== */ | |||
/* Manifest einbinden */ | |||
(function () { | |||
var link = document.createElement("link"); | |||
link.rel = "manifest"; | |||
link.href = "/app/labelscan/manifest.webmanifest"; | |||
document.head.appendChild(link); | |||
})(); | |||
/* Service Worker registrieren (nur wenn vorhanden) */ | |||
(function () { | |||
if ("serviceWorker" in navigator) { | |||
navigator.serviceWorker.register("/app/labelscan/sw.js")["catch"](function () {}); | |||
} | |||
})(); | |||
/* Install-Button steuern (Button-ID: ados-install) */ | |||
(function () { | |||
var installPrompt = null; | |||
window.addEventListener("beforeinstallprompt", function (e) { | |||
try { e.preventDefault(); } catch (ex) {} | |||
installPrompt = e; | |||
var btn = document.getElementById("ados-install"); | |||
if (btn) btn.style.display = "inline-block"; | |||
}); | |||
function onReady(fn){ if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", fn); else fn(); } | |||
onReady(function () { | |||
var btn = document.getElementById("ados-install"); | |||
if (!btn) return; | |||
btn.addEventListener("click", function () { | |||
if (!installPrompt) return; | |||
try { installPrompt.prompt(); } catch (ex) {} | |||
installPrompt = null; | |||
btn.style.display = "none"; | |||
}); | |||
}); | |||
})(); | |||
if ('serviceWorker' in navigator) { | |||
navigator.serviceWorker.register('/app/labelscan/sw.js').catch(function(){}); | |||
} | |||
// ============================================================ | |||
mw.loader.using('mediawiki.util').then(function () { | |||
function checkNeuBadges() { | |||
var badges = document.querySelectorAll('.ados-neu-badge'); | |||
var now = new Date(); | |||
badges.forEach(function (badge) { | |||
var expiry = badge.getAttribute('data-expiry'); | |||
if (!expiry) return; | |||
var expiryDate = new Date(expiry + "T23:59:59"); | |||
if (now > expiryDate) { | |||
badge.style.display = "none"; | |||
} | |||
}); | }); | ||
} | } | ||
if (document.readyState === "loading") { | |||
document.addEventListener("DOMContentLoaded", checkNeuBadges); | |||
} | } else { | ||
checkNeuBadges(); | |||
} | |||
}); | |||
// ============================================================ | |||
mw.loader.load('/wiki/MediaWiki:WhiskybaseBatch.js?action=raw&ctype=text/javascript'); | |||