Skip to content

Commit

Permalink
Revert "JENKINS-70922 Remove Prototype Ajax.Request usage from select…
Browse files Browse the repository at this point in the history
….js"

This reverts commit 13661a8.
  • Loading branch information
basil committed May 8, 2023
1 parent 3bc62ee commit 2cff5b8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 60 deletions.
76 changes: 30 additions & 46 deletions core/src/main/resources/lib/form/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,62 +53,46 @@ function updateListBox(listBox, url, config) {

var selectionSet = false; // is the selection forced by the server?
var possibleIndex = null; // if there's a new option that matches the current value, remember its index
rsp.json().then((result) => {
var opts = result.values;
for (var i = 0; i < opts.length; i++) {
l.options[i] = new Option(opts[i].name, opts[i].value);
if (opts[i].selected) {
l.selectedIndex = i;
selectionSet = true;
}
if (opts[i].value === currentSelection) {
possibleIndex = i;
}
var opts = JSON.parse(rsp.responseText).values;
for (var i = 0; i < opts.length; i++) {
l.options[i] = new Option(opts[i].name, opts[i].value);
if (opts[i].selected) {
l.selectedIndex = i;
selectionSet = true;
}

// if no value is explicitly selected by the server, try to select the same value
if (!selectionSet && possibleIndex != null) {
l.selectedIndex = possibleIndex;
if (opts[i].value == currentSelection) {
possibleIndex = i;
}
}

if (originalOnSuccess !== undefined) {
originalOnSuccess(rsp);
}
});
// if no value is explicitly selected by the server, try to select the same value
if (!selectionSet && possibleIndex != null) {
l.selectedIndex = possibleIndex;
}

if (originalOnSuccess !== undefined) {
originalOnSuccess(rsp);
}
};
config.onFailure = function (rsp) {
l.classList.remove("select-ajax-pending");
rsp.text().then((responseText) => {
status.innerHTML = responseText;
if (status.firstElementChild) {
status.firstElementChild.setAttribute("data-select-ajax-error", "true");
}
Behaviour.applySubtree(status);
// deleting values can result in the data loss, so let's not do that unless instructed
var header = rsp.headers.get("X-Jenkins-Select-Error");
if (header && "clear" === header.toLowerCase()) {
// clear the contents
while (l.length > 0) {
l.options[0] = null;
}
status.innerHTML = rsp.responseText;
if (status.firstElementChild) {
status.firstElementChild.setAttribute("data-select-ajax-error", "true");
}
Behaviour.applySubtree(status);
// deleting values can result in the data loss, so let's not do that unless instructed
var header = rsp.getResponseHeader("X-Jenkins-Select-Error");
if (header && "clear" === header.toLowerCase()) {
// clear the contents
while (l.length > 0) {
l.options[0] = null;
}
});
}
};

l.classList.add("select-ajax-pending");
fetch(url, {
method: "post",
headers: crumb.wrap({
"Content-Type": "application/x-www-form-urlencoded",
}),
body: objectToUrlFormEncoded(config.parameters),
}).then((response) => {
if (response.ok) {
config.onSuccess(response);
} else {
config.onFailure(response);
}
});
new Ajax.Request(url, config);
}

Behaviour.specify("SELECT.select", "select", 1000, function (e) {
Expand Down
1 change: 0 additions & 1 deletion war/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ module.exports = {
makeButton: "readonly",
notificationBar: "readonly",
object: "readonly",
objectToUrlFormEncoded: "readonly",
onSetupWizardInitialized: "readonly",
Prototype: "readonly",
refillOnChange: "readonly",
Expand Down
13 changes: 0 additions & 13 deletions war/src/main/webapp/scripts/hudson-behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,19 +257,6 @@ var FormChecker = {
},
};

function objectToUrlFormEncoded(parameters) {
// https://stackoverflow.com/a/37562814/4951015
// Code could be simplified if support for HTMLUnit is dropped
// body: new URLSearchParams(parameters) is enough then, but it doesn't work in HTMLUnit currently
let formBody = [];
for (const property in parameters) {
const encodedKey = encodeURIComponent(property);
const encodedValue = encodeURIComponent(parameters[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
return formBody.join("&");
}

/**
* Detects if http2 protocol is enabled.
*/
Expand Down

0 comments on commit 2cff5b8

Please sign in to comment.