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

148 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/.
*/
function openTrackingInfoPage(code) {
if (typeof code == "undefined" || code == null || code == "") {
app.input.validate("#trackingcode");
return;
}
router.navigate("/track/" + code);
}
function addTrackingSuggestions() {
$("#tracking-suggestion-list ul").html("");
var history = getTrackingHistory();
for (var i = 0; i < history.length; i++) {
$("#tracking-suggestion-list ul").append('<li><a class="item-link item-content" href="/track/' + history[i] + '">'
+ '<div class="item-inner"><div class="item-title">'
+ history[i]
+ '</div></div></a></li>');
}
}
function openTrackingBarcodeScanner() {
scanBarcode(function (result) {
var code = "";
var coderegex = /^[0-9a-zA-Z]{5,40}$/;
if (result.startsWith("https://helena.express/track#")) {
code = result.split("#")[1];
} else if (result.startsWith("http") && result.includes("#")) {
if (coderegex.test(result.split("#")[1])) {
code = result.split("#")[1];
} else {
app.dialog.alert("This app can't understand what's in that barcode.", "Error");
return;
}
} else if (coderegex.test(result)) {
code = result;
} else {
app.dialog.alert("This app can't understand what's in that barcode.", "Error");
return;
}
code = code.toUpperCase();
openTrackingInfoPage(code);
}, function () {
app.dialog.alert("Something went wrong and we can't scan right now.", "Error");
});
}
function trackOpenAsync(routeTo, routeFrom, resolve, reject) {
app.dialog.preloader("Loading...");
apirequest(
SETTINGS.apis.track,
{
code: routeTo.params.code,
format: "json"
},
function (resp) {
app.dialog.close();
if (resp.status == "OK") {
addToTrackingHistory(resp.code);
var context = {
code: resp.code,
info: [
{label: "Tracking Code", value: resp.code},
],
events: [],
map: {
enabled: (typeof resp.info.latitude == "number" && typeof resp.info.longitude == "number"),
latitude: resp.info.latitude,
longitude: resp.info.longitude,
accurate: resp.info.geoaccurate
}
};
if (resp.info.statustext) {
context.info.push({label: "Status", value: resp.info.statustext});
}
if (resp.info.carrier) {
context.info.push({label: "Carrier", value: resp.info.carrier});
}
if (resp.info.delivery_date) {
var deliverydatelabel = "Estimated delivery on";
if (resp.info.status == "DELIVERED") {
deliverydatelabel = "Delivered on";
}
context.info.push({label: deliverydatelabel, value: formatTimestamp("F j Y", resp.info.delivery_date_unixtime)});
}
for (var i = 0; i < resp.events.length; i++) {
context.events.push({
text: resp.events[i].text,
date: formatTimestamp("F j g:i a", resp.events[i].unixtime),
icon: "./assets/images/icons/" + resp.events[i].icon + ".svg"
});
}
resolve({
templateUrl: "pages/trackresult.html",
}, {
context: context
});
} else {
app.dialog.alert(resp.msg, "Error");
reject();
}
},
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("trackinghistory");
if (history != null) {
return JSON.parse(history);
} else {
return [];
}
}
function addToTrackingHistory(code) {
var history = getTrackingHistory();
for (var i = 0; i < history.length; i++) {
if (history[i] == code) {
history.splice(i, 1);
}
}
// Add the code back to the list so it's at the top
history.push(code);
while (history.length > 10) {
history.shift();
}
setStorage("trackinghistory", JSON.stringify(history));
}