Skip to content

Commit

Permalink
Remove dependency on Prototype or jQuery (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored May 3, 2023
1 parent a11e76c commit 56a94d9
Show file tree
Hide file tree
Showing 21 changed files with 94 additions and 4,882 deletions.
64 changes: 34 additions & 30 deletions core/src/main/resources/org/kohsuke/stapler/bind.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// bind tag takes care of the dependency as an adjunct

function makeStaplerProxy(url,crumb,methods) {
function makeStaplerProxy(url,staplerCrumb,methods) {
if (url.substring(url.length - 1) !== '/') url+='/';
var proxy = {};

Expand All @@ -26,39 +26,43 @@ function makeStaplerProxy(url,crumb,methods) {
for (var i=0; i<args.length-(callback!=null?1:0); i++)
a.push(args[i]);

if(window.jQuery === 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",
success: function(data, textStatus, jqXHR) {
if (callback!=null) {
var 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);
}

fetch(url + methodName, {
method: 'POST',
headers: headers,
body: stringify(a),
})
.then(function(response) {
if (response.ok) {
if (response.headers.has('content-type') && response.headers.get('content-type').startsWith('application/json')) {
response.json().then(function(responseObject) {
var t = {};
t.responseObject = function() {
return data;
return responseObject;
};
callback(t);
}
}
});
} else { //Assume prototype should work
new Ajax.Request(url+methodName, {
method: 'post',
requestHeaders: {'Content-type':'application/x-stapler-method-invocation;charset=UTF-8','Crumb':crumb},
postBody: stringify(a),
onSuccess: function(t) {
if (callback!=null) {
t.responseObject = function() {
return eval('('+this.responseText+')');
};
callback(t);
}
if (callback != null) {
callback(t);
}
});
} else {
response.text().then(function(responseText) {
var t = {};
t.responseText = responseText;
if (callback != null) {
callback(t);
}
});
}
});
}
}
});
}
};

Expand Down
Loading

0 comments on commit 56a94d9

Please sign in to comment.