all files / pivotchart/actions/ chart-export.js

95.24% Statements 100/105
72.34% Branches 68/94
91.67% Functions 11/12
95.24% Lines 100/105
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   18× 18×                                                                                                                                       15×   15× 15×          
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-svg-base", "@syncfusion/ej2-pdf-export", "../../common/base/constant", "../../base/util", "../../pivotview/actions/pdf-export-helper"], function (require, exports, ej2_base_1, ej2_svg_base_1, ej2_pdf_export_1, events, util_1, pdf_export_helper_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var ChartExport = (function () {
        function ChartExport(parent) {
            this.parent = parent;
            this.pdfExportHelper = new pdf_export_helper_1.PDFExportHelper();
        }
        ChartExport.prototype.getModuleName = function () {
            return 'chartExport';
        };
        ChartExport.prototype.pdfChartExport = function (pdfExportProperties, pdfDoc, isMultipleExport, isBlob) {
            var _this = this;
            var controls = [this.parent.chart];
            var chartInfo = this.getChartInfo(controls);
            var width = chartInfo.width;
            var height = chartInfo.height;
            var element = this.parent.chart.svgObject;
            var isCanvas = this.parent.chart.enableCanvas;
            Eif (!isCanvas) {
                element = ej2_base_1.createElement('canvas', {
                    id: 'ej2-canvas',
                    attrs: {
                        'width': width.toString(),
                        'height': height.toString()
                    }
                });
            }
            var url = window.URL.createObjectURL(new Blob([(new XMLSerializer()).serializeToString(chartInfo.svg)], { type: 'image/svg+xml' }));
            var image = new Image();
            var ctx = element.getContext('2d');
            image.src = url;
            return new Promise(function (resolve) {
                image.onload = (function () {
                    var pdfDocument;
                    Iif (!ej2_base_1.isNullOrUndefined(pdfDoc)) {
                        pdfDocument = pdfDoc;
                    }
                    else {
                        pdfDocument = new ej2_pdf_export_1.PdfDocument();
                    }
                    ctx.drawImage(image, 0, 0);
                    window.URL.revokeObjectURL(url);
                    _this.exportPdf(element, pdfDocument, isMultipleExport, isBlob, width, height, pdfExportProperties);
                    resolve(pdfDocument);
                });
            });
        };
        ChartExport.prototype.getChartInfo = function (controls, isVertical) {
            var width = 0;
            var height = 0;
            var isCanvas = this.parent.chart.enableCanvas;
            var svgObject = new ej2_svg_base_1.SvgRenderer('').createSvg({
                id: 'Svg_Export_Element',
                width: 200, height: 200
            });
            var backgroundColor;
            controls.map(function (control) {
                var svg = control.svgObject.cloneNode(true);
                var groupEle = control.renderer.createGroup({
                    style: (ej2_base_1.isNullOrUndefined(isVertical) || isVertical) ? 'transform: translateY(' + height + 'px)' :
                        'transform: translateX(' + width + 'px)'
                });
                backgroundColor = svg.childNodes[0] ? svg.childNodes[0].getAttribute('fill') : 'transparent';
                Eif (backgroundColor === 'transparent') {
                    Iif (control.theme.indexOf('Dark') > -1 || control.theme === 'HighContrast') {
                        backgroundColor = 'rgba(0, 0, 0, 1)';
                    }
                    else {
                        backgroundColor = 'rgba(255, 255, 255, 1)';
                    }
                }
                Eif (!isCanvas) {
                    groupEle.appendChild(svg);
                }
                width = control.availableSize.width;
                height = control.availableSize.height;
                Eif (!isCanvas) {
                    svgObject.appendChild(groupEle);
                }
            });
            Eif (!isCanvas) {
                svgObject.setAttribute('width', width + '');
                svgObject.setAttribute('height', height + '');
                svgObject.setAttribute('style', 'background-color: ' + backgroundColor + ';');
            }
            return {
                'width': width,
                'height': height,
                'svg': svgObject
            };
        };
        ChartExport.prototype.exportPdf = function (element, pdfDocument, isMultipleExport, isBlob, width, height, pdfExportProperties) {
            var documentSection = pdfDocument.sections.add();
            var documentWidth = pdfDocument.pageSettings.width;
            var documentHeight = pdfDocument.pageSettings.height;
            var margin = pdfDocument.pageSettings.margins;
            var chartWidth = (width + margin.left + margin.right);
            pdfDocument.pageSettings.size = new ej2_pdf_export_1.SizeF(chartWidth, documentHeight);
            var fileName = this.exportProperties.fileName ? this.exportProperties.fileName :
                (!ej2_base_1.isNullOrUndefined(pdfExportProperties) && !ej2_base_1.isNullOrUndefined(pdfExportProperties.fileName)) ?
                    pdfExportProperties.fileName : 'default';
            if (this.exportProperties.width || this.exportProperties.height) {
                pdfDocument.pageSettings.orientation = ((this.exportProperties.width > this.exportProperties.height) ||
                    (!this.exportProperties.height && (this.exportProperties.width > documentHeight)) || (!this.exportProperties.width &&
                    (documentWidth > this.exportProperties.height))) ? ej2_pdf_export_1.PdfPageOrientation.Landscape : ej2_pdf_export_1.PdfPageOrientation.Portrait;
                pdfDocument.pageSettings.size = new ej2_pdf_export_1.SizeF(this.exportProperties.width ? this.exportProperties.width
                    : documentWidth, this.exportProperties.height ? this.exportProperties.height : documentHeight);
            }
            else {
                pdfDocument.pageSettings.orientation = (this.exportProperties.orientation === 0 || this.exportProperties.orientation)
                    ? this.exportProperties.orientation : (!ej2_base_1.isNullOrUndefined(pdfExportProperties) &&
                    !ej2_base_1.isNullOrUndefined(pdfExportProperties.pageOrientation)) ? (pdfExportProperties.pageOrientation === 'Landscape' ?
                    ej2_pdf_export_1.PdfPageOrientation.Landscape : ej2_pdf_export_1.PdfPageOrientation.Portrait) : ej2_pdf_export_1.PdfPageOrientation.Landscape;
                Iif (!ej2_base_1.isNullOrUndefined(pdfExportProperties) && !ej2_base_1.isNullOrUndefined(pdfExportProperties.pageSize)) {
                    pdfDocument.pageSettings.size = util_1.PivotUtil.getPageSize(pdfExportProperties.pageSize);
                }
            }
            Eif (!ej2_base_1.isNullOrUndefined(this.exportProperties.pdfMargins)) {
                var margins = pdfDocument.pageSettings.margins;
                margins.top = !ej2_base_1.isNullOrUndefined(this.exportProperties.pdfMargins.top) ? this.exportProperties.pdfMargins.top : margins.top;
                margins.bottom = !ej2_base_1.isNullOrUndefined(this.exportProperties.pdfMargins.bottom) ? this.exportProperties.pdfMargins.bottom :
                    margins.bottom;
                margins.left = !ej2_base_1.isNullOrUndefined(this.exportProperties.pdfMargins.left) ? this.exportProperties.pdfMargins.left : margins.left;
                margins.right = !ej2_base_1.isNullOrUndefined(this.exportProperties.pdfMargins.right) ? this.exportProperties.pdfMargins.right :
                    margins.right;
            }
            documentSection.setPageSettings(pdfDocument.pageSettings);
            documentHeight = pdfDocument.pageSettings.height;
            var imageString = element.toDataURL('image/jpeg').replace('image/jpeg', 'image/octet-stream');
            imageString = imageString.slice(imageString.indexOf(',') + 1);
            var image = new ej2_pdf_export_1.PdfBitmap(imageString);
            var pdfPage = documentSection.pages.add();
            if (!ej2_base_1.isNullOrUndefined(pdfExportProperties) && !ej2_base_1.isNullOrUndefined(pdfExportProperties.header)) {
                this.pdfExportHelper.drawHeader(pdfExportProperties, pdfDocument);
            }
            if (!ej2_base_1.isNullOrUndefined(pdfExportProperties) && !ej2_base_1.isNullOrUndefined(pdfExportProperties.footer)) {
                this.pdfExportHelper.drawFooter(pdfExportProperties, pdfDocument);
            }
            pdfPage.graphics.drawImage(image, 0, 0, (documentHeight < height || this.exportProperties.width
                || pdfDocument.pageSettings.size) ? pdfPage.getClientSize().width : chartWidth, documentHeight < height
                ? pdfPage.getClientSize().height : height);
            var blobPromise;
            if (isBlob || isMultipleExport) {
                Iif (isBlob) {
                    blobPromise = pdfDocument.save();
                }
            }
            else {
                pdfDocument.save(fileName + '.pdf');
                pdfDocument.destroy();
            }
            var exportCompleteEventArgs = {
                type: 'PDF',
                promise: isBlob ? blobPromise : null
            };
            this.parent.trigger(events.exportComplete, exportCompleteEventArgs);
            return new Promise(function () {
                pdfDocument;
            });
        };
        ChartExport.prototype.destroy = function () {
            if (this.exportProperties) {
                this.exportProperties = null;
            }
            Eif (this.pdfExportHelper) {
                this.pdfExportHelper = null;
            }
        };
        return ChartExport;
    }());
    exports.ChartExport = ChartExport;
});