-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathactions.js
338 lines (294 loc) · 8.73 KB
/
actions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import getApp from './app/get'
import getContextAndFocus from './router/get-context-and-focus'
import getIndexOfPanelToShow from './runtime/get-index-of-panel-to-show'
import getNextPosition from './runtime/get-next-position'
import getRegions from './runtime/get-regions'
import normaliseUri from './utils/normalise-uri/index.js'
import parse from './router/parse'
function ensurePanelShape(panel) {
if (typeof panel.width === 'undefined') {
panel.width = 360
}
}
export const NAVIGATE = 'panels/NAVIGATE'
export function navigate(rawUri, nextFocus = 1, nextContext) {
return async function navigateThunk(dispatch, getState) {
const { apps, panels, router, runtime } = getState()
const uri = normaliseUri(rawUri)
if (uri === router.uri) {
return
}
const parsed = parse(uri, router.parsers)
const routes = {
byContext: parsed.routes.byContext,
items: parsed.routes.items,
}
const createAppContext = (appName, appModule) => {
const access = async name => {
const app = getState().apps.byName[name]
if (app) {
if (app.access(appName, appModule)) {
return app.store
} else {
throw Error(`Access to ${name} denied.`)
}
} else {
throw Error(`App ${name} doesn't exist or it doesn't have a store.`)
}
}
return {
access,
navigate: (...args) => dispatch(navigate(...args)),
routes,
}
}
// get the next apps
const nextApps = {
byName: {},
items: parsed.apps.items.filter(name => apps.items.indexOf(name) === -1),
}
if (nextApps.items.length) {
dispatch({
type: NAVIGATE,
sequence: {
type: 'start',
},
meta: {
uri,
},
})
}
await Promise.all(
nextApps.items.map(async name => {
try {
// otherwise fetch it! :)
nextApps.byName[name] = await getApp(name, createAppContext)
} catch (error) {
// TODO
console.error(`Can't load app ${name}`, error)
}
})
)
const nextPanels = {
byId: {},
items: [],
}
// we still need to go through all the apps
parsed.apps.items.forEach(name => {
// get the list of panels to load
const panelsToLoad = parsed.apps.byName[name].panels
// get the app
const app = nextApps.byName[name] || apps.byName[name]
panelsToLoad.forEach(path => {
const panelId = `${name}${path}`
// exit early if the panel is already loaded
if (panels.byId[panelId]) {
return
}
try {
// find the panel within the app
let { panel, props } = app.findPanel(path)
if (typeof panel === 'function') {
panel = panel(props, app.store)
} else {
panel = {
...panel,
props: props,
}
}
// ensure that the panel has a valid shape and defaults
ensurePanelShape(panel)
nextPanels.byId[panelId] = panel
nextPanels.items.push(panelId)
} catch (error) {
// TODO
console.error(`Can't load panel ${panelId}`, error)
}
})
})
const maxFullPanelWidth = runtime.viewportWidth - runtime.snapPoint
const isFirstLoad = typeof router.focus === 'undefined'
const last = parsed.routes.items.length - 1
// determine the context and focus panel
const opts = {
currentFocus: router.focus,
next: {
context: nextContext,
focus: nextFocus,
},
uri,
last,
}
if (isFirstLoad) {
const focusRoute = parsed.routes.byContext[parsed.routes.items[last]]
const focusPanel = nextPanels.byId[focusRoute.panelId]
opts.currentFocus = last
opts.next.focus = 0
if (typeof focusPanel.context !== 'undefined') {
opts.next.context = focusPanel.context
}
}
const { context, focus } = getContextAndFocus(opts)
const widths = routes.items.map(routeContext => {
// if (routes.byContext[routeContext]) {
// return routes.byContext[routeContext].width
// }
const route = routes.byContext[routeContext]
const panel = panels.byId[route.panelId] || nextPanels.byId[route.panelId]
let width
if (route.isVisible) {
if (runtime.shouldGoMobile) {
width = runtime.viewportWidth
} else {
const prevRoute = router.routes.byContext[routeContext]
width = route.isExpanded
? panel.maxWidth
: (prevRoute && prevRoute.width) || panel.width
const percentageMatch =
typeof width === 'string' && width.match(/([0-9]+)%/)
if (percentageMatch) {
width = maxFullPanelWidth * parseInt(percentageMatch, 10) / 100
}
}
} else {
width = 0
}
route.width = width
return width
})
// get how large our focus panel is
const focusWidth = widths[focus] // >> 500
// get the focus panel's x real position in our runtime if it were flat
let x = widths.slice(0, focus).reduce((a, b) => a + b, 0) // >> 860
// get how much space we have left for context panels
let leftForContext = maxFullPanelWidth - focusWidth // >> 1089
// assess how many context panels we should try to show
let contextsLeft = focus - context
// try to fit those context panels within that space that's left
while (contextsLeft > 0) {
// decrease the amount of contexts left
contextsLeft--
// get the context's width
const contextWidth = widths[contextsLeft]
// check if we have space left for this panel to be a visible context panel
if (leftForContext < contextWidth) {
break
}
// if we do, remove it from the space left for context
leftForContext -= contextWidth
// shift x to include that panel
x -= contextWidth
}
const regions = getRegions(widths)
const snappedAt = getIndexOfPanelToShow(x, regions)
dispatch({
type: NAVIGATE,
sequence: {
type: 'next',
},
payload: {
apps: nextApps,
panels: nextPanels,
router: {
context,
focus,
routes,
uri,
},
runtime: {
maxFullPanelWidth,
regions,
snappedAt,
width: maxFullPanelWidth + widths.reduce((a, b) => a + b, 0),
widths,
x,
},
},
meta: {
uri,
},
})
}
}
export const TOGGLE_EXPAND = 'panels/panels/TOGGLE_EXPAND'
export function toggleExpand(routeContext) {
return function toggleExpandThunk(dispatch, getState) {
const { panels, router, runtime } = getState()
const routes = router.routes
const route = routes.byContext[routeContext]
routes.byContext = {
...routes.byContext,
[routeContext]: {
...route,
isExpanded: !route.isExpanded,
},
}
const nextPosition = getNextPosition({
// snap at the expanded position!
context: router.context, // routeIndex - runtime.snappedAt,
focus: router.focus, // routeIndex,
maxFullPanelWidth: runtime.maxFullPanelWidth,
routes,
panels,
shouldGoMobile: runtime.shouldGoMobile,
viewportWidth: runtime.viewportWidth,
})
dispatch({
type: TOGGLE_EXPAND,
payload: nextPosition,
})
}
}
export const UPDATE_SETTINGS = 'panels/panels/UPDATE_SETTINGS'
export function updateSettings(
routeContext,
{ maxWidth, title, styleBackground, width }
) {
return function updateSettingsThunk(dispatch, getState) {
const { panels, router, runtime } = getState()
const nextPanels = {
byId: panels.byId,
items: panels.items,
}
const route = router.routes.byContext[routeContext]
if (!route.isVisible) return
const panel = {
...nextPanels.byId[route.panelId],
}
if (maxWidth) {
panel.maxWidth = maxWidth
}
if (title) {
panel.title = title
}
if (styleBackground) {
panel.styleBackground = styleBackground
}
if (width) {
panel.width = width
}
nextPanels.byId = {
...nextPanels.byId,
[route.panelId]: panel,
}
let nextPosition
if (maxWidth || width) {
nextPosition = getNextPosition({
context: router.context,
focus: router.focus,
maxFullPanelWidth: runtime.maxFullPanelWidth,
routes: router.routes,
panels: nextPanels,
shouldGoMobile: runtime.shouldGoMobile,
viewportWidth: runtime.viewportWidth,
})
}
dispatch({
type: UPDATE_SETTINGS,
payload: {
nextPanelsById: nextPanels.byId,
nextPosition,
},
})
}
}