all files / grid/actions/ export-helper.js

22.73% Statements 40/176
0.84% Branches 1/119
33.33% Functions 8/24
22.73% Lines 40/176
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   12× 12× 12×                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
define(["require", "exports", "./../models/row", "../base/enum", "@syncfusion/ej2-base", "../models/cell", "./../services/value-formatter", "@syncfusion/ej2-data", "../base/util"], function (require, exports, row_1, enum_1, ej2_base_1, cell_1, value_formatter_1, ej2_data_1, util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var ExportHelper = (function () {
        function ExportHelper(parent) {
            this.hideColumnInclude = false;
            this.foreignKeyData = {};
            this.parent = parent;
        }
        ExportHelper.getQuery = function (parent, data) {
            return data.isRemote() ?
                data.generateQuery(true).requiresCount().take(parent.pageSettings.totalRecordsCount) :
                data.generateQuery(true).requiresCount();
        };
        ExportHelper.prototype.getFData = function (value, column) {
            var foreignKeyData = util_1.getForeignData(column, {}, value, this.foreignKeyData[column.field])[0];
            return foreignKeyData;
        };
        ExportHelper.prototype.getColumnData = function (gridObj) {
            var _this = this;
            var columnPromise = [];
            var promise;
            var fColumns = gridObj.getForeignKeyColumns();
            Eif (fColumns.length) {
                fColumns.forEach(function (col) {
                    columnPromise.push(col.dataSource.executeQuery(new ej2_data_1.Query()));
                });
                promise = Promise.all(columnPromise).then(function (e) {
                    fColumns.forEach(function (col, index) {
                        _this.foreignKeyData[col.field] = e[index].result;
                    });
                });
            }
            return promise;
        };
        ExportHelper.prototype.getHeaders = function (column, isHideColumnInclude) {
            if (isHideColumnInclude) {
                this.hideColumnInclude = true;
            }
            else {
                this.hideColumnInclude = false;
            }
            var cols = column;
            this.colDepth = this.measureColumnDepth(cols);
            var rows = [];
            var actualColumns = [];
            for (var i = 0; i < this.colDepth; i++) {
                rows[i] = new row_1.Row({});
                rows[i].cells = [];
            }
            rows = this.processColumns(rows);
            rows = this.processHeaderCells(rows);
            for (var _i = 0, rows_1 = rows; _i < rows_1.length; _i++) {
                var row = rows_1[_i];
                for (var i = 0; i < row.cells.length; i++) {
                    var cell = row.cells[i];
                    if (cell.visible === undefined && cell.cellType !== enum_1.CellType.StackedHeader) {
                        row.cells = this.removeCellFromRow(row.cells, i);
                        i = i - 1;
                    }
                    if ((!isHideColumnInclude) && cell.visible !== undefined && (!cell.visible)) {
                        row.cells = this.removeCellFromRow(row.cells, i);
                        i = i - 1;
                    }
                }
            }
            for (var i = 0; i < cols.length; i++) {
                this.generateActualColumns(cols[i], actualColumns);
            }
            return { rows: rows, columns: actualColumns };
        };
        ExportHelper.prototype.getConvertedWidth = function (input) {
            var value = parseFloat(input);
            return (input.indexOf('%') !== -1) ? (this.parent.element.getBoundingClientRect().width * value / 100) : value;
        };
        ExportHelper.prototype.generateActualColumns = function (column, actualColumns) {
            if (column.commands) {
                return;
            }
            if (!column.columns) {
                if (column.visible || this.hideColumnInclude) {
                    actualColumns.push(column);
                }
            }
            else {
                if (column.visible || this.hideColumnInclude) {
                    var colSpan = this.getCellCount(column, 0);
                    if (colSpan !== 0) {
                        for (var i = 0; i < column.columns.length; i++) {
                            this.generateActualColumns(column.columns[i], actualColumns);
                        }
                    }
                }
            }
        };
        ExportHelper.prototype.removeCellFromRow = function (cells, cellIndex) {
            var resultCells = [];
            for (var i = 0; i < cellIndex; i++) {
                resultCells.push(cells[i]);
            }
            for (var i = (cellIndex + 1); i < cells.length; i++) {
                resultCells.push(cells[i]);
            }
            return resultCells;
        };
        ExportHelper.prototype.processHeaderCells = function (rows) {
            var columns = this.parent.enableColumnVirtualization ? this.parent.getColumns() : this.parent.columns;
            for (var i = 0; i < columns.length; i++) {
                if (!columns[i].commands) {
                    rows = this.appendGridCells(columns[i], rows, 0, i === 0, false, i === (columns.length - 1));
                }
            }
            return rows;
        };
        ExportHelper.prototype.appendGridCells = function (cols, gridRows, index, isFirstObj, isFirstColumn, isLastColumn) {
            var lastCol = isLastColumn ? 'e-lastcell' : '';
            if (!cols.columns) {
                gridRows[index].cells.push(this.generateCell(cols, enum_1.CellType.Header, this.colDepth - index, (isFirstObj ? '' : (isFirstColumn ? 'e-firstcell' : '')) + lastCol, index, this.parent.getColumnIndexByUid(cols.uid)));
            }
            else {
                var colSpan = this.getCellCount(cols, 0);
                if (colSpan) {
                    gridRows[index].cells.push(new cell_1.Cell({
                        cellType: enum_1.CellType.StackedHeader, column: cols, colSpan: colSpan
                    }));
                }
                var isFirstCell = void 0;
                var isIgnoreFirstCell = void 0;
                for (var i = 0, len = cols.columns.length; i < len; i++) {
                    isFirstCell = false;
                    if (cols.columns[i].visible && !isIgnoreFirstCell) {
                        isFirstCell = true;
                        isIgnoreFirstCell = true;
                    }
                    gridRows = this.appendGridCells(cols.columns[i], gridRows, index + 1, isFirstObj, i === 0, i === (len - 1) && isLastColumn);
                }
            }
            return gridRows;
        };
        ExportHelper.prototype.generateCell = function (gridColumn, cellType, rowSpan, className, rowIndex, columnIndex) {
            var option = {
                'visible': gridColumn.visible,
                'isDataCell': false,
                'isTemplate': !ej2_base_1.isNullOrUndefined(gridColumn.headerTemplate),
                'rowID': '',
                'column': gridColumn,
                'cellType': cellType,
                'rowSpan': rowSpan,
                'className': className,
                'index': rowIndex,
                'colIndex': columnIndex
            };
            if (!option.rowSpan || option.rowSpan < 2) {
                delete option.rowSpan;
            }
            return new cell_1.Cell(option);
        };
        ExportHelper.prototype.processColumns = function (rows) {
            var gridObj = this.parent;
            var columnIndexes = this.parent.getColumnIndexesInView();
            for (var i = 0, len = rows.length; i < len; i++) {
                if (gridObj.allowGrouping) {
                    for (var j = 0, len_1 = gridObj.groupSettings.columns.length; j < len_1; j++) {
                        if (this.parent.enableColumnVirtualization && columnIndexes.indexOf(j) === -1) {
                            continue;
                        }
                        rows[i].cells.push(this.generateCell({}, enum_1.CellType.HeaderIndent));
                    }
                }
                if (gridObj.detailTemplate || gridObj.childGrid) {
                    rows[i].cells.push(this.generateCell({}, enum_1.CellType.DetailHeader));
                }
            }
            return rows;
        };
        ExportHelper.prototype.getCellCount = function (column, count) {
            if (column.columns) {
                for (var i = 0; i < column.columns.length; i++) {
                    count = this.getCellCount(column.columns[i], count);
                }
            }
            else {
                if (column.visible || this.hideColumnInclude) {
                    count++;
                }
            }
            return count;
        };
        ExportHelper.prototype.measureColumnDepth = function (column) {
            var max = 0;
            for (var i = 0; i < column.length; i++) {
                var depth = this.checkDepth(column[i], 0);
                if (max < depth) {
                    max = depth;
                }
            }
            return max + 1;
        };
        ExportHelper.prototype.checkDepth = function (col, index) {
            var max = index;
            var indices = [];
            if (col.columns) {
                index++;
                for (var i = 0; i < col.columns.length; i++) {
                    indices[i] = this.checkDepth(col.columns[i], index);
                }
                for (var j = 0; j < indices.length; j++) {
                    if (max < indices[j]) {
                        max = indices[j];
                    }
                }
                index = max;
            }
            return index;
        };
        ;
        return ExportHelper;
    }());
    exports.ExportHelper = ExportHelper;
    var ExportValueFormatter = (function () {
        function ExportValueFormatter(culture) {
            this.valueFormatter = new value_formatter_1.ValueFormatter(culture);
            this.internationalization = new ej2_base_1.Internationalization(culture);
        }
        ExportValueFormatter.prototype.returnFormattedValue = function (args, customFormat) {
            if (!ej2_base_1.isNullOrUndefined(args.value) && args.value) {
                return this.valueFormatter.getFormatFunction(customFormat)(args.value);
            }
            else {
                return '';
            }
        };
        ExportValueFormatter.prototype.formatCellValue = function (args) {
            if (args.isForeignKey) {
                args.value = ej2_base_1.getValue(args.column.foreignKeyValue, util_1.getForeignData(args.column, {}, args.value)[0]);
            }
            if (args.column.type === 'number' && args.column.format !== undefined && args.column.format !== '') {
                return args.value ? this.internationalization.getNumberFormat({ format: args.column.format })(args.value) : '';
            }
            else if (args.column.type === 'boolean') {
                return args.value ? 'true' : 'false';
            }
            else if ((args.column.type === 'date' || args.column.type === 'datetime' || args.column.type === 'time') && args.column.format !== undefined) {
                if (typeof args.column.format === 'string') {
                    var format = void 0;
                    if (args.column.type === 'date') {
                        format = { type: 'date', skeleton: args.column.format };
                    }
                    else if (args.column.type === 'time') {
                        format = { type: 'time', skeleton: args.column.format };
                    }
                    else {
                        format = { type: 'dateTime', skeleton: args.column.format };
                    }
                    return this.returnFormattedValue(args, format);
                }
                else {
                    if (args.column.format instanceof Object && args.column.format.type === undefined) {
                        return (args.value.toString());
                    }
                    else {
                        var customFormat = void 0;
                        if (args.column.type === 'date') {
                            customFormat = { type: args.column.format.type, format: args.column.format.format, skeleton: args.column.format.skeleton };
                        }
                        else if (args.column.type === 'time') {
                            customFormat = { type: 'time', format: args.column.format.format, skeleton: args.column.format.skeleton };
                        }
                        else {
                            customFormat = { type: 'dateTime', format: args.column.format.format, skeleton: args.column.format.skeleton };
                        }
                        return this.returnFormattedValue(args, customFormat);
                    }
                }
            }
            else {
                if ((!ej2_base_1.isNullOrUndefined(args.column.type) && !ej2_base_1.isNullOrUndefined(args.value)) || !ej2_base_1.isNullOrUndefined(args.value)) {
                    return (args.value).toString();
                }
                else {
                    return '';
                }
            }
        };
        return ExportValueFormatter;
    }());
    exports.ExportValueFormatter = ExportValueFormatter;
});