Zum Inhalt springen

MediaWiki:Gadget-GlobalVariables.js: Unterschied zwischen den Versionen

Aus Home Wiki
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Zeile 1: Zeile 1:
mw.loader.using(['mediawiki.util']).then(function () {
mw.loader.using(['mediawiki.util']).then(function () {


     const globalVars = {}; // speichert die Variablenwerte
     const globalVars = {};       // speichert die Variablenwerte
     const seenVars = new Set(); // speichert, welche Variablen schon hinzugefügt wurden
     const seenVars = new Set(); // um Duplikate zu vermeiden
     const variableBlocks = {}; // speichert alle Codeblöcke pro Variable
     const variableBlocks = {};   // speichert die Blöcke pro Variable


     // Leiste oben erstellen
     // Leiste oben erstellen
Zeile 17: Zeile 17:
     bar.style.marginBottom = '12px';
     bar.style.marginBottom = '12px';
     document.querySelector('#content').prepend(bar);
     document.querySelector('#content').prepend(bar);
    // Button für alle Variablen
    const applyAllBtn = document.createElement('button');
    applyAllBtn.textContent = '→ Auf alle anwenden';
    applyAllBtn.style.padding = '4px 8px';
    applyAllBtn.style.cursor = 'pointer';
    applyAllBtn.style.marginBottom = '8px';
    applyAllBtn.style.border = '1px solid #888';
    applyAllBtn.style.borderRadius = '4px';
    applyAllBtn.style.background = '#eee';
    bar.appendChild(applyAllBtn);


     function addVarOnce(varName) {
     function addVarOnce(varName) {
         if (seenVars.has(varName)) return; // schon hinzugefügt
         if (seenVars.has(varName)) return;
         seenVars.add(varName);
         seenVars.add(varName);


Zeile 29: Zeile 40:
         lbl.style.fontSize = '12px';
         lbl.style.fontSize = '12px';
         lbl.style.color = '#333';
         lbl.style.color = '#333';
        const container = document.createElement('div');
        container.style.display = 'flex';
        container.style.alignItems = 'center';
        container.style.gap = '4px';


         const span = document.createElement('span');
         const span = document.createElement('span');
         span.textContent = varName + ':';
         span.textContent = varName + ':';
         container.appendChild(span);
         lbl.appendChild(span);


         const inp = document.createElement('input');
         const inp = document.createElement('input');
Zeile 43: Zeile 49:
         inp.value = '';
         inp.value = '';
         inp.dataset.varId = varName;
         inp.dataset.varId = varName;
         inp.style.width = '100px';
         inp.style.width = '120px';
         inp.style.padding = '2px 4px';
         inp.style.padding = '2px 4px';
         inp.style.border = '1px solid #ccc';
         inp.style.border = '1px solid #ccc';
Zeile 49: Zeile 55:
         inp.style.fontFamily = 'monospace';
         inp.style.fontFamily = 'monospace';
         inp.style.fontSize = '12px';
         inp.style.fontSize = '12px';
         container.appendChild(inp);
         lbl.appendChild(inp);
 
        // Button hinzufügen
        const btn = document.createElement('button');
        btn.textContent = '→ Anwenden';
        btn.style.padding = '2px 6px';
        btn.style.fontSize = '11px';
        btn.style.cursor = 'pointer';
        btn.onclick = () => {
            globalVars[varName] = inp.value;
            // Alle Blöcke updaten
            const blocks = variableBlocks[varName] || [];
            blocks.forEach(pre => {
                pre.textContent = pre.textContent.replace(new RegExp(`{{${varName}}}`, 'g'), inp.value);
            });
        };
        container.appendChild(btn);


        lbl.appendChild(container);
         bar.appendChild(lbl);
         bar.appendChild(lbl);
     }
     }


     // Alle dynamischen Codeblöcke auf der Seite initialisieren
     // Alle dynamischen Codeblöcke initialisieren
     document.querySelectorAll('pre.dynamic-code').forEach(pre => {
     document.querySelectorAll('pre.dynamic-code').forEach(pre => {
         const regex = /{{(\w+)}}/g;
         const regex = /{{(\w+)}}/g;
Zeile 79: Zeile 68:
             addVarOnce(varName);
             addVarOnce(varName);


            // Codeblock für diese Variable merken
             if (!variableBlocks[varName]) variableBlocks[varName] = [];
             if (!variableBlocks[varName]) variableBlocks[varName] = [];
             variableBlocks[varName].push(pre);
             variableBlocks[varName].push(pre);
         }
         }
     });
     });
    // Button-Event für alle Variablen
    applyAllBtn.onclick = () => {
        // alle globalen Inputs nehmen
        const inputs = bar.querySelectorAll('input');
        inputs.forEach(inp => {
            const name = inp.dataset.varId;
            globalVars[name] = inp.value;
            // alle Blöcke updaten
            const blocks = variableBlocks[name] || [];
            blocks.forEach(pre => {
                pre.textContent = pre.textContent.replace(new RegExp(`{{${name}}}`, 'g'), inp.value);
                // auch lokale Inputs in den Codeblöcken synchronisieren, falls du welche später einfügst
                const container = pre.previousSibling;
                if (container && container.querySelector) {
                    const localInp = container.querySelector(`input[data-var-id="${name}"]`);
                    if (localInp) localInp.value = inp.value;
                }
            });
        });
    };


     console.log('Globale Variablenleiste initialisiert:', Array.from(seenVars));
     console.log('Globale Variablenleiste initialisiert:', Array.from(seenVars));


});
});

Version vom 14. Februar 2026, 12:31 Uhr

mw.loader.using(['mediawiki.util']).then(function () {

    const globalVars = {};       // speichert die Variablenwerte
    const seenVars = new Set();  // um Duplikate zu vermeiden
    const variableBlocks = {};   // speichert die Blöcke pro Variable

    // Leiste oben erstellen
    const bar = document.createElement('div');
    bar.id = 'global-var-bar';
    bar.style.display = 'flex';
    bar.style.flexWrap = 'wrap';
    bar.style.gap = '8px';
    bar.style.padding = '8px';
    bar.style.border = '1px solid #bbb';
    bar.style.borderRadius = '6px';
    bar.style.background = '#f5f5f5';
    bar.style.marginBottom = '12px';
    document.querySelector('#content').prepend(bar);

    // Button für alle Variablen
    const applyAllBtn = document.createElement('button');
    applyAllBtn.textContent = '→ Auf alle anwenden';
    applyAllBtn.style.padding = '4px 8px';
    applyAllBtn.style.cursor = 'pointer';
    applyAllBtn.style.marginBottom = '8px';
    applyAllBtn.style.border = '1px solid #888';
    applyAllBtn.style.borderRadius = '4px';
    applyAllBtn.style.background = '#eee';
    bar.appendChild(applyAllBtn);

    function addVarOnce(varName) {
        if (seenVars.has(varName)) return;
        seenVars.add(varName);

        globalVars[varName] = '';

        const lbl = document.createElement('label');
        lbl.style.display = 'flex';
        lbl.style.flexDirection = 'column';
        lbl.style.fontSize = '12px';
        lbl.style.color = '#333';

        const span = document.createElement('span');
        span.textContent = varName + ':';
        lbl.appendChild(span);

        const inp = document.createElement('input');
        inp.type = 'text';
        inp.value = '';
        inp.dataset.varId = varName;
        inp.style.width = '120px';
        inp.style.padding = '2px 4px';
        inp.style.border = '1px solid #ccc';
        inp.style.borderRadius = '4px';
        inp.style.fontFamily = 'monospace';
        inp.style.fontSize = '12px';
        lbl.appendChild(inp);

        bar.appendChild(lbl);
    }

    // Alle dynamischen Codeblöcke initialisieren
    document.querySelectorAll('pre.dynamic-code').forEach(pre => {
        const regex = /{{(\w+)}}/g;
        let match;
        while ((match = regex.exec(pre.textContent)) !== null) {
            const varName = match[1];
            addVarOnce(varName);

            if (!variableBlocks[varName]) variableBlocks[varName] = [];
            variableBlocks[varName].push(pre);
        }
    });

    // Button-Event für alle Variablen
    applyAllBtn.onclick = () => {
        // alle globalen Inputs nehmen
        const inputs = bar.querySelectorAll('input');
        inputs.forEach(inp => {
            const name = inp.dataset.varId;
            globalVars[name] = inp.value;

            // alle Blöcke updaten
            const blocks = variableBlocks[name] || [];
            blocks.forEach(pre => {
                pre.textContent = pre.textContent.replace(new RegExp(`{{${name}}}`, 'g'), inp.value);

                // auch lokale Inputs in den Codeblöcken synchronisieren, falls du welche später einfügst
                const container = pre.previousSibling;
                if (container && container.querySelector) {
                    const localInp = container.querySelector(`input[data-var-id="${name}"]`);
                    if (localInp) localInp.value = inp.value;
                }
            });
        });
    };

    console.log('Globale Variablenleiste initialisiert:', Array.from(seenVars));

});