UNPKG

@gooddata/react-components

Version:

GoodData.UI - A powerful JavaScript library for building analytical applications

109 lines 5.26 kB
"use strict"; // (C) 2007-2019 GoodData Corporation var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var typings_1 = require("@gooddata/typings"); var agGridUtils_1 = require("./agGridUtils"); var agGridConst_1 = require("./agGridConst"); var invariant = require("invariant"); var zipObject = require("lodash/zipObject"); /* * All code related to transforming data from our backend to ag-grid structures */ var getSubtotalLabelCellIndex = function (resultHeaderItems, rowIndex) { return resultHeaderItems.findIndex(function (headerItem) { return typings_1.Execution.isTotalHeaderItem(headerItem[rowIndex]); }); }; var getCell = function (rowHeaderData, rowIndex, rowHeader, rowHeaderIndex, intl) { var rowHeaderDataItem = rowHeaderData[rowHeaderIndex][rowIndex]; var cell = { field: rowHeader.field, rowHeaderDataItem: rowHeaderDataItem, isSubtotal: false, }; if (typings_1.Execution.isAttributeHeaderItem(rowHeaderDataItem)) { return __assign({}, cell, { value: rowHeaderDataItem.attributeHeaderItem.name }); } if (typings_1.Execution.isTotalHeaderItem(rowHeaderDataItem)) { var totalName = rowHeaderDataItem.totalHeaderItem.name; return __assign({}, cell, { isSubtotal: true, value: getSubtotalLabelCellIndex(rowHeaderData, rowIndex) === rowHeaderIndex ? intl.formatMessage({ id: "visualizations.totals.dropdown.title." + totalName }) : null }); } invariant(rowHeaderDataItem, "row header is not of type IResultAttributeHeaderItem or IResultTotalHeaderItem"); }; exports.getRow = function (cellData, rowIndex, columnFields, rowHeaders, rowHeaderData, subtotalStyles, intl) { var row = { headerItemMap: {}, }; rowHeaders.forEach(function (rowHeader, rowHeaderIndex) { var _a = getCell(rowHeaderData, rowIndex, rowHeader, rowHeaderIndex, intl), isSubtotal = _a.isSubtotal, field = _a.field, value = _a.value, rowHeaderDataItem = _a.rowHeaderDataItem; if (isSubtotal) { row.type = agGridConst_1.ROW_SUBTOTAL; if (!row.subtotalStyle) { row.subtotalStyle = subtotalStyles[rowHeaderIndex]; } } row[field] = value; row.headerItemMap[field] = rowHeaderDataItem; }); cellData.forEach(function (cell, cellIndex) { var field = columnFields[cellIndex]; if (field) { row[field] = cell; } }); return row; }; exports.getRowTotals = function (totals, columnKeys, headers, resultSpec, measureIds, intl) { if (!totals) { return null; } return totals[0].map(function (totalRow, totalIndex) { var _a; var attributeKeys = []; var measureKeys = []; // assort keys by type columnKeys.forEach(function (key) { var currentKey = key.split(agGridConst_1.FIELD_SEPARATOR).pop(); var fieldType = currentKey.split(agGridConst_1.ID_SEPARATOR)[0]; if (fieldType === agGridConst_1.FIELD_TYPE_ATTRIBUTE) { attributeKeys.push(currentKey); } if (fieldType === agGridConst_1.FIELD_TYPE_MEASURE) { measureKeys.push(key); } }); var totalAttributeKey = attributeKeys[0]; var totalAttributeId = totalAttributeKey.split(agGridConst_1.ID_SEPARATOR).pop(); var totalHeader = headers.find(function (header) { return typings_1.Execution.isAttributeHeader(header) && agGridUtils_1.getIdsFromUri(header.attributeHeader.uri)[0] === totalAttributeId; }); invariant(totalHeader, "Could not find header for " + totalAttributeKey); var measureCells = zipObject(measureKeys, totalRow); var grandTotalName = totalHeader.attributeHeader.totalItems[totalIndex].totalHeaderItem.name; var grandTotalAttributeIdentifier = totalHeader.attributeHeader.localIdentifier; // create measure ids in the form of "m_index" for measures having the current type of grand total // this makes it easier to match against in the cell renderer var rowTotalActiveMeasures = resultSpec.dimensions[0].totals .filter(function (t) { return t.type === grandTotalName && t.attributeIdentifier === grandTotalAttributeIdentifier; }) .map(function (t) { return "m_" + measureIds.indexOf(t.measureIdentifier); }); return __assign({ colSpan: { count: attributeKeys.length, headerKey: totalAttributeKey, } }, measureCells, (_a = {}, _a[totalAttributeKey] = intl.formatMessage({ id: "visualizations.totals.dropdown.title." + grandTotalName, }), _a.rowTotalActiveMeasures = rowTotalActiveMeasures, _a.type = agGridConst_1.ROW_TOTAL, _a)); }); }; //# sourceMappingURL=agGridData.js.map