MediaWiki:Common.js: Unterschied zwischen den Versionen

Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Zeile 280: Zeile 280:
     }
     }


    // --- Stats laden ---
    function updateStats() {
      const api = new mw.Api();
      api.get({
        action: 'query',
        prop: 'pagerating',
        pageids: pageId,
        prcontest: contest || undefined,
        format: 'json'
      }).done(function (data) {
        try {
          const page = data.query.pages[pageId];
          const pr = page && page.pagerating;
          if (!pr) { meta.textContent = ''; return; }


          if (typeof pr.canSee !== 'undefined' && pr.canSee === 0) {
// --- Stats laden (Ø & Stimmen, eigene Stimme, canVote/see) ---
            meta.textContent = 'Bewertungen sind verborgen.';
function updateStats() {
          } else {
  const api = new mw.Api();
            const hist = pr.pageRating || {};
  api.get({
            let total = 0, sum = 0;
    action: 'query',
            Object.keys(hist).forEach(k => {
    prop: 'pagerating',
              const s = parseInt(k, 10), c = parseInt(hist[k], 10);
    pageids: pageId,
              if (!isNaN(s) && !isNaN(c)) { total += c; sum += s * c; }
    prcontest: contest || undefined,
            });
    format: 'json'
            meta.textContent = total
  }).done(function (data) {
              ? ('Ø ' + (Math.round((sum/total)*10)/10) + ' (' + total + ' Stimmen)')
    try {
              : 'Noch keine Bewertungen';
      // Robuster Zugriff: Page-ID-String aus dem Response nehmen
          }
      const pages = (data && data.query && data.query.pages) || {};
      const pid = Object.keys(pages)[0];
      const page = pages[pid] || {};
      const pr = page.pagerating;


          if (pr.userVote) {
      if (!pr) {
            current = pr.userVote;
        // Keine Daten – lieber neutralen Text setzen
            highlight(current);
        meta.textContent = meta.textContent || 'Noch keine Bewertungen';
          }
        return;
      }


           if (typeof pr.canVote !== 'undefined' && pr.canVote === 0) {
      // Ergebnisse sichtbar?
            box.classList.add('whisky-rating--disabled');
      if (typeof pr.canSee !== 'undefined' && pr.canSee === 0) {
            widget.querySelectorAll('.whisky-glass').forEach(b => b.disabled = true);
        meta.textContent = 'Bewertungen sind verborgen.';
            meta.textContent += (meta.textContent ? ' • ' : '') + 'Du darfst hier nicht abstimmen.';
      } else {
          }
        const hist = pr.pageRating || {};
        } catch (e) {
        let total = 0, sum = 0;
          console.error(e);
        Object.keys(hist).forEach(k => {
          const s = parseInt(k, 10), c = parseInt(hist[k], 10);
           if (!isNaN(s) && !isNaN(c)) { total += c; sum += s * c; }
        });
        meta.textContent = total
          ? ('Ø ' + (Math.round((sum/total)*10)/10) + ' (' + total + ' Stimmen)')
          : 'Noch keine Bewertungen';
      }
 
      // eigene Stimme hervorheben
      if (pr.userVote) {
        current = pr.userVote;
        highlight(current);
      }
 
      // Voting ggf. deaktivieren
      if (typeof pr.canVote !== 'undefined' && pr.canVote === 0) {
        box.classList.add('whisky-rating--disabled');
        widget.querySelectorAll('.whisky-glass').forEach(b => b.disabled = true);
        if (!/nicht abstimmen/.test(meta.textContent)) {
          meta.textContent += (meta.textContent ? ' • ' : '') + 'Du darfst hier nicht abstimmen.';
         }
         }
       }).fail(function (xhr) {
       }
        console.error('Pagerating-Load-Error', xhr);
    } catch (e) {
       });
      console.error(e);
       if (!meta.textContent) meta.textContent = 'Bewertungen konnten nicht geladen werden.';
     }
     }
  }).fail(function (xhr) {
    console.error('Pagerating-Load-Error', xhr);
    if (!meta.textContent) meta.textContent = 'Bewertungen konnten nicht geladen werden.';
  });
}


    function vote(value) {
// --- Vote senden (per pageid) ---
      const api = new mw.Api();
function vote(value) {
      meta.textContent = 'Wird gespeichert …';
  const api = new mw.Api();
  meta.textContent = 'Wird gespeichert …';


      api.postWithToken('csrf', {
  // Sicherheitsnetz: nach 8s nicht hängen bleiben
        action: 'ratepage',
  let saved = false;
        pageid: pageId,
  const failTimer = setTimeout(function () {
        answer: value,
    if (!saved) meta.textContent = 'Speichern dauert ungewöhnlich lange … bitte Seite neu laden.';
        contest: contest || undefined,
  }, 8000);
        format: 'json'
 
      }).done(function () {
  api.postWithToken('csrf', {
        current = value;
    action: 'ratepage',
        highlight(current);
    pageid: pageId,                 // ROBUST
        updateStats();
    answer: value,                 // 1..10
      }).fail(function (xhr) {
    contest: contest || undefined, // Kategorie
        let msg = 'Unbekannter Fehler';
    format: 'json'
        try {
  }).done(function () {
          const j = xhr && xhr.responseJSON ? xhr.responseJSON : xhr;
    saved = true;
          if (j && j.error) {
    clearTimeout(failTimer);
            msg = (j.error.code ? j.error.code + ': ' : '') + (j.error.info || '');
    current = value;
          }
    highlight(current);
        } catch(e){}
 
        console.error('RatePage-API-Fehler:', xhr);
    // Sofort eine Bestätigung anzeigen …
        meta.textContent = 'Speichern fehlgeschlagen: ' + msg;
    meta.textContent = 'Danke! Deine Bewertung: ' + value + ' / ' + scale;
      });
    }


    // … und dann die echten Ø/Count nachladen
    updateStats();
  }).fail(function (xhr) {
    clearTimeout(failTimer);
    let msg = 'Unbekannter Fehler';
    try {
      const 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);
    meta.textContent = 'Speichern fehlgeschlagen: ' + msg;
  });
}
     updateStats();
     updateStats();
   }
   }