Skip to content

Commit

Permalink
WIP @asc3ns10n's suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
kinow committed Aug 18, 2024
1 parent ab295ae commit e91b2be
Showing 1 changed file with 61 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ var UnoChoice = UnoChoice || ($ => {
let _self = this; // re-reference this to use within the inner function
console.log('Calling Java server code to update HTML elements...');
this.proxy.getChoicesForUI(t => {
let choices = t.responseText;
let choices = t.responseObject();
console.log(`Values returned from server: ${choices}`);
let data = JSON.parse(choices);
let newValues = data[0];
Expand Down Expand Up @@ -464,7 +464,7 @@ var UnoChoice = UnoChoice || ($ => {
console.log('Calling Java server code to update HTML elements...');
this.proxy.getChoicesForUI(t => {
$(parameterElement).empty(); // remove all children elements
let choices = t.responseText;
let choices = t.responseObject();
console.log(`Values returned from server: ${choices}`);
let data = JSON.parse(choices);
let newValues = data[0];
Expand All @@ -479,7 +479,7 @@ var UnoChoice = UnoChoice || ($ => {
$(parameterElement).empty(); // remove all children elements
console.log('Calling Java server code to update HTML elements...');
this.proxy.getChoicesForUI(t => {
let choices = t.responseText;
let choices = t.responseObject();
console.log(`Values returned from server: ${choices}`);
let data = JSON.parse(choices);
let newValues = data[0];
Expand All @@ -492,11 +492,11 @@ var UnoChoice = UnoChoice || ($ => {
});
} else if (parameterElement.id.indexOf('inputElement_') > -1) { // handle input text boxes
this.proxy.getChoicesAsStringForUI(t => {
parameterElement.value = t.responseText;
parameterElement.value = t.responseObject();
});
} else if (parameterElement.id.indexOf('formattedHtml_') > -1) { // handle formatted HTML
this.proxy.getChoicesAsStringForUI(t => {
let options = t.responseText;
let options = t.responseObject();
parameterElement.innerHTML = JSON.parse(options);
});
}
Expand Down Expand Up @@ -800,20 +800,24 @@ var UnoChoice = UnoChoice || ($ => {
* <strong>synchronously</strong>. Since many parameters must be filled only after other parameters have been
* updated, calling Jenkins methods asynchronously causes several unpredictable errors.</p>
*
* <p>JENKINS-71909: Stapler had to be updated when Prototype and jQuery dependencies
* were removed from Jenkins. This means that we also had to update this function to
* match what was done there - thanks asc3ns10n (GH).</p>
*
* @param url {string} The URL
* @param crumb {string} The crumb
* @param staplerCrumb {string} The crumb
* @param methods {Array<string>} The methods
*/
function makeStaplerProxy2(url, crumb, methods) {
function makeStaplerProxy2(url, staplerCrumb, methods) {
if (url.substring(url.length - 1) !== '/') url+='/';
let proxy = {};
var stringify;
let stringify;
if (Object.toJSON) // needs to use Prototype.js if it's present. See commit comment for discussion
stringify = Object.toJSON; // from prototype
else if (typeof(JSON)=="object" && JSON.stringify)
stringify = JSON.stringify; // standard
let genMethod = methodName => {
proxy[methodName] = function() {
proxy[methodName] = async function() {
let args = arguments;
// the final argument can be a callback that receives the return value
let callback = (() => {
Expand All @@ -825,46 +829,56 @@ var UnoChoice = UnoChoice || ($ => {
let a = [];
for (let i=0; i<args.length-(callback!=null?1:0); i++)
a.push(args[i]);
if(window.jQuery3 === window.$) { //Is jQuery the active framework?
$.ajax({
type: "POST",
url: url+methodName,
data: stringify(a),
contentType: 'application/x-stapler-method-invocation;charset=UTF-8',
headers: {'Crumb':crumb},
dataType: "json",
async: "false", // Here's the juice
success: function(data, textStatus, jqXHR) {
if (callback!==null) {
let t = {};
t.responseObject = () => data;
callback(t);
}
}
});
} else { //Assume prototype should work
const xmlhttp = new XMLHttpRequest()

xmlhttp.onreqadystatechange = function () {
if (callback!==null) {
t.responseObject = function() {
return eval(`(${this.responseText})`);
};
callback(t);
let headers = {
'Content-Type': 'application/x-stapler-method-invocation;charset=UTF-8',
'Crumb': staplerCrumb,
}
// If running in Jenkins, add Jenkins-Crumb header.
if (typeof crumb !== 'undefined') {
headers = crumb.wrap(headers);
}
// Active-Choices: this is the main difference to Jenkins' proxy;
// we block the call so that each parameter is resolved in-order.
// Not optimal, but without reactivity in Jenkins, it is hard to
// design a model where async code works and elements are rendered
// correctly -- we tried, and failed big-time:
// https://github.com/jenkinsci/active-choices-plugin/pull/79
// If you'd like this, we need to have control on how parameters
// are rendered, and have a proper reactivity system that control
// what is rendered when, and properly chain certain actions (e.g.
// a dependant parameter is only rendered after its parent/referenced
// parameter).
await fetch(url + methodName, {
method: 'POST',
headers: headers,
body: stringify(a),
})
.then(function(response) {
if (response.ok) {
const t = {
status: response.status,
statusText: response.statusText,
};
if (response.headers.has('content-type') && response.headers.get('content-type').startsWith('application/json')) {
response.json().then(function (responseObject) {
t.responseObject = function () {
return responseObject;
};
t.responseJSON = responseObject;
if (callback != null) {
callback(t);
}
});
} else {
response.text().then(function (responseText) {
t.responseText = responseText;
if (callback != null) {
callback(t);
}
});
}
}

xmlhttp.open(
"POST",
url+methodName,
false
)

xmlhttp.setRequestHeader('Content-type', 'application/x-stapler-method-invocation;charset=UTF-8')
xmlhttp.setRequestHeader('Crumb', crumb)

xmlhttp.send(stringify(a))
}
})
}
};
for(let mi = 0; mi < methods.length; mi++) {
Expand Down

0 comments on commit e91b2be

Please sign in to comment.