Skip to content

Commit

Permalink
Get symbols working
Browse files Browse the repository at this point in the history
  • Loading branch information
janfaracik committed May 25, 2022
1 parent 8b50d9e commit 9ef136f
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 30 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/AbstractItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ public String getSearchUrl() {
}

@Override
public String getSearchItemCategory() {
public SearchItemCategory getSearchItemCategory() {
return SearchItemCategory.JOBS;
}

Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/hudson/model/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/hudson/model/Actionable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
1 change: 0 additions & 1 deletion core/src/main/java/hudson/model/ManagementLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/hudson/search/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -210,15 +210,15 @@ public static class Item {
@Exported
public String description;
@Exported
public Icon icon;
public String icon;
@Exported
public String category;

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;
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/java/hudson/search/SearchItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
37 changes: 27 additions & 10 deletions core/src/main/java/hudson/search/SearchItemCategory.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
107 changes: 107 additions & 0 deletions core/src/main/java/jenkins/Symbols.java
Original file line number Diff line number Diff line change
@@ -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 <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
* @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.
* <p>
* URL: {@code i18n/resourceBundle}.
* <p>
* Parameters:
* <ul>
* <li>{@code baseName}: The resource bundle base name.</li>
* <li>{@code language}: {@link Locale} Language. (optional)</li>
* <li>{@code country}: {@link Locale} Country. (optional)</li>
* <li>{@code variant}: {@link Locale} Language variant. (optional)</li>
* </ul>
*
* @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);
}
}
7 changes: 7 additions & 0 deletions war/src/main/js/api/symbols.js
Original file line number Diff line number Diff line change
@@ -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});
}
26 changes: 18 additions & 8 deletions war/src/main/js/components/command-palette/command-palette.js
Original file line number Diff line number Diff line change
@@ -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];

Expand All @@ -20,9 +20,9 @@ window.addEventListener('load', () => {
// Events
headerCommandPaletteButton.addEventListener("click", function () {
if (commandPalette.hasAttribute("open")) {
hideCommandCenter();
hideCommandPalette();
} else {
showCommandCenter();
showCommandPalette();
}
});

Expand All @@ -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;
Expand All @@ -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",
Expand Down Expand Up @@ -135,16 +145,16 @@ 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();

// Fire empty input event to command bar to set appropriate UI states (OOBE, results, no results)
commandPaletteInput.dispatchEvent(new Event("input"));
}

function hideCommandCenter() {
function hideCommandPalette() {
commandPalette.close();
}

Expand Down
11 changes: 10 additions & 1 deletion war/src/main/js/components/command-palette/models.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Symbols from "./symbols";
import {getSymbol} from "@/api/symbols";

export class LinkResult {
constructor(icon, label, description, category, url, isExternal) {
Expand All @@ -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}) : `<img src="${this.icon}" alt="" />`

return `<a class="jenkins-command-palette__results__item" href="${this.url}">
<div class="jenkins-command-palette__results__item__icon">${this.icon ? `${this.icon.svg ? this.icon.svg : `<img src="${this.icon.url}" alt="" />`}` : ``}</div>
<div class="jenkins-command-palette__results__item__icon">${icon2}</div>
${this.label}
${this.description ? `<span class="jenkins-command-palette__results__item__description">${this.description}</span>` : ``}
${this.isExternal ? Symbols.EXTERNAL_LINK : Symbols.CHEVRON_RIGHT}
Expand Down
1 change: 0 additions & 1 deletion war/src/main/js/components/command-palette/symbols.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9ef136f

Please sign in to comment.