| 1 | import { createSelector } from 'reselect';
|
|---|
| 2 | import { selectUnfilteredCartesianItems } from './axisSelectors';
|
|---|
| 3 | import { selectBarRectangles } from './barSelectors';
|
|---|
| 4 | var pickStackId = (state, stackId) => stackId;
|
|---|
| 5 | var pickIsPanorama = (state, stackId, isPanorama) => isPanorama;
|
|---|
| 6 | export var selectAllBarsInStack = createSelector([pickStackId, selectUnfilteredCartesianItems, pickIsPanorama], (stackId, allItems, isPanorama) => {
|
|---|
| 7 | return allItems.filter(i => i.type === 'bar').filter(i => i.stackId === stackId).filter(i => i.isPanorama === isPanorama).filter(i => !i.hide);
|
|---|
| 8 | });
|
|---|
| 9 | var selectAllBarIdsInStack = createSelector([selectAllBarsInStack], allBars => {
|
|---|
| 10 | return allBars.map(bar => bar.id);
|
|---|
| 11 | });
|
|---|
| 12 | /**
|
|---|
| 13 | * Takes two rectangles and returns a new rectangle that encompasses both.
|
|---|
| 14 | * It takes the minimum x and y, and the maximum width and height.
|
|---|
| 15 | * It handles overlapping rectangles, and rectangles with a gap between them.
|
|---|
| 16 | * @param rect1
|
|---|
| 17 | * @param rect2
|
|---|
| 18 | */
|
|---|
| 19 | export var expandRectangle = (rect1, rect2) => {
|
|---|
| 20 | if (!rect1) {
|
|---|
| 21 | return rect2;
|
|---|
| 22 | }
|
|---|
| 23 | if (!rect2) {
|
|---|
| 24 | return rect1;
|
|---|
| 25 | }
|
|---|
| 26 | var x = Math.min(rect1.x, rect1.x + rect1.width, rect2.x, rect2.x + rect2.width);
|
|---|
| 27 | var y = Math.min(rect1.y, rect1.y + rect1.height, rect2.y, rect2.y + rect2.height);
|
|---|
| 28 | var maxX = Math.max(rect1.x, rect1.x + rect1.width, rect2.x, rect2.x + rect2.width);
|
|---|
| 29 | var maxY = Math.max(rect1.y, rect1.y + rect1.height, rect2.y, rect2.y + rect2.height);
|
|---|
| 30 | var width = maxX - x;
|
|---|
| 31 | var height = maxY - y;
|
|---|
| 32 | return {
|
|---|
| 33 | x,
|
|---|
| 34 | y,
|
|---|
| 35 | width,
|
|---|
| 36 | height
|
|---|
| 37 | };
|
|---|
| 38 | };
|
|---|
| 39 | var combineStackRects = (state, stackId, isPanorama) => {
|
|---|
| 40 | var allBarIds = selectAllBarIdsInStack(state, stackId, isPanorama);
|
|---|
| 41 | var stackRects = [];
|
|---|
| 42 | allBarIds.forEach(barId => {
|
|---|
| 43 | var rectangles = selectBarRectangles(state, barId, isPanorama, undefined);
|
|---|
| 44 | rectangles === null || rectangles === void 0 || rectangles.forEach((rect, index) => {
|
|---|
| 45 | stackRects[index] = expandRectangle(stackRects[index], rect);
|
|---|
| 46 | });
|
|---|
| 47 | });
|
|---|
| 48 | return stackRects;
|
|---|
| 49 | };
|
|---|
| 50 | export var selectStackRects = createSelector([state => state, pickStackId, pickIsPanorama], combineStackRects); |
|---|