[JENKINS-70922] Remove Prototype Ajax.Request usage from select.js (#7982)

This commit is contained in:
Tim Jacomb authored and GitHub committed 2023-05-17 17:35:14 -07:00
1 parent 04ee8e8134
commit d3f7958198
1 file changed
+59 -42
@@ -43,56 +43,73 @@ function updateListBox(listBox, url, config) {
status.innerHTML = "";
}
config.onSuccess = function (rsp) {
l.classList.remove("select-ajax-pending");
var currentSelection = l.value;
rsp.json().then((result) => {
l.classList.remove("select-ajax-pending");
var currentSelection = l.value;
// clear the contents
while (l.length > 0) {
l.options[0] = null;
}
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
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 (opts[i].value == currentSelection) {
possibleIndex = i;
}
}
// 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");
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;
}
}
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
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;
}
}
// 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) {
rsp.text().then((responseText) => {
l.classList.remove("select-ajax-pending");
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;
}
}
});
};
l.classList.add("select-ajax-pending");
new Ajax.Request(url, config);
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);
}
});
}
Behaviour.specify("SELECT.select", "select", 1000, function (e) {