MediaWiki:Common.js: Unterschied zwischen den Versionen

Aus Holopedia
Zur Navigation springen Zur Suche springen
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
 
Zeile 83: Zeile 83:
           <input id="' + prefix + 'real" type="number" step="any" style="flex:1" placeholder="1995">\
           <input id="' + prefix + 'real" type="number" step="any" style="flex:1" placeholder="1995">\
         </div>\
         </div>\
         <div style="font-size:90%;color:#666">Fixpunkte: 4 NSY = 0 n.E.; 0 n.E. = 1995</div>\
         <div style="font-size:90%;color:#666">Fixpunkt: 4 NSY = 0 n.E. = 1995</div>\
       </div>';
       </div>';



Aktuelle Version vom 9. Januar 2026, 23:10 Uhr

/* Das folgende JavaScript wird für alle Benutzer geladen. */

// BBY / v.E. / real-time converter
// Installation: put this code into MediaWiki:BBYConverter.js and load it via MediaWiki:Common.js
// or paste into MediaWiki:Common.js directly, or register it as a gadget.
// The script automatically initialises any <div class="bby-converter"> on the page.

( function () {
  'use strict';

  // Configuration: anchor points
  var NE_AT_ABY = 4;     // 4 ABY = 0 n.E.  => t_ne = y_ab_signed - 4
  var REAL_AT_NE0 = 1995; // 0 n.E. = 1995 real-time year => real = 1995 + t_ne

  // Utility formatting
  function fmt(n) {
    if (!isFinite(n)) return '';
    // show integer if whole, otherwise up to 3 decimals without trailing zeros
    if (Math.abs(n - Math.round(n)) < 1e-9) return String(Math.round(n));
    return parseFloat(n.toFixed(3)).toString();
  }

  // Conversions:
  // BBY/ABY value + era => t_ne (number, can be negative)
  function bbyToNE(value, era) {
    var v = parseFloat(value);
    if (isNaN(v)) return NaN;
    var y_ab_signed = (era === 'ABY') ? v : -v;
    return y_ab_signed - NE_AT_ABY;
  }

  // t_ne => BBY/ABY {value, era}
  function neToBBY(t_ne) {
    var y_ab_signed = t_ne + NE_AT_ABY;
    if (y_ab_signed >= 0) return { value: Math.abs(y_ab_signed), era: 'ABY' };
    return { value: Math.abs(y_ab_signed), era: 'BBY' };
  }

  // t_ne => v.E./n.E. {value, era}
  function neToVE(t_ne) {
    if (t_ne >= 0) return { value: t_ne, era: 'n.E.' };
    return { value: Math.abs(t_ne), era: 'v.E.' };
  }

  // real <-> t_ne
  function realToNE(real) {
    var r = parseFloat(real);
    if (isNaN(r)) return NaN;
    return r - REAL_AT_NE0;
  }
  function neToReal(t_ne) {
    return REAL_AT_NE0 + t_ne;
  }

  // Build the widget inside a container element
  function buildForm(container) {
    // create a stable unique prefix so multiple widgets do not conflict
    var prefix = 'bby-' + Math.random().toString(36).slice(2, 9) + '-';

    container.classList.add('bby-converter-container');

    container.innerHTML = '\
      <div class="bby-form" style="max-width:460px;border:1px solid #ddd;padding:10px;border-radius:6px;background:#fafafa;">\
        <div style="margin-bottom:8px;font-weight:600">Umrechnung NSY, n.E. und Echtzeit</div>\
        <div style="display:flex;gap:8px;align-items:center;margin-bottom:6px;">\
          <label for="' + prefix + 'bby" style="min-width:90px">VSY / NSY</label>\
          <input id="' + prefix + 'bby" type="number" step="any" style="flex:1" placeholder="4">\
          <select id="' + prefix + 'bby-era" aria-label="BBY or ABY" style="width:100px;margin-left:6px">\
            <option value="BBY">VSY (BBY)</option>\
            <option value="ABY" selected="true">NSY (ABY)</option>\
          </select>\
        </div>\
        <div style="display:flex;gap:8px;align-items:center;margin-bottom:6px;">\
          <label for="' + prefix + 'ne" style="min-width:90px">v.E. / n.E.</label>\
          <input id="' + prefix + 'ne" type="number" step="any" style="flex:1" placeholder="0">\
          <select id="' + prefix + 'ne-era' + '" aria-label="v.E. or n.E." style="width:100px;margin-left:6px">\
            <option value="v.E.">v.E.</option>\
            <option value="n.E." selected="true">n.E.</option>\
          </select>\
        </div>\
        <div style="display:flex;gap:8px;align-items:center;margin-bottom:8px;">\
          <label for="' + prefix + 'real" style="min-width:90px">Echtzeit</label>\
          <input id="' + prefix + 'real" type="number" step="any" style="flex:1" placeholder="1995">\
        </div>\
        <div style="font-size:90%;color:#666">Fixpunkt: 4 NSY = 0 n.E. = 1995</div>\
      </div>';

    // return references
    return {
      bbyInput: container.querySelector('#' + prefix + 'bby'),
      bbyEra: container.querySelector('#' + prefix + 'bby-era'),
      neInput: container.querySelector('#' + prefix + 'ne'),
      neEra: container.querySelector('#' + prefix + 'ne-era'),
      realInput: container.querySelector('#' + prefix + 'real')
    };
  }

  // main init for a container
  function initConverter(container) {
    if (container.dataset.bbyInit) return;
    container.dataset.bbyInit = '1';

    var els = buildForm(container);
    var isUpdating = false;

    function updateTargetsFromNE(t_ne) {
      isUpdating = true;
      // update v.E./n.E.
      var ve = neToVE(t_ne);
      els.neInput.value = fmt(ve.value);
      els.neEra.value = ve.era;

      // update BBY/ABY
      var b = neToBBY(t_ne);
      els.bbyInput.value = fmt(b.value);
      els.bbyEra.value = b.era;

      // update real time
      els.realInput.value = fmt(neToReal(t_ne));
      isUpdating = false;
    }

    function onBBYChanged() {
      if (isUpdating) return;
      var v = els.bbyInput.value;
      var era = els.bbyEra.value;
      var t_ne = bbyToNE(v, era);
      if (!isFinite(t_ne)) {
        // clear other fields
        isUpdating = true;
        els.neInput.value = '';
        els.realInput.value = '';
        isUpdating = false;
        return;
      }
      updateTargetsFromNE(t_ne);
    }

    function onNEChanged() {
      if (isUpdating) return;
      var v = els.neInput.value;
      var era = els.neEra.value;
      var t_ne;
      var parsed = parseFloat(v);
      if (isNaN(parsed)) {
        isUpdating = true;
        els.bbyInput.value = '';
        els.realInput.value = '';
        isUpdating = false;
        return;
      }
      t_ne = (era === 'n.E.') ? parsed : -parsed;
      updateTargetsFromNE(t_ne);
    }

    function onRealChanged() {
      if (isUpdating) return;
      var v = els.realInput.value;
      var parsed = parseFloat(v);
      if (isNaN(parsed)) {
        isUpdating = true;
        els.bbyInput.value = '';
        els.neInput.value = '';
        isUpdating = false;
        return;
      }
      var t_ne = realToNE(parsed);
      updateTargetsFromNE(t_ne);
    }

    // attach events
    els.bbyInput.addEventListener('input', onBBYChanged);
    els.bbyEra.addEventListener('change', onBBYChanged);
    els.neInput.addEventListener('input', onNEChanged);
    els.neEra.addEventListener('change', onNEChanged);
    els.realInput.addEventListener('input', onRealChanged);

    // optionally initialise with defaults (4 ABY = 0 n.E.)
    // we'll leave fields blank by default; user can click a helper link if you want defaults
  }

  // Auto-init on DOM ready for every container element
  function autoInit() {
    var nodes = document.querySelectorAll('.bby-converter');
    nodes.forEach(function (n) {
      try { initConverter(n); } catch (e) { /* fail silently per container */ }
    });
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', autoInit);
  } else {
    setTimeout(autoInit, 0);
  }

  // expose a global init function if someone wants to programmatically init a container
  window.BBYConverter = {
    init: initConverter,
    // also expose the configuration anchors if you want to change them programmatically:
    _config: {
      NE_AT_ABY: NE_AT_ABY,
      REAL_AT_NE0: REAL_AT_NE0
    }
  };

})();