Files
WP-Multi-Wiki/assets/js/search.js
2026-03-18 21:56:43 +01:00

110 lines
3.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* WP Multi Wiki AJAX Search Script
*/
(function ($) {
'use strict';
$(document).ready(function () {
$('.wmw-search-widget').each(function () {
var $widget = $(this);
var $input = $widget.find('.wmw-search-input');
var $results = $widget.find('.wmw-search-results');
var $clear = $widget.find('.wmw-search-clear');
var wikiId = parseInt($widget.data('wiki-id'), 10) || 0;
var timer = null;
// ── Input handler ─────────────────────────────────────────────────
$input.on('input', function () {
clearTimeout(timer);
var term = $input.val().trim();
if (term.length < 2) {
closeResults();
$clear.hide();
return;
}
$clear.show();
timer = setTimeout(function () {
doSearch(term);
}, 280);
});
// ── Clear button ──────────────────────────────────────────────────
$clear.on('click', function () {
$input.val('').trigger('input').focus();
});
// ── Close on outside click ────────────────────────────────────────
$(document).on('click.wmw', function (e) {
if (!$widget.is(e.target) && !$widget.has(e.target).length) {
closeResults();
}
});
// ── Keyboard: escape closes ───────────────────────────────────────
$input.on('keydown', function (e) {
if (e.key === 'Escape') closeResults();
});
function doSearch(term) {
$results.html('<div class="wmw-search-no-results">Suche...</div>').addClass('wmw-open');
$.ajax({
url: wmwData.ajaxUrl,
type: 'POST',
data: {
action: 'wmw_search',
nonce: wmwData.nonce,
term: term,
wiki_id: wikiId,
},
success: function (response) {
if (!response.success) {
$results.html('<div class="wmw-search-no-results">Fehler bei der Suche.</div>');
return;
}
var items = response.data;
if (!items.length) {
$results.html('<div class="wmw-search-no-results">Keine Ergebnisse fuer &ldquo;' + escHtml(term) + '&rdquo;</div>');
return;
}
var html = '';
items.forEach(function (item) {
html += '<a href="' + escAttr(item.url) + '" class="wmw-search-result-item">';
html += '<div class="wmw-search-result-title">' + escHtml(item.title) + '</div>';
if (item.wiki_title) {
html += '<div class="wmw-search-result-meta">' + escHtml(item.wiki_title) + '</div>';
}
if (item.excerpt) {
html += '<div class="wmw-search-result-excerpt">' + item.excerpt + '</div>';
}
html += '</a>';
});
$results.html(html).addClass('wmw-open');
},
error: function () {
$results.html('<div class="wmw-search-no-results">Verbindungsfehler.</div>');
},
});
}
function closeResults() {
$results.removeClass('wmw-open').html('');
}
});
// ── Helpers ───────────────────────────────────────────────────────────
function escHtml(str) {
return String(str)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
}
function escAttr(str) { return escHtml(str); }
});
})(jQuery);