MediaWiki:Common.js: Unterschied zwischen den Versionen

Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
 
(130 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) */
/* 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.api', 'mediawiki.user']).then(function () {
Zeile 397: Zeile 398:




/* Fireworks Popup (v3) – animierter "ADOS"-Schriftzug mit Feuerwerk */
/* --- Whisky Top-5 (multi-root, recursive, robust, namespaces, ES5) ------- */
mw.loader.using(['mediawiki.util','jquery']).then(function(){
  (function($, mw){
    'use strict';
 
    var CONFIG = {
      enabled: true,
      id: 'fireworks_popup_v3',
      title: 'Neu: Abfüllungen bewerten 🎉',
      messageHTML:
        '<p>Ab sofort kannst du im Wiki <strong>jede Abfüllung bewerten</strong> – ' +
        'teile deine Meinung und hilf anderen bei der Auswahl!</p>',
      cta: { text: 'Jetzt bewerten', url: 'https://ados-wiki.de/index.php?title=Spezial:Zuf%C3%A4llige_Seite' },
      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 g(k){ try{return localStorage.getItem(k);}catch(e){return null;} }
    function s(k,v){ try{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 (g(key) === today) return;
    function markSeen(){ s(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-fw-title'});
 
      var $fwWrap = $('<div>', {'class':'mw-fw-canvas-wrap'});
      var $canvas = $('<canvas>', {'class':'mw-fw-canvas', 'aria-hidden':'true'});
      $fwWrap.append($canvas);
 
      var $title = $('<h2>', { id: 'mw-fw-title' }).text(CONFIG.title);
      var $content = $('<div>', {'class':'mw-popup-content'}).html(CONFIG.messageHTML);
 
      var $buttons = $('<div>', {'class':'mw-popup-button-row'});
      var $ok = $('<button>', {'class':'mw-popup-close', type:'button'}).text('OK');
      $buttons.append($ok);
      if (CONFIG.cta && CONFIG.cta.url) {
        $buttons.append($('<a>', {
          'class':'mw-popup-wiki-button',
          'href': CONFIG.cta.url,
          'target': '_blank',
          'rel': 'noopener'
        }).text(CONFIG.cta.text || 'Mehr'));
      }
 
      $modal.append($fwWrap, $title, $content, $buttons);
      $('body').append($overlay, $modal);
 
      function close(){
        stopFireworks();
        markSeen();
        $overlay.remove(); $modal.remove();
        $(document).off('keydown.mwfw');
      }
      $ok.on('click', close);
      if (CONFIG.clickBackdropToClose) $overlay.on('click', close);
      if (CONFIG.escToClose) {
        $(document).on('keydown.mwfw', function(e){
          var k = e.key || e.keyCode;
          if (k==='Escape' || k==='Esc' || k===27){ e.preventDefault(); close(); }
        });
      }
 
      // ==== Fireworks + animiertes ADOS ====
      var canvas = $canvas[0], ctx = canvas.getContext('2d');
      var dpr = Math.max(1, window.devicePixelRatio || 1);
      var w=0,h=0, raf=null, particles=[], startTime=0;
      var reduceMotion = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
 
      function resize(){
        var rect = $fwWrap[0].getBoundingClientRect();
        w = Math.floor(rect.width * dpr);
        h = Math.floor(rect.height * dpr);
        canvas.width = w; canvas.height = h;
        canvas.style.width = rect.width+'px';
        canvas.style.height = rect.height+'px';
      }
 
      function rand(min,max){ return Math.random()*(max-min)+min; }
      function hsla(h,s,l,a){ return 'hsla('+h+','+s+'%,'+l+'%,'+a+')'; }
 
      function spawnBurst(x,y){
        var hue = Math.floor(rand(0,360));
        for (var i=0;i<80;i++){
          var angle = rand(0, Math.PI*2);
          var speed = rand(1.2, 3.2);
          particles.push({
            x:x, y:y,
            vx: Math.cos(angle)*speed,
            vy: Math.sin(angle)*speed,
            life: rand(45, 80),
            age: 0,
            hue: hue + rand(-15,15),
          });
        }
      }
 
      // animierter Schriftzug
      var adosPoints = [];
      function createTextPoints(){
        var tempCanvas = document.createElement('canvas');
        var tctx = tempCanvas.getContext('2d');
        tempCanvas.width = w; tempCanvas.height = h;
        tctx.fillStyle = '#fff';
        tctx.font = 'bold 120px "Segoe UI", Arial, sans-serif';
        tctx.textAlign = 'center';
        tctx.textBaseline = 'middle';
        tctx.fillText('ADOS', w/2, h/2);
        var img = tctx.getImageData(0, 0, w, h).data;
        for (var y=0; y<h; y+=6){
          for (var x=0; x<w; x+=6){
            var i = (y*w + x)*4;
            if (img[i+3] > 128){
              adosPoints.push({x:x, y:y, life:rand(50,100), age:0});
            }
          }
        }
      }
 
      function drawADOSParticles(){
        for (var i=0; i<adosPoints.length; i++){
          var p = adosPoints[i];
          p.age++;
          var alpha = Math.sin((p.age/p.life)*Math.PI);
          var hue = (p.age*3 + p.x/5) % 360;
          ctx.fillStyle = hsla(hue,100,60,alpha);
          ctx.beginPath();
          ctx.arc(p.x, p.y, 1.6, 0, Math.PI*2);
          ctx.fill();
        }
      }
 
      function tick(t){
        ctx.fillStyle = 'rgba(0,0,0,0.08)';
        ctx.globalCompositeOperation = 'source-over';
        ctx.fillRect(0,0,w,h);
 
        ctx.globalCompositeOperation = 'lighter';
 
        var next = [];
        for (var i=0;i<particles.length;i++){
          var p = particles[i];
          p.age++;
          p.vy += 0.02;
          p.vx *= 0.99; p.vy *= 0.99;
          p.x += p.vx*dpr; p.y += p.vy*dpr;
          var alpha = Math.max(0, 1 - p.age/p.life);
          if (alpha>0){
            ctx.beginPath();
            ctx.fillStyle = hsla(p.hue,100,60,alpha);
            ctx.arc(p.x,p.y,Math.max(0.5,2*alpha),0,Math.PI*2);
            ctx.fill();
            next.push(p);
          }
        }
        particles = next;
 
        var elapsed = t - startTime;
        if (elapsed < 4000) {
          drawADOSParticles();
        }
 
        // Feuerwerk nach 2s starten
        if (elapsed > 2000 && Math.random() < 0.08) {
          var bx = rand(w*0.15, w*0.85);
          var by = rand(h*0.2,  h*0.6);
          spawnBurst(bx, by);
        }
 
        raf = requestAnimationFrame(tick);
      }
 
      function startFireworks(){
        if (reduceMotion) return;
        resize();
        createTextPoints();
        startTime = performance.now();
        if (!raf) raf = requestAnimationFrame(tick);
        window.addEventListener('resize', resize);
      }
      function stopFireworks(){
        if (raf){ cancelAnimationFrame(raf); raf=null; }
        window.removeEventListener('resize', resize);
      }
 
      startFireworks();
      markSeen();
    });
  })(jQuery, mw);
});
 
 
 
 
 
 
 
 
 
/* --- Whisky Top-5 (rekursiv über Unterkategorien) ----------------------- */
mw.loader.using(['mediawiki.api']).then(function () {
mw.loader.using(['mediawiki.api']).then(function () {


   // kleines Hilfs-get ohne optional chaining
   /* ========== Utils ========== */
   function get(obj, path) {
   function get(obj, path) {
     var cur = obj, i;
     var cur = obj, i;
     for (i = 0; i < path.length; i++) {
     for (i = 0; i < path.length; i++) { if (!cur || typeof cur !== 'object') return; cur = cur[path[i]]; }
      if (!cur || typeof cur !== 'object') return;
      cur = cur[path[i]];
    }
     return cur;
     return cur;
   }
   }
  // Gewichte parsen, z. B. "NASE:1,GESCHMACK:2,ABGANG:1"
   function parseWeights(raw, contests) {
   function parseWeights(raw, contests) {
     var map = {}, parts = (raw || '').split(','), i;
     var map = {}, parts = (raw || '').split(','), i;
     for (i = 0; i < parts.length; i++) {
     for (i = 0; i < parts.length; i++) {
       var kv = parts[i].split(':');
       var kv = parts[i].split(':'); if (kv.length !== 2) continue;
      if (kv.length === 2) {
      var k = kv[0].replace(/^\s+|\s+$/g, ''), v = parseFloat(kv[1]);
        var k = kv[0].replace(/^\s+|\s+$/g, '');
      if (!isNaN(v)) map[k] = v;
        var v = parseFloat(kv[1]);
        if (!isNaN(v)) map[k] = v;
      }
     }
     }
     for (i = 0; i < contests.length; i++) {
     for (i = 0; i < contests.length; i++) if (typeof map[contests[i]] !== 'number') map[contests[i]] = 1;
      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);
     }
     }
     return map;
     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;
    });
   }
   }


   // Holt Artikel (NS0) aus einer Kategorie inkl. ALLER Unterkategorien (BFS), bis limit erreicht ist
   /* ========== Kategorien rekursiv einsammeln (inkl. Subkats, Namespaces) ========== */
  function fetchCategoryMembersRecursive(rootCat, limit) {
function fetchCategoryMembersRecursiveSingleResolved(api, catTitle, limit, outSet, pages, nsStr){
    var api = new mw.Api();
  var visited = {}, queue = [catTitle];
    var visitedCats = {};
  var cmNS = (nsStr && nsStr.trim()) ? nsStr.trim() : '0|14'; // default: Artikel+Kategorie
    var queue = ['Kategorie:' + rootCat]; // Startkategorie (mit Präfix)
    var pages = [];                       // { pageid, title }


    function fetchOneCat(catTitle, cmcontinue) {
  function fetchOne(title, cont){
      var params = {
    var params = {
        action: 'query',
      action: 'query',
        list: 'categorymembers',
      list: 'categorymembers',
        cmtitle: catTitle,
      cmtitle: title,
        cmnamespace: '0|14',  // 0=Seiten, 14=Kategorien
      cmtype: 'page|subcat',
        cmtype: 'page|subcat',
      cmlimit: Math.min(200, limit),
        cmlimit: Math.min(200, limit),
      format: 'json'
        format: 'json'
    };
      };
    if (cmNS !== '*' && cmNS !== '') {
       if (cmcontinue) params.cmcontinue = cmcontinue;
       params.cmnamespace = cmNS; // z.B. "0|102|14"
    }
    if (cont) params.cmcontinue = cont;


      return api.get(params).then(function (data) {
    return api.get(params).then(function(d){
        var cms = get(data, ['query', 'categorymembers']) || [];
      var cms = (d.query && d.query.categorymembers) || [], i, it;
        var i;
      for (i = 0; i < cms.length; i++) {
        for (i = 0; i < cms.length; i++) {
        it = cms[i];
          var item = cms[i];
        if (it.ns !== 14) { // alles außer Kategorien zählt als Seite
          if (item.ns === 0) {
          var pid = String(it.pageid);
            if (pages.length < limit) pages.push({ pageid: String(item.pageid), title: item.title });
           if (!outSet[pid] && pages.length < limit) {
           } else if (item.ns === 14) {
            outSet[pid] = 1;
            var subcatTitle = item.title; // enthält schon "Kategorie:"
            pages.push({ pageid: pid, title: it.title });
            if (!visitedCats[subcatTitle]) {
              visitedCats[subcatTitle] = true;
              queue.push(subcatTitle);
            }
           }
           }
        } 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);
    });
  }


        var cont = get(data, ['continue', 'cmcontinue']);
  function loop(){
         if (cont && pages.length < limit) {
    if (pages.length >= limit || !queue.length) return Promise.resolve();
           return fetchOneCat(catTitle, cont);
    var next = queue.shift();
         }
    if (visited[next]) return loop();
        return null;
    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();
         });
       });
       });
     }
     }


     function loop() {
     return next();
      if (pages.length >= limit || queue.length === 0) {
        return Promise.resolve(pages);
      }
      var nextCat = queue.shift();
      if (visitedCats[nextCat]) return loop();
      visitedCats[nextCat] = true;
      return fetchOneCat(nextCat).then(loop);
    }
 
    return loop();
   }
   }


   // Holt für pageIds die RatePage-Daten zu einem Contest
   /* ========== Ratings laden / auswerten ========== */
  // includeHidden=true => zählt auch, wenn canSee=0 (nur für Ranking)
   function fetchRatingsForContest(pageIds, contest, includeHidden) {
   function fetchRatingsForContest(pageIds, contest, includeHidden) {
     var api = new mw.Api();
     var api = new mw.Api(), res = {}, i, chunk = 50, chunks = [];
    var res = {}; // pageId -> { avg, total }
    var i, chunk = 50, chunks = [];
     for (i = 0; i < pageIds.length; i += chunk) chunks.push(pageIds.slice(i, i + chunk));
     for (i = 0; i < pageIds.length; i += chunk) chunks.push(pageIds.slice(i, i + chunk));


Zeile 705: Zeile 535:
       if (idx >= chunks.length) return Promise.resolve(res);
       if (idx >= chunks.length) return Promise.resolve(res);
       var ids = chunks[idx];
       var ids = chunks[idx];
       return api.get({
       return api.get({
         action: 'query',
         action:'query', prop:'pagerating', pageids: ids.join('|'),
        prop: 'pagerating',
         prcontest: contest, format:'json'
        pageids: ids.join('|'),
       }).then(function(d){
         prcontest: contest,
         var pages = get(d,['query','pages']) || {}, pid, pr, hist, k, total, sum, s, c;
        format: 'json'
         for (pid in pages) if (Object.prototype.hasOwnProperty.call(pages,pid)) {
       }).then(function (d) {
          if (!res[pid]) res[pid] = { avg:null, total:0 };
         var pages = get(d, ['query', 'pages']) || {};
        var pid, pr, hist, k, total, sum, s, c;
         for (pid in pages) if (Object.prototype.hasOwnProperty.call(pages, pid)) {
           pr = pages[pid].pagerating;
           pr = pages[pid].pagerating;
          if (!res[pid]) res[pid] = { avg: null, total: 0 };
           if (pr && (includeHidden || !('canSee' in pr) || pr.canSee !== 0)) {
           if (pr && (includeHidden || !('canSee' in pr) || pr.canSee !== 0)) {
             hist = pr.pageRating || {};
             hist = pr.pageRating || {}; total=0; sum=0;
            total = 0; sum = 0;
             for (k in hist) if (Object.prototype.hasOwnProperty.call(hist,k)) {
             for (k in hist) if (Object.prototype.hasOwnProperty.call(hist, k)) {
               s = parseInt(k,10); c = parseInt(hist[k],10);
               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; }
            }
            if (total > 0) {
              res[pid] = { avg: Math.round((sum / total) * 10) / 10, total: total };
             }
             }
            if (total > 0) res[pid] = { avg: Math.round((sum/total)*10)/10, total: total };
           }
           }
         }
         }
         return step(idx + 1);
         return step(idx + 1);
       }, function () {
       }, function(){ return step(idx + 1); });
        // Fehler in diesem Chunk: einfach weitermachen
        return step(idx + 1);
      });
     }
     }
     return step(0);
     return step(0);
   }
   }


  // Gesamt berechnen (gewichtet) + Stimmen summieren
   function computeOverall(entry, contests, weights) {
   function computeOverall(entry, contests, weights) {
     var wSum = 0, wAvgSum = 0, present = 0, totalVotes = 0, i;
     var wSum=0, wAvgSum=0, present=0, totalVotes=0, i, sc, w;
     for (i = 0; i < contests.length; i++) {
     for (i=0;i<contests.length;i++){
       var c = contests[i];
       sc = entry.scores[contests[i]];
      var sc = entry.scores[c];
       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.avg !== null) {
        var w = (typeof weights[c] === 'number') ? weights[c] : 1;
        wSum += w;
        wAvgSum += sc.avg * w;
        present++;
      }
       if (sc && sc.total) totalVotes += sc.total;
       if (sc && sc.total) totalVotes += sc.total;
     }
     }
     entry.totalVotes = totalVotes;
     entry.totalVotes = totalVotes;
     entry.overall = (present > 0 && wSum > 0) ? Math.round((wAvgSum / wSum) * 10) / 10 : null;
     entry.overall = (present>0 && wSum>0) ? Math.round((wAvgSum/wSum)*10)/10 : null;
   }
   }


   // Rendering der Top-N Liste (kompakte Karte mit Mini-Balken)
   /* ========== Render ========== */
   function renderTopN(container, rows, N, minVotes) {
   function renderTopN(container, rows, N, minVotes) {
     // nur Seiten mit Stimmen >= minVotes
     var keep = (container.getAttribute && container.getAttribute('data-keep-status') === 'true');
    rows = rows.filter(function (r) {
     var statusBox = keep ? container.querySelector('.whisky-top5__status') : null;
      return (r.overall !== null) && (r.totalVotes >= minVotes);
     });


     // Sortierung: Gesamt desc, Stimmen desc, Titel asc
     rows = rows.filter(function(r){ return (r.overall !== null) && (r.totalVotes >= minVotes); });
     rows.sort(function (a, b) {
     rows.sort(function(a,b){
       if (a.overall === null && b.overall !== null) return 1;
       if (a.overall===null && b.overall!==null) return 1;
       if (a.overall !== null && b.overall === null) return -1;
       if (a.overall!==null && b.overall===null) return -1;
       if (b.overall !== a.overall) return (b.overall - a.overall);
       if (b.overall!==a.overall) return b.overall - a.overall;
       if (b.totalVotes !== a.totalVotes) return (b.totalVotes - a.totalVotes);
       if (b.totalVotes!==a.totalVotes) return b.totalVotes - a.totalVotes;
       return a.title.localeCompare(b.title);
       return a.title.localeCompare(b.title);
     });
     });
    rows = rows.slice(0, N);


     rows = rows.slice(0, N);
     while (container.firstChild) container.removeChild(container.firstChild);
    if (statusBox) container.appendChild(statusBox);


     var frag = document.createDocumentFragment();
     if (!rows.length) { container.appendChild(document.createTextNode('Noch keine Bewertungen vorhanden.')); return; }
    var i, r, item, rank, name, a, right, mini, track, fill, val, votes;


     for (i = 0; i < rows.length; i++) {
    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];
       r = rows[i];
      item = document.createElement('div'); item.className = 'whisky-top5__item';


       item = document.createElement('div');
       rank = document.createElement('div'); rank.className = 'whisky-top5__rank'; rank.textContent = (i+1);
      item.className = 'whisky-top5__item';


       rank = document.createElement('div');
       name = document.createElement('div'); name.className = 'whisky-top5__name';
      rank.className = 'whisky-top5__rank';
       a = document.createElement('a'); a.href = mw.util.getUrl(r.title); a.textContent = r.title; name.appendChild(a);
       rank.textContent = (i + 1);


       name = document.createElement('div');
       right = document.createElement('div'); right.style.minWidth = '160px';
      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');
       mini = document.createElement('div'); mini.className = 'whisky-mini';
       right.style.minWidth = '160px';
      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));


      mini = document.createElement('div');
       track.appendChild(fill); mini.appendChild(track); mini.appendChild(val);
      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 = document.createElement('div'); votes.className = 'whisky-top5__votes';
      votes.className = 'whisky-top5__votes';
       votes.textContent = r.totalVotes + ' Stimmen';
       votes.textContent = r.totalVotes + ' Stimmen';


       right.appendChild(mini);
       right.appendChild(mini); right.appendChild(votes);
      right.appendChild(votes);


       item.appendChild(rank);
       item.appendChild(rank); item.appendChild(name); item.appendChild(right);
      item.appendChild(name);
      item.appendChild(right);
       frag.appendChild(item);
       frag.appendChild(item);
     }
     }
 
     container.appendChild(frag);
     while (container.firstChild) container.removeChild(container.firstChild);
    if (rows.length) {
      container.appendChild(frag);
    } else {
      container.textContent = 'Noch keine Bewertungen vorhanden.';
    }
   }
   }


   // Boot: findet .whisky-top5 Container und rendert sie
   /* ========== Boot ========== */
   function bootTop5(root) {
   function bootTop5(root) {
     var nodes = (root || document).querySelectorAll('.whisky-top5');
     var nodes = (root || document).querySelectorAll('.whisky-top5');
     if (!nodes.length) return;
     if (!nodes.length) return;


    var n;
     for (var n=0;n<nodes.length;n++) (function(container){
     for (n = 0; n < nodes.length; n++) (function (container) {
       if (container.getAttribute('data-top5-init')==='1') return;
       if (container.getAttribute('data-top5-init') === '1') return;
       container.setAttribute('data-top5-init','1');
       container.setAttribute('data-top5-init', '1');


       var cat = container.getAttribute('data-category') || 'Whisky'; // ohne "Kategorie:"
       var rawCats = container.getAttribute('data-categories') || container.getAttribute('data-category') || '';
       var lim = parseInt(container.getAttribute('data-limit') || '300', 10);
      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; }
 
       var lim = parseInt(container.getAttribute('data-limit') || '2000', 10);
       var cnt = parseInt(container.getAttribute('data-count') || '5', 10);
       var cnt = parseInt(container.getAttribute('data-count') || '5', 10);
       var minVotes = parseInt(container.getAttribute('data-min-votes') || '1', 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';


      // Contests + Gewichte
       var rawC = container.getAttribute('data-contests') || 'NASE,GESCHMACK,ABGANG,GESAMTEINDRUCK';
       var rawC = container.getAttribute('data-contests') || 'NASE,GESCHMACK,ABGANG,GESAMTEINDRUCK';
       var includeHidden = (container.getAttribute('data-include-hidden') === 'true'); // canSee=0 trotzdem zählen?
       var cParts = rawC.split(','), contests = [], seen = {};
      var parts = rawC.split(','), contests = [], seen = {}, i;
       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; } }
       for (i = 0; i < parts.length; i++) {
        var c = parts[i].replace(/^\s+|\s+$/g, '');
        if (c && !seen[c]) { contests.push(c); seen[c] = 1; }
      }
       var weights = parseWeights(container.getAttribute('data-weights') || '', contests);
       var weights = parseWeights(container.getAttribute('data-weights') || '', contests);


       container.textContent = 'Lade Topliste …';
       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; }


      // 1) Alle Artikel aus Kategorie + Subkategorien holen
      fetchCategoryMembersRecursive(cat, lim).then(function (members) {
        if (!members || !members.length) {
          container.textContent = 'Keine Seiten in Kategorie „' + cat + '“.';
          return;
        }
         var pageIds = [], byId = {}, i;
         var pageIds = [], byId = {}, i;
         for (i = 0; i < members.length; 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: {} }; }
          pageIds.push(members[i].pageid);
          byId[members[i].pageid] = { pageid: members[i].pageid, title: members[i].title, scores: {} };
        }


        // 2) Je Contest die Werte holen (nacheinander, um Last zu senken)
         function loopContest(idx){
         function loopContest(idx) {
           if (idx >= contests.length) return Promise.resolve();
           if (idx >= contests.length) return Promise.resolve();
           var contest = contests[idx];
           var contest = contests[idx];
           return fetchRatingsForContest(pageIds, contest, includeHidden).then(function (map) {
           return fetchRatingsForContest(pageIds, contest, includeHidden).then(function(map){
             var pid;
             var pid; for (pid in map) if (Object.prototype.hasOwnProperty.call(map,pid)) byId[pid].scores[contest] = map[pid];
            for (pid in map) if (Object.prototype.hasOwnProperty.call(map, pid)) {
              byId[pid].scores[contest] = map[pid];
            }
             return loopContest(idx + 1);
             return loopContest(idx + 1);
           });
           });
         }
         }


         loopContest(0).then(function () {
         function buildAndRender(){
          // 3) Gesamt berechnen + Array bauen
           var rows = [], pid, e, withVotes = 0;
           var rows = [], pid, e;
           for (pid in byId) if (Object.prototype.hasOwnProperty.call(byId,pid)) {
           for (pid in byId) if (Object.prototype.hasOwnProperty.call(byId, pid)) {
             e = byId[pid]; computeOverall(e, contests, weights); rows.push(e);
             e = byId[pid];
            if (e.totalVotes > 0 && e.overall !== null) withVotes++;
            computeOverall(e, contests, weights);
          }
            rows.push(e);
          if (withVotes === 0 && !includeHidden) {
            status('Keine sichtbaren Stimmen – zweiter Versuch (versteckte Ergebnisse mitzählen) …', true);
            includeHidden = true;
            return loopContest(0).then(buildAndRender);
           }
           }
          // 4) Rendern
           renderTopN(container, rows, cnt, minVotes);
           renderTopN(container, rows, cnt, minVotes);
         }).catch(function () {
          status.done(keep ? 0 : 3000);
          container.textContent = 'Topliste konnte nicht geladen werden.';
         }
        });
 
       }).catch(function () {
        loopContest(0).then(buildAndRender).catch(function(){ status('Topliste konnte nicht geladen werden.', true); });
        container.textContent = 'Topliste konnte nicht geladen werden.';
       }).catch(function(){ status('Topliste konnte nicht geladen werden.'); });
      });


     })(nodes[n]);
     })(nodes[n]);
Zeile 906: Zeile 688:


   if (document.readyState === 'loading') {
   if (document.readyState === 'loading') {
     document.addEventListener('DOMContentLoaded', function () { bootTop5(document); });
     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 {
   } else {
     bootTop5(document);
     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 };
   }
   }
  mw.hook('wikipage.content').add(function ($c) { if ($c && $c[0]) bootTop5($c[0]); });


});
  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';
      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;


/* Render star ratings from data-rating on .rating elements (0..5, step .5) */
  window.addEventListener("beforeinstallprompt", function (e) {
mw.hook('wikipage.content').add(function($content){
    try { e.preventDefault(); } catch (ex) {}
  $content.find('.rating').each(function(){
    installPrompt = e;
     var el = this, val = parseFloat(el.getAttribute('data-rating') || '0');
     var btn = document.getElementById("ados-install");
     if (isNaN(val)) val = 0;
     if (btn) btn.style.display = "inline-block";
    // clamp 0..5
  });
     val = Math.max(0, Math.min(5, val));
 
     // set CSS variable for width percentage (0..5 -> 0..5 stars)
  function onReady(fn){ if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", fn); else fn(); }
     el.style.setProperty('--stars', (val).toString());
  onReady(function () {
    el.setAttribute('aria-label', val + ' von 5 Sternen');
     var btn = document.getElementById("ados-install");
     el.setAttribute('title', val + ' von 5 Sternen');
     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');