Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 'Add credentials' dropdown button #551

Merged
merged 9 commits into from
Oct 6, 2024
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
4 changes: 1 addition & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.426.x</artifactId>
<!-- when updating remove the bouncycastle-api override -->
<artifactId>bom-2.452.x</artifactId>
<version>3208.vb_21177d4b_cd9</version>
<scope>import</scope>
<type>pom</type>
Expand All @@ -110,7 +109,6 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>bouncycastle-api</artifactId>
<version>2.30.1.78.1-248.ve27176eb_46cb_</version>
</dependency>
<!-- test dependencies -->
<dependency>
Expand Down
52 changes: 20 additions & 32 deletions src/main/resources/lib/credentials/select.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:l="/lib/layout"
xmlns:f="/lib/form" xmlns:t="/lib/hudson">
xmlns:f="/lib/form" xmlns:t="/lib/hudson" xmlns:dd="/lib/layout/dropdowns">
<st:documentation>
A <code>select</code> control that supports the data binding and AJAX updates with support for adding credentials.
Your descriptor should have the 'doFillXyzItems' method, which returns a StandardListBoxModel
Expand Down Expand Up @@ -114,46 +114,34 @@
<j:set var="storeItems" value="${selectHelper.getStoreItems(context, includeUser)}"/>
<j:choose>
<j:when test="${selectHelper.hasCreatePermission(context, includeUser) and storeItems != null and !storeItems.isEmpty()}">
<button class="credentials-add-menu hetero-list-add jenkins-!-margin-top-2" menualign="${attrs.menuAlign}"
suffix="${attrs.name}">
<l:icon src="symbol-add"/>
${%Add}
</button>
<div class="credentials-add-menu-items yuimenu">
<div class="bd">
<ul class="first-of-type">
<l:overflowButton icon="symbol-add"
text="${%Add}"
tooltip="${null}"
clazz="jenkins-!-margin-top-2 credentials-add-menu">
<j:forEach var="storeItem" items="${storeItems}">
<j:choose>
<j:when test="${storeItem.enabled}">
<li class="yuimenuitem" data-url="${request.contextPath}/${storeItem.url}/dialog">
<span class="yuimenuitemlabel" tooltip="${storeItem.description}">
<l:icon class="${storeItem.iconClassName} icon-sm"/>
${storeItem.displayName}
</span>
</li>
</j:when>
<j:otherwise>
<li class="yuimenuitem" disabled="disabled">
<span class="yuimenuitemlabel" tooltip="${storeItem.description}" disabled="disabled">
<l:icon class="${storeItem.iconClassName} icon-sm"/>
${storeItem.displayName}
</span>
</li>
</j:otherwise>
</j:choose>
<dd:custom>
<button class="jenkins-dropdown__item"
tooltip="${storeItem.description}"
data-type="credentials-add-store-item"
data-url="${request.contextPath}/${storeItem.url}/dialog"
disabled="${storeItem.enabled ? null : true}">
<div class="jenkins-dropdown__item__icon">
<l:icon src="${storeItem.iconClassName} icon-sm" />
</div>
${storeItem.displayName}
</button>
</dd:custom>
</j:forEach>
</ul>
</div>
</div>
</l:overflowButton>
</j:when>
<j:otherwise>
<button class="credentials-add jenkins-!-margin-top-2">
<button class="jenkins-button credentials-add jenkins-!-margin-top-2"
type="button">
<l:icon src="symbol-add"/>
${%Add}
</button>
</j:otherwise>
</j:choose>

</div>
<j:if test="${attrs.expressionAllowed}">
<div class="credentials-select-content-param ${paramValue?'credentials-select-content-active':'credentials-select-content-inactive'}">
Expand Down
47 changes: 6 additions & 41 deletions src/main/resources/lib/credentials/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,49 +127,14 @@ window.credentials.addSubmit = function (_) {
}
};

Behaviour.specify("BUTTON.credentials-add-menu", 'credentials-select', -99, function(e) {
var btn = e;
var menu = btn.nextElementSibling;
while (menu && !menu.matches('DIV.credentials-add-menu-items')) {
menu = menu.nextElementSibling;
}
if (menu) {
var menuAlign = (btn.getAttribute("menualign") || "tl-bl");

var menuButton = new YAHOO.widget.Button(btn, {
type: "menu",
menu: menu,
menualignment: menuAlign.split("-"),
menuminscrollheight: 250
});
// copy class names
for (var i = 0; i < btn.classList.length; i++) {
menuButton._button.classList.add(btn.classList.item(i));
}
menuButton._button.setAttribute("suffix", btn.getAttribute("suffix"));
menuButton.getMenu().clickEvent.subscribe(function (type, args, value) {
var item = args[1];
if (item.cfg.getProperty("disabled")) {
return;
}
window.credentials.add(item.srcElement.getAttribute('data-url'));
});
// YUI menu will not parse disabled when using DIV-LI only when using SELECT-OPTION
// but SELECT-OPTION doesn't support images, so we need to catch the rendering and roll our
// own disabled attribute support
menuButton.getMenu().beforeShowEvent.subscribe(function(type,args,value){
var items = this.getItems();
for (var i = 0; i < items.length; i++) {
if (items[i].srcElement.getAttribute('disabled')) {
items[i].cfg.setProperty('disabled', true);
}
}
});
}
e=null;
Behaviour.specify("[data-type='credentials-add-store-item']", 'credentials-add-store-item', -99, function(e) {
e.addEventListener("click", function (event) {
window.credentials.add(event.target.dataset.url);
});
e = null;
});
Behaviour.specify("BUTTON.credentials-add", 'credentials-select', 0, function (e) {
makeButton(e, e.disabled ? null : window.credentials.add);
e.addEventListener("click", window.credentials.add);
e = null; // avoid memory leak
});
Behaviour.specify("DIV.credentials-select-control", 'credentials-select', 100, function (d) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import org.htmlunit.html.HtmlButton;
import org.htmlunit.html.HtmlForm;
import org.htmlunit.html.HtmlInput;
import org.htmlunit.html.HtmlListItem;
import org.htmlunit.html.HtmlPage;
import org.htmlunit.html.HtmlSpan;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
Expand All @@ -26,10 +24,15 @@ public class CredentialsSelectHelperTest {
public void doAddCredentialsFromPopupWorksAsExpected() throws Exception {
try (JenkinsRule.WebClient wc = j.createWebClient()) {
HtmlPage htmlPage = wc.goTo("credentials-selection");

HtmlButton addCredentialsButton = htmlPage.querySelector(".credentials-add-menu");
// The 'click' event doesn't fire a 'mouseenter' event causing the menu not to show, so let's fire one
addCredentialsButton.fireEvent("mouseenter");
addCredentialsButton.click();
HtmlListItem li = htmlPage.querySelector(".credentials-add-menu-items li");
li.click();

HtmlButton jenkinsCredentialsOption = htmlPage.querySelector(".jenkins-dropdown__item");
jenkinsCredentialsOption.click();

wc.waitForBackgroundJavaScript(4000);
HtmlForm form = htmlPage.querySelector("#credentials-dialog-form");

Expand Down