Emulate Android back button with Esc

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

Loading…
Cancel
Save