all files / common/utils/ export.js

95.95% Statements 71/74
88.46% Branches 23/26
100% Functions 11/11
95.95% Lines 71/74
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   37×   19× 19× 19× 19×     19× 19× 16×     19× 19×               15×   19×   18× 18× 18× 18× 18×             18× 18× 18×     18×   18×     15× 15× 15× 15× 15× 15× 12× 12× 12× 12× 12× 12×                       15×                         18× 18× 18× 18×       18× 24× 24×     24× 24× 24× 24× 24×   18× 18× 18×                
define(["require", "exports", "@syncfusion/ej2-base", "../utils/helper", "../model/constants", "@syncfusion/ej2-pdf-export"], function (require, exports, ej2_base_1, helper_1, constants_1, ej2_pdf_export_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var ExportUtils = (function () {
        function ExportUtils(control) {
            this.control = control;
        }
        ExportUtils.prototype.print = function (elements) {
            this.printWindow = window.open('', 'print', 'height=' + window.outerHeight + ',width=' + window.outerWidth + ',tabbar=no');
            this.printWindow.moveTo(0, 0);
            this.printWindow.resizeTo(screen.availWidth, screen.availHeight);
            var argsData = {
                cancel: false, htmlContent: this.getHTMLContent(elements), name: constants_1.beforePrint
            };
            this.control.trigger(constants_1.beforePrint, argsData);
            if (!argsData.cancel) {
                ej2_base_1.print(argsData.htmlContent, this.printWindow);
            }
        };
        ExportUtils.prototype.getHTMLContent = function (elements) {
            var div = ej2_base_1.createElement('div');
            if (elements) {
                if (elements instanceof Array) {
                    elements.forEach(function (value) {
                        div.appendChild(helper_1.getElement(value).cloneNode(true));
                    });
                }
                else if (elements instanceof Element) {
                    div.appendChild(elements.cloneNode(true));
                }
                else {
                    div.appendChild(helper_1.getElement(elements).cloneNode(true));
                }
            }
            else {
                div.appendChild(this.control.element.cloneNode(true));
            }
            return div;
        };
        ExportUtils.prototype.export = function (type, fileName, orientation, controls, width, height) {
            var _this = this;
            var controlValue = this.getControlsValue(controls);
            width = width ? width : controlValue.width;
            height = height ? height : controlValue.height;
            var element = ej2_base_1.createElement('canvas', {
                id: 'ej2-canvas',
                attrs: {
                    'width': width.toString(),
                    'height': height.toString()
                }
            });
            var isDownload = !(ej2_base_1.Browser.userAgent.toString().indexOf('HeadlessChrome') > -1);
            orientation = ej2_base_1.isNullOrUndefined(orientation) ? ej2_pdf_export_1.PdfPageOrientation.Landscape : orientation;
            var svgData = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">' +
                controlValue.svg.outerHTML +
                '</svg>';
            var url = window.URL.createObjectURL(new Blob(type === 'SVG' ? [svgData] :
                [(new XMLSerializer()).serializeToString(controlValue.svg)], { type: 'image/svg+xml' }));
            if (type === 'SVG') {
                this.triggerDownload(fileName, type, url, isDownload);
            }
            else {
                var image_1 = new Image();
                var ctx_1 = element.getContext('2d');
                image_1.onload = (function () {
                    ctx_1.drawImage(image_1, 0, 0);
                    window.URL.revokeObjectURL(url);
                    if (type === 'PDF') {
                        var document_1 = new ej2_pdf_export_1.PdfDocument();
                        var imageString = element.toDataURL('image/jpeg').replace('image/jpeg', 'image/octet-stream');
                        document_1.pageSettings.orientation = orientation;
                        imageString = imageString.slice(imageString.indexOf(',') + 1);
                        document_1.pages.add().graphics.drawImage(new ej2_pdf_export_1.PdfBitmap(imageString), 0, 0, width, height);
                        Iif (isDownload) {
                            document_1.save(fileName + '.pdf');
                            document_1.destroy();
                        }
                    }
                    else {
                        Iif (window.navigator.msSaveOrOpenBlob) {
                            window.navigator.msSaveOrOpenBlob(element.msToBlob(), fileName + '.' + type.toLocaleLowerCase());
                        }
                        else {
                            _this.triggerDownload(fileName, type, element.toDataURL('image/png').replace('image/png', 'image/octet-stream'), isDownload);
                        }
                    }
                });
                image_1.src = url;
            }
        };
        ExportUtils.prototype.triggerDownload = function (fileName, type, url, isDownload) {
            ej2_base_1.createElement('a', {
                attrs: {
                    'download': fileName + '.' + type.toLocaleLowerCase(),
                    'href': url
                }
            }).dispatchEvent(new MouseEvent(isDownload ? 'click' : 'move', {
                view: window,
                bubbles: false,
                cancelable: true
            }));
        };
        ExportUtils.prototype.getControlsValue = function (controls) {
            var width = 0;
            var height = 0;
            var content = '';
            var svgObject = new ej2_base_1.SvgRenderer('').createSvg({
                id: 'Svg_Export_Element',
                width: 200, height: 200
            });
            controls.map(function (control) {
                var svg = control.svgObject.cloneNode(true);
                var groupEle = control.renderer.createGroup({
                    style: 'transform: translateY(' + height + 'px)'
                });
                groupEle.appendChild(svg);
                width = Math.max(control.availableSize.width, width);
                height += control.availableSize.height;
                content += control.svgObject.outerHTML;
                svgObject.appendChild(groupEle);
            });
            svgObject.setAttribute('width', width + '');
            svgObject.setAttribute('height', height + '');
            return {
                'width': width,
                'height': height,
                'svg': svgObject
            };
        };
        return ExportUtils;
    }());
    exports.ExportUtils = ExportUtils;
});