You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
5.3 KiB
JavaScript
165 lines
5.3 KiB
JavaScript
/*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
/**
|
|
* Get the API endpoint URL for an action
|
|
* @param {String} action
|
|
* @return {String}
|
|
*/
|
|
function getActionUrl(action) {
|
|
return SETTINGS["server"] + "/" + action;
|
|
}
|
|
|
|
/**
|
|
* Compare two version strings.
|
|
* http://stackoverflow.com/a/16187766/2534036
|
|
* @param {string} a
|
|
* @param {string} b
|
|
* @returns a number < 0 if a < b, a number > 0 if a > b, 0 if a = b
|
|
*/
|
|
function compareVersions(a, b) {
|
|
var i, diff;
|
|
var regExStrip0 = /(\.0+)+$/;
|
|
var segmentsA = a.replace(regExStrip0, '').split('.');
|
|
var segmentsB = b.replace(regExStrip0, '').split('.');
|
|
var l = Math.min(segmentsA.length, segmentsB.length);
|
|
|
|
for (i = 0; i < l; i++) {
|
|
diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10);
|
|
if (diff) {
|
|
return diff;
|
|
}
|
|
}
|
|
return segmentsA.length - segmentsB.length;
|
|
}
|
|
|
|
function checkClientVersion(success, failure) {
|
|
return $.ajax({
|
|
type: 'GET',
|
|
url: getActionUrl("version.txt"),
|
|
timeout: 5000,
|
|
success: function (version) {
|
|
$.getJSON("package.json", {}, function (local) {
|
|
if (compareVersions(local.version, version) < 0) {
|
|
success(false);
|
|
} else {
|
|
success(true);
|
|
}
|
|
});
|
|
},
|
|
error: function (xhr, textStatus, errorThrown) {
|
|
if (typeof failure != 'function') {
|
|
return;
|
|
}
|
|
switch (textStatus) {
|
|
case "timeout":
|
|
failure("Couldn't connect to the server (timeout).", xhr);
|
|
break;
|
|
case "error":
|
|
failure("Couldn't connect to the server (error).", xhr);
|
|
break;
|
|
case "parseerror":
|
|
failure("The server sent a malformed response.", xhr);
|
|
break;
|
|
default:
|
|
failure("Couldn't connect to the server.", xhr);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Call an API function on the server.
|
|
* @param {string} action
|
|
* @param {array} data key: value array of arguments for the server.
|
|
* @param {function} success Called with the JSON response object.
|
|
* @param {function} failure Called with an error message string.
|
|
* @return {undefined}
|
|
*/
|
|
function callAPI(action, data, success, failure) {
|
|
data.username = localStorage.getItem("username");
|
|
data.password = localStorage.getItem("password");
|
|
return $.ajax({
|
|
type: 'POST',
|
|
dataType: 'json',
|
|
url: getActionUrl(action),
|
|
data: data,
|
|
timeout: 5000,
|
|
success: function (data, textStatus) {
|
|
if (data.status === "OK") {
|
|
success(data);
|
|
return;
|
|
}
|
|
if (typeof failure != 'function') {
|
|
return;
|
|
}
|
|
if (data.status === "ERROR") {
|
|
failure(data.msg);
|
|
} else {
|
|
failure("The server sent an invalid response.");
|
|
}
|
|
},
|
|
error: function (xhr, textStatus, errorThrown) {
|
|
if (typeof failure != 'function') {
|
|
return;
|
|
}
|
|
switch (textStatus) {
|
|
case "timeout":
|
|
failure("Couldn't connect to the server (timeout).");
|
|
break;
|
|
case "error":
|
|
failure("Couldn't connect to the server (error).");
|
|
break;
|
|
case "parseerror":
|
|
failure("The server sent a malformed response.");
|
|
break;
|
|
default:
|
|
failure("Couldn't connect to the server.");
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Call an API function on the server. Makes fewer assumptions about what
|
|
* constitutes a success response.
|
|
* @param {string} action
|
|
* @param {array} data key: value array of arguments for the server.
|
|
* @param {function} success Called with the JSON response object.
|
|
* @param {function} failure Called with an error message string.
|
|
* @return {undefined}
|
|
*/
|
|
function callAPIRawResponse(action, data, success, failure) {
|
|
data.username = localStorage.getItem("username");
|
|
data.password = localStorage.getItem("password");
|
|
return $.ajax({
|
|
type: 'POST',
|
|
url: getActionUrl(action),
|
|
data: data,
|
|
timeout: 5000,
|
|
success: function (data, textStatus) {
|
|
success(data);
|
|
},
|
|
error: function (xhr, textStatus, errorThrown) {
|
|
if (typeof failure != 'function') {
|
|
return;
|
|
}
|
|
switch (textStatus) {
|
|
case "timeout":
|
|
failure("Couldn't connect to the server (timeout).", xhr);
|
|
break;
|
|
case "error":
|
|
failure("Couldn't connect to the server (error).", xhr);
|
|
break;
|
|
case "parseerror":
|
|
failure("The server sent a malformed response.", xhr);
|
|
break;
|
|
default:
|
|
failure("Couldn't connect to the server.", xhr);
|
|
}
|
|
}
|
|
});
|
|
} |