UNPKG

@gooddata/react-components

Version:

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

152 lines 6.2 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) 2007-2020 GoodData Corporation var get = require("lodash/get"); var dimensions_1 = require("../constants/dimensions"); var sort_1 = require("../constants/sort"); var STACK_BY_DIMENSION = 0; var VIEW_BY_DIMENSION = 1; function getMeasureSortItems(identifier, direction) { return [ { measureSortItem: { direction: direction, locators: [ { measureLocatorItem: { measureIdentifier: identifier, }, }, ], }, }, ]; } function getAttributeSortItems(identifier, direction, aggregation) { if (!identifier) { return []; } var attributeSortItem = { attributeSortItem: __assign({ attributeIdentifier: identifier, direction: direction }, (aggregation ? { aggregation: "sum" } : {})), }; return [attributeSortItem]; } function getAllMeasuresSorts(afm) { return (afm.measures || []).reduce(function (sortItems, measure) { return sortItems.concat(getMeasureSortItems(measure.localIdentifier, sort_1.DESC)); }, []); } function ignoreMeasureGroup(item) { return item !== dimensions_1.MEASUREGROUP; } function getDimensionItems(resultSpec, dimensionIndex) { var dimensionItems = get(resultSpec, ["dimensions", dimensionIndex, "itemIdentifiers"], []); return dimensionItems.filter(ignoreMeasureGroup) || []; } function getFirstAttributeIdentifier(resultSpec, dimensionIndex) { var dimensionItems = getDimensionItems(resultSpec, dimensionIndex); return dimensionItems[0] || null; } exports.getFirstAttributeIdentifier = getFirstAttributeIdentifier; function getAttributeSortItem(identifier, direction, aggregation) { if (direction === void 0) { direction = sort_1.ASC; } if (aggregation === void 0) { aggregation = false; } var attributeSortItemWithoutAggregation = { attributeIdentifier: identifier, direction: direction, }; var attributeSortItem = { attributeSortItem: aggregation ? __assign({}, attributeSortItemWithoutAggregation, { aggregation: "sum" }) : attributeSortItemWithoutAggregation, }; return attributeSortItem; } exports.getAttributeSortItem = getAttributeSortItem; function getFirstMeasureSort(afm) { var measure = get(afm, "measures.0"); if (measure) { return getMeasureSortItems(measure.localIdentifier, sort_1.DESC); } return []; } exports.getFirstMeasureSort = getFirstMeasureSort; function getDefaultAttributeGeoPushpinSort(afm) { var attributes = afm.attributes; // sort by second attribute (1st: location, 2nd: segmentBy, 3rd: tooltipText) if (attributes.length > 1) { return getAttributeSortItems(attributes[1].localIdentifier, sort_1.ASC); } return []; } exports.getDefaultAttributeGeoPushpinSort = getDefaultAttributeGeoPushpinSort; function getDefaultHeatmapSort(resultSpec) { if (!resultSpec) { return []; } var sorts = resultSpec.sorts; if (sorts) { return sorts; } var dimensions = resultSpec.dimensions; if (dimensions && dimensions.length) { var rowsAttributeIdentifier = dimensions[0].itemIdentifiers[0]; return ignoreMeasureGroup(rowsAttributeIdentifier) ? getAttributeSortItems(rowsAttributeIdentifier, sort_1.DESC) : []; } return []; } exports.getDefaultHeatmapSort = getDefaultHeatmapSort; function getDefaultBarChartSort(afm, resultSpec, canSortStackTotalValue, enableSortingByTotalGroup) { if (canSortStackTotalValue === void 0) { canSortStackTotalValue = false; } if (enableSortingByTotalGroup === void 0) { enableSortingByTotalGroup = false; } var measureItemsCount = get(afm, "measures", []).length; var viewByDimensionItems = getDimensionItems(resultSpec, VIEW_BY_DIMENSION); var viewByAttributeIdentifier = getFirstAttributeIdentifier(resultSpec, VIEW_BY_DIMENSION); var stackByAttributeIdentifier = getFirstAttributeIdentifier(resultSpec, STACK_BY_DIMENSION); var shouldSortByFirstViewByAttribute = (viewByAttributeIdentifier && stackByAttributeIdentifier) || canSortStackTotalValue; if (enableSortingByTotalGroup) { if (viewByDimensionItems.length === 2) { if (measureItemsCount >= 2 && !canSortStackTotalValue) { return [ getAttributeSortItem(viewByDimensionItems[0], sort_1.DESC, true) ].concat(getFirstMeasureSort(afm)); } return [ getAttributeSortItem(viewByDimensionItems[0], sort_1.DESC, true), getAttributeSortItem(viewByDimensionItems[1], sort_1.DESC, true), ]; } shouldSortByFirstViewByAttribute = viewByAttributeIdentifier && (stackByAttributeIdentifier || canSortStackTotalValue); } if (shouldSortByFirstViewByAttribute) { return [getAttributeSortItem(viewByAttributeIdentifier, sort_1.DESC, true)]; } if (!stackByAttributeIdentifier) { return getFirstMeasureSort(afm); } return []; } exports.getDefaultBarChartSort = getDefaultBarChartSort; function getDefaultTreemapSort(afm, resultSpec) { var viewByAttributeIdentifier = get(resultSpec, "dimensions[0].itemIdentifiers[0]"); var stackByAttributeIdentifier = get(resultSpec, "dimensions[0].itemIdentifiers[1]"); if (viewByAttributeIdentifier && stackByAttributeIdentifier) { return getAttributeSortItems(viewByAttributeIdentifier, sort_1.ASC, false).concat(getAllMeasuresSorts(afm)); } return []; } exports.getDefaultTreemapSort = getDefaultTreemapSort; //# sourceMappingURL=sorts.js.map