Files: 1c2926f12ff8e365e1d469f23180b50d50b9c749 / app / sync / goTo.js
1998 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 | if (err) throw err |
36 | const locationId = api.app.sync.locationId(loc) |
37 | |
38 | var page = tabs.get(locationId) |
39 | |
40 | if (page) { |
41 | tabs.select(locationId) |
42 | api.history.sync.push(loc) |
43 | |
44 | if (loc.action === 'quote' && page.firstChild && page.firstChild.addQuote) { |
45 | page.firstChild.addQuote(loc.value) |
46 | tabs.currentPage().scroll('last') |
47 | } else if (loc.action === 'reply') { tabs.currentPage().scroll('last') } |
48 | |
49 | return true |
50 | } |
51 | |
52 | api.router.async.router(loc, (err, page) => { |
53 | if (err) throw err |
54 | |
55 | if (!page) return |
56 | |
57 | page.id = page.id || locationId |
58 | tabs.add(page, !openBackground, split) |
59 | |
60 | if (openBackground) { |
61 | const history = api.history.obs.store() |
62 | var _history = history() |
63 | var current = _history.pop() |
64 | |
65 | history.set([ ..._history, loc, current ]) |
66 | } else { |
67 | api.history.sync.push(loc) |
68 | } |
69 | }) |
70 | }) |
71 | } |
72 | } |
73 |
Built with git-ssb-web