All files / src/controllers BarcodeController.ts

100% Statements 107/107
78.95% Branches 15/19
100% Functions 41/41
100% Lines 89/89

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297    5x 5x                 5x           5x       31x           31x                 31x 10x 10x   10x                 31x 1x 1x                 31x 1x 1x                 31x 1x 1x                 31x 1x 1x                 31x 3x 3x               31x 1x 1x                 31x 2x 2x                 31x 1x 1x                 31x 1x 1x                     31x 2x                 2x 2x   2x               31x 1x   1x 1x   1x                 31x 1x 1x   1x                 31x 1x 1x   1x                   31x 1x 1x                   31x 1x 1x               31x 10x 10x 10x 10x 10x 10x 10x 10x 10x   1x 1x               2x 2x 2x 2x 2x   1x 1x 1x   1x       3x 3x     2x         2x   1x 1x   10x                          
import { EditorAPI, Id } from '../types/CommonTypes';
import { ColorUsage } from '../types/ColorStyleTypes';
import { getEditorResponseData } from '../utils/EditorResponseData';
import {
    BarcodeConfigurationOptions,
    BarcodeProperties,
    BarcodeType,
    QuietZoneDeltaUpdate,
    BarcodeCharacterSet,
    BarcodeErrorCorrectionLevel,
} from '../types/BarcodeTypes';
import { BarcodeSource } from '../types/FrameTypes';
import { PositionEnum } from '../types/LayoutTypes';
 
/**
 * The BarcodeController is responsible for all communication regarding Barcodes.
 * Methods inside this controller can be called by `window.SDK.barcode.{method-name}`
 */
export class BarcodeController {
    /**
     * @ignore
     */
    #editorAPI: EditorAPI;
 
    /**
     * @ignore
     */
    constructor(editorAPI: EditorAPI) {
        this.#editorAPI = editorAPI;
    }
 
    /**
     * @experimental This method updates properties of the barcode
     * @param id the id of the barcodeFrame that needs to be updated.
     * @param properties A property to update
     * @returns
     */
    private setBarcodeProperties = async (id: Id, properties: BarcodeProperties) => {
        const res = await this.#editorAPI;
        return res
            .setBarcodeProperties(id, JSON.stringify(properties))
            .then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * @experimental This method will set the background of a barcode enabled or disabled
     * @param id the id of the barcodeFrame that needs to be updated.
     * @param enableBackground Whether the barcode's background is visible.
     * @returns
     */
    setEnableBackground = async (id: Id, enableBackground: boolean) => {
        const properties: BarcodeProperties = { enableBackground: enableBackground };
        return this.setBarcodeProperties(id, properties);
    };
 
    /**
     * @experimental This method will set the barcode background color of a specified barcode frame.
     * @param id the id of the barcodeFrame that needs to be updated.
     * @param backgroundColor the new barcode background color that you want to set to the barcodeFrame.
     * @returns
     */
    setBackgroundColor = async (id: Id, backgroundColor: ColorUsage) => {
        const properties: BarcodeProperties = { backgroundColor: backgroundColor };
        return this.setBarcodeProperties(id, properties);
    };
 
    /**
     * @experimental This method will set the visibility of the barcode's bars.
     * @param id the id of the barcodeFrame that needs to be updated.
     * @param enableBars Whether the barcode bars are visible.
     * @returns
     */
    setEnableBars = async (id: Id, enableBars: boolean) => {
        const properties: BarcodeProperties = { enableBars: enableBars };
        return this.setBarcodeProperties(id, properties);
    };
 
    /**
     * @experimental This method will set the bar color of a specified barcode frame.
     * @param id the id of the barcodeFrame that needs to be updated.
     * @param barColor the new bar color that you want to set to the barcodeFrame.
     * @returns
     */
    setBarColor = async (id: Id, barColor: ColorUsage) => {
        const properties: BarcodeProperties = { barColor: barColor };
        return this.setBarcodeProperties(id, properties);
    };
 
    /**
     * @experimental This method will set the source of the barcode to the given source.
     * @param id the id of the barcodeFrame that needs to be updated.
     * @param source the new source that you want to set to the barcodeFrame.
     * @returns
     */
    setBarcodeSource = async (id: Id, source: BarcodeSource) => {
        const res = await this.#editorAPI;
        return res.setBarcodeSource(id, JSON.stringify(source)).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * @experimental This method will remove the source from the barcode frame provided.
     * @param id the id of the barcodeFrame that needs to have the source removed.
     * @returns
     */
    removeBarcodeSource = async (id: Id) => {
        const res = await this.#editorAPI;
        return res.setBarcodeSource(id, null).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * @experimental This method enables/disables displaying the text of the barcode.
     * @param id the id of the barcodeFrame that needs to be updated.
     * @param enableText true if the text should be displayed, false otherwise.
     * @returns
     */
    setEnableText = async (id: Id, enableText: boolean) => {
        const properties: BarcodeProperties = { enableText: enableText };
        return this.setBarcodeProperties(id, properties);
    };
 
    /**
     * @experimental This method sets the bar height for 1-dimensional barcodes.
     * @param id the id of the barcodeFrame that needs to be updated.
     * @param barHeight the height of the bars in the barcode. The string value will be calculated (f.e. 1+1 will result in 2)
     * @returns
     */
    setBarHeight = async (id: Id, barHeight: string) => {
        const properties: BarcodeProperties = { barHeight: barHeight };
        return this.setBarcodeProperties(id, properties);
    };
 
    /**
     * @experimental This method sets the magnification for 1-dimensional barcodes.
     * @param id the id of the barcodeFrame that needs to be updated.
     * @param magnification the magnification of the barcode. This is a decimal value where 1 denotes 100%. (f.e. 1.5 will result in 150%)
     * @returns
     */
    setMagnification = async (id: Id, magnification: number) => {
        const properties: BarcodeProperties = { magnification: magnification };
        return this.setBarcodeProperties(id, properties);
    };
 
    /**
     * @experimental This method sets the quiet zone value of a specific barcode.
     * @param id The id of the specific barcode frame
     * @param value The quiet zone value
     * @param position When defined will update the quiet value of a single position,
     * otherwise will set all positions to the same value. Depending on this parameter
     * being defined, the `areQuietZoneValuesCombined` will also be updated.
     */
    setQuietZoneValue = async (id: Id, value: string, position?: PositionEnum) => {
        const update: QuietZoneDeltaUpdate = position
            ? {
                  left: position === PositionEnum.left ? value : undefined,
                  top: position === PositionEnum.top ? value : undefined,
                  right: position === PositionEnum.right ? value : undefined,
                  bottom: position === PositionEnum.bottom ? value : undefined,
              }
            : { left: value, top: value, right: value, bottom: value };
 
        const res = await this.#editorAPI;
        return res
            .setBarcodeProperties(id, JSON.stringify({ quietZone: update }))
            .then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * @experimental This method sets the combined state of the quiet zone values.
     * @param id The id of the specific barcode frame
     * @param value Whether the quiet zone values are combined
     */
    setAreQuietZoneValuesCombined = async (id: Id, value: boolean) => {
        const update: QuietZoneDeltaUpdate = { areQuietZoneValuesCombined: value };
 
        const res = await this.#editorAPI;
        return res
            .setBarcodeProperties(id, JSON.stringify({ quietZone: update }))
            .then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * @experimental This method sets the character set for the barcode.
     * @param id The id of the specific barcode frame.
     * @param errorCorrectionLevel The error correction level to set.
     * @returns
     */
    setErrorCorrectionLevel = async (id: Id, errorCorrectionLevel: BarcodeErrorCorrectionLevel) => {
        const res = await this.#editorAPI;
        return res
            .setBarcodeProperties(id, JSON.stringify({ errorCorrectionLevel: errorCorrectionLevel }))
            .then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * @experimental This method sets the character set for the barcode.
     * @param id The id of the specific barcode frame.
     * @param characterSet The character set to set.
     * @returns
     */
    setCharacterSet = async (id: Id, characterSet: BarcodeCharacterSet) => {
        const res = await this.#editorAPI;
        return res
            .setBarcodeProperties(id, JSON.stringify({ characterSet: characterSet }))
            .then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * @experimental This method sets the flag to render the end characters of the barcode. This is not
     * supported for all barcode types.
     * @param id The id of the specific barcode frame.
     * @param drawLightMarginIndicator The flag indicating if the end characters should be drawn or not
     * @returns
     */
    setDrawLightMarginIndicator = async (id: Id, drawLightMarginIndicator: boolean) => {
        const properties: BarcodeProperties = { drawStartStopChars: drawLightMarginIndicator };
        return this.setBarcodeProperties(id, properties);
    };
 
    /**
     * @experimental This method sets the flag to render the start and end characters of the barcode. This is not
     * supported for all barcode types.
     * @param id The id of the specific barcode frame.
     * @param drawStartStopChars The flag indicating if the start and stop characters should be drawn or not
     * @returns
     */
    setDrawStartStopChars = async (id: Id, drawStartStopChars: boolean) => {
        const properties: BarcodeProperties = { drawStartStopChars: drawStartStopChars };
        return this.setBarcodeProperties(id, properties);
    };
 
    /**
     * @experimental This method returns the possible configuration options which are valid for the given barcode type.
     * @param type the barcode type for which the configuration options are requested.
     * @returns a BarcodeConfigurationOptions object
     */
    getBarcodeConfigationOptions = (type: BarcodeType): BarcodeConfigurationOptions => {
        let allowToggleText = true;
        let allowBarHeight = true;
        let allowEnableMagnification = true;
        let allowedCharacterSets = undefined;
        let allowedErrorCorrectionLevels = undefined;
        let quietZoneAlwaysCombined = false;
        let allowToggleLightMarginIndicator = false;
        let allowToggleDrawStartAndEndChar = false;
        switch (type) {
            case BarcodeType.qr:
                allowedCharacterSets = [BarcodeCharacterSet.iso8859_1, BarcodeCharacterSet.utf8];
                allowedErrorCorrectionLevels = [
                    BarcodeErrorCorrectionLevel.low,
                    BarcodeErrorCorrectionLevel.medium,
                    BarcodeErrorCorrectionLevel.quartile,
                    BarcodeErrorCorrectionLevel.high,
                ];
            // Intentional fall-through
            case BarcodeType.dataMatrix:
                allowToggleText = false;
                allowBarHeight = false;
                allowEnableMagnification = false;
                quietZoneAlwaysCombined = true;
                break;
            case BarcodeType.ean13:
                allowToggleText = false;
                allowToggleLightMarginIndicator = true;
                break;
            case BarcodeType.ean8:
                allowToggleLightMarginIndicator = true;
            // Intentional fall-through
            case BarcodeType.upca:
            case BarcodeType.upce:
                allowToggleText = false;
                break;
            case BarcodeType.code128:
            case BarcodeType.gs1128:
                allowedCharacterSets = [
                    BarcodeCharacterSet.code128a,
                    BarcodeCharacterSet.code128b,
                    BarcodeCharacterSet.code128c,
                ];
                break;
            case BarcodeType.code39:
                allowToggleDrawStartAndEndChar = true;
                break;
        }
        return {
            allowEnableMagnification: allowEnableMagnification,
            allowBarHeight: allowBarHeight,
            allowQuietZone: true,
            allowedCharacterSets: allowedCharacterSets,
            allowedErrorCorrectionLevels: allowedErrorCorrectionLevels,
            allowToggleText: allowToggleText,
            quietZoneAlwaysCombined: quietZoneAlwaysCombined,
            allowToggleLightMarginIndicator: allowToggleLightMarginIndicator,
            allowToggleDrawStartAndEndChar: allowToggleDrawStartAndEndChar,
        };
    };
}