MediaWiki

BibTest.js: Difference between revisions

From Illustrations in German Translations of Mark Twain's Works

No edit summary
No edit summary
Line 16: Line 16:
     if (!tagBar || !bibliography) return;
     if (!tagBar || !bibliography) return;


     // Verstecke leere Abstract-Buttons
     // Verstecke leere Abstract- und Notes-Buttons
     hideEmptyAbstracts();
     hideEmptyAbstracts();
    hideEmptyNotes();


     // Sammle alle Original-Taggamen aus den Einträgen
     // Sammle alle Original-Taggamen aus den Einträgen
Line 67: Line 68:
         expandBtn.style.display = 'none';
         expandBtn.style.display = 'none';
         abstractDiv.parentElement.style.display = 'none';
         abstractDiv.parentElement.style.display = 'none';
      }
    });
  }
  function hideEmptyNotes() {
    document.querySelectorAll('.bib-entry').forEach(entry => {
      const notesDiv = entry.querySelector('.notes p');
      const expandBtn = entry.querySelector('.expand-btn-notes');
     
      if (!notesDiv || !expandBtn) return;
     
      const notesText = notesDiv.textContent.trim();
     
      // Verstecke Button wenn Notes leer sind
      if (!notesText) {
        expandBtn.style.display = 'none';
        notesDiv.parentElement.style.display = 'none';
       }
       }
     });
     });
Line 123: Line 141:
       const abstractText = abstractDiv.textContent || '';
       const abstractText = abstractDiv.textContent || '';
       if (abstractText.toLowerCase().includes(searchText)) return true;
       if (abstractText.toLowerCase().includes(searchText)) return true;
    }
    // Suche in den Notes
    const notesDiv = entry.querySelector('.notes');
    if (notesDiv) {
      const notesText = notesDiv.textContent || '';
      if (notesText.toLowerCase().includes(searchText)) return true;
     }
     }


Line 150: Line 175:
   }
   }


   // Abstract-Button Toggle
   // Abstract- und Notes-Button Toggle
   document.addEventListener('click', function(e) {
   document.addEventListener('click', function(e) {
    // Abstract Button
     if (e.target.classList.contains('expand-btn')) {
     if (e.target.classList.contains('expand-btn')) {
       const abstractDiv = e.target.nextElementSibling;
       const abstractDiv = e.target.nextElementSibling;
Line 159: Line 185:
           ? 'Show Abstract'
           ? 'Show Abstract'
           : 'Hide Abstract';
           : 'Hide Abstract';
      }
    }
   
    // Notes Button
    if (e.target.classList.contains('expand-btn-notes')) {
      const notesDiv = e.target.nextElementSibling;
      if (notesDiv && notesDiv.classList.contains('notes')) {
        notesDiv.classList.toggle('hidden');
        e.target.textContent = notesDiv.classList.contains('hidden')
          ? 'Show Notes'
          : 'Hide Notes';
       }
       }
     }
     }

Revision as of 22:37, 21 May 2026

/**
 * Bibliography Tag Filter, Sorting and Search
 */

(function() {
  'use strict';

  let ACTIVE_TAGS = new Set();
  let SEARCH_TERM = '';

  function init() {
    const tagBar = document.getElementById('tagBar');
    const bibliography = document.getElementById('listView');
    const searchInput = document.getElementById('bibSearch');
    
    if (!tagBar || !bibliography) return;

    // Verstecke leere Abstract- und Notes-Buttons
    hideEmptyAbstracts();
    hideEmptyNotes();

    // Sammle alle Original-Taggamen aus den Einträgen
    const allTagsMap = new Map(); // normalized -> original name
    document.querySelectorAll('.bib-entry .tag').forEach(tagEl => {
      const originalName = tagEl.textContent.trim();
      const normalized = normalizeTag(originalName);
      allTagsMap.set(normalized, originalName);
    });

    // Lösche alte Tags aus tagBar
    tagBar.innerHTML = '';

    // Erstelle neue Tags mit Original-Namen
    Array.from(allTagsMap.entries())
      .sort((a, b) => a[1].localeCompare(b[1])) // Sortiere nach original name
      .forEach(([normalized, originalName]) => {
        const span = document.createElement('span');
        span.className = 'tag inactive';
        span.textContent = originalName; // Zeige original name
        span.dataset.normalized = normalized; // Speichere normalized für Filterung
        span.onclick = () => toggleTag(originalName, span, normalized);
        tagBar.appendChild(span);
      });

    // Search Input Listener
    if (searchInput) {
      searchInput.addEventListener('input', (e) => {
        SEARCH_TERM = e.target.value.toLowerCase();
        filterEntries();
      });
    }

    // Sortiere Einträge alphabetisch
    sortEntries();
  }

  function hideEmptyAbstracts() {
    document.querySelectorAll('.bib-entry').forEach(entry => {
      const abstractDiv = entry.querySelector('.abstract p');
      const expandBtn = entry.querySelector('.expand-btn');
      
      if (!abstractDiv || !expandBtn) return;
      
      const abstractText = abstractDiv.textContent.trim();
      
      // Verstecke Button wenn Abstract leer ist oder nur "No abstract available"
      if (!abstractText || abstractText === 'No abstract available.') {
        expandBtn.style.display = 'none';
        abstractDiv.parentElement.style.display = 'none';
      }
    });
  }

  function hideEmptyNotes() {
    document.querySelectorAll('.bib-entry').forEach(entry => {
      const notesDiv = entry.querySelector('.notes p');
      const expandBtn = entry.querySelector('.expand-btn-notes');
      
      if (!notesDiv || !expandBtn) return;
      
      const notesText = notesDiv.textContent.trim();
      
      // Verstecke Button wenn Notes leer sind
      if (!notesText) {
        expandBtn.style.display = 'none';
        notesDiv.parentElement.style.display = 'none';
      }
    });
  }

  function toggleTag(tagName, element, normalized) {
    if (ACTIVE_TAGS.has(normalized)) {
      ACTIVE_TAGS.delete(normalized);
      element.classList.remove('active');
      element.classList.add('inactive');
    } else {
      ACTIVE_TAGS.add(normalized);
      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);
    });

    entries.forEach(entry => container.appendChild(entry));
  }

  function normalizeTag(tag) {
    return tag.toLowerCase().replace(/\s+/g, '_').replace(/,/g, '').replace(/\./g, '').replace(/'/g, '').replace(/"/g, '');
  }

  function matchesSearch(entry) {
    if (!SEARCH_TERM) return true;

    const searchText = SEARCH_TERM;
    
    // Suche im Titel
    const title = entry.querySelector('h3')?.textContent || '';
    if (title.toLowerCase().includes(searchText)) return true;

    // Suche in den Tags
    const tagElements = entry.querySelectorAll('.tag');
    for (let tag of tagElements) {
      if (tag.textContent.toLowerCase().includes(searchText)) return true;
    }

    // Suche im Abstract
    const abstractDiv = entry.querySelector('.abstract');
    if (abstractDiv) {
      const abstractText = abstractDiv.textContent || '';
      if (abstractText.toLowerCase().includes(searchText)) return true;
    }

    // Suche in den Notes
    const notesDiv = entry.querySelector('.notes');
    if (notesDiv) {
      const notesText = notesDiv.textContent || '';
      if (notesText.toLowerCase().includes(searchText)) return true;
    }

    return false;
  }

  function filterEntries() {
    const entries = document.querySelectorAll('.bib-entry');
    
    entries.forEach(entry => {
      const entryTags = (entry.getAttribute('data-tags') || '').split(' ').filter(t => t);
      
      // Tag-Filter
      let matchesTags = true;
      if (ACTIVE_TAGS.size > 0) {
        matchesTags = Array.from(ACTIVE_TAGS).every(activeTag => {
          return entryTags.includes(activeTag);
        });
      }

      // Search-Filter
      const matchesSearchTerm = matchesSearch(entry);

      // Beide Bedingungen müssen erfüllt sein (AND-Logik)
      entry.style.display = (matchesTags && matchesSearchTerm) ? 'block' : 'none';
    });
  }

  // Abstract- und Notes-Button Toggle
  document.addEventListener('click', function(e) {
    // Abstract Button
    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';
      }
    }
    
    // Notes Button
    if (e.target.classList.contains('expand-btn-notes')) {
      const notesDiv = e.target.nextElementSibling;
      if (notesDiv && notesDiv.classList.contains('notes')) {
        notesDiv.classList.toggle('hidden');
        e.target.textContent = notesDiv.classList.contains('hidden')
          ? 'Show Notes'
          : 'Hide Notes';
      }
    }
  });

  // Starte wenn DOM bereit
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
  } else {
    init();
  }
})();