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.
HelenaExpressApp/www/assets/js/track.js

115 lines
4.1 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/.
*/
function openTrackingInfoPage(id) {
if (typeof id == "undefined" || id == null || id == "") {
app.input.validate("#deviceid");
return;
}
router.navigate("/track/" + id);
}
function addClientMachineSuggestions() {
$("#client-link-account-message").addClass("display-none");
$("#client-machine-suggestion-list").addClass("display-none");
$("#client-machine-suggestion-list-preloader").addClass("display-none");
$("#client-machine-suggestion-list ul").html("");
if (inStorage("client_uuid")) {
$("#client-machine-suggestion-list-preloader").removeClass("display-none");
apirequest(
SETTINGS.apis.getmachines,
{
clientuuid: getStorage("client_uuid")
},
function (resp) {
if (resp.status == "OK") {
if (resp.machines.length > 0) {
for (var i = 0; i < resp.machines.length; i++) {
var id = resp.machines[i].id;
var type = resp.machines[i].type.label;
var icon = resp.machines[i].icon;
$("#client-machine-suggestion-list ul").append('<li><a class="item-link item-content" href="/track/' + id + '">'
+ '<div class="item-inner"><div class="item-title">'
+ '<i class="' + icon + '"></i> ' + type + " #" + id
+ '</div></div></a></li>');
}
$("#client-machine-suggestion-list").removeClass("display-none");
}
$("#client-machine-suggestion-list-preloader").addClass("display-none");
}
}, "GET");
} else {
$("#client-link-account-message").removeClass("display-none");
}
}
function trackOpenAsync(routeTo, routeFrom, resolve, reject) {
app.dialog.preloader("Loading...");
apirequest(
SETTINGS.apis.track,
{
id: routeTo.params.id,
format: "json"
},
function (resp) {
app.dialog.close();
if (resp.status == "ERROR") {
app.dialog.alert(resp.msg, "Error");
reject();
} else {
addToTrackingHistory(resp.id);
var context = {};
context.info = resp.info;
context.events = resp.events;
context.components = resp.components;
resolve({
templateUrl: "pages/trackresult.html",
}, {
context: context
});
}
},
function (xhr) {
app.dialog.close();
var error = $.parseJSON(xhr.responseText);
if (error && typeof error.msg != 'undefined') {
app.dialog.alert(error.msg, "Error");
} else {
app.dialog.alert("A server or network error occurred.", "Error");
}
reject();
}, "GET");
}
function getTrackingHistory() {
var history = getStorage("trackhistory");
if (history != null) {
return JSON.parse(history);
} else {
return [];
}
}
function addToTrackingHistory(id) {
var history = getTrackingHistory();
for (var i = 0; i < history.length; i++) {
if (history[i] == id) {
history.splice(i, 1);
}
}
// Add the code back to the list so it's at the top
history.push(id);
while (history.length > 10) {
history.shift();
}
setStorage("trackhistory", JSON.stringify(history));
}