all files / treegrid/actions/ excel-export.js

87.31% Statements 117/134
77.5% Branches 62/80
100% Functions 18/18
87.31% Lines 117/134
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   20× 20× 20× 20× 20×   11279×   20× 20× 20× 20×   19×   19× 19×             1160×                                                                             798× 181× 181× 181× 181× 181× 181×   798× 798×   252× 193× 193× 193× 193× 193× 96× 96× 96× 96×       96×     97× 36×             26×        
define(["require", "exports", "@syncfusion/ej2-grids", "@syncfusion/ej2-grids", "../utils", "@syncfusion/ej2-base", "@syncfusion/ej2-data", "../base/constant"], function (require, exports, ej2_grids_1, ej2_grids_2, utils_1, ej2_base_1, ej2_data_1, event) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var ExcelExport = (function () {
        function ExcelExport(parent) {
            this.isCollapsedStatePersist = false;
            ej2_grids_1.Grid.Inject(ej2_grids_1.ExcelExport);
            this.parent = parent;
            this.dataResults = {};
            this.addEventListener();
        }
        ExcelExport.prototype.getModuleName = function () {
            return 'ExcelExport';
        };
        ExcelExport.prototype.addEventListener = function () {
            this.parent.on('updateResults', this.updateExcelResultModel, this);
            this.parent.on('excelCellInfo', this.excelQueryCellInfo, this);
            this.parent.grid.on('export-RowDataBound', this.exportRowDataBound, this);
            this.parent.grid.on('finalPageSetup', this.finalPageSetup, this);
        };
        ExcelExport.prototype.destroy = function () {
            this.removeEventListener();
        };
        ExcelExport.prototype.removeEventListener = function () {
            Eif (this.parent.isDestroyed) {
                return;
            }
            this.parent.off('updateResults', this.updateExcelResultModel);
            this.parent.off('excelCellInfo', this.excelQueryCellInfo);
            this.parent.grid.off('export-RowDataBound', this.exportRowDataBound);
            this.parent.grid.off('finalPageSetup', this.finalPageSetup);
        };
        ExcelExport.prototype.updateExcelResultModel = function (returnResult) {
            this.dataResults = returnResult;
        };
        ExcelExport.prototype.Map = function (excelExportProperties, isMultipleExport, workbook, isBlob, isCsv) {
            var _this = this;
            var dataSource = this.parent.dataSource;
            var data = new ej2_grids_2.Data(this.parent.grid);
            var property = Object();
            ej2_base_1.setValue('isCsv', isCsv, property);
            ej2_base_1.setValue('cancel', false, property);
            if (!ej2_base_1.isNullOrUndefined(excelExportProperties)) {
                this.isCollapsedStatePersist = excelExportProperties.isCollapsedStatePersist;
            }
            if (!ej2_base_1.isNullOrUndefined(excelExportProperties)) {
                if (!ej2_base_1.isNullOrUndefined(excelExportProperties.dataSource) && !excelExportProperties.dataSource['dataSource']) {
                    return this.parent.grid.excelExportModule.Map(this.parent.grid, excelExportProperties, isMultipleExport, workbook, isCsv, isBlob);
                }
                if (excelExportProperties.exportType === 'CurrentPage') {
                    excelExportProperties.dataSource = this.parent.getCurrentViewRecords();
                    return this.parent.grid.excelExportModule.Map(this.parent.grid, excelExportProperties, isMultipleExport, workbook, isCsv, isBlob);
                }
            }
            return new Promise(function (resolve) {
                var dm = _this.isLocal() && !(dataSource instanceof ej2_data_1.DataManager) ? new ej2_data_1.DataManager(dataSource)
                    : _this.parent.dataSource;
                var query = new ej2_data_1.Query();
                if (!_this.isLocal()) {
                    query = _this.generateQuery(query);
                    query.queries = _this.parent.grid.getDataModule().generateQuery().queries;
                    query = ej2_grids_1.ExportHelper.getQuery(_this.parent.grid, data);
                    if (ej2_base_1.isNullOrUndefined(_this.parent.filterModule)) {
                        query.queries = query.queries.slice(1, 2);
                        query.params = query.params.slice(0, 0);
                    }
                    ej2_base_1.setValue('query', query, property);
                }
                _this.parent.trigger(event.beforeExcelExport, ej2_base_1.extend(property, excelExportProperties));
                Iif (ej2_grids_1.getObject('cancel', property)) {
                    return null;
                }
                dm.executeQuery(query).then(function (e) {
                    var customData = null;
                    if (!ej2_base_1.isNullOrUndefined(excelExportProperties) && !ej2_base_1.isNullOrUndefined(excelExportProperties.dataSource)) {
                        customData = excelExportProperties.dataSource;
                    }
                    excelExportProperties = _this.manipulateExportProperties(excelExportProperties, dataSource, e);
                    return _this.parent.grid.excelExportModule.Map(_this.parent.grid, excelExportProperties, isMultipleExport, workbook, isCsv, isBlob).then(function (book) {
                        Iif (customData != null) {
                            excelExportProperties.dataSource = customData;
                        }
                        else {
                            delete excelExportProperties.dataSource;
                        }
                        resolve(book);
                    });
                });
            });
        };
        ExcelExport.prototype.generateQuery = function (query, property) {
            Iif (!ej2_base_1.isNullOrUndefined(property) && property.exportType === 'CurrentPage'
                && this.parent.allowPaging) {
                property.exportType = 'AllPages';
                query.addParams('ExportType', 'CurrentPage');
                query.where(this.parent.parentIdMapping, 'equal', null);
                query = ej2_grids_1.getObject('grid.renderModule.data.pageQuery', this.parent)(query);
            }
            return query;
        };
        ExcelExport.prototype.manipulateExportProperties = function (property, dtSrc, queryResult) {
            var args = Object();
            Eif (!ej2_base_1.isNullOrUndefined(this.parent.grid.getDataModule())) {
                ej2_base_1.setValue('query', this.parent.grid.getDataModule().generateQuery(true), args);
            }
            ej2_base_1.setValue('isExport', true, args);
            if (!ej2_base_1.isNullOrUndefined(property) && !ej2_base_1.isNullOrUndefined(property.exportType)) {
                ej2_base_1.setValue('exportType', property.exportType, args);
            }
            if (!this.isLocal()) {
                this.parent.parentData = [];
                this.parent.dataModule.convertToFlatData(ej2_grids_1.getObject('result', queryResult));
                ej2_base_1.setValue('expresults', this.parent.flatData, args);
            }
            this.parent.notify('dataProcessor', args);
            args = this.dataResults;
            dtSrc = ej2_base_1.isNullOrUndefined(args.result) ? this.parent.flatData.slice(0) : args.result;
            if (!this.isLocal()) {
                this.parent.flatData = [];
            }
            Iif (property && property.dataSource) {
                var flatsData = this.parent.flatData;
                var dataSrc = property.dataSource instanceof ej2_data_1.DataManager ? property.dataSource.dataSource.json : property.dataSource;
                this.parent.dataModule.convertToFlatData(dataSrc);
                dtSrc = this.parent.flatData;
                this.parent.flatData = flatsData;
            }
            property = ej2_base_1.isNullOrUndefined(property) ? Object() : property;
            property.dataSource = new ej2_data_1.DataManager({ json: dtSrc });
            if (this.parent.aggregates.length > 0) {
                property.query = args['query'];
            }
            return property;
        };
        ExcelExport.prototype.excelQueryCellInfo = function (args) {
            if (this.parent.grid.getColumnIndexByUid(args.column.uid) === this.parent.treeColumnIndex) {
                var style = {};
                var data = args.data;
                var ispadfilter = ej2_base_1.isNullOrUndefined(data.filterLevel);
                var pad = ispadfilter ? data.level : data.filterLevel;
                style.indent = pad;
                args.style = style;
            }
            this.parent.notify('updateResults', args);
            this.parent.trigger('excelQueryCellInfo', args);
        };
        ExcelExport.prototype.exportRowDataBound = function (excelRow) {
            if (excelRow.type === 'excel') {
                var excelrowobj = excelRow.rowObj.data;
                var filtercolumnlength = this.parent.grid.filterSettings.columns.length;
                var rowlength = excelRow.excelRows.length;
                var rowlevel = excelrowobj.level;
                if (excelrowobj.parentItem && utils_1.getParentData(this.parent, excelrowobj.parentItem.uniqueID, Boolean(filtercolumnlength))) {
                    var expandedStatus = false;
                    var sublevelState = false;
                    var state = utils_1.getExpandStatus(this.parent, excelrowobj, this.parent.parentData);
                    Iif (this.isCollapsedStatePersist && (!state || !this.parent.isLocalData)) {
                        expandedStatus = true;
                        sublevelState = excelrowobj.expanded ? false : true;
                    }
                    excelRow.excelRows[rowlength - 1].grouping = { outlineLevel: rowlevel, isCollapsed: sublevelState,
                        isHidden: expandedStatus };
                }
                else if (excelrowobj.hasChildRecords && ej2_base_1.isNullOrUndefined(excelrowobj.parentItem)) {
                    excelRow.excelRows[rowlength - 1].grouping = { outlineLevel: rowlevel };
                }
            }
        };
        ExcelExport.prototype.finalPageSetup = function (workbook) {
            for (var i = 0; i < workbook.worksheets.length; i++) {
                Eif (workbook.worksheets[parseInt(i.toString(), 10)].rows) {
                    workbook.worksheets[parseInt(i.toString(), 10)].pageSetup = { isSummaryRowBelow: false };
                }
            }
        };
        ExcelExport.prototype.isLocal = function () {
            return !utils_1.isRemoteData(this.parent) && utils_1.isOffline(this.parent);
        };
        return ExcelExport;
    }());
    exports.ExcelExport = ExcelExport;
});