liftinstall/static/js/helpers.js

145 lines
4.0 KiB
JavaScript
Raw Normal View History

2018-01-27 05:02:49 +01:00
/**
* helpers.js
*
* Additional state-less helper methods.
*/
var request_id = 0;
/**
* Makes a AJAX request.
*
* @param path The path to connect to.
* @param successCallback A callback with a JSON payload.
* @param failCallback A fail callback. Optional.
* @param data POST data. Optional.
*/
function ajax(path, successCallback, failCallback, data) {
if (failCallback === undefined) {
failCallback = defaultFailHandler;
}
2018-08-09 07:21:50 +02:00
console.log("Making HTTP request to " + path);
2018-01-27 05:02:49 +01:00
var req = new XMLHttpRequest();
req.addEventListener("load", function() {
// The server can sometimes return a string error. Make sure we handle this.
if (this.status === 200 && this.getResponseHeader('Content-Type').indexOf("application/json") !== -1) {
successCallback(JSON.parse(this.responseText));
} else {
2018-08-07 07:48:49 +02:00
failCallback(this.responseText);
2018-01-27 05:02:49 +01:00
}
});
req.addEventListener("error", failCallback);
req.open(data == null ? "GET" : "POST", path + "?nocache=" + request_id++, true);
// Rocket only currently supports URL encoded forms.
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
if (data != null) {
var form = "";
for (var key in data) {
2018-08-07 12:23:28 +02:00
if (!data.hasOwnProperty(key)) {
continue;
}
2018-01-27 05:02:49 +01:00
if (form !== "") {
form += "&";
}
2018-08-07 12:23:28 +02:00
2018-01-27 05:02:49 +01:00
form += encodeURIComponent(key) + "=" + encodeURIComponent(data[key]);
}
req.send(form);
} else {
req.send();
}
}
/**
* Makes a AJAX request, streaming each line as it arrives. Type should be text/plain,
2018-08-04 14:18:12 +02:00
* each line will be interpreted as JSON separately.
*
* @param path The path to connect to.
* @param callback A callback with a JSON payload. Called for every line as it comes.
* @param successCallback A callback with a raw text payload.
* @param failCallback A fail callback. Optional.
* @param data POST data. Optional.
*/
function stream_ajax(path, callback, successCallback, failCallback, data) {
var req = new XMLHttpRequest();
2018-08-09 07:21:50 +02:00
console.log("Making streaming HTTP request to " + path);
req.addEventListener("load", function() {
// The server can sometimes return a string error. Make sure we handle this.
if (this.status === 200) {
successCallback(this.responseText);
} else {
2018-08-07 07:48:49 +02:00
failCallback(this.responseText);
}
});
var buffer = "";
2018-08-07 12:23:28 +02:00
var seenBytes = 0;
req.onreadystatechange = function() {
if(req.readyState > 2) {
2018-08-07 12:23:28 +02:00
buffer += req.responseText.substr(seenBytes);
var pointer;
while ((pointer = buffer.indexOf("\n")) >= 0) {
var line = buffer.substring(0, pointer).trim();
buffer = buffer.substring(pointer + 1);
if (line.length === 0) {
continue;
}
var contents = JSON.parse(line);
callback(contents);
}
2018-08-07 12:23:28 +02:00
seenBytes = req.responseText.length;
}
};
req.addEventListener("error", failCallback);
req.open(data == null ? "GET" : "POST", path + "?nocache=" + request_id++, true);
// Rocket only currently supports URL encoded forms.
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
if (data != null) {
var form = "";
for (var key in data) {
2018-08-07 12:23:28 +02:00
if (!data.hasOwnProperty(key)) {
continue;
}
if (form !== "") {
form += "&";
}
2018-08-07 12:23:28 +02:00
form += encodeURIComponent(key) + "=" + encodeURIComponent(data[key]);
}
req.send(form);
} else {
req.send();
}
}
2018-01-27 05:02:49 +01:00
/**
* The default handler if a AJAX request fails. Not to be used directly.
*
* @param e The XMLHttpRequest that failed.
*/
function defaultFailHandler(e) {
console.error("A AJAX request failed, and was not caught:");
console.error(e);
}