| 
									
										
										
										
											2020-10-10 19:26:30 -06:00
										 |  |  | /* | 
					
						
							|  |  |  |  * 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/.
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 12:06:20 -07:00
										 |  |  | function openTrackingInfoPage(code) { | 
					
						
							|  |  |  |     if (typeof code == "undefined" || code == null || code == "") { | 
					
						
							|  |  |  |         app.input.validate("#trackingcode"); | 
					
						
							| 
									
										
										
										
											2020-10-24 17:28:17 -06:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-24 18:23:00 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 12:06:20 -07:00
										 |  |  |     router.navigate("/track/" + code); | 
					
						
							| 
									
										
										
										
											2020-10-10 19:26:30 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 12:06:20 -07:00
										 |  |  | 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>'); | 
					
						
							| 
									
										
										
										
											2020-10-24 18:23:00 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 19:17:19 -07:00
										 |  |  | function openTrackingBarcodeScanner() { | 
					
						
							|  |  |  |     scanBarcode(function (result) { | 
					
						
							|  |  |  |         var code = ""; | 
					
						
							| 
									
										
										
										
											2021-01-22 19:33:09 -07:00
										 |  |  |         var coderegex = /^[0-9a-zA-Z]{5,40}$/; | 
					
						
							| 
									
										
										
										
											2021-01-22 19:17:19 -07:00
										 |  |  |         if (result.startsWith("https://helena.express/track#")) { | 
					
						
							|  |  |  |             code = result.split("#")[1]; | 
					
						
							|  |  |  |         } else if (result.startsWith("http") && result.includes("#")) { | 
					
						
							| 
									
										
										
										
											2021-01-22 19:33:09 -07:00
										 |  |  |             if (coderegex.test(result.split("#")[1])) { | 
					
						
							| 
									
										
										
										
											2021-01-22 19:17:19 -07:00
										 |  |  |                 code = result.split("#")[1]; | 
					
						
							| 
									
										
										
										
											2021-01-22 19:33:43 -07:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 app.dialog.alert("This app can't understand what's in that barcode.", "Error"); | 
					
						
							|  |  |  |                 return; | 
					
						
							| 
									
										
										
										
											2021-01-22 19:17:19 -07:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-01-22 19:33:09 -07:00
										 |  |  |         } else if (coderegex.test(result)) { | 
					
						
							| 
									
										
										
										
											2021-01-22 19:17:19 -07:00
										 |  |  |             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"); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 19:26:30 -06:00
										 |  |  | function trackOpenAsync(routeTo, routeFrom, resolve, reject) { | 
					
						
							|  |  |  |     app.dialog.preloader("Loading..."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     apirequest( | 
					
						
							|  |  |  |             SETTINGS.apis.track, | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2021-01-22 12:06:20 -07:00
										 |  |  |                 code: routeTo.params.code, | 
					
						
							| 
									
										
										
										
											2020-10-10 19:26:30 -06:00
										 |  |  |                 format: "json" | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             function (resp) { | 
					
						
							|  |  |  |                 app.dialog.close(); | 
					
						
							| 
									
										
										
										
											2021-01-22 12:06:20 -07:00
										 |  |  |                 if (resp.status == "OK") { | 
					
						
							|  |  |  |                     addToTrackingHistory(resp.code); | 
					
						
							| 
									
										
										
										
											2020-10-10 19:26:30 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 12:06:20 -07:00
										 |  |  |                     var context = { | 
					
						
							|  |  |  |                         code: resp.code, | 
					
						
							|  |  |  |                         info: [ | 
					
						
							|  |  |  |                             {label: "Tracking Code", value: resp.code} | 
					
						
							|  |  |  |                         ], | 
					
						
							|  |  |  |                         events: [] | 
					
						
							|  |  |  |                     }; | 
					
						
							|  |  |  |                     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" | 
					
						
							|  |  |  |                         }); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2020-10-10 19:26:30 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |                     resolve({ | 
					
						
							|  |  |  |                         templateUrl: "pages/trackresult.html", | 
					
						
							|  |  |  |                     }, { | 
					
						
							|  |  |  |                         context: context | 
					
						
							|  |  |  |                     }); | 
					
						
							| 
									
										
										
										
											2021-01-22 12:06:20 -07:00
										 |  |  |                 } else { | 
					
						
							|  |  |  |                     app.dialog.alert(resp.msg, "Error"); | 
					
						
							|  |  |  |                     reject(); | 
					
						
							| 
									
										
										
										
											2020-10-10 19:26:30 -06:00
										 |  |  |                 } | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             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() { | 
					
						
							| 
									
										
										
										
											2021-01-22 12:06:20 -07:00
										 |  |  |     var history = getStorage("trackinghistory"); | 
					
						
							| 
									
										
										
										
											2020-10-10 19:26:30 -06:00
										 |  |  |     if (history != null) { | 
					
						
							|  |  |  |         return JSON.parse(history); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         return []; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 12:06:20 -07:00
										 |  |  | function addToTrackingHistory(code) { | 
					
						
							| 
									
										
										
										
											2020-10-10 19:26:30 -06:00
										 |  |  |     var history = getTrackingHistory(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (var i = 0; i < history.length; i++) { | 
					
						
							| 
									
										
										
										
											2021-01-22 12:06:20 -07:00
										 |  |  |         if (history[i] == code) { | 
					
						
							| 
									
										
										
										
											2020-10-10 19:26:30 -06:00
										 |  |  |             history.splice(i, 1); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // Add the code back to the list so it's at the top
 | 
					
						
							| 
									
										
										
										
											2021-01-22 12:06:20 -07:00
										 |  |  |     history.push(code); | 
					
						
							| 
									
										
										
										
											2020-10-10 19:26:30 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     while (history.length > 10) { | 
					
						
							|  |  |  |         history.shift(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-01-22 12:06:20 -07:00
										 |  |  |     setStorage("trackinghistory", JSON.stringify(history)); | 
					
						
							| 
									
										
										
										
											2020-10-10 19:26:30 -06:00
										 |  |  | } |