all files / barcode/utility/ barcode-util.js

100% Statements 60/60
87.5% Branches 14/16
100% Functions 11/11
100% Lines 60/60
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   29× 29×   20× 20×   29×   120419×     120419×         12× 12× 12× 12×     12×   12× 12× 12× 12× 12× 12× 12× 12× 12× 12× 12×           12×         12×      
define(["require", "exports", "./../../barcode/rendering/renderer", "./../../barcode/rendering/canvas-renderer"], function (require, exports, renderer_1, canvas_renderer_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    function removeChildElements(newProp, barcodeCanvas, mode, id) {
        var barCodeSVG = barcodeCanvas;
        if (mode === 'SVG' && !newProp.mode) {
            barCodeSVG.innerHTML = '';
        }
        else Eif (newProp.mode) {
            barCodeSVG.parentNode.removeChild(barCodeSVG);
        }
        return new renderer_1.BarcodeRenderer(id, mode === 'SVG');
    }
    exports.removeChildElements = removeChildElements;
    function getBaseAttributes(width, height, offSetX, offsetY, color, strokeColor) {
        var options = {
            width: width, height: height, x: offSetX, y: offsetY, color: color, strokeColor: strokeColor
        };
        return options;
    }
    exports.getBaseAttributes = getBaseAttributes;
    function clearCanvas(view, barcodeCanvas) {
        var width = view.element.offsetWidth * 1.5;
        var height = view.element.offsetHeight * 1.5;
        var ctx = canvas_renderer_1.BarcodeCanvasRenderer.getContext(barcodeCanvas);
        ctx.clearRect(0, 0, width, height);
    }
    exports.clearCanvas = clearCanvas;
    function refreshCanvasBarcode(qrCodeGenerator, barcodeCanvas) {
        clearCanvas(qrCodeGenerator, barcodeCanvas);
    }
    exports.refreshCanvasBarcode = refreshCanvasBarcode;
    function triggerDownload(type, fileName, url) {
        var anchorElement = document.createElement('a');
        anchorElement.download = fileName + '.' + type.toLocaleLowerCase();
        anchorElement.href = url;
        anchorElement.click();
    }
    exports.triggerDownload = triggerDownload;
    function exportAsImage(exportType, fileName, element, isReturnBase64, code) {
        var returnValue = imageExport(exportType, fileName, element, isReturnBase64, code);
        Eif (returnValue instanceof Promise) {
            returnValue.then(function (data) {
                return data;
            });
        }
        return returnValue;
    }
    exports.exportAsImage = exportAsImage;
    function imageExport(type, fileName, element, isReturnBase64, code) {
        var promise = new Promise(function (resolve, reject) {
            var canvas = element.children[0];
            var serializer = 'XMLSerializer';
            var canvasElement = document.createElement('canvas');
            canvasElement.height = element.clientHeight;
            canvasElement.width = element.clientWidth;
            var context = canvasElement.getContext('2d');
            var image = new Image();
            image.onload = function () {
                context.drawImage(image, 0, 0);
                if (!isReturnBase64) {
                    triggerDownload(type, fileName, canvasElement.toDataURL('image/png').replace('image/png', 'image/octet-stream'));
                    resolve(null);
                }
                else {
                    var base64String = (type === 'JPG') ? canvasElement.toDataURL('image/jpg') :
                        canvasElement.toDataURL('image/png');
                    resolve(base64String);
                }
            };
            if (code.mode === 'Canvas') {
                image.src = (type === 'JPG') ? canvas.toDataURL('image/jpg') : canvas.toDataURL('image/png');
                canvasElement.height = element.clientHeight * 1.5;
                canvasElement.width = element.clientWidth * 1.5;
                context.scale(2 / 3, 2 / 3);
            }
            else {
                image.src = window.URL.createObjectURL(new Blob([new window["" + serializer]().serializeToString(element.children[0])], { type: 'image/svg+xml' }));
            }
        });
        return promise;
    }
    exports.imageExport = imageExport;
});