You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devdocs/assets/javascripts/lib/ajax.js

178 lines
3.8 KiB

// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS205: Consider reworking code to avoid use of IIFEs
* DS207: Consider shorter variations of null checks
* DS208: Avoid top-level this
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
const MIME_TYPES = {
1 year ago
json: "application/json",
html: "text/html",
};
11 years ago
1 year ago
this.ajax = function (options) {
applyDefaults(options);
serializeData(options);
11 years ago
const xhr = new XMLHttpRequest();
xhr.open(options.type, options.url, options.async);
11 years ago
applyCallbacks(xhr, options);
applyHeaders(xhr, options);
11 years ago
xhr.send(options.data);
11 years ago
if (options.async) {
1 year ago
return { abort: abort.bind(undefined, xhr) };
} else {
return parseResponse(xhr, options);
}
};
11 years ago
ajax.defaults = {
async: true,
1 year ago
dataType: "json",
timeout: 30,
1 year ago
type: "GET",
};
1 year ago
// contentType
// context
// data
// error
// headers
// progress
// success
// url
var applyDefaults = function (options) {
for (var key in ajax.defaults) {
1 year ago
if (options[key] == null) {
options[key] = ajax.defaults[key];
}
}
};
1 year ago
var serializeData = function (options) {
if (!options.data) {
return;
}
1 year ago
if (options.type === "GET") {
options.url += "?" + serializeParams(options.data);
options.data = null;
} else {
options.data = serializeParams(options.data);
}
};
1 year ago
var serializeParams = (params) =>
(() => {
const result = [];
for (var key in params) {
var value = params[key];
result.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
}
return result;
})().join("&");
1 year ago
var applyCallbacks = function (xhr, options) {
if (!options.async) {
return;
}
1 year ago
xhr.timer = setTimeout(
onTimeout.bind(undefined, xhr, options),
options.timeout * 1000,
);
if (options.progress) {
xhr.onprogress = options.progress;
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
clearTimeout(xhr.timer);
onComplete(xhr, options);
}
};
};
1 year ago
var applyHeaders = function (xhr, options) {
if (!options.headers) {
options.headers = {};
}
if (options.contentType) {
1 year ago
options.headers["Content-Type"] = options.contentType;
}
1 year ago
if (
!options.headers["Content-Type"] &&
options.data &&
options.type !== "GET"
) {
options.headers["Content-Type"] = "application/x-www-form-urlencoded";
}
if (options.dataType) {
1 year ago
options.headers["Accept"] =
MIME_TYPES[options.dataType] || options.dataType;
}
for (var key in options.headers) {
var value = options.headers[key];
xhr.setRequestHeader(key, value);
}
};
1 year ago
var onComplete = function (xhr, options) {
if (200 <= xhr.status && xhr.status < 300) {
let response;
if ((response = parseResponse(xhr, options)) != null) {
onSuccess(response, xhr, options);
} else {
1 year ago
onError("invalid", xhr, options);
}
} else {
1 year ago
onError("error", xhr, options);
}
};
1 year ago
var onSuccess = function (response, xhr, options) {
if (options.success != null) {
options.success.call(options.context, response, xhr, options);
}
};
1 year ago
var onError = function (type, xhr, options) {
if (options.error != null) {
options.error.call(options.context, type, xhr, options);
}
};
1 year ago
var onTimeout = function (xhr, options) {
xhr.abort();
1 year ago
onError("timeout", xhr, options);
};
1 year ago
var abort = function (xhr) {
clearTimeout(xhr.timer);
xhr.onreadystatechange = null;
xhr.abort();
};
1 year ago
var parseResponse = function (xhr, options) {
if (options.dataType === "json") {
return parseJSON(xhr.responseText);
} else {
return xhr.responseText;
}
};
1 year ago
var parseJSON = function (json) {
try {
return JSON.parse(json);
} catch (error) {}
};