Emulate Android back button with Esc

master
Skylar Ittner 4 years ago
parent a7c41a7577
commit 5ac43e4aa3

@ -50,6 +50,28 @@ router.on("pageInit", function (pagedata) {
}); });
}); });
/**
* Perform back button behavior.
* Call this function whenever the equivalent to the Android back button is pressed.
* @returns {undefined}
*/
function handleBackButton() {
// Close map sheet if it's open
if ($(".sheet-modal").hasClass("modal-in")) {
app.sheet.close();
} else if (scanningBarcode) {
return;
} else {
router.back({force: true, ignoreCache: true});
}
}
$(document).keyup(function (e) {
if (e.key === "Escape" || e.keyCode == 27) {
handleBackButton();
}
});
router.on("routeChange", function (newRoute) { router.on("routeChange", function (newRoute) {
console.log("Info", "Navigating to ", newRoute.path); console.log("Info", "Navigating to ", newRoute.path);
}); });

@ -24,6 +24,8 @@ var scanBarcode = function (success, error) {
app.dialog.alert("You can't scan barcodes with this device.", "Sorry!"); app.dialog.alert("You can't scan barcodes with this device.", "Sorry!");
} }
var scanningBarcode = false;
var getLocation = function (success, error) { var getLocation = function (success, error) {
if ("geolocation" in navigator) { if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(function (position) { navigator.geolocation.getCurrentPosition(function (position) {
@ -68,6 +70,7 @@ function setupHTML5BarcodeScanner() {
$("body").append('<script src="node_modules/@zxing/library/umd/index.min.js"></script>'); $("body").append('<script src="node_modules/@zxing/library/umd/index.min.js"></script>');
scanBarcode = function (success, error) { scanBarcode = function (success, error) {
scanningBarcode = true;
$("#web-barcode-ui").removeClass("hidden"); $("#web-barcode-ui").removeClass("hidden");
// Stolen from https://zxing-js.github.io/library/examples/multi-camera/ // Stolen from https://zxing-js.github.io/library/examples/multi-camera/
const codeReader = new ZXing.BrowserMultiFormatReader(); const codeReader = new ZXing.BrowserMultiFormatReader();
@ -83,6 +86,7 @@ function setupHTML5BarcodeScanner() {
selectedDeviceId = videoInputDevices[0].deviceId; selectedDeviceId = videoInputDevices[0].deviceId;
codeReader.decodeFromInputVideoDeviceContinuously(selectedDeviceId, 'barcode-viewer', (result, err) => { codeReader.decodeFromInputVideoDeviceContinuously(selectedDeviceId, 'barcode-viewer', (result, err) => {
scanningBarcode = false;
if (result) { if (result) {
codeReader.reset(); codeReader.reset();
$("#web-barcode-ui").addClass("hidden"); $("#web-barcode-ui").addClass("hidden");
@ -99,10 +103,12 @@ function setupHTML5BarcodeScanner() {
}); });
}) })
.catch((err) => { .catch((err) => {
scanningBarcode = false;
console.error(err); console.error(err);
}); });
$("#web-barcode-ui").on("click", function () { $("#web-barcode-ui").on("click", function () {
codeReader.reset(); codeReader.reset();
scanningBarcode = false;
$("#web-barcode-ui").addClass("hidden"); $("#web-barcode-ui").addClass("hidden");
}); });
}; };
@ -111,14 +117,7 @@ function setupHTML5BarcodeScanner() {
function initCordova() { function initCordova() {
platform_type = "cordova"; platform_type = "cordova";
// Handle back button to close things // Handle back button to close things
document.addEventListener("backbutton", function (event) { document.addEventListener("backbutton", handleBackButton, false);
// Close map sheet if it's open
if ($(".sheet-modal").hasClass("modal-in")) {
app.sheet.close();
} else {
router.back({force: true, ignoreCache: true});
}
}, false);
document.addEventListener("deviceready", function () { document.addEventListener("deviceready", function () {
if (getStorage("wakelock") == "true") { if (getStorage("wakelock") == "true") {
window.powerManagement.acquire(function () { window.powerManagement.acquire(function () {
@ -144,13 +143,16 @@ function initCordova() {
if (typeof device != "undefined" && device.platform != "browser") { if (typeof device != "undefined" && device.platform != "browser") {
scanBarcode = function (success, error) { scanBarcode = function (success, error) {
scanningBarcode = true;
cordova.plugins.barcodeScanner.scan( cordova.plugins.barcodeScanner.scan(
function (result) { function (result) {
scanningBarcode = false;
if (!result.cancelled) { if (!result.cancelled) {
success(result.text); success(result.text);
} }
}, },
function (err) { function (err) {
scanningBarcode = false;
if (typeof error == "function") { if (typeof error == "function") {
error(err); error(err);
} }

Loading…
Cancel
Save