From 9ef136f7de51e8426e247c3db4cefb149d4c11ac Mon Sep 17 00:00:00 2001 From: Jan Faracik <43062514+janfaracik@users.noreply.github.com> Date: Wed, 25 May 2022 10:19:58 +0100 Subject: [PATCH] Get symbols working --- .../main/java/hudson/model/AbstractItem.java | 2 +- core/src/main/java/hudson/model/Action.java | 1 - .../main/java/hudson/model/Actionable.java | 3 +- .../java/hudson/model/ManagementLink.java | 1 - core/src/main/java/hudson/search/Search.java | 8 +- .../main/java/hudson/search/SearchItem.java | 6 +- .../hudson/search/SearchItemCategory.java | 37 ++++-- core/src/main/java/jenkins/Symbols.java | 107 ++++++++++++++++++ war/src/main/js/api/symbols.js | 7 ++ .../command-palette/command-palette.js | 26 +++-- .../js/components/command-palette/models.js | 11 +- .../js/components/command-palette/symbols.js | 1 - 12 files changed, 180 insertions(+), 30 deletions(-) create mode 100644 core/src/main/java/jenkins/Symbols.java create mode 100644 war/src/main/js/api/symbols.js diff --git a/core/src/main/java/hudson/model/AbstractItem.java b/core/src/main/java/hudson/model/AbstractItem.java index 6a4eed7ee31a..9148ecee581e 100644 --- a/core/src/main/java/hudson/model/AbstractItem.java +++ b/core/src/main/java/hudson/model/AbstractItem.java @@ -589,7 +589,7 @@ public String getSearchUrl() { } @Override - public String getSearchItemCategory() { + public SearchItemCategory getSearchItemCategory() { return SearchItemCategory.JOBS; } diff --git a/core/src/main/java/hudson/model/Action.java b/core/src/main/java/hudson/model/Action.java index bddd094a7422..17b21a2ca7c9 100644 --- a/core/src/main/java/hudson/model/Action.java +++ b/core/src/main/java/hudson/model/Action.java @@ -26,7 +26,6 @@ import edu.umd.cs.findbugs.annotations.CheckForNull; import hudson.Functions; -import hudson.search.SearchItemCategory; /** * Object that contributes additional information, behaviors, and UIs to {@link ModelObject} diff --git a/core/src/main/java/hudson/model/Actionable.java b/core/src/main/java/hudson/model/Actionable.java index b0f0479dbb61..33cb03037202 100644 --- a/core/src/main/java/hudson/model/Actionable.java +++ b/core/src/main/java/hudson/model/Actionable.java @@ -35,7 +35,6 @@ import java.util.logging.Level; import java.util.logging.Logger; -import hudson.search.SearchItem; import hudson.search.SearchItemCategory; import jenkins.model.ModelObjectWithContextMenu; import jenkins.model.TransientActionFactory; @@ -357,7 +356,7 @@ public Object getDynamic(String token, StaplerRequest req, StaplerResponse rsp) private static final Logger LOGGER = Logger.getLogger(Actionable.class.getName()); - public String getSearchItemCategory() { + public SearchItemCategory getSearchItemCategory() { return SearchItemCategory.OTHER; } } diff --git a/core/src/main/java/hudson/model/ManagementLink.java b/core/src/main/java/hudson/model/ManagementLink.java index 4b567953a988..666c2fd12539 100644 --- a/core/src/main/java/hudson/model/ManagementLink.java +++ b/core/src/main/java/hudson/model/ManagementLink.java @@ -30,7 +30,6 @@ import hudson.ExtensionList; import hudson.ExtensionListView; import hudson.ExtensionPoint; -import hudson.search.SearchItemCategory; import hudson.security.Permission; import java.util.List; import java.util.logging.Level; diff --git a/core/src/main/java/hudson/search/Search.java b/core/src/main/java/hudson/search/Search.java index 32322f6e0e7a..e1e4036ae0a3 100644 --- a/core/src/main/java/hudson/search/Search.java +++ b/core/src/main/java/hudson/search/Search.java @@ -129,8 +129,8 @@ public void doSuggest(StaplerRequest req, StaplerResponse rsp, @QueryParameter S item.getPath(), item.item.getSearchDescription(), item.item.getSearchUrl(), - item.item.getSearchItemIcon(), - item.item.getSearchItemCategory())); + item.item.getSearchItemIcon().getClassSpec(), + item.item.getSearchItemCategory().getName())); } rsp.serveExposedBean(req, r, Flavor.JSON); @@ -210,7 +210,7 @@ public static class Item { @Exported public String description; @Exported - public Icon icon; + public String icon; @Exported public String category; @@ -218,7 +218,7 @@ public Item(String name) { this.name = name; } - public Item(String name, String description, String url, Icon icon, String category) { + public Item(String name, String description, String url, String icon, String category) { this.name = name; this.description = description; this.url = url; diff --git a/core/src/main/java/hudson/search/SearchItem.java b/core/src/main/java/hudson/search/SearchItem.java index e541e658c52a..cce6553f64bb 100644 --- a/core/src/main/java/hudson/search/SearchItem.java +++ b/core/src/main/java/hudson/search/SearchItem.java @@ -60,13 +60,17 @@ default String getSearchDescription() { } // TODO - default String getSearchItemCategory() { + default SearchItemCategory getSearchItemCategory() { return SearchItemCategory.OTHER; } // TODO // Overrides the icon from search item category default Icon getSearchItemIcon() { + if (getSearchItemCategory() != null) { + return getSearchItemCategory().getIcon(); + } + return null; } diff --git a/core/src/main/java/hudson/search/SearchItemCategory.java b/core/src/main/java/hudson/search/SearchItemCategory.java index 6e63571638bf..1a1950b89518 100644 --- a/core/src/main/java/hudson/search/SearchItemCategory.java +++ b/core/src/main/java/hudson/search/SearchItemCategory.java @@ -1,13 +1,30 @@ package hudson.search; -public class SearchItemCategory { - public final static String PAGES = "Pages"; - public final static String SETTINGS = "Settings"; - public final static String VIEWS = "Views"; - public final static String JOBS = "Jobs"; - public final static String BUILDS = "Builds"; - public final static String PEOPLE = "People"; - public final static String NODES = "Nodes"; - public final static String IN_PAGE_ACTIONS = "In-page action"; - public final static String OTHER = "Other"; +import org.jenkins.ui.icon.Icon; + +public enum SearchItemCategory { + PAGES("Pages", "symbol-details"), + SETTINGS("Settings", "symbol-settings"), + VIEWS("Views", "symbol-settings"), + JOBS("Jobs", "symbol-reload"), + BUILDS("Builds", "symbol-settings"), + PEOPLE("People", "symbol-person"), + NODES("Nodes", "symbol-computer"), + OTHER("Other", "symbol-cube"); + + private final String name; + private final Icon icon; + + SearchItemCategory(String name, String iconSource) { + this.name = name; + this.icon = new Icon(iconSource, ""); + } + + public String getName() { + return name; + } + + public Icon getIcon() { + return icon; + } } diff --git a/core/src/main/java/jenkins/Symbols.java b/core/src/main/java/jenkins/Symbols.java new file mode 100644 index 000000000000..8e0c811f34b1 --- /dev/null +++ b/core/src/main/java/jenkins/Symbols.java @@ -0,0 +1,107 @@ +/* + * The MIT License + * + * Copyright (c) 2015, CloudBees, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package jenkins; + +import hudson.Extension; +import hudson.model.RootAction; +import hudson.util.HttpResponses; +import net.sf.json.JSONObject; +import org.jenkins.ui.icon.IconSet; +import org.kohsuke.accmod.Restricted; +import org.kohsuke.accmod.restrictions.NoExternalUse; +import org.kohsuke.stapler.HttpResponse; +import org.kohsuke.stapler.StaplerRequest; + +import java.util.Locale; + +import static hudson.Functions.extractPluginNameFromIconSrc; + +/** + * Internationalization REST (ish) API. + * @author tom.fennelly@gmail.com + * @since 2.0 + */ +@Extension +@Restricted(NoExternalUse.class) +public class Symbols implements RootAction { + + @Override + public String getIconFileName() { + return null; + } + + @Override + public String getDisplayName() { + return null; + } + + @Override + public String getUrlName() { + return "symbols"; + } + + /** + * Get a localised resource bundle. + *

+ * URL: {@code i18n/resourceBundle}. + *

+ * Parameters: + *

+ * + * @param request The request. + * @return The JSON response. + */ + public HttpResponse doIndex(StaplerRequest request) { + String symbol = request.getParameter("symbol"); + + if (symbol == null) { + return HttpResponses.errorJSON("Mandatory parameter 'symbol' not specified."); + } + + symbol = symbol.replace("symbol-", ""); + +// String title = request.getParameter("title"); +// String tooltip = request.getParameter("tooltip"); +// String classes = request.getParameter("classes"); +// String pluginName = extractPluginNameFromIconSrc(symbol); +// String id = request.getParameter("id"); + + String title = ""; + String tooltip = ""; + String classes = ""; + String pluginName = extractPluginNameFromIconSrc(symbol); + String id = ""; + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("symbol", IconSet.getSymbol(symbol, title, tooltip, classes, pluginName, id)); + + return HttpResponses.okJSON(jsonObject); + } +} diff --git a/war/src/main/js/api/symbols.js b/war/src/main/js/api/symbols.js new file mode 100644 index 000000000000..c68cab6c7425 --- /dev/null +++ b/war/src/main/js/api/symbols.js @@ -0,0 +1,7 @@ +import jenkins from "@/util/jenkins"; + +export function getSymbol(symbol, handler) { + jenkins.get("/symbols?symbol=" + symbol, function(res) { + handler(res.data["symbol"]) + }, {async: false}); +} diff --git a/war/src/main/js/components/command-palette/command-palette.js b/war/src/main/js/components/command-palette/command-palette.js index d17880e2ce15..2496f4aba422 100644 --- a/war/src/main/js/components/command-palette/command-palette.js +++ b/war/src/main/js/components/command-palette/command-palette.js @@ -1,8 +1,8 @@ import "regenerator-runtime/runtime"; import {LinkResult} from "@/components/command-palette/models"; -import * as Symbols from "./symbols"; import {JenkinsSearchSource} from "./datasources"; import Helpers from './helpers'; +import debounce from "lodash/debounce"; const datasources = [JenkinsSearchSource]; @@ -20,9 +20,9 @@ window.addEventListener('load', () => { // Events headerCommandPaletteButton.addEventListener("click", function () { if (commandPalette.hasAttribute("open")) { - hideCommandCenter(); + hideCommandPalette(); } else { - showCommandCenter(); + showCommandPalette(); } }); @@ -31,9 +31,19 @@ window.addEventListener('load', () => { return } - hideCommandCenter(); + hideCommandPalette(); }) + // const debouncedFilter = debounce(handleFilter, 300); + // + // const handleFilter = function () { + // loadPage({}, true); + // }; + // + // function renderResults() { + // + // } + commandPaletteInput.addEventListener("input", async (e) => { commandPaletteLoadingSymbol.classList.add("icon--loading") const query = e.target.value; @@ -42,7 +52,7 @@ window.addEventListener('load', () => { if (query.length === 0) { results = [ new LinkResult( - {svg: Symbols.HELP}, + "symbol-help-circle", i18n.dataset.getHelp, undefined, "Help", @@ -135,8 +145,8 @@ window.addEventListener('load', () => { } }) - // Helper methods for visibility of command center - function showCommandCenter() { + // Helper methods for visibility of command palette + function showCommandPalette() { commandPalette.showModal(); commandPaletteInput.focus(); @@ -144,7 +154,7 @@ window.addEventListener('load', () => { commandPaletteInput.dispatchEvent(new Event("input")); } - function hideCommandCenter() { + function hideCommandPalette() { commandPalette.close(); } diff --git a/war/src/main/js/components/command-palette/models.js b/war/src/main/js/components/command-palette/models.js index 7c47947e111c..6f829a28f3b5 100644 --- a/war/src/main/js/components/command-palette/models.js +++ b/war/src/main/js/components/command-palette/models.js @@ -1,4 +1,5 @@ import * as Symbols from "./symbols"; +import {getSymbol} from "@/api/symbols"; export class LinkResult { constructor(icon, label, description, category, url, isExternal) { @@ -10,8 +11,16 @@ export class LinkResult { this.isExternal = isExternal; } render() { + let icon2; + + getSymbol(this.icon, (e) => { + icon2 = e + }) + + // this.icon.includes("symbol-") ? getSymbol(this.icon, (e) => {icon = e}) : `` + return ` -
${this.icon ? `${this.icon.svg ? this.icon.svg : ``}` : ``}
+
${icon2}
${this.label} ${this.description ? `${this.description}` : ``} ${this.isExternal ? Symbols.EXTERNAL_LINK : Symbols.CHEVRON_RIGHT} diff --git a/war/src/main/js/components/command-palette/symbols.js b/war/src/main/js/components/command-palette/symbols.js index 50ea8d58733c..60d3b2c9695f 100644 --- a/war/src/main/js/components/command-palette/symbols.js +++ b/war/src/main/js/components/command-palette/symbols.js @@ -1,3 +1,2 @@ export const CHEVRON_RIGHT = ``; export const EXTERNAL_LINK = ``; -export const HELP = ``;