MediaWiki:Common.js: Unterschied zwischen den Versionen
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
| Zeile 1: | Zeile 1: | ||
/* Das folgende JavaScript wird für alle Benutzer geladen. */ | /* Das folgende JavaScript wird für alle Benutzer geladen. */ | ||
/* ==== Sidebar: klickbare Kopfzeile + Gruppen mit Auf/Zu + Persistenz (Vector-2022 | /* ==== Sidebar: klickbare Kopfzeile + Gruppen mit Auf/Zu + Persistenz (Vector-2022 & Drawer) ==== */ | ||
(function () { | (function () { | ||
if (mw.config.get('skin') !== 'vector-2022') return; | if (mw.config.get('skin') !== 'vector-2022') return; | ||
var STORAGE_KEY = 'kr-sb-state:v3:' + (mw.config.get('wgUserName') || 'anon'); | var STORAGE_KEY = 'kr-sb-state:v3:' + (mw.config.get('wgUserName') || 'anon'); | ||
var STATE = loadState(); | var STATE = loadState(); | ||
function loadState() { | function loadState(){ try { return JSON.parse(localStorage.getItem(STORAGE_KEY) || '{}'); } catch(e){ return {}; } } | ||
function saveState(){ try { localStorage.setItem(STORAGE_KEY, JSON.stringify(STATE)); } catch(e){} } | |||
function saveState() { | |||
function extractDirective(a){ | |||
function extractDirective(a) { | |||
if (!a) return null; | if (!a) return null; | ||
var href = a.getAttribute('href') || ''; | var href = a.getAttribute('href') || ''; | ||
try { href = new URL(href, location.href).hash || ''; } catch (e) {} | try { href = new URL(href, location.href).hash || ''; } catch(e){} | ||
var s = (href || '').trim().toLowerCase(); | var s = (href || '').trim().toLowerCase(); | ||
if (s.includes('#group:')) return { kind: 'group', value: decodeURIComponent(s.split('#group:').pop()) }; | if (s.includes('#group:')) return { kind:'group', value: decodeURIComponent(s.split('#group:').pop()) }; | ||
if (s.includes('#link:')) return { kind: 'link', value: decodeURIComponent(s.split('#link:').pop()) }; | if (s.includes('#link:')) return { kind:'link', value: decodeURIComponent(s.split('#link:').pop()) }; | ||
return null; | return null; | ||
} | } | ||
function stripDirectiveHash(a){ | function stripDirectiveHash(a){ | ||
if (!a) return; | if (!a) return; | ||
a.setAttribute('href', (a.getAttribute('href') || '').replace(/#(?:link|group):[^#]*$/i,'')); | a.setAttribute('href', (a.getAttribute('href') || '').replace(/#(?:link|group):[^#]*$/i, '')); | ||
} | } | ||
function hasRealHref(a){ | function hasRealHref(a){ | ||
| Zeile 34: | Zeile 28: | ||
return !/^#/.test(h); | return !/^#/.test(h); | ||
} | } | ||
function normId(s){ | function normId(s){ return String(s||'').trim().toLowerCase().replace(/\s+/g,'-').replace(/[^\w\-:.|]/g,''); } | ||
function toggleSet(li, arrow, expanded){ | function toggleSet(li, arrow, expanded){ | ||
if (expanded) { | if (expanded){ li.classList.remove('is-collapsed'); arrow.setAttribute('aria-expanded','true'); } | ||
else { li.classList.add('is-collapsed'); arrow.setAttribute('aria-expanded','false'); } | |||
} | } | ||
function makeHead(li, keepLink, containerUL, labelOverride){ | |||
function makeHead(li, keepLink, containerUL, labelOverride) { | |||
var a = li.querySelector(':scope > a'); | var a = li.querySelector(':scope > a'); | ||
var head = document.createElement('div'); | var head = document.createElement('div'); head.className = 'kr-head'; | ||
var btn = document.createElement('button'); btn.type='button'; btn.className='kr-arrow'; btn.setAttribute('aria-expanded','false'); | |||
var btn = document.createElement('button'); | |||
head.appendChild(btn); | head.appendChild(btn); | ||
if (keepLink && a) { stripDirectiveHash(a); head.appendChild(a); } | if (keepLink && a){ stripDirectiveHash(a); head.appendChild(a); } | ||
else { | else { | ||
var span = document.createElement('span'); | var span = document.createElement('span'); span.className='kr-title'; | ||
span.textContent = labelOverride || (a && a.textContent) || ''; | span.textContent = labelOverride || (a && a.textContent) || ''; | ||
head.appendChild(span); | head.appendChild(span); if (a) a.remove(); | ||
} | } | ||
li.insertBefore(head, li.firstChild); | li.insertBefore(head, li.firstChild); | ||
var portlet = containerUL.closest('.vector-menu'); | var portlet = containerUL.closest('.vector-menu'); | ||
var pid = (portlet && (portlet.id || portlet.getAttribute('id'))) || 'portlet'; | var pid = (portlet && (portlet.id || portlet.getAttribute('id'))) || 'portlet'; | ||
| Zeile 76: | Zeile 54: | ||
li.dataset.krId = normId(pid + '::' + labelText); | li.dataset.krId = normId(pid + '::' + labelText); | ||
// Default: zu | // Default: zu; dann gespeicherten Zustand anwenden | ||
toggleSet(li, btn, | toggleSet(li, btn, false); | ||
var st = STATE[li.dataset.krId]; | var st = STATE[li.dataset.krId]; | ||
if (st === 1) toggleSet(li, btn, true); | if (st === 1) toggleSet(li, btn, true); | ||
| Zeile 84: | Zeile 62: | ||
} | } | ||
function handleArrowClick(btn) { | function handleArrowClick(btn){ | ||
var li = btn.closest('li.kr-group, li.kr-top'); | var li = btn.closest('li.kr-group, li.kr-top'); if (!li) return; | ||
var willExpand = li.classList.contains('is-collapsed'); | var willExpand = li.classList.contains('is-collapsed'); | ||
toggleSet(li, btn, willExpand); | |||
var id = li.dataset.krId; | var id = li.dataset.krId; | ||
if (id) { | if (id){ | ||
STATE[id] = willExpand ? 1 : 0; | |||
saveState(); | |||
} | } | ||
} | } | ||
// Globaler Delegations-Listener | // Globaler Delegations-Listener – funktioniert auch im Drawer | ||
document.addEventListener('click', function (e) { | document.addEventListener('click', function(e){ | ||
var btn = e.target && e.target.closest && e.target.closest('.kr-arrow'); | var btn = e.target && e.target.closest && e.target.closest('.kr-arrow'); | ||
if (!btn) return; | if (!btn) return; | ||
e.preventDefault(); | e.preventDefault(); | ||
handleArrowClick(btn); | handleArrowClick(btn); | ||
}, true); | }, true); | ||
function buildGroups(ul) { | function buildGroups(ul){ | ||
var lis = Array.from(ul.querySelectorAll(':scope > li.mw-list-item')); | var lis = Array.from(ul.querySelectorAll(':scope > li.mw-list-item')); | ||
var current = null; | var current = null; | ||
lis.forEach(function (li) { | lis.forEach(function (li){ | ||
var a = li.querySelector(':scope > a'); | var a = li.querySelector(':scope > a'); | ||
var d = extractDirective(a); | var d = extractDirective(a); | ||
if (d && d.kind === 'group') { | if (d && d.kind === 'group'){ | ||
var clickable = hasRealHref(a); | var clickable = hasRealHref(a); | ||
li.classList.add('kr-group'); | li.classList.add('kr-group'); | ||
makeHead(li, clickable, ul, (a && a.textContent) || d.value || ''); | makeHead(li, clickable, ul, (a && a.textContent) || d.value || ''); | ||
if (!clickable && a) a.remove(); | if (!clickable && a) a.remove(); else if (clickable) stripDirectiveHash(a); | ||
var sub = document.createElement('ul'); | var sub = document.createElement('ul'); sub.className='kr-sub'; li.appendChild(sub); | ||
current = sub; | current = sub; | ||
return; | return; | ||
} | } | ||
if (current && !(d && (d.kind === 'group' || d.kind === 'link'))) | if (current && !(d && (d.kind === 'group' || d.kind === 'link'))) current.appendChild(li); | ||
}); | }); | ||
} | } | ||
function buildPortlets(root) { | function buildPortlets(root){ | ||
var lists = root.querySelectorAll('.vector-menu .vector-menu-content-list'); | var lists = root.querySelectorAll('.vector-menu .vector-menu-content-list'); | ||
lists.forEach(function (ul) { | lists.forEach(function (ul){ | ||
if (ul.dataset.krSbDone) return; | if (ul.dataset.krSbDone) return; | ||
ul.dataset.krSbDone = '1'; | ul.dataset.krSbDone = '1'; | ||
var items = Array.from(ul.querySelectorAll(':scope > li.mw-list-item')); | var items = Array.from(ul.querySelectorAll(':scope > li.mw-list-item')); | ||
var topIdx = items.findIndex(function (li) { | var topIdx = items.findIndex(function(li){ | ||
var a = li.querySelector(':scope > a'); | var a = li.querySelector(':scope > a'); var d = extractDirective(a); | ||
return d && d.kind === 'link'; | return d && d.kind === 'link'; | ||
}); | }); | ||
if (topIdx >= 0) { | if (topIdx >= 0){ | ||
var top = items[topIdx]; | var top = items[topIdx]; | ||
top.classList.add('kr-top'); | top.classList.add('kr-top'); | ||
makeHead(top, true, ul); | makeHead(top, true, ul); | ||
stripDirectiveHash(top.querySelector(':scope > a')); | stripDirectiveHash(top.querySelector(':scope > a')); | ||
var sub = document.createElement('ul'); | var sub = document.createElement('ul'); sub.className='kr-sub'; top.appendChild(sub); | ||
for (var i = topIdx + 1; i < items.length; i++) sub.appendChild(items[i]); | for (var i = topIdx + 1; i < items.length; i++) sub.appendChild(items[i]); | ||
buildGroups(sub); | buildGroups(sub); | ||
} else { | } else { | ||
buildGroups(ul); | buildGroups(ul); | ||
| Zeile 194: | Zeile 145: | ||
} | } | ||
function init() { | function init(){ | ||
document.querySelectorAll('#mw-panel, #vector-main-menu, #vector-main-menu-pinned, .vector-drawer') | document.querySelectorAll('#mw-panel, #vector-main-menu, #vector-main-menu-pinned, .vector-drawer') | ||
.forEach(processRoot); | .forEach(processRoot); | ||
| Zeile 202: | Zeile 153: | ||
mw.hook('wikipage.content').add(init); | mw.hook('wikipage.content').add(init); | ||
// | // Drawer erscheint dynamisch → beobachten | ||
var mo = new MutationObserver(function (muts) { | var mo = new MutationObserver(function(muts){ | ||
muts.forEach(function (m) { | muts.forEach(function(m){ | ||
m.addedNodes && m.addedNodes.forEach(function (n) { | m.addedNodes && m.addedNodes.forEach(function(n){ | ||
if (!(n instanceof Element)) return; | if (!(n instanceof Element)) return; | ||
if (n.matches('#vector-main-menu, #vector-main-menu-pinned, .vector-drawer, #mw-panel')) processRoot(n); | if (n.matches('#vector-main-menu, #vector-main-menu-pinned, .vector-drawer, #mw-panel')) processRoot(n); | ||
| Zeile 213: | Zeile 164: | ||
}); | }); | ||
}); | }); | ||
mo.observe(document.body, { childList: true, subtree: true }); | mo.observe(document.body, { childList:true, subtree:true }); | ||
})(); | })(); | ||
Version vom 10. Oktober 2025, 16:01 Uhr
/* Das folgende JavaScript wird für alle Benutzer geladen. */
/* ==== Sidebar: klickbare Kopfzeile + Gruppen mit Auf/Zu + Persistenz (Vector-2022 & Drawer) ==== */
(function () {
if (mw.config.get('skin') !== 'vector-2022') return;
var STORAGE_KEY = 'kr-sb-state:v3:' + (mw.config.get('wgUserName') || 'anon');
var STATE = loadState();
function loadState(){ try { return JSON.parse(localStorage.getItem(STORAGE_KEY) || '{}'); } catch(e){ return {}; } }
function saveState(){ try { localStorage.setItem(STORAGE_KEY, JSON.stringify(STATE)); } catch(e){} }
function extractDirective(a){
if (!a) return null;
var href = a.getAttribute('href') || '';
try { href = new URL(href, location.href).hash || ''; } catch(e){}
var s = (href || '').trim().toLowerCase();
if (s.includes('#group:')) return { kind:'group', value: decodeURIComponent(s.split('#group:').pop()) };
if (s.includes('#link:')) return { kind:'link', value: decodeURIComponent(s.split('#link:').pop()) };
return null;
}
function stripDirectiveHash(a){
if (!a) return;
a.setAttribute('href', (a.getAttribute('href') || '').replace(/#(?:link|group):[^#]*$/i, ''));
}
function hasRealHref(a){
if (!a) return false;
var h = a.getAttribute('href') || '';
return !/^#/.test(h);
}
function normId(s){ return String(s||'').trim().toLowerCase().replace(/\s+/g,'-').replace(/[^\w\-:.|]/g,''); }
function toggleSet(li, arrow, expanded){
if (expanded){ li.classList.remove('is-collapsed'); arrow.setAttribute('aria-expanded','true'); }
else { li.classList.add('is-collapsed'); arrow.setAttribute('aria-expanded','false'); }
}
function makeHead(li, keepLink, containerUL, labelOverride){
var a = li.querySelector(':scope > a');
var head = document.createElement('div'); head.className = 'kr-head';
var btn = document.createElement('button'); btn.type='button'; btn.className='kr-arrow'; btn.setAttribute('aria-expanded','false');
head.appendChild(btn);
if (keepLink && a){ stripDirectiveHash(a); head.appendChild(a); }
else {
var span = document.createElement('span'); span.className='kr-title';
span.textContent = labelOverride || (a && a.textContent) || '';
head.appendChild(span); if (a) a.remove();
}
li.insertBefore(head, li.firstChild);
var portlet = containerUL.closest('.vector-menu');
var pid = (portlet && (portlet.id || portlet.getAttribute('id'))) || 'portlet';
var labelText = (labelOverride || (head.querySelector('a, .kr-title') || {}).textContent || '').trim();
li.dataset.krId = normId(pid + '::' + labelText);
// Default: zu; dann gespeicherten Zustand anwenden
toggleSet(li, btn, false);
var st = STATE[li.dataset.krId];
if (st === 1) toggleSet(li, btn, true);
return head;
}
function handleArrowClick(btn){
var li = btn.closest('li.kr-group, li.kr-top'); if (!li) return;
var willExpand = li.classList.contains('is-collapsed');
toggleSet(li, btn, willExpand);
var id = li.dataset.krId;
if (id){
STATE[id] = willExpand ? 1 : 0;
saveState();
}
}
// Globaler Delegations-Listener – funktioniert auch im Drawer
document.addEventListener('click', function(e){
var btn = e.target && e.target.closest && e.target.closest('.kr-arrow');
if (!btn) return;
e.preventDefault();
handleArrowClick(btn);
}, true);
function buildGroups(ul){
var lis = Array.from(ul.querySelectorAll(':scope > li.mw-list-item'));
var current = null;
lis.forEach(function (li){
var a = li.querySelector(':scope > a');
var d = extractDirective(a);
if (d && d.kind === 'group'){
var clickable = hasRealHref(a);
li.classList.add('kr-group');
makeHead(li, clickable, ul, (a && a.textContent) || d.value || '');
if (!clickable && a) a.remove(); else if (clickable) stripDirectiveHash(a);
var sub = document.createElement('ul'); sub.className='kr-sub'; li.appendChild(sub);
current = sub;
return;
}
if (current && !(d && (d.kind === 'group' || d.kind === 'link'))) current.appendChild(li);
});
}
function buildPortlets(root){
var lists = root.querySelectorAll('.vector-menu .vector-menu-content-list');
lists.forEach(function (ul){
if (ul.dataset.krSbDone) return;
ul.dataset.krSbDone = '1';
var items = Array.from(ul.querySelectorAll(':scope > li.mw-list-item'));
var topIdx = items.findIndex(function(li){
var a = li.querySelector(':scope > a'); var d = extractDirective(a);
return d && d.kind === 'link';
});
if (topIdx >= 0){
var top = items[topIdx];
top.classList.add('kr-top');
makeHead(top, true, ul);
stripDirectiveHash(top.querySelector(':scope > a'));
var sub = document.createElement('ul'); sub.className='kr-sub'; top.appendChild(sub);
for (var i = topIdx + 1; i < items.length; i++) sub.appendChild(items[i]);
buildGroups(sub);
} else {
buildGroups(ul);
}
// aufräumen
ul.querySelectorAll('a[href*="#link:"], a[href*="#group:"]').forEach(stripDirectiveHash);
var portlet = ul.closest('.vector-menu');
var heading = portlet && portlet.querySelector(':scope > .vector-menu-heading');
if (heading) heading.remove();
});
}
function processRoot(root){
if (!root || root.dataset.krSbRootDone) return;
root.dataset.krSbRootDone = '1';
root.classList.add('kr-sb');
buildPortlets(root);
}
function init(){
document.querySelectorAll('#mw-panel, #vector-main-menu, #vector-main-menu-pinned, .vector-drawer')
.forEach(processRoot);
}
init();
mw.hook('wikipage.content').add(init);
// Drawer erscheint dynamisch → beobachten
var mo = new MutationObserver(function(muts){
muts.forEach(function(m){
m.addedNodes && m.addedNodes.forEach(function(n){
if (!(n instanceof Element)) return;
if (n.matches('#vector-main-menu, #vector-main-menu-pinned, .vector-drawer, #mw-panel')) processRoot(n);
n.querySelectorAll && n.querySelectorAll('#vector-main-menu, #vector-main-menu-pinned, .vector-drawer, #mw-panel')
.forEach(processRoot);
});
});
});
mo.observe(document.body, { childList:true, subtree:true });
})();