1 | export 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 |
|
47 | const focusWidth = widths[focus]
|
48 |
|
49 | let x = widths.slice(0, focus).reduce((a, b) => a + b, 0)
|
50 |
|
51 | let leftForContext = maxFullPanelWidth - focusWidth
|
52 |
|
53 | let contextsLeft = focus - context - 1
|
54 |
|
55 |
|
56 | while (contextsLeft >= 0 && leftForContext >= widths[contextsLeft]) {
|
57 |
|
58 | const contextWidth = widths[contextsLeft]
|
59 |
|
60 | leftForContext -= contextWidth
|
61 |
|
62 | x -= contextWidth
|
63 |
|
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 | }
|