Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose error callbacks, prevent always setting header #908

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -250,6 +250,9 @@ export type QrcodeSuccessCallback
export type QrcodeErrorCallback
= (errorMessage: string, error: Html5QrcodeError) => void;

export type StartErrorCallback
= (error: Html5QrcodeError) => void;

/** Code decoder interface. */
export interface QrcodeDecoderAsync {
/**
36 changes: 25 additions & 11 deletions src/html5-qrcode-scanner.ts
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ import {
Html5QrcodeConstants,
Html5QrcodeScanType,
QrcodeSuccessCallback,
StartErrorCallback,
QrcodeErrorCallback,
Html5QrcodeResult,
Html5QrcodeError,
@@ -196,6 +197,11 @@ export class Html5QrcodeScanner {
private cameraScanImage: HTMLImageElement | null = null;
private fileScanImage: HTMLImageElement | null = null;
private fileSelectionUi: FileSelectionUi | null = null;

public startErrorCallback: StartErrorCallback;
public torchButtonErrorCallback: StartErrorCallback;
public getCamaraErrorCallback: StartErrorCallback;
public clickListenerErrorCallback: StartErrorCallback;
//#endregion

/**
@@ -228,6 +234,17 @@ export class Html5QrcodeScanner {
if (config!.rememberLastUsedCamera !== true) {
this.persistedDataManager.reset();
}
const $this = this;

const warnUserViaHeader : StartErrorCallback = (error) => {
$this.setHeaderMessage(
error.toString(), Html5QrcodeScannerStatus.STATUS_WARNING);
};

this.startErrorCallback = warnUserViaHeader;
this.torchButtonErrorCallback = warnUserViaHeader;
this.getCamaraErrorCallback = warnUserViaHeader;
this.clickListenerErrorCallback = warnUserViaHeader;
}

/**
@@ -584,8 +601,7 @@ export class Html5QrcodeScanner {
// time.
createPermissionButtonIfNotExists();
}
$this.setHeaderMessage(
error, Html5QrcodeScannerStatus.STATUS_WARNING);
$this.getCamaraErrorCallback(error);
$this.showHideScanTypeSwapLink(true);
});
}
@@ -765,6 +781,7 @@ export class Html5QrcodeScanner {

// Optional torch button support.
let torchButton: TorchButton;

const createAndShowTorchButtonIfSupported
= (cameraCapabilities: CameraCapabilities) => {
if (!cameraCapabilities.torchFeature().isSupported()) {
@@ -782,9 +799,7 @@ export class Html5QrcodeScanner {
{ display: "none", marginLeft: "5px" },
// Callback in case of torch action failure.
(errorMessage) => {
$this.setHeaderMessage(
errorMessage,
Html5QrcodeScannerStatus.STATUS_WARNING);
$this.torchButtonErrorCallback(Html5QrcodeErrorFactory.createFrom(errorMessage));
}
);
} else {
@@ -853,8 +868,7 @@ export class Html5QrcodeScanner {
$this.showHideScanTypeSwapLink(true);
cameraSelectUi.enable();
resetCameraActionStartButton(/* shouldShow= */ true);
$this.setHeaderMessage(
error, Html5QrcodeScannerStatus.STATUS_WARNING);
$this.startErrorCallback(error);
});
});

@@ -890,8 +904,7 @@ export class Html5QrcodeScanner {
$this.insertCameraScanImageToScanRegion();
}).catch((error) => {
cameraActionStopButton.disabled = false;
$this.setHeaderMessage(
error, Html5QrcodeScannerStatus.STATUS_WARNING);
$this.clickListenerErrorCallback(error);
});
});

@@ -996,13 +1009,14 @@ export class Html5QrcodeScanner {
}
}

private resetHeaderMessage() {
public resetHeaderMessage() {
const messageDiv = document.getElementById(
this.getHeaderMessageContainerId())!;
messageDiv.style.display = "none";
}

private setHeaderMessage(
/*eslint complexity: ["error", 5]*/
public setHeaderMessage(
messageText: string, scannerStatus?: Html5QrcodeScannerStatus) {
if (!scannerStatus) {
scannerStatus = Html5QrcodeScannerStatus.STATUS_DEFAULT;