BibTest.js: Difference between revisions
From Illustrations in German Translations of Mark Twain's Works
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
/** | |||
* Bibliography Tag Filter and Sorting | |||
* Lädt automatisch wenn die Seite "Bibliography" aufgerufen wird | |||
*/ | |||
(function() { | |||
'use strict'; | |||
let ACTIVE_TAGS = new Set(); | |||
let ACTIVE_TAGS = new Set(); | |||
function init() { | |||
const tagBar = document.getElementById('tagBar'); | |||
document. | const bibliography = document.getElementById('listView'); | ||
if (!tagBar || !bibliography) return; | |||
// Sammle alle verfügbaren Tags | |||
const allTags = new Set(); | |||
document.querySelectorAll('.bib-entry').forEach(entry => { | |||
const tags = entry.getAttribute('data-tags'); | |||
if (tags) { | |||
tags.split(' ').forEach(tag => allTags.add(tag)); | |||
} | |||
}); | |||
// | |||
const | |||
// Sortiere und zeige Tags | |||
const sortedTags = Array.from(allTags).sort(); | |||
tagBar.innerHTML = ''; | |||
sortedTags.forEach(tag => { | |||
const span = document.createElement('span'); | |||
span.className = 'tag inactive'; | |||
span.textContent = tag; | |||
span.style.cursor = 'pointer'; | |||
span.onclick = () => toggleTag(tag, span); | |||
tagBar.appendChild(span); | |||
}); | }); | ||
// Sortiere Einträge alphabetisch | |||
sortEntries(); | |||
} | |||
} | |||
function toggleTag(tag) { | function toggleTag(tag, element) { | ||
if (ACTIVE_TAGS.has(tag)) { | if (ACTIVE_TAGS.has(tag)) { | ||
ACTIVE_TAGS.delete(tag); | |||
element.classList.remove('active'); | |||
element.classList.add('inactive'); | |||
} else { | } else { | ||
ACTIVE_TAGS.add(tag); | |||
element.classList.add('active'); | |||
element.classList.remove('inactive'); | |||
} | } | ||
filterEntries(); | |||
} | |||
function sortEntries() { | |||
const container = document.getElementById('listView'); | |||
if (!container) return; | |||
const entries = Array.from(container.querySelectorAll('.bib-entry')); | |||
entries.sort((a, b) => { | |||
const titleA = a.querySelector('h3')?.textContent || ''; | |||
const titleB = b.querySelector('h3')?.textContent || ''; | |||
return titleA.localeCompare(titleB); | |||
}); | }); | ||
// | // Neu einfügen | ||
entries.forEach(entry => container.appendChild(entry)); | |||
} | |||
function | function filterEntries() { | ||
const | const entries = document.querySelectorAll('.bib-entry'); | ||
if ( | |||
entries.forEach(entry => { | |||
const entryTags = (entry.getAttribute('data-tags') || '').split(' ').filter(t => t); | |||
if (ACTIVE_TAGS.size === 0) { | |||
entry.style.display = 'block'; | |||
} else { | |||
// AND-Logik: Entry muss ALLE aktiven Tags haben | |||
const hasAllTags = Array.from(ACTIVE_TAGS).every(tag => entryTags.includes(tag)); | |||
entry.style.display = hasAllTags ? 'block' : 'none'; | |||
} | |||
}); | |||
} | |||
if ( | // Abstract-Button Toggle | ||
document.addEventListener('click', function(e) { | |||
if (e.target.classList.contains('expand-btn')) { | |||
const abstractDiv = e.target.nextElementSibling; | |||
if (abstractDiv && abstractDiv.classList.contains('abstract')) { | |||
abstractDiv.classList.toggle('hidden'); | |||
e.target.textContent = abstractDiv.classList.contains('hidden') | |||
? 'Show Abstract' | |||
: 'Hide Abstract'; | |||
} | |||
} | } | ||
}); | |||
// Starte wenn DOM bereit | |||
if (document.readyState === 'loading') { | |||
document.addEventListener('DOMContentLoaded', init); | |||
} else { | |||
init(); | |||
} | |||
// | |||
} | |||
})(); | })(); | ||
Revision as of 01:06, 12 May 2026
/**
* Bibliography Tag Filter and Sorting
* Lädt automatisch wenn die Seite "Bibliography" aufgerufen wird
*/
(function() {
'use strict';
let ACTIVE_TAGS = new Set();
function init() {
const tagBar = document.getElementById('tagBar');
const bibliography = document.getElementById('listView');
if (!tagBar || !bibliography) return;
// Sammle alle verfügbaren Tags
const allTags = new Set();
document.querySelectorAll('.bib-entry').forEach(entry => {
const tags = entry.getAttribute('data-tags');
if (tags) {
tags.split(' ').forEach(tag => allTags.add(tag));
}
});
// Sortiere und zeige Tags
const sortedTags = Array.from(allTags).sort();
tagBar.innerHTML = '';
sortedTags.forEach(tag => {
const span = document.createElement('span');
span.className = 'tag inactive';
span.textContent = tag;
span.style.cursor = 'pointer';
span.onclick = () => toggleTag(tag, span);
tagBar.appendChild(span);
});
// Sortiere Einträge alphabetisch
sortEntries();
}
function toggleTag(tag, element) {
if (ACTIVE_TAGS.has(tag)) {
ACTIVE_TAGS.delete(tag);
element.classList.remove('active');
element.classList.add('inactive');
} else {
ACTIVE_TAGS.add(tag);
element.classList.add('active');
element.classList.remove('inactive');
}
filterEntries();
}
function sortEntries() {
const container = document.getElementById('listView');
if (!container) return;
const entries = Array.from(container.querySelectorAll('.bib-entry'));
entries.sort((a, b) => {
const titleA = a.querySelector('h3')?.textContent || '';
const titleB = b.querySelector('h3')?.textContent || '';
return titleA.localeCompare(titleB);
});
// Neu einfügen
entries.forEach(entry => container.appendChild(entry));
}
function filterEntries() {
const entries = document.querySelectorAll('.bib-entry');
entries.forEach(entry => {
const entryTags = (entry.getAttribute('data-tags') || '').split(' ').filter(t => t);
if (ACTIVE_TAGS.size === 0) {
entry.style.display = 'block';
} else {
// AND-Logik: Entry muss ALLE aktiven Tags haben
const hasAllTags = Array.from(ACTIVE_TAGS).every(tag => entryTags.includes(tag));
entry.style.display = hasAllTags ? 'block' : 'none';
}
});
}
// Abstract-Button Toggle
document.addEventListener('click', function(e) {
if (e.target.classList.contains('expand-btn')) {
const abstractDiv = e.target.nextElementSibling;
if (abstractDiv && abstractDiv.classList.contains('abstract')) {
abstractDiv.classList.toggle('hidden');
e.target.textContent = abstractDiv.classList.contains('hidden')
? 'Show Abstract'
: 'Hide Abstract';
}
}
});
// Starte wenn DOM bereit
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();