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

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

Merged
merged 1 commit into from
May 18, 2023
Merged
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
95 changes: 56 additions & 39 deletions core/src/main/resources/lib/form/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
// clear the contents
while (l.length > 0) {
l.options[0] = null;
}
if (opts[i].value == currentSelection) {
possibleIndex = i;

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 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);
}
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;
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) {
Expand Down