Skip to content

Commit

Permalink
Add support for images
Browse files Browse the repository at this point in the history
  • Loading branch information
timja committed Dec 11, 2024
1 parent a3fdb3e commit 436a02b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
36 changes: 29 additions & 7 deletions core/src/main/java/hudson/search/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.StaplerResponse2;
import org.kohsuke.stapler.export.DataWriter;
import org.kohsuke.stapler.export.ExportConfig;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import org.kohsuke.stapler.export.Flavor;
Expand Down Expand Up @@ -159,17 +160,23 @@ public void doSuggestOpenSearch(StaplerRequest2 req, StaplerResponse2 rsp, @Quer
*/
public void doSuggest(StaplerRequest2 req, StaplerResponse2 rsp, @QueryParameter String query) throws IOException, ServletException {
Result r = new Result();
for (SuggestedItem item : getSuggestions(req, query)) {
String symbolName = item.item.getSearchIcon();
for (SuggestedItem curItem : getSuggestions(req, query)) {
String iconName = curItem.item.getSearchIcon();

if (symbolName == null || !symbolName.startsWith("symbol-")) {
symbolName = "symbol-search";
if (iconName == null ||
(!iconName.startsWith("symbol-") && !iconName.startsWith("http"))
) {
iconName = "symbol-search";
}

r.suggestions.add(new Item(item.getPath(), item.getUrl(),
Symbol.get(new SymbolRequest.Builder().withRaw(symbolName).build())));
if (iconName.startsWith("symbol")) {
r.suggestions.add(new Item(curItem.getPath(), curItem.getUrl(),
Symbol.get(new SymbolRequest.Builder().withRaw(iconName).build())));
} else {
r.suggestions.add(new Item(curItem.getPath(), curItem.getUrl(), iconName, "image"));
}
}
rsp.serveExposedBean(req, r, Flavor.JSON);
rsp.serveExposedBean(req, r, new ExportConfig());
}

/**
Expand Down Expand Up @@ -268,6 +275,8 @@ public static class Item {

private final String url;

private final String type;

public final String iconXml;

public Item(String name) {
Expand All @@ -278,6 +287,14 @@ public Item(String name, String url, String iconXml) {
this.name = name;
this.url = url;
this.iconXml = iconXml;
this.type = "symbol";
}

public Item(String name, String url, String iconXml, String type) {
this.name = name;
this.url = url;
this.iconXml = iconXml;
this.type = type;
}

@Exported
Expand All @@ -289,6 +306,11 @@ public String getUrl() {
public String getIconXml() {
return iconXml;
}

@Exported
public String getType() {
return type;
}
}

private enum Mode {
Expand Down
1 change: 1 addition & 0 deletions src/main/js/components/command-palette/datasources.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const JenkinsSearchSource = {
return data["suggestions"].slice().map((e) =>
LinkResult({
icon: e.iconXml,
type: e.type,
label: e.name,
url: correctAddress(e.url),
}),
Expand Down
1 change: 1 addition & 0 deletions src/main/js/components/command-palette/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function init() {
results = Promise.all([
LinkResult({
icon: Symbols.HELP,
type: "symbol",
label: i18n.dataset.getHelp,
url: headerCommandPaletteButton.dataset.searchHelpUrl,
isExternal: true,
Expand Down
6 changes: 3 additions & 3 deletions src/main/js/components/command-palette/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { xmlEscape } from "@/util/security";
* @param {Object} params
* @param {string} params.icon
* @param {string} params.label
* @param {string} params.type
* @param {string} params.url
* @param {boolean | undefined} params.isExternal
*/
Expand All @@ -16,9 +17,8 @@ export function LinkResult(params) {
return `<a class="jenkins-command-palette__results__item" href="${xmlEscape(
params.url,
)}">
<div class="jenkins-command-palette__results__item__icon">${
params.icon
}</div>
${params.type === "image" ? `<img alt="${xmlEscape(params.label)}" class="jenkins-command-palette__results__item__icon" src="${params.icon}" />` : ""}
${params.type !== "image" ? `<div class="jenkins-command-palette__results__item__icon">${params.icon}</div>` : ""}
${xmlEscape(params.label)}
${params.isExternal ? Symbols.EXTERNAL_LINK : ""}
</a>`;
Expand Down

0 comments on commit 436a02b

Please sign in to comment.