all files / common/utils/ export.js

81.38% Statements 153/188
66.67% Branches 78/117
93.33% Functions 14/15
81.38% Lines 153/188
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 298 299 300 301 302   44×   21× 21× 21× 21× 21× 21× 21× 21× 21× 21× 21× 21× 21× 21× 21×               21×     21×   21×             20×                 18× 18× 18× 18× 18× 18× 18× 11× 11×                   18×   21× 21×     21× 21× 21×                                                                       25× 25× 25×       25× 25× 25× 25× 25× 25× 25×       25× 25× 21×     20×     25× 25×           25×   25×   25× 25× 25×                 25×   25×   25× 25×   25× 25× 25× 25× 25×   25×           25×           25×       13× 13× 13× 13× 13× 13× 13× 13×   13×   13×   13×   13× 13× 13×   13×                                                          
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-svg-base", "../utils/helper", "../model/constants", "@syncfusion/ej2-pdf-export"], function (require, exports, ej2_base_1, ej2_svg_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.export = function (type, fileName, orientation, controls, width, height, isVertical, header, footer, exportToMultiplePage) {
            var _this = this;
            var controlValue = this.getControlsValue(controls, isVertical, (exportToMultiplePage && type === 'PDF'), type);
            var canvasElements = [];
            var controlWidth = [];
            var controlHeight = [];
            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 _loop_1 = function (i) {
                controlWidth.push(width ? width : controlValue[i].width);
                controlHeight.push(height ? height : controlValue[i].height);
                var element = controls[i].svgObject;
                var isCanvas = controls[i].enableCanvas;
                var image = void 0;
                Eif (!isCanvas) {
                    element = ej2_base_1.createElement('canvas', {
                        id: 'ej2-canvas',
                        attrs: {
                            'width': controlWidth[i].toString(),
                            'height': controlHeight[i].toString()
                        }
                    });
                }
                var svgData = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">' +
                    controlValue[i].svg.outerHTML +
                    '</svg>';
                var url = window.URL.createObjectURL(new Blob(type === 'SVG' ? [svgData] :
                    [(new XMLSerializer()).serializeToString(controlValue[i].svg)], { type: 'image/svg+xml' }));
                if (type === 'SVG') {
                    Iif (ej2_base_1.Browser.info.name === 'msie') {
                        var svg = new Blob([(new XMLSerializer()).serializeToString(controlValue[i].svg)], { type: 'application/octet-stream' });
                        window.navigator.msSaveOrOpenBlob(svg, fileName + '.' + type.toLocaleLowerCase());
                    }
                    else {
                        this_1.triggerDownload(fileName, type, url, isDownload);
                    }
                }
                else if (ej2_base_1.Browser.info.name === 'msie') {
                    var canvas = element;
                    Eif (!isCanvas) {
                        canvas = this_1.createCanvas();
                    }
                    image = canvas.toDataURL();
                    canvasElements.push(element);
                    Eif (type === 'PDF') {
                        Eif (canvasElements.length === controlValue.length) {
                            this_1.exportPdf(canvasElements, orientation, controlWidth, controlHeight, isDownload, fileName, header, footer);
                        }
                    }
                    else {
                        this_1.doexport(type, image, fileName);
                    }
                }
                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);
                        canvasElements.push(element);
                        if (type === 'PDF') {
                            Eif (canvasElements.length === controlValue.length) {
                                _this.exportPdf(canvasElements, orientation, controlWidth, controlHeight, isDownload, fileName, header, footer);
                            }
                        }
                        else {
                            Iif (window.navigator.msSaveOrOpenBlob) {
                                window.navigator.msSaveOrOpenBlob(element.toBlob(null), fileName + '.' + type.toLocaleLowerCase());
                            }
                            else {
                                _this.triggerDownload(fileName, type, element.toDataURL('image/' + type.toLowerCase()), isDownload);
                            }
                        }
                    });
                    image_1.src = url;
                }
                Eif (!isCanvas) {
                    helper_1.removeElement(document.getElementById(controls[i].element.id + '_canvas'));
                }
            };
            var this_1 = this;
            for (var i = 0; i < controlValue.length; i++) {
                _loop_1(i);
            }
        };
        ExportUtils.prototype.getDataUrl = function (chart) {
            var controlValue = this.getControlsValue([chart]);
            var element = this.control.svgObject;
            var isCanvas = this.control.enableCanvas;
            Eif (!isCanvas) {
                element = ej2_base_1.createElement('canvas', {
                    id: 'ej2-canvas',
                    attrs: {
                        'width': controlValue[0].width.toString(),
                        'height': controlValue[0].height.toString()
                    }
                });
            }
            var url = window.URL.createObjectURL(new Blob([(new XMLSerializer()).serializeToString(controlValue[0].svg)], { type: 'image/svg+xml' }));
            Iif (ej2_base_1.Browser.info.name === 'msie') {
                var canvas = element;
                if (!isCanvas) {
                    canvas = this.createCanvas();
                }
                var argsData = {
                    name: constants_1.afterExport, cancel: false, dataUrl: element.toDataURL('image/png')
                };
                chart.trigger(constants_1.afterExport, argsData);
                return { element: canvas, dataUrl: canvas.toDataURL() };
            }
            else {
                var image_2 = new Image();
                var ctx_2 = element.getContext('2d');
                image_2.onload = (function () {
                    ctx_2.drawImage(image_2, 0, 0);
                    window.URL.revokeObjectURL(url);
                    var argsData = {
                        name: constants_1.afterExport, cancel: false, dataUrl: element.toDataURL('image/png')
                    };
                    chart.trigger(constants_1.afterExport, argsData);
                    return argsData.dataUrl;
                });
                image_2.src = url;
                return { element: element, blobUrl: 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, isVertical, isMultiPages, type) {
            var width = 0;
            var height = 0;
            var svgObject = new ej2_svg_base_1.SvgRenderer('').createSvg({
                id: 'Svg_Export_Element',
                width: 200, height: 200
            });
            var controlValues = [];
            var backgroundColor;
            for (var i = 0; i < controls.length; i++) {
                var control = controls[i];
                var isCanvas = control.enableCanvas;
                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';
                if (backgroundColor === 'transparent') {
                    if (control.theme.indexOf('Dark') > -1 || control.theme.indexOf('HighContrast') > -1) {
                        backgroundColor = 'rgba(0, 0, 0, 1)';
                    }
                    else {
                        backgroundColor = 'rgba(255, 255, 255, 1)';
                    }
                }
                Eif (!isCanvas) {
                    Iif (control.getModuleName() === 'stockChart') {
                        svg.childNodes[0].firstChild.setAttribute('fill', backgroundColor);
                        for (var index = 1; index < svg.childNodes.length; index++) {
                            svg.childNodes[index].childNodes[0].setAttribute('fill', backgroundColor);
                        }
                    }
                    else if (type === 'SVG') {
                        svg.childNodes[0].setAttribute('fill', backgroundColor);
                    }
                    groupEle.appendChild(svg);
                }
                var top_1 = 0;
                var left = 0;
                Iif (control.stockLegendModule && control.legendSettings.visible) {
                    if (control.legendSettings.position === 'Bottom' || control.legendSettings.position === 'Top'
                        || control.legendSettings.position === 'Auto') {
                        top_1 += control.stockLegendModule.legendBounds.height;
                    }
                    else if (control.legendSettings.position === 'Left' || control.legendSettings.position === 'Right') {
                        left += control.stockLegendModule.legendBounds.width;
                    }
                }
                width = (ej2_base_1.isNullOrUndefined(isVertical) || isVertical) ? Math.max(control.availableSize.width + left, width) :
                    width + control.availableSize.width + left;
                height = (ej2_base_1.isNullOrUndefined(isVertical) || isVertical) ? height + control.availableSize.height + top_1 :
                    Math.max(control.availableSize.height + top_1, height);
                Eif (!isCanvas) {
                    svgObject.appendChild(groupEle);
                }
                Eif (isMultiPages || i === controls.length - 1) {
                    Eif ((!isMultiPages && !(this.control.enableCanvas)) || (isMultiPages && !isCanvas)) {
                        svgObject.setAttribute('width', width + '');
                        svgObject.setAttribute('height', height + '');
                        svgObject.setAttribute('style', 'background-color: ' + backgroundColor + ';');
                    }
                    controlValues.push({
                        'width': width,
                        'height': height,
                        'svg': svgObject
                    });
                }
                if (isMultiPages && (i < controls.length)) {
                    width = 0;
                    height = 0;
                    svgObject = new ej2_svg_base_1.SvgRenderer('').createSvg({
                        id: 'Svg_Export_Element',
                        width: 200, height: 200
                    });
                }
            }
            return controlValues;
        };
        ExportUtils.prototype.createCanvas = function () {
            var chart = this.control;
            this.canvasRender(true, chart);
            var canvas = chart.svgObject;
            this.canvasRender(false, chart);
            return canvas;
        };
        ExportUtils.prototype.canvasRender = function (enableCanvas, chart) {
            chart.enableCanvas = enableCanvas;
            chart['preRender']();
            chart['render']();
        };
        ExportUtils.prototype.exportPdf = function (element, orientation, width, height, isDownload, fileName, header, footer) {
            var document = new ej2_pdf_export_1.PdfDocument();
            var margin = document.pageSettings.margins;
            var pdfDefaultWidth = document.pageSettings.width;
            var pdfDefaultHeight = document.pageSettings.height;
            for (var i = 0; element.length > i; i++) {
                var imageString = element[i].toDataURL('image/jpeg').replace('image/jpeg', 'image/octet-stream');
                document.pageSettings.orientation = orientation;
                var exactWidth = (pdfDefaultWidth < width[i]) ? (width[i] + margin.left + margin.right) :
                    pdfDefaultWidth;
                var exactHeight = orientation === 0 ? (width[i] + margin.left + margin.right) :
                    (pdfDefaultHeight < height[i]) ? (height[i] + margin.top + margin.bottom) : pdfDefaultHeight;
                if (header !== undefined) {
                    var font = new ej2_pdf_export_1.PdfStandardFont(1, header.fontSize || 15);
                    var pdfHeader = new ej2_pdf_export_1.PdfPageTemplateElement(exactWidth, 40);
                    pdfHeader.graphics.drawString(header.content + '', font, null, new ej2_pdf_export_1.PdfSolidBrush(new ej2_pdf_export_1.PdfColor(0, 0, 0)), header.x, header.y, null);
                    document.template.top = pdfHeader;
                }
                if (footer !== undefined) {
                    var font = new ej2_pdf_export_1.PdfStandardFont(1, footer.fontSize || 15);
                    var pdfFooter = new ej2_pdf_export_1.PdfPageTemplateElement(exactWidth, 40);
                    pdfFooter.graphics.drawString(footer.content + '', font, null, new ej2_pdf_export_1.PdfSolidBrush(new ej2_pdf_export_1.PdfColor(0, 0, 0)), footer.x, footer.y, null);
                    document.template.bottom = pdfFooter;
                }
                document.pageSettings.size = new ej2_pdf_export_1.SizeF(exactWidth, exactHeight);
                imageString = imageString.slice(imageString.indexOf(',') + 1);
                document.pages.add().graphics.drawImage(new ej2_pdf_export_1.PdfBitmap(imageString), 0, 0, width[i], height[i]);
            }
            Iif (isDownload) {
                document.save(fileName + '.pdf');
                document.destroy();
            }
        };
        ExportUtils.prototype.doexport = function (type, image, fileName) {
            var images = [];
            var fileType = type || 'JPG';
            images = [image];
            this.exportImage(images, fileName, fileType, image);
        };
        ExportUtils.prototype.exportImage = function (images, fileName, fileType, image) {
            var buffers = [];
            var length = (!(images instanceof HTMLElement)) ? images.length : 0;
            for (var g = 0; g < length; g++) {
                image = images[g];
                image = image.replace(/^data:[a-z]*;,/, '');
                var image1 = image.split(',');
                var byteString = atob(image1[1]);
                var buffer = new ArrayBuffer(byteString.length);
                var intArray = new Uint8Array(buffer);
                for (var i = 0; i < byteString.length; i++) {
                    intArray[i] = byteString.charCodeAt(i);
                }
                buffers.push(buffer);
            }
            for (var j = 0; j < buffers.length; j++) {
                var b = new Blob([buffers[j]], { type: 'application/octet-stream' });
                if (ej2_base_1.Browser.info.name === 'msie') {
                    window.navigator.msSaveOrOpenBlob(b, fileName + '.' + fileType.toLocaleLowerCase());
                }
            }
        };
        return ExportUtils;
    }());
    exports.ExportUtils = ExportUtils;
});