2026-03-17 13:04:43 +01:00
|
|
|
|
/*
|
|
|
|
|
|
Copyright 2026 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
|
limitations under the License.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2026-03-24 10:20:31 +01:00
|
|
|
|
Adapted from [1] to combine Docsy's built-in search UI with the Pagefind
|
2026-03-17 13:04:43 +01:00
|
|
|
|
search backend.
|
|
|
|
|
|
|
|
|
|
|
|
[1]: https://github.com/matrix-org/docsy/blob/71d103ebb20ace3d528178c4b6d92b6cc4f7fd53/assets/js/offline-search.js
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
(function ($) {
|
2026-03-19 08:50:45 +01:00
|
|
|
|
"use strict";
|
2026-03-17 13:04:43 +01:00
|
|
|
|
|
|
|
|
|
|
$(document).ready(async function () {
|
2026-03-24 10:56:03 +01:00
|
|
|
|
// This is going to be loaded from ${deployment}/js/main.js so to use a relative path
|
|
|
|
|
|
// to the Pagefind script we need to navigate one level up.
|
|
|
|
|
|
const pagefind = await import("../pagefind/pagefind.js");
|
2026-03-19 08:50:45 +01:00
|
|
|
|
const $searchInput = $(".td-search input");
|
2026-03-17 13:04:43 +01:00
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
// Lazily initialise Pagefind only when the user is about to start a search.
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
$searchInput.focus(() => {
|
|
|
|
|
|
pagefind.init();
|
2026-03-17 13:21:42 +01:00
|
|
|
|
});
|
2026-03-17 13:04:43 +01:00
|
|
|
|
|
|
|
|
|
|
//
|
2026-03-20 12:52:01 +01:00
|
|
|
|
// Set up search input handler.
|
2026-03-17 13:04:43 +01:00
|
|
|
|
//
|
|
|
|
|
|
|
2026-03-24 11:32:27 +01:00
|
|
|
|
$searchInput.on("keypress", (event) => {
|
|
|
|
|
|
// Start searching only upon Enter.
|
|
|
|
|
|
if (event.which === 13) {
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
render($(event.target));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2026-03-17 13:04:43 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Prevent reloading page by enter key on sidebar search.
|
2026-03-19 08:50:45 +01:00
|
|
|
|
$searchInput.closest("form").on("submit", () => {
|
2026-03-17 13:04:43 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2026-03-20 12:52:01 +01:00
|
|
|
|
// Callback for searching and rendering the results.
|
2026-03-17 13:04:43 +01:00
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
const render = async ($targetSearchInput) => {
|
|
|
|
|
|
//
|
2026-03-20 12:52:01 +01:00
|
|
|
|
// Dispose any existing popover.
|
2026-03-17 13:04:43 +01:00
|
|
|
|
//
|
|
|
|
|
|
|
2026-03-24 15:17:22 +01:00
|
|
|
|
disposePopover($targetSearchInput);
|
2026-03-17 13:04:43 +01:00
|
|
|
|
|
|
|
|
|
|
//
|
2026-03-24 15:11:52 +01:00
|
|
|
|
// Check if we need to do a search at all.
|
2026-03-17 13:04:43 +01:00
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
const searchQuery = $targetSearchInput.val();
|
2026-03-19 08:50:45 +01:00
|
|
|
|
if (searchQuery === "") {
|
2026-03-17 13:04:43 +01:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2026-03-24 15:11:52 +01:00
|
|
|
|
// Prepare the results popover.
|
2026-03-17 13:04:43 +01:00
|
|
|
|
//
|
|
|
|
|
|
|
2026-03-19 08:50:45 +01:00
|
|
|
|
const $html = $("<div>");
|
2026-03-17 13:04:43 +01:00
|
|
|
|
|
2026-03-20 12:52:01 +01:00
|
|
|
|
// Add the header and close button.
|
|
|
|
|
|
$html.append($("<div>")
|
|
|
|
|
|
.css({
|
|
|
|
|
|
display: "flex",
|
|
|
|
|
|
justifyContent: "space-between",
|
|
|
|
|
|
marginBottom: "1em",
|
|
|
|
|
|
})
|
|
|
|
|
|
.append($("<span>")
|
|
|
|
|
|
.text("Search results")
|
|
|
|
|
|
.css({ fontWeight: "bold" }))
|
2026-03-24 11:16:37 +01:00
|
|
|
|
.append($("<button>")
|
2026-03-20 12:52:01 +01:00
|
|
|
|
.addClass("td-offline-search-results__close-button")
|
2026-03-24 11:16:37 +01:00
|
|
|
|
.addClass("btn")
|
|
|
|
|
|
.addClass("btn-sm")
|
|
|
|
|
|
.addClass("btn-link")
|
|
|
|
|
|
.attr("type", "button")
|
2026-03-20 13:13:05 +01:00
|
|
|
|
.attr("aria-label", "Close")
|
2026-03-20 12:52:01 +01:00
|
|
|
|
.on("click", () => {
|
|
|
|
|
|
$targetSearchInput.val("");
|
2026-03-24 15:17:22 +01:00
|
|
|
|
disposePopover($targetSearchInput);
|
2026-03-17 13:04:43 +01:00
|
|
|
|
})
|
2026-03-20 12:52:01 +01:00
|
|
|
|
)
|
2026-03-17 13:04:43 +01:00
|
|
|
|
);
|
|
|
|
|
|
|
2026-03-20 12:52:01 +01:00
|
|
|
|
// Add the main search results body.
|
2026-03-19 08:50:45 +01:00
|
|
|
|
const $searchResultBody = $("<div>").css({
|
2026-03-17 13:04:43 +01:00
|
|
|
|
maxHeight: `calc(100vh - ${
|
|
|
|
|
|
$targetSearchInput.offset().top - $(window).scrollTop() + 180
|
|
|
|
|
|
}px)`,
|
2026-03-19 08:50:45 +01:00
|
|
|
|
overflowY: "auto",
|
2026-03-17 13:04:43 +01:00
|
|
|
|
});
|
|
|
|
|
|
$html.append($searchResultBody);
|
|
|
|
|
|
|
2026-03-24 15:11:52 +01:00
|
|
|
|
// Append a spinner while we're busy.
|
2026-03-24 15:48:57 +01:00
|
|
|
|
const $spinner = createSpinner();
|
2026-03-24 15:11:52 +01:00
|
|
|
|
$searchResultBody.append($spinner)
|
|
|
|
|
|
|
|
|
|
|
|
// Display the popover.
|
|
|
|
|
|
const popover = new bootstrap.Popover($targetSearchInput[0], {
|
|
|
|
|
|
content: $html[0],
|
|
|
|
|
|
html: true,
|
|
|
|
|
|
customClass: "td-offline-search-results",
|
|
|
|
|
|
placement: "bottom",
|
|
|
|
|
|
});
|
|
|
|
|
|
popover.show();
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
// Kick off the search, load the results and inject them into the popover.
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
const search = await pagefind.debouncedSearch(searchQuery);
|
|
|
|
|
|
if (search === null) {
|
|
|
|
|
|
// A more recent search call has been made, nothing to do.
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (search.results.length === 0) {
|
2026-03-17 13:04:43 +01:00
|
|
|
|
$searchResultBody.append(
|
2026-03-19 08:50:45 +01:00
|
|
|
|
$("<p>").text(`No results found for query "${searchQuery}"`)
|
2026-03-17 13:04:43 +01:00
|
|
|
|
);
|
|
|
|
|
|
} else {
|
2026-03-24 15:48:57 +01:00
|
|
|
|
await loadAndRenderResults(search.results, 0, $spinner, $searchResultBody);
|
2026-03-24 15:11:52 +01:00
|
|
|
|
}
|
2026-03-17 13:04:43 +01:00
|
|
|
|
};
|
|
|
|
|
|
});
|
|
|
|
|
|
})(jQuery);
|
2026-03-20 13:44:11 +01:00
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
// Helpers
|
|
|
|
|
|
//
|
|
|
|
|
|
|
2026-03-24 15:17:22 +01:00
|
|
|
|
const disposePopover = ($targetSearchInput) => {
|
|
|
|
|
|
const popover = bootstrap.Popover.getInstance($targetSearchInput[0]);
|
|
|
|
|
|
if (popover !== null) {
|
|
|
|
|
|
popover.dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-24 15:48:57 +01:00
|
|
|
|
const createSpinner = () => {
|
|
|
|
|
|
return $("<div>")
|
|
|
|
|
|
.addClass("spinner-container")
|
|
|
|
|
|
.append($("<div>")
|
|
|
|
|
|
.addClass("spinner-border")
|
|
|
|
|
|
.attr("role", "status")
|
|
|
|
|
|
.append($("<div>")
|
|
|
|
|
|
.addClass("visually-hidden")
|
|
|
|
|
|
.text("Loading...")))
|
|
|
|
|
|
.append($("<p>")
|
|
|
|
|
|
.text("Loading..."));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const loadAndRenderResults = async (results, offset, $spinner, $searchResultBody) => {
|
|
|
|
|
|
// Load and render the first three results and hide the remainder behind a
|
|
|
|
|
|
// button to not freeze the browser by loading results that may not be
|
|
|
|
|
|
// displayed.
|
|
|
|
|
|
const LIMIT = 3;
|
|
|
|
|
|
|
|
|
|
|
|
for (const [index, result] of results.entries()) {
|
|
|
|
|
|
if (index < LIMIT) {
|
|
|
|
|
|
// Insert a container for the result *before* the spinner. This
|
|
|
|
|
|
// will push down the spinner as new content is loaded and keep
|
|
|
|
|
|
// it at the end of the popover.
|
|
|
|
|
|
const $container = $("<div>");
|
|
|
|
|
|
$spinner.before($container);
|
|
|
|
|
|
|
|
|
|
|
|
renderResult(await result.data(), index + offset, $container);
|
|
|
|
|
|
} else if (index === LIMIT) {
|
|
|
|
|
|
const num_hidden_results = results.length - index;
|
|
|
|
|
|
const $loader = $("<button>")
|
|
|
|
|
|
.attr("type", "button")
|
|
|
|
|
|
.addClass("td-offline-search-results__expander-button")
|
|
|
|
|
|
.addClass("btn")
|
|
|
|
|
|
.addClass("btn-sm")
|
|
|
|
|
|
.addClass("btn-link")
|
|
|
|
|
|
.text(`Load more results from ${num_hidden_results} other ${pagesString(num_hidden_results)}`)
|
|
|
|
|
|
.on("click", async () => {
|
|
|
|
|
|
// Remove the button.
|
|
|
|
|
|
$loader.remove();
|
|
|
|
|
|
|
|
|
|
|
|
// Add a spinner while we're busy.
|
|
|
|
|
|
const $spinner = createSpinner();
|
|
|
|
|
|
$searchResultBody.append($spinner)
|
|
|
|
|
|
|
|
|
|
|
|
// Load and render the results.
|
|
|
|
|
|
await loadAndRenderResults(results.slice(LIMIT), LIMIT + offset, $spinner, $searchResultBody);
|
|
|
|
|
|
});
|
|
|
|
|
|
$spinner.before($loader)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Remove the spinner now that everything was loaded.
|
|
|
|
|
|
$spinner.remove();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-24 15:11:52 +01:00
|
|
|
|
const renderResult = (data, index, $container) => {
|
|
|
|
|
|
// Add the main result's page title.
|
|
|
|
|
|
$container.append($("<div>")
|
|
|
|
|
|
.append($("<a>")
|
|
|
|
|
|
.css({
|
|
|
|
|
|
fontSize: "1.2rem",
|
|
|
|
|
|
})
|
|
|
|
|
|
.attr("href", data.url)
|
|
|
|
|
|
.text(data.meta.title))
|
|
|
|
|
|
.append($("<span>")
|
|
|
|
|
|
.addClass("text-body-secondary")
|
|
|
|
|
|
.text(` – ${data.sub_results.length} ${resultsString(data.sub_results.length)}`)));
|
|
|
|
|
|
|
|
|
|
|
|
// Render the first 3 subresults per page and wrap the rest
|
|
|
|
|
|
// in a collapsed container.
|
|
|
|
|
|
const LIMIT = 3;
|
|
|
|
|
|
let $wrapper = null;
|
|
|
|
|
|
|
|
|
|
|
|
data.sub_results.forEach((s, index_s) => {
|
|
|
|
|
|
if (index_s === LIMIT) {
|
|
|
|
|
|
const num_hidden_results = data.sub_results.length - index_s;
|
|
|
|
|
|
const wrapper_id = `collapsible-subresults-${index}`;
|
|
|
|
|
|
const $action = $("<span>").text("▶ Show");
|
|
|
|
|
|
const $expander = $("<button>")
|
|
|
|
|
|
.attr("data-bs-toggle", "collapse")
|
|
|
|
|
|
.attr("data-bs-target", `#${wrapper_id}`)
|
|
|
|
|
|
.attr("aria-expanded", "false")
|
|
|
|
|
|
.attr("aria-controls", wrapper_id)
|
|
|
|
|
|
.attr("type", "button")
|
|
|
|
|
|
.addClass("td-offline-search-results__expander-button")
|
|
|
|
|
|
.addClass("btn")
|
|
|
|
|
|
.addClass("btn-sm")
|
|
|
|
|
|
.addClass("btn-link")
|
|
|
|
|
|
.append($action)
|
|
|
|
|
|
.append($("<span>").text(` ${num_hidden_results} more ${resultsString(num_hidden_results)} from ${data.meta.title}`));
|
|
|
|
|
|
|
|
|
|
|
|
$container.append($("<p>").append($expander));
|
|
|
|
|
|
$wrapper = $("<div>")
|
|
|
|
|
|
.addClass("collapse td-offline-search-results__subresults")
|
|
|
|
|
|
.attr("id", wrapper_id)
|
|
|
|
|
|
.on("hide.bs.collapse", _ => $action.text("▶ Show"))
|
|
|
|
|
|
.on("show.bs.collapse", _ => $action.text("▼ Hide"));
|
|
|
|
|
|
$container.append($wrapper);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const $entry = $("<div>")
|
|
|
|
|
|
.css("margin-top", "0.5rem");
|
|
|
|
|
|
|
|
|
|
|
|
$entry.append(
|
|
|
|
|
|
$("<a>")
|
|
|
|
|
|
.addClass("d-block")
|
|
|
|
|
|
.attr("href", s.url)
|
|
|
|
|
|
.text(s.title)
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$entry.append($("<p>").html(s.excerpt));
|
|
|
|
|
|
|
|
|
|
|
|
if (index_s < LIMIT) {
|
|
|
|
|
|
$container.append($entry);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$wrapper.append($entry);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-20 13:44:11 +01:00
|
|
|
|
const resultsString = (n) => {
|
2026-03-24 10:22:03 +01:00
|
|
|
|
return n === 1 ? "result" : "results";
|
2026-03-20 13:44:11 +01:00
|
|
|
|
};
|
2026-03-24 15:48:57 +01:00
|
|
|
|
|
|
|
|
|
|
const pagesString = (n) => {
|
|
|
|
|
|
return n === 1 ? "page" : "pages";
|
|
|
|
|
|
};
|