Files: 3d3edbbfdd8ca30c4cc7695192b3883fb399ab26 / app / sync / goTo.js
1893 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, location) => { |
35 | const locationId = api.app.sync.locationId(location) |
36 | |
37 | var page = tabs.get(locationId) |
38 | if (page) { |
39 | tabs.select(locationId) |
40 | if (page && page.firstChild && page.firstChild.scrollDownToMessage) { |
41 | page.firstChild.scrollDownToMessage(location.key) |
42 | } |
43 | api.history.sync.push(location) |
44 | |
45 | return true |
46 | } |
47 | |
48 | api.router.async.router(location, (err, page) => { |
49 | if (err) throw err |
50 | |
51 | if (!page) return |
52 | |
53 | page.id = page.id || locationId |
54 | tabs.add(page, !openBackground, split) |
55 | |
56 | if (openBackground) { |
57 | const history = api.history.obs.store() |
58 | var _history = history() |
59 | var current = _history.pop() |
60 | |
61 | history.set([ ..._history, location, current ]) |
62 | } else { |
63 | api.history.sync.push(location) |
64 | } |
65 | }) |
66 | }) |
67 | } |
68 | } |
69 |
Built with git-ssb-web