@gooddata/react-components
Version:
GoodData.UI - A powerful JavaScript library for building analytical applications
135 lines • 7.34 kB
JavaScript
;
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 flatMap = require("lodash/flatMap");
var typings_1 = require("@gooddata/typings");
var BucketNames = require("../../../../constants/bucketNames");
var Visualization_1 = require("../../../interfaces/Visualization");
// removes attribute sortItems with invalid identifiers
// removes measure sortItems with invalid identifiers and invalid number of locators
function adaptSortItemsToPivotTable(originalSortItems, measureLocalIdentifiers, rowAttributeLocalIdentifiers, columnAttributeLocalIdentifiers) {
var attributeLocalIdentifiers = rowAttributeLocalIdentifiers.concat(columnAttributeLocalIdentifiers);
var changedSortItems = [];
for (var _i = 0, originalSortItems_1 = originalSortItems; _i < originalSortItems_1.length; _i++) {
var sortItem = originalSortItems_1[_i];
if (typings_1.AFM.isMeasureSortItem(sortItem)) {
// filter out invalid locators
var filteredSortItem = {
measureSortItem: __assign({}, sortItem.measureSortItem, { locators: sortItem.measureSortItem.locators.filter(function (locator) {
// filter out invalid measure locators
if (typings_1.AFM.isMeasureLocatorItem(locator)) {
return includes(measureLocalIdentifiers, locator.measureLocatorItem.measureIdentifier);
}
// filter out invalid column attribute locators
return includes(columnAttributeLocalIdentifiers, locator.attributeLocatorItem.attributeIdentifier);
}) }),
};
var hasMeasureLocatorItem = filteredSortItem.measureSortItem.locators.some(function (locator) {
return typings_1.AFM.isMeasureLocatorItem(locator);
});
var hasCorrectLength = filteredSortItem.measureSortItem.locators.length ===
columnAttributeLocalIdentifiers.length + 1;
// keep sortItem if measureLocator is present and locators are correct length
if (hasMeasureLocatorItem && hasCorrectLength) {
changedSortItems.push(filteredSortItem);
}
}
if (typings_1.AFM.isAttributeSortItem(sortItem) &&
includes(attributeLocalIdentifiers, sortItem.attributeSortItem.attributeIdentifier)) {
changedSortItems.push(sortItem);
}
}
return changedSortItems;
}
function adaptReferencePointSortItemsToPivotTable(originalSortItems, measures, rowAttributes, columnAttributes) {
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; });
return adaptSortItemsToPivotTable(originalSortItems, measureLocalIdentifiers, rowAttributeLocalIdentifiers, columnAttributeLocalIdentifiers);
}
exports.adaptReferencePointSortItemsToPivotTable = adaptReferencePointSortItemsToPivotTable;
var bucketItemGetter = function (bucketId) { return function (buckets) { return flatMap(buckets.filter(function (b) { return b.localIdentifier === bucketId; }), function (i) { return i.items; }); }; };
exports.getMeasures = bucketItemGetter(BucketNames.MEASURES);
exports.getRows = bucketItemGetter(BucketNames.ATTRIBUTE);
exports.getColumns = bucketItemGetter(BucketNames.COLUMNS);
function adaptMdObjectSortItemsToPivotTable(originalSortItems, buckets) {
var measureLocalIdentifiers = exports.getMeasures(buckets).map(function (measure) { return measure.measure.localIdentifier; });
var rowAttributeLocalIdentifiers = exports.getRows(buckets).map(function (rowAttribute) { return rowAttribute.visualizationAttribute.localIdentifier; });
var columnAttributeLocalIdentifiers = exports.getColumns(buckets).map(function (columnAttribute) { return columnAttribute.visualizationAttribute.localIdentifier; });
return adaptSortItemsToPivotTable(originalSortItems, measureLocalIdentifiers, rowAttributeLocalIdentifiers, columnAttributeLocalIdentifiers);
}
exports.adaptMdObjectSortItemsToPivotTable = adaptMdObjectSortItemsToPivotTable;
var isMeasureSortItemMatchedByFilter = function (sortItem, filter) {
return filter.selectedElements.some(function (selectedElement) {
return sortItem.measureSortItem.locators.some(function (locator) {
return !typings_1.AFM.isMeasureLocatorItem(locator) &&
locator.attributeLocatorItem.element === selectedElement.uri;
});
});
};
var isMeasureSortItemVisible = function (sortItem, filters) {
return filters.reduce(function (isVisible, filter) {
if (Visualization_1.isAttributeFilter(filter)) {
var shouldBeMatched = !filter.isInverted;
return isVisible && shouldBeMatched === isMeasureSortItemMatchedByFilter(sortItem, filter);
}
return isVisible;
}, true);
};
exports.isSortItemVisible = function (sortItem, filters) {
return typings_1.AFM.isAttributeSortItem(sortItem) || isMeasureSortItemVisible(sortItem, filters);
};
function addDefaultSort(sortItems, filters, rowAttributes, previousRowAttributes) {
// cannot construct default sort without a row
if (rowAttributes.length < 1) {
return sortItems;
}
// detect custom sort
var firstRow = rowAttributes[0];
var previousFirstRow = previousRowAttributes && previousRowAttributes[0];
var hasVisibleCustomSort = sortItems.some(function (sortItem) {
if (!exports.isSortItemVisible(sortItem, filters)) {
return false;
}
// non attribute sort is definitely custom
if (!typings_1.AFM.isAttributeSortItem(sortItem)) {
return true;
}
// asc sort on first row is considered default
if (sortItem.attributeSortItem.attributeIdentifier === firstRow.localIdentifier &&
sortItem.attributeSortItem.direction === "asc") {
return false;
}
// asc sort on row that was first until now is considered default as well
if (previousFirstRow &&
sortItem.attributeSortItem.attributeIdentifier === previousFirstRow.localIdentifier &&
sortItem.attributeSortItem.direction === "asc") {
return false;
}
return true;
});
return hasVisibleCustomSort
? sortItems
: [
{
attributeSortItem: {
attributeIdentifier: firstRow.localIdentifier,
direction: "asc",
},
},
];
}
exports.addDefaultSort = addDefaultSort;
//# sourceMappingURL=sortItemsHelpers.js.map