/* * 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 locationArrayToString(location) { var locarray = []; if (location.street != "" && location.street != null) { locarray.push(location.street); } if (location.city != "" && location.city != null) { locarray.push(location.city); } if (location.state != "" && location.state != null) { locarray.push(location.state); } if (location.zip != "" && location.zip != null) { locarray.push(location.zip); } if (location.country != "" && location.country != "US" && location.country != null) { locarray.push(location.country); } return locarray.join(", "); } function timestampToDateTimeString(timestamp) { var date = new Date(timestamp * 1000); var pm = date.getHours() >= 12; var hours = date.getHours() > 12 ? date.getHours() - 12 : date.getHours(); hours = (hours == 0 ? 12 : hours); var minutes = date.getMinutes(); var time = hours + ":" + (minutes < 10 ? "0" + minutes : minutes) + " " + (pm ? "PM" : "AM"); return date.toLocaleDateString() + " " + time; } function trackingStatusToNiceString(status, icon) { if (typeof icon == 'undefined' || icon !== true) { var icon = false; } switch (status) { case "UNKNOWN": return (icon ? ' ' : '') + "Unknown"; case "PRE_TRANSIT": return (icon ? ' ' : '') + "Pre-transit"; case "TRANSIT": return (icon ? ' ' : '') + "In Transit"; case "DELIVERED": return (icon ? ' ' : '') + "Delivered"; case "RETURNED": return (icon ? ' ' : '') + "Returned"; case "FAILURE": return (icon ? ' ' : '') + "Failure"; default: return status; } } function openTrackingHistory(trackingcode) { var requestfinished = false; var trackingdialogopen = false; $.ajax({ url: SETTINGS.trackingapi, dataType: 'json', data: { code: trackingcode }, timeout: 15 * 1000, success: function (resp) { if (trackingdialogopen) { app.dialog.close(); trackingdialogopen = false; } requestfinished = true; if (resp.status == "OK") { var infocontext = resp; infocontext.current.location.display = locationArrayToString(infocontext.current.location); infocontext.current.date = timestampToDateTimeString(infocontext.current.date); infocontext.current.status = trackingStatusToNiceString(infocontext.current.status, true); infocontext.addresses.from = locationArrayToString(infocontext.addresses.from); infocontext.addresses.to = locationArrayToString(infocontext.addresses.to); for (var i = 0; i < infocontext.history.length; i++) { infocontext.history[i].location.display = locationArrayToString(infocontext.history[i].location); infocontext.history[i].date = timestampToDateTimeString(infocontext.history[i].date); infocontext.history[i].status = trackingStatusToNiceString(infocontext.history[i].status, true); } // TODO: format timestamps as local time router.navigate("/toolbox/track/info", { context: infocontext }); } else { playSound("error"); app.dialog.alert(resp.message, "Error"); } }, error: function (jqXHR, status, errorThrown) { if (trackingdialogopen) { app.dialog.close(); trackingdialogopen = false; } requestfinished = true; playSound("error"); app.dialog.alert("There was a network issue while tracking the item. Please try again.", "Error"); } }); // Open a loading message if there's a delay setTimeout(function () { if (!requestfinished) { app.dialog.preloader("Tracking..."); trackingdialogopen = true; } }, 750); } function scanTrackingBarcode() { scanBarcode(function (code) { if (code != "" && code.length > 5) { openTrackingHistory(code); } else { app.dialog.alert("That's not a valid tracking code.", "Error"); } }, function (error) { app.dialog.alert(error, "Error"); }) } $("#trackbtn").click(function () { var code = $("input[name=trackingcode]").val(); if (code != "" && code.length > 5) { openTrackingHistory(code); } else { app.dialog.alert("That's not a valid tracking code.", "Error"); } });