From 2cff5b87b52fa80be8c9cb650724e8e3475e8a97 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 8 May 2023 11:11:48 -0700 Subject: [PATCH] Revert "JENKINS-70922 Remove Prototype Ajax.Request usage from select.js" This reverts commit 13661a897dd24840cd102a283290faa2b3833acf. --- .../main/resources/lib/form/select/select.js | 76 ++++++++----------- war/.eslintrc.js | 1 - .../main/webapp/scripts/hudson-behavior.js | 13 ---- 3 files changed, 30 insertions(+), 60 deletions(-) diff --git a/core/src/main/resources/lib/form/select/select.js b/core/src/main/resources/lib/form/select/select.js index 315d0c4ace93..78265d20b69a 100644 --- a/core/src/main/resources/lib/form/select/select.js +++ b/core/src/main/resources/lib/form/select/select.js @@ -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) { diff --git a/war/.eslintrc.js b/war/.eslintrc.js index cb93245fb38f..223b360aca8d 100644 --- a/war/.eslintrc.js +++ b/war/.eslintrc.js @@ -49,7 +49,6 @@ module.exports = { makeButton: "readonly", notificationBar: "readonly", object: "readonly", - objectToUrlFormEncoded: "readonly", onSetupWizardInitialized: "readonly", Prototype: "readonly", refillOnChange: "readonly", diff --git a/war/src/main/webapp/scripts/hudson-behavior.js b/war/src/main/webapp/scripts/hudson-behavior.js index eb42f1d844a3..78357e084dd8 100644 --- a/war/src/main/webapp/scripts/hudson-behavior.js +++ b/war/src/main/webapp/scripts/hudson-behavior.js @@ -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. */