Houjie
2025-04-11 1bf977929dd324f3ac64b70debd8a79443c54392
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
 * @fileoverview
 * Util class to help with scan type selection in scanner class.
 * 
 * @author mebjas <minhazav@gmail.com>
 * 
 * The word "QR Code" is registered trademark of DENSO WAVE INCORPORATED
 * http://www.denso-wave.com/qrcode/faqpatent-e.html
 */
 
import {
    Html5QrcodeScanType,
    Html5QrcodeConstants
} from "../../core";
 
/** Util class to help with scan type selection in scanner class. */
export class ScanTypeSelector {
    private supportedScanTypes: Array<Html5QrcodeScanType>;
 
    constructor(supportedScanTypes?: Array<Html5QrcodeScanType> | []) {
        this.supportedScanTypes = this.validateAndReturnScanTypes(
            supportedScanTypes);
    }
 
    /**
     * Returns the default {@link Html5QrcodeScanType} scanner UI should be
     * created with.
     */
    public getDefaultScanType(): Html5QrcodeScanType {
        return this.supportedScanTypes[0];
    }
 
    /**
     * Returns {@code true} if more than one {@link Html5QrcodeScanType} are
     * set.
     */
    public hasMoreThanOneScanType(): boolean {
        return this.supportedScanTypes.length > 1;
    }
 
    /** Returns {@code true} if camera scan is required at all. */
    public isCameraScanRequired(): boolean {
        for (const scanType of this.supportedScanTypes) {
            if (ScanTypeSelector.isCameraScanType(scanType)) {
                return true;
            }
        }
        return false;
    }
 
    /** Returns {@code true} is {@code scanType} is camera based. */
    public static isCameraScanType(scanType: Html5QrcodeScanType): boolean {
        return scanType === Html5QrcodeScanType.SCAN_TYPE_CAMERA;
    }
 
    /** Returns {@code true} is {@code scanType} is file based. */
    public static isFileScanType(scanType: Html5QrcodeScanType): boolean {
        return scanType === Html5QrcodeScanType.SCAN_TYPE_FILE;
    }
 
    //#region Private methods.
    /**
     * Validates the input {@code supportedScanTypes}.
     * 
     * Fails early if the config values is incorrectly set.
     */
    private validateAndReturnScanTypes(
        supportedScanTypes?:Array<Html5QrcodeScanType>):
        Array<Html5QrcodeScanType> {
        // If not set, use the default values and order.
        if (!supportedScanTypes || supportedScanTypes.length === 0) {
            return Html5QrcodeConstants.DEFAULT_SUPPORTED_SCAN_TYPE;
        }
 
        // Fail if more than expected number of values exist.
        let maxExpectedValues
            = Html5QrcodeConstants.DEFAULT_SUPPORTED_SCAN_TYPE.length;
        if (supportedScanTypes.length > maxExpectedValues) {
            throw `Max ${maxExpectedValues} values expected for `
                + "supportedScanTypes";
        }
 
        // Fail if any of the scan types are not supported.
        for (const scanType of supportedScanTypes) {
            if (!Html5QrcodeConstants.DEFAULT_SUPPORTED_SCAN_TYPE
                    .includes(scanType)) {
                throw `Unsupported scan type ${scanType}`;
            }
        }
 
        return supportedScanTypes;
    }
    //#endregion
}