Files: b01dbbea52f7a3b9f101ce6e29d8180184110ffb / app / sync / goTo.js
1715 bytesRaw
1 | const nest = require('depnest') |
2 | |
3 | exports.gives = nest({ 'app.sync.goTo': true }) |
4 | |
5 | exports.needs = nest({ |
6 | 'app.html.tabs': 'first', |
7 | 'app.sync.locationId': 'first', |
8 | 'history.obs.store': 'first', |
9 | 'history.sync.push': 'first', |
10 | 'router.async.normalise': 'first', |
11 | 'router.async.router': 'first' |
12 | }) |
13 | |
14 | exports.create = function (api) { |
15 | return nest('app.sync.goTo', goTo) |
16 | |
17 | // TODO consider rolling single arg: |
18 | // goTo({ ...location, tmp: { openBackground, split, position } }) |
19 | // |
20 | // prune `tmp` before pushing into history |
21 | // allows a refactor of catch-keyboard-shortcut + patch-inbox |
22 | // - extracts scrollToMessage into app.page.thread |
23 | // - router.sync.router would take (location, { position }) ? |
24 | |
25 | function goTo (location, options = {}) { |
26 | const { |
27 | openBackground = false, |
28 | split = false |
29 | } = options |
30 | |
31 | const tabs = api.app.html.tabs() |
32 | |
33 | // currently do normalisation here only to generate normalised locationId |
34 | api.router.async.normalise(location, (err, loc) => { |
35 | const locationId = api.app.sync.locationId(loc) |
36 | |
37 | var page = tabs.get(locationId) |
38 | if (page) { |
39 | tabs.select(locationId) |
40 | api.history.sync.push(loc) |
41 | |
42 | return true |
43 | } |
44 | |
45 | api.router.async.router(loc, (err, page) => { |
46 | if (err) throw err |
47 | |
48 | if (!page) return |
49 | |
50 | page.id = page.id || locationId |
51 | tabs.add(page, !openBackground, split) |
52 | |
53 | if (openBackground) { |
54 | const history = api.history.obs.store() |
55 | var _history = history() |
56 | var current = _history.pop() |
57 | |
58 | history.set([ ..._history, loc, current ]) |
59 | } else { |
60 | api.history.sync.push(loc) |
61 | } |
62 | }) |
63 | }) |
64 | } |
65 | } |
66 |
Built with git-ssb-web