Skip to content

Commit

Permalink
[JENKINS-73563] Create a jenkins-button instead of a YUI button in …
Browse files Browse the repository at this point in the history
…`makeButton` (jenkinsci#9604)

Co-authored-by: Kevin Guerroudj <91883215+Kevin-CB@users.noreply.github.com>
  • Loading branch information
mawinter69 and Kevin-CB authored Aug 13, 2024
1 parent 2c3d25c commit f615612
Showing 1 changed file with 54 additions and 32 deletions.
86 changes: 54 additions & 32 deletions war/src/main/webapp/scripts/hudson-behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -903,57 +903,79 @@ function escapeHTML(html) {
}

/**
* Wraps a <button> into YUI button.
* Replaces a <input> with a <button class="jenkins-button">
*
* @param e
* button element
* @param onclick
* onclick handler
* @return
* YUI Button widget.
* wrapper with some functions (formerly a YUI widget).
* @deprecated use <button class="jenkins-button"> and attach event listeners with standard javascript
*/
function makeButton(e, onclick) {
var h = e.onclick;
var clsName = e.className;
var n = e.name;

var attributes = {};
// YUI Button class interprets value attribute of <input> as HTML
// similar to how the child nodes of a <button> are treated as HTML.
// in standard HTML, we wouldn't expect the former case, yet here we are!
if (e.tagName === "INPUT") {
attributes.label = escapeHTML(e.value);
}
var btn = new YAHOO.widget.Button(e, attributes);
console.warn(
"Deprecated call to makeButton - use <button class='jenkins-button'> instead and standard javascript to attach listeners.",
);
const h = e.onclick;
const n = e.name;

const button = document.createElement("button");
if (e.id) {
button.id = e.id;
}
if (onclick != null) {
btn.addListener("click", onclick);
button.addEventListener("click", onclick);
}
if (h != null) {
btn.addListener("click", h);
}
var be = btn.get("element");
var classesSeparatedByWhitespace = clsName.split(" ");
for (let i = 0; i < classesSeparatedByWhitespace.length; i++) {
var singleClass = classesSeparatedByWhitespace[i];
if (singleClass) {
be.classList.add(singleClass);
}
button.addEventListener("click", h);
}
if (n) {
// copy the name
be.setAttribute("name", n);
button.setAttribute("name", n);
}

// keep the data-* attributes from the source
var length = e.attributes.length;
if (e.type === "submit" || e.type === "button") {
button.type = e.type;
}
const length = e.attributes.length;
for (let i = 0; i < length; i++) {
var attribute = e.attributes[i];
var attributeName = attribute.name;
const attribute = e.attributes[i];
const attributeName = attribute.name;
if (attributeName.startsWith("data-")) {
btn._button.setAttribute(attributeName, attribute.value);
button.setAttribute(attributeName, attribute.value);
}
}
return btn;
button.innerText = e.value;
button.classList.add("jenkins-button");
const classNames = e.classList;
if (classNames.contains("primary") || classNames.contains("submit-button")) {
button.classList.add("jenkins-button--primary");
}
classNames.remove("primary");
classNames.remove("submit-button");
classNames.remove("yui-button");
for (let i = 0; i < classNames.length; i++) {
button.classList.add(classNames.item(i));
}

function Button(button) {
this.button = button;
}
Button.prototype.set = function (attributeName, value) {
if (attributeName === "disabled") {
if (value) {
this.button.disabled = "disabled";
} else {
this.button.removeAttribute("disabled");
}
}
};
Button.prototype.getForm = function () {
return this.button.closest("form");
};
e.parentNode.insertBefore(button, e);
e.remove();
return new Button(button);
}

/*
Expand Down

0 comments on commit f615612

Please sign in to comment.