Files
WP-Multi-Wiki/public/js/wmw-public.js
2026-03-18 21:56:38 +01:00

127 lines
4.6 KiB
JavaScript

/* jshint esversion: 6 */
jQuery(function ($) {
'use strict';
// ── TOC Toggle ────────────────────────────────────────────────────────
$(document).on('click', '.wmw-toc__title, .wmw-toc__toggle', function () {
var $toc = $(this).closest('.wmw-toc');
var $btn = $toc.find('.wmw-toc__toggle');
$toc.toggleClass('is-collapsed');
$btn.text($toc.hasClass('is-collapsed') ? '▶' : '▼');
});
// ── TOC Active Heading Highlight ──────────────────────────────────────
if ($('.wmw-toc').length && $('[id]').length) {
var headingIds = [];
$('.wmw-toc a').each(function () {
var href = $(this).attr('href');
if (href && href.startsWith('#')) {
headingIds.push(href.substring(1));
}
});
var $tocLinks = $('.wmw-toc a');
function highlightToc() {
var scrollTop = $(window).scrollTop() + 120;
var active = null;
headingIds.forEach(function (id) {
var $el = $('#' + id);
if ($el.length && $el.offset().top <= scrollTop) {
active = id;
}
});
$tocLinks.removeClass('is-active');
if (active) {
$tocLinks.filter('[href="#' + active + '"]').addClass('is-active');
}
}
$(window).on('scroll.wmwToc', highlightToc);
highlightToc();
}
// ── AJAX Search ───────────────────────────────────────────────────────
var searchTimer = null;
$(document).on('input', '.wmw-search__input', function () {
var $input = $(this);
var $wrap = $input.closest('.wmw-search');
var $results = $wrap.find('.wmw-search__results');
var $inner = $wrap.find('.wmw-search__results-inner');
var query = $input.val().trim();
var wikiId = $wrap.data('wiki-id') || 0;
clearTimeout(searchTimer);
if (query.length < 2) {
$results.attr('hidden', '');
return;
}
searchTimer = setTimeout(function () {
$inner.html('<div class="wmw-search-no-results">🔍 Suche…</div>');
$results.removeAttr('hidden');
$.post(wmwPublic.ajaxUrl, {
action: 'wmw_search',
query: query,
wiki_id: wikiId,
nonce: wmwPublic.nonce,
}, function (res) {
if (!res.success) { $inner.html('<div class="wmw-search-no-results">Fehler bei der Suche.</div>'); return; }
var results = res.data.results;
if (!results || results.length === 0) {
$inner.html('<div class="wmw-search-no-results">Keine Ergebnisse für „' + escHtml(query) + '"</div>');
return;
}
var html = '';
results.forEach(function (r) {
html += '<a href="' + r.url + '" class="wmw-search-result">';
html += '<span class="wmw-search-result__icon">' + r.icon + '</span>';
html += '<div class="wmw-search-result__body">';
if (r.wiki) html += '<div class="wmw-search-result__wiki">' + escHtml(r.wiki) + '</div>';
html += '<div class="wmw-search-result__title">' + r.title + '</div>';
if (r.excerpt) html += '<div class="wmw-search-result__exc">' + r.excerpt + '</div>';
html += '</div></a>';
});
$inner.html(html);
});
}, 280);
});
// Close search on outside click
$(document).on('click', function (e) {
if (!$(e.target).closest('.wmw-search').length) {
$('.wmw-search__results').attr('hidden', '');
}
});
// Prevent closing when clicking inside results
$(document).on('click', '.wmw-search__results', function (e) {
e.stopPropagation();
});
// ── Smooth Scroll for TOC links ───────────────────────────────────────
$(document).on('click', '.wmw-toc a', function (e) {
var href = $(this).attr('href');
if (href && href.startsWith('#')) {
var $target = $(href);
if ($target.length) {
e.preventDefault();
$('html, body').animate({ scrollTop: $target.offset().top - 80 }, 400);
}
}
});
// ── Helper ────────────────────────────────────────────────────────────
function escHtml(str) {
return str.replace(/[&<>"']/g, function (c) {
return { '&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;' }[c];
});
}
});