/* * 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/. */ $(".view-main").on("click", "#addresslist .package-list-item .deliver-btn", function () { var pid = $(this).data("packageid"); markDelivered(pid); loadPackageList(); }); // Open geo: url $(".view-main").on("click", "#addresslist .package-list-item .directions-btn", function () { window.open($(this).attr("href"), "_system"); }); $(".view-main").on("swipeout:delete", "#addresslist .package-list-item", function () { console.log("Deleting package", $(this).data("packageid")); deletePackage($(this).data("packageid")); }); // Searchbar is setup in routes.js, this is for forcing a wider scope var searchbar = null; /** * Update package distances relative to the passed coordinates. * @param {type} latitude * @param {type} longitude * @returns {undefined} */ function updateDistances(latitude, longitude) { for (var i = 0; i < packages.length; i++) { var distance = getDistance(latitude, longitude, packages[i].coords[0], packages[i].coords[1]).toFixed(0); packages[i].distance = distance; $("#addresslist .package-list-item[data-packageid=\"" + i + "\"] .item-content").data("distance", distance); $("#addresslist .package-list-item[data-packageid=\"" + i + "\"] .item-content .distance").text(getDisplayDistance(distance)); } } function loadPackageList(sortType) { // If no sort type is specified, use the saved pref or a default one if (typeof sortType == 'undefined') { if (localStorage.getItem("sorttype") == null) { localStorage.setItem("sorttype", "distance_asc"); } sortType = localStorage.getItem("sorttype"); } else { // save the current sorting order so it'll stay consistent next time the list is refreshed localStorage.setItem("sorttype", sortType); } updateDistances(userPosition.coords.latitude, userPosition.coords.longitude); var sortedPackages = packages.map(function (el, i) { return {index: i, value: el}; }); sortedPackages.sort(function (a, b) { var aalpha = a.value.address.substr(a.value.address.indexOf(' ') + 1); var balpha = b.value.address.substr(b.value.address.indexOf(' ') + 1); var anum = parseInt(a.value.address.split(" ", 1)[0], 10); var bnum = parseInt(b.value.address.split(" ", 1)[0], 10); console.log("aalpha", aalpha); console.log("balpha", balpha); switch (sortType) { case "alpha_desc": if (aalpha > balpha) { return -1; } else if (aalpha < balpha) { return 1; } return 0; case "alpha_asc": if (aalpha > balpha) { return 1; } else if (aalpha < balpha) { return -1; } return 0; case "number_desc": return bnum - anum; case "number_asc": return anum - bnum; case "distance_desc": return b.value.distance - a.value.distance; case "distance_asc": default: return a.value.distance - b.value.distance; } }); $("#addresslist").html(""); if (packages.length == 0) { $("#no-packages-display").removeClass("display-none"); } else { $("#no-packages-display").addClass("display-none"); } for (var i = 0; i < sortedPackages.length; i++) { for (var j = 0; j < sortedPackages[i].value.items.length; j++) { var item = sortedPackages[i].value.items[j]; var icon = getIconForType(item.type); var classes = ""; var delivered = false; if (item.delivered) { delivered = true; icon = "far fa-check-circle"; classes = "text-color-green"; } else if (typeof sortedPackages[i].value.distance != 'undefined' && sortedPackages[i].value.distance * 1 < localStorage.getItem("alertradius") * 1) { classes = "text-color-deeporange"; } $("#addresslist").append( '
  • ' + '
    ' + '
    ' + ' ' + '
    ' + '
    ' + '
    ' + ' ' + item.address + '
    ' + ' ' + '
    ' + '
    ' + '
    ' + ' ' + (delivered ? "  Undeliver" : "  Deliver") + '' + '   Directions' + '
    ' + '
    ' + '   Delete' + '
    ' + '
  • ' ); } } // If there was a search open when the reload was triggered if (router.currentRoute.name == "list") { var searchboxVal = $(".package-list-searchbar input[type=search]").val(); if (searchboxVal != "") { searchbar.clear(); searchbar.search(searchboxVal); } } } function confirmDeleteAllPackages() { app.dialog.confirm( "Really delete all packages from list?", "Clear Packages", function () { // clear packages = []; localStorage.setItem("packages", JSON.stringify(packages)); loadPackageList(); if (map != null) { map.updatePackageLayer(packages); } }, function () { // cancel } ); }