UNPKG

@gooddata/react-components

Version:

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

136 lines • 8.73 kB
"use strict"; 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 }); // (C) 2020 GoodData Corporation var includes = require("lodash/includes"); var PivotTable_1 = require("../../../../interfaces/PivotTable"); var Visualization_1 = require("../../../interfaces/Visualization"); var sortItemsHelpers_1 = require("./sortItemsHelpers"); var isMeasureWidthItemMatchedByFilter = function (widthItem, filter) { return filter.selectedElements.some(function (selectedElement) { return widthItem.measureColumnWidthItem.locators.some(function (locator) { return !PivotTable_1.isMeasureLocatorItem(locator) && locator.attributeLocatorItem.element === selectedElement.uri; }); }); }; var matchesMeasureColumnWidthItemFilters = function (widthItem, filters) { return filters.reduce(function (isMatching, filter) { if (Visualization_1.isAttributeFilter(filter)) { var shouldBeMatched = !filter.isInverted; return isMatching && shouldBeMatched === isMeasureWidthItemMatchedByFilter(widthItem, filter); } return isMatching; }, true); }; var matchesWidthItemFilters = function (widthItem, filters) { if (PivotTable_1.isMeasureColumnWidthItem(widthItem)) { return matchesMeasureColumnWidthItemFilters(widthItem, filters); } return true; }; var containsMeasureLocator = function (widthItem) { return widthItem.measureColumnWidthItem.locators.some(function (locator) { return PivotTable_1.isMeasureLocatorItem(locator); }); }; var widthItemLocatorsHaveProperLength = function (widthItem, measuresCount, columnAttributesCount) { var widthItemLocatorsLength = widthItem.measureColumnWidthItem.locators.length; var hasWidthItemLocators = widthItemLocatorsLength > 0; var hasMeasureLocators = measuresCount > 0 && containsMeasureLocator(widthItem); var hasNotMeasureLocators = measuresCount === 0 && !containsMeasureLocator(widthItem); var widthItemLocatorsMatchesColumnAttributesLength = widthItemLocatorsLength === columnAttributesCount + 1; var widthItemLocatorsHasLengthAsColumnAttributesLength = widthItemLocatorsLength === columnAttributesCount; var locatorsMatchesLength = hasMeasureLocators && widthItemLocatorsMatchesColumnAttributesLength; var locatorsAreEmpty = hasNotMeasureLocators && widthItemLocatorsHasLengthAsColumnAttributesLength; return hasWidthItemLocators && (locatorsMatchesLength || locatorsAreEmpty); }; function removeInvalidLocators(columnWidth, measureLocalIdentifiers, columnAttributeLocalIdentifiers) { return columnWidth.measureColumnWidthItem.locators.filter(function (locator) { // filter out invalid measure locators if (PivotTable_1.isMeasureLocatorItem(locator)) { return includes(measureLocalIdentifiers, locator.measureLocatorItem.measureIdentifier); } // filter out invalid column attribute locators return includes(columnAttributeLocalIdentifiers, locator.attributeLocatorItem.attributeIdentifier); }); } function transformToWeakMeasureColumnWidthItem(columnWidth) { if (PivotTable_1.isAbsoluteColumnWidth(columnWidth.measureColumnWidthItem.width) && columnWidth.measureColumnWidthItem.locators.length === 1 && PivotTable_1.isMeasureLocatorItem(columnWidth.measureColumnWidthItem.locators[0])) { return { measureColumnWidthItem: { width: columnWidth.measureColumnWidthItem.width, locator: columnWidth.measureColumnWidthItem.locators.filter(PivotTable_1.isMeasureLocatorItem)[0], }, }; } } // removes attribute widthItems with invalid identifiers // removes measure widthItems with invalid identifiers and invalid number of locators function adaptWidthItemsToPivotTable(originalColumnWidths, measureLocalIdentifiers, rowAttributeLocalIdentifiers, columnAttributeLocalIdentifiers, filters, firstColumnAttributeAdded) { if (!originalColumnWidths) { return originalColumnWidths; } var attributeLocalIdentifiers = rowAttributeLocalIdentifiers.concat(columnAttributeLocalIdentifiers); return originalColumnWidths.reduce(function (columnWidths, columnWidth) { if (PivotTable_1.isMeasureColumnWidthItem(columnWidth)) { var filteredMeasureColumnWidthItem = { measureColumnWidthItem: __assign({}, columnWidth.measureColumnWidthItem, { locators: removeInvalidLocators(columnWidth, measureLocalIdentifiers, columnAttributeLocalIdentifiers) }), }; if (firstColumnAttributeAdded) { var transformedWeakMeasureWidthItem = transformToWeakMeasureColumnWidthItem(columnWidth); if (transformedWeakMeasureWidthItem) { return columnWidths.concat([transformedWeakMeasureWidthItem]); } } if (matchesWidthItemFilters(filteredMeasureColumnWidthItem, filters) && widthItemLocatorsHaveProperLength(filteredMeasureColumnWidthItem, measureLocalIdentifiers.length, columnAttributeLocalIdentifiers.length)) { return columnWidths.concat([filteredMeasureColumnWidthItem]); } } else if (PivotTable_1.isAttributeColumnWidthItem(columnWidth)) { if (includes(attributeLocalIdentifiers, columnWidth.attributeColumnWidthItem.attributeIdentifier)) { return columnWidths.concat([columnWidth]); } } else if ((PivotTable_1.isAllMeasureColumnWidthItem(columnWidth) || PivotTable_1.isWeakMeasureColumnWidthItem(columnWidth)) && measureLocalIdentifiers.length > 0) { return columnWidths.concat([columnWidth]); } return columnWidths; }, []); } function adaptReferencePointWidthItemsToPivotTable(originalColumnWidths, measures, rowAttributes, columnAttributes, previousRowAttributes, previousColumnAttributes, filters) { var measureLocalIdentifiers = measures.map(function (measure) { return measure.localIdentifier; }); var rowAttributeLocalIdentifiers = rowAttributes.map(function (rowAttribute) { return rowAttribute.localIdentifier; }); var columnAttributeLocalIdentifiers = columnAttributes.map(function (columnAttribute) { return columnAttribute.localIdentifier; }); var previousRowAttributeLocalIdentifiers = previousRowAttributes.map(function (rowAttribute) { return rowAttribute.localIdentifier; }); var previousColumnAttributeLocalIdentifiers = previousColumnAttributes.map(function (columnAttribute) { return columnAttribute.localIdentifier; }); var filteredRowAttributeLocalIdentifiers = rowAttributeLocalIdentifiers.filter(function (rowAttributeLocalIdentifier) { return !previousColumnAttributeLocalIdentifiers.includes(rowAttributeLocalIdentifier); }); var filteredColumnAttributeLocalIdentifiers = columnAttributeLocalIdentifiers.filter(function (columnAttributeLocalIdentifier) { return !previousRowAttributeLocalIdentifiers.includes(columnAttributeLocalIdentifier); }); var firstColumnAttributeAdded = previousColumnAttributes.length === 0 && columnAttributes.length === 1; return adaptWidthItemsToPivotTable(originalColumnWidths, measureLocalIdentifiers, filteredRowAttributeLocalIdentifiers, filteredColumnAttributeLocalIdentifiers, filters, firstColumnAttributeAdded); } exports.adaptReferencePointWidthItemsToPivotTable = adaptReferencePointWidthItemsToPivotTable; function adaptMdObjectWidthItemsToPivotTable(originalColumnWidths, buckets) { var measureLocalIdentifiers = sortItemsHelpers_1.getMeasures(buckets).map(function (measure) { return measure.measure.localIdentifier; }); var rowAttributeLocalIdentifiers = sortItemsHelpers_1.getRows(buckets).map(function (rowAttribute) { return rowAttribute.visualizationAttribute.localIdentifier; }); var columnAttributeLocalIdentifiers = sortItemsHelpers_1.getColumns(buckets).map(function (columnAttribute) { return columnAttribute.visualizationAttribute.localIdentifier; }); return adaptWidthItemsToPivotTable(originalColumnWidths, measureLocalIdentifiers, rowAttributeLocalIdentifiers, columnAttributeLocalIdentifiers, [], false); } exports.adaptMdObjectWidthItemsToPivotTable = adaptMdObjectWidthItemsToPivotTable; //# sourceMappingURL=widthItemsHelpers.js.map