mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-12-06 15:48:02 +00:00
Fix search suggestions race between search bar init and page scripts (#27410)
This commit is contained in:
1 parent
a7d51d5be4
commit
9b4d646662
3 files changed
+170
-132
No files matched your search
@@ -96,13 +96,13 @@ THE SOFTWARE.
|
||||
<j:choose>
|
||||
<j:when test="${newManageJenkins}">
|
||||
<l:side-panel>
|
||||
<script src="${resURL}/jsbundles/pages/manage-jenkins.js" type="text/javascript" defer="true" />
|
||||
|
||||
<l:app-bar title="${manageJenkinsAction.displayName}" />
|
||||
|
||||
<l:tasks>
|
||||
<l:search-bar placeholder="${%Search}" id="settings-search-bar" />
|
||||
|
||||
<script src="${resURL}/jsbundles/pages/manage-jenkins.js" type="text/javascript" />
|
||||
|
||||
<j:invokeStatic var="j" className="jenkins.model.Jenkins" method="get" />
|
||||
<j:forEach var="category" items="${j.categorizedManagementLinks.entrySet()}">
|
||||
<summary class="jenkins-side-nav__heading">
|
||||
|
||||
@@ -7,73 +7,90 @@ const SELECTED_CLASS = "jenkins-dropdown__item--selected";
|
||||
function init() {
|
||||
const searchBarInputs = document.querySelectorAll(".jenkins-search__input");
|
||||
|
||||
Array.from(searchBarInputs)
|
||||
.filter((searchBar) => searchBar.suggestions)
|
||||
.forEach((searchBar) => {
|
||||
const searchWrapper = searchBar.parentElement.parentElement;
|
||||
const searchResultsContainer = createElementFromHtml(
|
||||
Array.from(searchBarInputs).forEach((searchBar) => {
|
||||
let suggestions = searchBar.suggestions;
|
||||
let initialized = false;
|
||||
let searchWrapper;
|
||||
let searchResultsContainer;
|
||||
let searchResults;
|
||||
|
||||
function showResultsContainer() {
|
||||
searchResultsContainer.classList.add(
|
||||
"jenkins-search__results-container--visible",
|
||||
);
|
||||
}
|
||||
|
||||
function hideResultsContainer() {
|
||||
searchResultsContainer.classList.remove(
|
||||
"jenkins-search__results-container--visible",
|
||||
);
|
||||
searchResultsContainer.style.height = "1px";
|
||||
}
|
||||
|
||||
function appendResults(container, results) {
|
||||
results.forEach((item, index) => {
|
||||
container.appendChild(
|
||||
createElementFromHtml(
|
||||
`<a class="jenkins-dropdown__item ${index === 0 ? SELECTED_CLASS : ""}" href="${
|
||||
item.url
|
||||
}"><div class="jenkins-dropdown__item__icon">${item.icon}</div>${xmlEscape(item.label)}</a>`,
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
if (results.length === 0 && container === searchResults) {
|
||||
container.appendChild(
|
||||
createElementFromHtml(
|
||||
`<p class="jenkins-search__results__no-results-label">No results</p>`,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function refreshResults() {
|
||||
if (!initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
const query = searchBar.value.toLowerCase();
|
||||
|
||||
// Hide the suggestions if the search query is empty
|
||||
if (query.length === 0 || typeof searchBar.suggestions !== "function") {
|
||||
searchResults.innerHTML = "";
|
||||
hideResultsContainer();
|
||||
return;
|
||||
}
|
||||
|
||||
showResultsContainer();
|
||||
|
||||
// Filter results
|
||||
const results = searchBar
|
||||
.suggestions()
|
||||
.filter((item) => item.label.toLowerCase().includes(query))
|
||||
.slice(0, 5);
|
||||
|
||||
searchResults.innerHTML = "";
|
||||
appendResults(searchResults, results);
|
||||
searchResultsContainer.style.height = searchResults.offsetHeight + "px";
|
||||
}
|
||||
|
||||
function initializeSearchBar() {
|
||||
if (initialized || typeof searchBar.suggestions !== "function") {
|
||||
return;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
searchWrapper = searchBar.parentElement.parentElement;
|
||||
searchResultsContainer = createElementFromHtml(
|
||||
`<div class="jenkins-search__results-container"></div>`,
|
||||
);
|
||||
searchWrapper.appendChild(searchResultsContainer);
|
||||
const searchResults = createElementFromHtml(
|
||||
searchResults = createElementFromHtml(
|
||||
`<div class="jenkins-dropdown"></div>`,
|
||||
);
|
||||
searchResultsContainer.appendChild(searchResults);
|
||||
|
||||
searchBar.addEventListener("input", () => {
|
||||
const query = searchBar.value.toLowerCase();
|
||||
|
||||
// Hide the suggestions if the search query is empty
|
||||
if (query.length === 0) {
|
||||
hideResultsContainer();
|
||||
return;
|
||||
}
|
||||
|
||||
showResultsContainer();
|
||||
|
||||
function appendResults(container, results) {
|
||||
results.forEach((item, index) => {
|
||||
container.appendChild(
|
||||
createElementFromHtml(
|
||||
`<a class="jenkins-dropdown__item ${index === 0 ? SELECTED_CLASS : ""}" href="${
|
||||
item.url
|
||||
}"><div class="jenkins-dropdown__item__icon">${item.icon}</div>${xmlEscape(item.label)}</a>`,
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
if (results.length === 0 && container === searchResults) {
|
||||
container.appendChild(
|
||||
createElementFromHtml(
|
||||
`<p class="jenkins-search__results__no-results-label">No results</p>`,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Filter results
|
||||
const results = searchBar
|
||||
.suggestions()
|
||||
.filter((item) => item.label.toLowerCase().includes(query))
|
||||
.slice(0, 5);
|
||||
|
||||
searchResults.innerHTML = "";
|
||||
appendResults(searchResults, results);
|
||||
searchResultsContainer.style.height = searchResults.offsetHeight + "px";
|
||||
});
|
||||
|
||||
function showResultsContainer() {
|
||||
searchResultsContainer.classList.add(
|
||||
"jenkins-search__results-container--visible",
|
||||
);
|
||||
}
|
||||
|
||||
function hideResultsContainer() {
|
||||
searchResultsContainer.classList.remove(
|
||||
"jenkins-search__results-container--visible",
|
||||
);
|
||||
searchResultsContainer.style.height = "1px";
|
||||
}
|
||||
searchBar.addEventListener("input", refreshResults);
|
||||
|
||||
searchBar.addEventListener("keydown", (e) => {
|
||||
if (e.key === "ArrowUp" || e.key === "ArrowDown") {
|
||||
@@ -90,7 +107,7 @@ function init() {
|
||||
// Workaround: Firefox doesn't update the dropdown height correctly so
|
||||
// let's bind the container's height to it's child
|
||||
// Disabled in HtmlUnit
|
||||
if (!window.isRunAsTest) {
|
||||
if (!document.head.getAttribute("data-unit-test")) {
|
||||
new ResizeObserver(() => {
|
||||
searchResultsContainer.style.height =
|
||||
searchResults.offsetHeight + "px";
|
||||
@@ -99,9 +116,7 @@ function init() {
|
||||
|
||||
searchBar.addEventListener("focusin", () => {
|
||||
if (searchBar.value.length !== 0) {
|
||||
searchResultsContainer.style.height =
|
||||
searchResults.offsetHeight + "px";
|
||||
showResultsContainer();
|
||||
refreshResults();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -112,7 +127,23 @@ function init() {
|
||||
|
||||
hideResultsContainer();
|
||||
});
|
||||
}
|
||||
|
||||
Object.defineProperty(searchBar, "suggestions", {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
get() {
|
||||
return suggestions;
|
||||
},
|
||||
set(value) {
|
||||
suggestions = value;
|
||||
initializeSearchBar();
|
||||
refreshResults();
|
||||
},
|
||||
});
|
||||
|
||||
initializeSearchBar();
|
||||
});
|
||||
}
|
||||
|
||||
export default { init };
|
||||
@@ -1,74 +1,81 @@
|
||||
const searchBarInput = document.querySelector("#settings-search-bar");
|
||||
|
||||
searchBarInput.suggestions = function () {
|
||||
return Array.from(
|
||||
document.querySelectorAll(
|
||||
".jenkins-section__item, #tasks .task-link-wrapper",
|
||||
),
|
||||
)
|
||||
.map((item) => ({
|
||||
url: item.querySelector("a").href,
|
||||
icon: item.querySelector(
|
||||
".jenkins-section__item__icon svg, .jenkins-section__item__icon img, .task-icon-link svg, .task-icon-link img",
|
||||
).outerHTML,
|
||||
label: (
|
||||
item.querySelector("dt") ||
|
||||
item.querySelector(".task-link-text") ||
|
||||
item.querySelector(".task-link")
|
||||
).textContent,
|
||||
}))
|
||||
.filter((item) => !item.url.endsWith("#"));
|
||||
};
|
||||
|
||||
document.addEventListener("DOMContentLoaded", function () {
|
||||
const messagesContainer = document.querySelector(".manage-messages");
|
||||
if (!messagesContainer) {
|
||||
return;
|
||||
function initSearchBar() {
|
||||
const searchBarInput = document.querySelector("#settings-search-bar");
|
||||
|
||||
searchBarInput.suggestions = function () {
|
||||
return Array.from(
|
||||
document.querySelectorAll(
|
||||
".jenkins-section__item, #tasks .task-link-wrapper",
|
||||
),
|
||||
)
|
||||
.map((item) => ({
|
||||
url: item.querySelector("a").href,
|
||||
icon: item.querySelector(
|
||||
".jenkins-section__item__icon svg, .jenkins-section__item__icon img, .task-icon-link svg, .task-icon-link img",
|
||||
).outerHTML,
|
||||
label: (
|
||||
item.querySelector("dt") ||
|
||||
item.querySelector(".task-link-text") ||
|
||||
item.querySelector(".task-link")
|
||||
).textContent,
|
||||
}))
|
||||
.filter((item) => !item.url.endsWith("#"));
|
||||
};
|
||||
}
|
||||
|
||||
const updateLastVisibleMessageMargin = () => {
|
||||
const messageDivs = Array.from(messagesContainer.children).filter(
|
||||
(el) => el.tagName === "DIV",
|
||||
);
|
||||
|
||||
messageDivs.forEach((el) => {
|
||||
el.style.marginBottom = "";
|
||||
});
|
||||
|
||||
const visibleDivs = messageDivs.filter((el) => {
|
||||
const style = window.getComputedStyle(el);
|
||||
return (
|
||||
style.display !== "none" &&
|
||||
style.visibility !== "hidden" &&
|
||||
!el.hasAttribute("hidden") &&
|
||||
el.getClientRects().length > 0
|
||||
);
|
||||
});
|
||||
|
||||
const lastVisible = visibleDivs[visibleDivs.length - 1];
|
||||
if (lastVisible) {
|
||||
lastVisible.style.marginBottom = "var(--section-padding)";
|
||||
}
|
||||
};
|
||||
|
||||
let rafId = null;
|
||||
const scheduleUpdate = () => {
|
||||
if (rafId !== null) {
|
||||
function initMessages() {
|
||||
const messagesContainer = document.querySelector(".manage-messages");
|
||||
if (!messagesContainer) {
|
||||
return;
|
||||
}
|
||||
rafId = requestAnimationFrame(() => {
|
||||
rafId = null;
|
||||
updateLastVisibleMessageMargin();
|
||||
|
||||
const updateLastVisibleMessageMargin = () => {
|
||||
const messageDivs = Array.from(messagesContainer.children).filter(
|
||||
(el) => el.tagName === "DIV",
|
||||
);
|
||||
|
||||
messageDivs.forEach((el) => {
|
||||
el.style.marginBottom = "";
|
||||
});
|
||||
|
||||
const visibleDivs = messageDivs.filter((el) => {
|
||||
const style = window.getComputedStyle(el);
|
||||
return (
|
||||
style.display !== "none" &&
|
||||
style.visibility !== "hidden" &&
|
||||
!el.hasAttribute("hidden") &&
|
||||
el.getClientRects().length > 0
|
||||
);
|
||||
});
|
||||
|
||||
const lastVisible = visibleDivs[visibleDivs.length - 1];
|
||||
if (lastVisible) {
|
||||
lastVisible.style.marginBottom = "var(--section-padding)";
|
||||
}
|
||||
};
|
||||
|
||||
let rafId = null;
|
||||
const scheduleUpdate = () => {
|
||||
if (rafId !== null) {
|
||||
return;
|
||||
}
|
||||
rafId = requestAnimationFrame(() => {
|
||||
rafId = null;
|
||||
updateLastVisibleMessageMargin();
|
||||
});
|
||||
};
|
||||
|
||||
updateLastVisibleMessageMargin();
|
||||
|
||||
const observer = new MutationObserver(scheduleUpdate);
|
||||
observer.observe(messagesContainer, {
|
||||
childList: true,
|
||||
subtree: true,
|
||||
attributes: true,
|
||||
attributeFilter: ["style", "class", "hidden", "aria-hidden"],
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
updateLastVisibleMessageMargin();
|
||||
|
||||
const observer = new MutationObserver(scheduleUpdate);
|
||||
observer.observe(messagesContainer, {
|
||||
childList: true,
|
||||
subtree: true,
|
||||
attributes: true,
|
||||
attributeFilter: ["style", "class", "hidden", "aria-hidden"],
|
||||
});
|
||||
initSearchBar();
|
||||
initMessages();
|
||||
});
|
||||
Reference in new issue
Block a user