Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions core/src/main/java/hudson/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import hudson.model.ParameterDefinition;
import hudson.model.ParameterDefinition.ParameterDescriptor;
import hudson.model.PasswordParameterDefinition;
import hudson.model.RootAction;
import hudson.model.Run;
import hudson.model.Slave;
import hudson.model.TimeZoneProperty;
Expand Down Expand Up @@ -2097,6 +2098,46 @@
return url.endsWith(href);
}

/**
* If the given {@code Action} is a {@link RootAction#isPrimaryAction() primary} {code RootAction}, or a parent of the current page, return {@code true}.
* Used in {@code actions.jelly} to decide if the action should shown in the main header or the hamburger.
*/
@Restricted(NoExternalUse.class)
public static boolean showInPrimaryHeader(Action action) {
// regular Actions can be injected into Jenkins via a factory.
if (action instanceof RootAction ra) {
if (ra.isPrimaryAction()) {
return true;
}
}
String path = Stapler.getCurrentRequest2().getPathInfo();
if (path == null || path.equals("/")) {
// we are in the root page so there is nothing current
return false;
}

String actionPath = action.getUrlName();
if (actionPath == null) {

Check warning on line 2120 in core/src/main/java/hudson/Functions.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 2120 is only partially covered, one branch is missing
// we are not a primary action and can not ever be current when we have no URL
return false;

Check warning on line 2122 in core/src/main/java/hudson/Functions.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 2122 is not covered by tests
}

// RootActions are not expected to start with a `/` but some do and some do, and not all Actions will be RootActions
// but path will always start with a "/"
if (!actionPath.startsWith("/")) {

Check warning on line 2127 in core/src/main/java/hudson/Functions.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 2127 is only partially covered, one branch is missing
actionPath = '/' + actionPath;
}

// the action /foo should not match /foobar but should match /foo/bar
if (!actionPath.endsWith("/")) {

Check warning on line 2132 in core/src/main/java/hudson/Functions.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 2132 is only partially covered, one branch is missing
actionPath = actionPath + '/';
}
if (!path.endsWith("/")) {
path = path + '/';
}
return path.startsWith(actionPath);
}

/**
* @deprecated From JEXL expressions ({@code ${…}}) in {@code *.jelly} files
* you can use {@code [obj]} syntax to construct an {@code Object[]}
Expand Down
18 changes: 17 additions & 1 deletion core/src/main/java/jenkins/views/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import hudson.ExtensionComponent;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.Functions;
import hudson.model.Action;
import hudson.model.RootAction;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -82,10 +84,24 @@
return Jenkins.get()
.getActions()
.stream()
.filter(e -> e.getIconFileName() != null || (e instanceof IconSpec is && is.getIconClassName() != null))
.filter(e -> e.getIconFileName() != null || (e instanceof IconSpec is && is.getIconClassName() != null) || hasLegacyView(e))

Check warning on line 87 in core/src/main/java/jenkins/views/Header.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 87 is only partially covered, one branch is missing
.sorted(Comparator.comparingDouble(
a -> rootActionsOrdinal.getOrDefault(a.getClass().getName(), Double.MAX_VALUE)
).reversed())
.toList();
}

/**
* Jenkins will show actions with a custom action.jelly even if its getIconFileName returned {@code null}.
* @param action the action to check if it has a custom view.
* @return {@code true} iff the action has an {@code action.jelly}
*/
private static boolean hasLegacyView(Action action) {
try {
return Functions.hasView(action, "action");
} catch (IOException ignored) {
// can not load the view so ignore for the header
return false;

Check warning on line 104 in core/src/main/java/jenkins/views/Header.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 102-104 are not covered by tests
}
}
}
4 changes: 2 additions & 2 deletions core/src/main/resources/lib/layout/header/actions.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<!-- first render all primary actions (except the user login )-->
<j:forEach var="action" items="${allActions}">
<j:set var="isCurrent" value="${h.hyperlinkMatchesCurrentPage(action.urlName)}" />
<j:set var="isPrimary" value="${action.isPrimaryAction()}"/>
<j:set var="isPrimary" value="${h.showInPrimaryHeader(action)}"/>

<j:if test="${isCurrent or isPrimary}">
<!-- do not show the user avatar -->
Expand All @@ -23,7 +23,7 @@
<j:set var="hamburgerEntries">
<j:forEach var="action" items="${allActions}">
<j:set var="isCurrent" value="${h.hyperlinkMatchesCurrentPage(action.urlName)}" />
<j:set var="isPrimary" value="${action.isPrimaryAction()}"/>
<j:set var="isPrimary" value="${h.showInPrimaryHeader(action)}"/>
<j:if test="${!isCurrent and !isPrimary}">
<h:secondaryAction action="${action}"/>
</j:if>
Expand Down
116 changes: 65 additions & 51 deletions core/src/main/resources/lib/layout/header/primaryAction.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,73 @@

</st:documentation>

<j:set var="jumplist">
<st:include it="${action}" page="jumplist.jelly" optional="true" />
</j:set>
<x:comment>rendering primary action: ${action.class.name}</x:comment>
<j:if test="${jumplist.length() == 0}">
<j:set var="tooltip">
<st:include it="${action}" page="tooltip.jelly" optional="true" />
</j:set>
</j:if>
<j:choose>
<j:when test="${h.hasView(action, 'action')}">
<j:scope>
<!-- Root actions expect to be in the context of Jenkins (as it) -->
<j:set var="it" value="${app}"/>
<j:set var="isInPrimaryHeader" value="true"/>
<st:include page="action.jelly" from="${action}" optional="true"/>
</j:scope>
</j:when>
<j:otherwise>
<j:set var="icon" value="${action.iconClassName != null ? action.iconClassName : action.iconFileName}"/>
<j:if test="${icon != null}">
<j:set var="jumplist">
<st:include it="${action}" page="jumplist.jelly" optional="true" />
</j:set>
<x:comment>rendering primary action: ${action.class.name}</x:comment>
<j:if test="${jumplist.length() == 0}">
<j:set var="tooltip">
<st:include it="${action}" page="tooltip.jelly" optional="true" />
</j:set>
</j:if>

<j:set var="badge" value="${action.badge}" />
<j:if test="${jumplist.length() == 0 and tooltip.length() == 0}">
<j:set var="tooltip">
<div style="text-align: center;">${action.displayName}</div>
<j:if test="${badge != null}">
<div style="text-align: center; color: var(--text-color-secondary)">${badge.tooltip}</div>
</j:if>
</j:set>
</j:if>
<j:set var="badge" value="${action.badge}" />
<j:if test="${jumplist.length() == 0 and tooltip.length() == 0}">
<j:set var="tooltip">
<div style="text-align: center;">${action.displayName}</div>
<j:if test="${badge != null}">
<div style="text-align: center; color: var(--text-color-secondary)">${badge.tooltip}</div>
</j:if>
</j:set>
</j:if>

<j:set var="interactive" value="${jumplist.length() gt 0}" />
<j:set var="icon" value="${action.iconClassName != null ? action.iconClassName : action.iconFileName}"/>
<x:element name="${action.urlName == null ? 'button' : 'a'}">
<x:attribute name="data-dropdown">${interactive}</x:attribute>
<x:attribute name="id">root-action-${action.class.simpleName}</x:attribute>
<x:attribute name="href">${h.getActionUrl(app.url, action)}</x:attribute>
<j:if test="${interactive}">
<x:attribute name="data-tippy-offset">[0, 10]</x:attribute>
</j:if>
<j:if test="${!interactive}">
<x:attribute name="data-html-tooltip" escapeText="false"><j:out value="${tooltip}" /></x:attribute>
</j:if>
<x:attribute name="data-tooltip-interactive">${interactive}</x:attribute>
<x:attribute name="data-tippy-animation">tooltip</x:attribute>
<x:attribute name="data-tippy-theme">${interactive ? 'dropdown' : 'tooltip'}</x:attribute>
<x:attribute name="data-tippy-trigger">mouseenter focus</x:attribute>
<x:attribute name="data-tippy-touch">${interactive}</x:attribute>
<x:attribute name="data-type">header-action</x:attribute>
<x:attribute name="draggable">false</x:attribute>
<x:attribute name="class">jenkins-button ${isCurrent ? '' : 'jenkins-button--tertiary'}</x:attribute>
<l:icon src="${icon}" class="${action.class.name == 'jenkins.model.navigation.UserAction' ? 'jenkins-avatar' : ''}"/>
<span class="jenkins-visually-hidden" data-type="action-label">${action.displayName}</span>
<j:if test="${badge != null}">
<span class="jenkins-badge jenkins-!-${badge.severity}-color" />
</j:if>
</x:element>
<j:set var="interactive" value="${jumplist.length() gt 0}" />
<x:element name="${action.urlName == null ? 'button' : 'a'}">
<x:attribute name="data-dropdown">${interactive}</x:attribute>
<x:attribute name="id">root-action-${action.class.simpleName}</x:attribute>
<x:attribute name="href">${h.getActionUrl(app.url, action)}</x:attribute>
<j:if test="${interactive}">
<x:attribute name="data-tippy-offset">[0, 10]</x:attribute>
</j:if>
<j:if test="${!interactive}">
<x:attribute name="data-html-tooltip" escapeText="false"><j:out value="${tooltip}" /></x:attribute>
</j:if>
<x:attribute name="data-tooltip-interactive">${interactive}</x:attribute>
<x:attribute name="data-tippy-animation">tooltip</x:attribute>
<x:attribute name="data-tippy-theme">${interactive ? 'dropdown' : 'tooltip'}</x:attribute>
<x:attribute name="data-tippy-trigger">mouseenter focus</x:attribute>
<x:attribute name="data-tippy-touch">${interactive}</x:attribute>
<x:attribute name="data-type">header-action</x:attribute>
<x:attribute name="draggable">false</x:attribute>
<x:attribute name="class">jenkins-button ${isCurrent ? '' : 'jenkins-button--tertiary'}</x:attribute>
<l:icon src="${icon}" class="${action.class.name == 'jenkins.model.navigation.UserAction' ? 'jenkins-avatar' : ''}"/>
<span class="jenkins-visually-hidden" data-type="action-label">${action.displayName}</span>
<j:if test="${badge != null}">
<span class="jenkins-badge jenkins-!-${badge.severity}-color" />
</j:if>
</x:element>

<j:if test="${interactive}">
<template>
<div class="jenkins-dropdown">
<j:out value="${jumplist}" />
</div>
</template>
</j:if>
<j:if test="${interactive}">
<template>
<div class="jenkins-dropdown">
<j:out value="${jumplist}" />
</div>
</template>
</j:if>
</j:if>
</j:otherwise>
</j:choose>

</j:jelly>
30 changes: 22 additions & 8 deletions core/src/main/resources/lib/layout/header/secondaryAction.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,27 @@
</st:attribute>
</st:documentation>

<j:set var="badge" value="${action.badge}"/>
<j:set var="badgeClazz" value="${badge != null ? 'jenkins-badge jenkins-!-${badge.severity}-color' : ''}"/>

<dd:item icon="${action.iconClassName != null ? action.iconClassName : action.iconFileName}"
text="${action.displayName}"
href="${h.getActionUrl(app.url, action)}"
clazz="${badgeClazz}">
</dd:item>
<j:choose>
<j:when test="${h.hasView(action, 'action')}">
<j:scope>
<!-- Root actions expect to be in the context of Jenkins (as it) -->
<j:set var="it" value="${app}"/>
<j:set var="isInSecondaryHeader" value="true"/>
<st:include page="action.jelly" from="${action}" optional="true"/>
</j:scope>
</j:when>
<j:otherwise>
<j:set var="icon" value="${action.iconClassName != null ? action.iconClassName : action.iconFileName}"/>
<j:if test="${icon != null}">
<j:set var="badge" value="${action.badge}"/>
<j:set var="badgeClazz" value="${badge != null ? 'jenkins-badge jenkins-!-${badge.severity}-color' : ''}"/>

<dd:item icon="${action.iconClassName != null ? action.iconClassName : action.iconFileName}"
text="${action.displayName}"
href="${h.getActionUrl(app.url, action)}"
clazz="${badgeClazz}">
</dd:item>
</j:if>
</j:otherwise>
</j:choose>
</j:jelly>
Loading
Loading