UNPKG

2 kBJavaScriptView Raw
1export default function getNextPosition({
2 context,
3 focus,
4 maxFullPanelWidth,
5 routes,
6 panels,
7 shouldGoMobile,
8 viewportWidth,
9}) {
10 let nextRoutesByContext = routes.byContext
11
12 const widths = routes.items.map(routeContext => {
13 const route = routes.byContext[routeContext]
14 const panel = panels.byId[route.panelId]
15
16 let width
17 if (route.isVisible) {
18 if (shouldGoMobile) {
19 width = viewportWidth
20 } else {
21 width = route.isExpanded ? panel.maxWidth : panel.width
22
23 const percentageMatch =
24 typeof width === 'string' && width.match(/([0-9]+)%/)
25 if (percentageMatch) {
26 width = maxFullPanelWidth * parseInt(percentageMatch, 10) / 100
27 }
28 }
29 } else {
30 width = 0
31 }
32
33 if (width !== route.width) {
34 nextRoutesByContext = {
35 ...nextRoutesByContext,
36 [routeContext]: {
37 ...route,
38 width,
39 },
40 }
41 }
42
43 return width
44 })
45
46 // get how large our focus panel is
47 const focusWidth = widths[focus] // >> 500
48 // get the focus panel's x real position in our runtime if it were flat
49 let x = widths.slice(0, focus).reduce((a, b) => a + b, 0) // >> 860
50 // get how much space we have left for context panels
51 let leftForContext = maxFullPanelWidth - focusWidth // >> 1089
52 // assess how many context panels we should try to show
53 let contextsLeft = focus - context - 1
54
55 // try to fit those context panels within that space that's left
56 while (contextsLeft >= 0 && leftForContext >= widths[contextsLeft]) {
57 // get the context's width
58 const contextWidth = widths[contextsLeft]
59 // remove it from the space left for context
60 leftForContext -= contextWidth
61 // shift x to include that panel
62 x -= contextWidth
63 // decrease the amount of contexts left
64 contextsLeft--
65 }
66
67 return {
68 routesByContext: nextRoutesByContext,
69 x,
70 width: maxFullPanelWidth + widths.reduce((a, b) => a + b, 0),
71 widths,
72 }
73}