Files: c4c384106bcf50acf4c0fcb3f7b842ad68f25bcb / app / html / tabs.js
2414 bytesRaw
1 | const nest = require('depnest') |
2 | const { h } = require('mutant') |
3 | const Tabs = require('hypertabs') |
4 | |
5 | exports.gives = nest({ |
6 | 'app.html.tabs': true |
7 | }) |
8 | |
9 | exports.needs = nest({ |
10 | 'app.html.connection': 'first', |
11 | 'app.html.menu': 'first', |
12 | 'app.html.searchBar': 'first', |
13 | 'app.sync.goTo': 'first', |
14 | 'app.sync.locationId': 'first', |
15 | 'history.obs.store': 'first', |
16 | 'history.sync.push': 'first' |
17 | }) |
18 | |
19 | exports.create = function (api) { |
20 | var _tabs |
21 | |
22 | return nest({ |
23 | 'app.html.tabs': tabs |
24 | }) |
25 | |
26 | function tabs ({ initial = [], prepend, append } = {}) { |
27 | if (_tabs) return _tabs |
28 | |
29 | const onSelect = (indexes) => { |
30 | const { id } = _tabs.get(indexes[0]).content |
31 | |
32 | try { |
33 | var location = JSON.parse(id) |
34 | } catch (e) { |
35 | throw new Error('app.html.tabs expects all page ids to be stringified location objects') |
36 | } |
37 | |
38 | api.history.sync.push(location) |
39 | search.input.value = buildSearchBarTermFromLocation(location) |
40 | } |
41 | const onClose = (page) => { |
42 | var history = api.history.obs.store() |
43 | const prunedHistory = history().filter(loc => { |
44 | return api.app.sync.locationId(loc) !== page.id |
45 | }) |
46 | history.set(prunedHistory) |
47 | } |
48 | |
49 | const search = api.app.html.searchBar() |
50 | if (append === undefined) { |
51 | append = h('div.navExtra', [ |
52 | search, |
53 | api.app.html.connection(), |
54 | api.app.html.menu() |
55 | ]) |
56 | } |
57 | |
58 | _tabs = Tabs({ onSelect, onClose, prepend, append }) |
59 | _tabs.currentPage = () => { |
60 | const currentPage = _tabs.get(_tabs.selected[0]) |
61 | return currentPage && currentPage.firstChild |
62 | } |
63 | _tabs.nextTab = () => _tabs.currentPage() && _tabs.selectRelative(1) |
64 | _tabs.previousTab = () => _tabs.currentPage() && _tabs.selectRelative(-1) |
65 | _tabs.closeCurrentTab = () => { _tabs.currentPage() && _tabs.remove(_tabs.selected[0]) } |
66 | |
67 | // # TODO: review - this works but is strange |
68 | initial.forEach(p => api.app.sync.goTo(p)) |
69 | if (initial[0]) api.app.sync.goTo(initial[0]) |
70 | return _tabs |
71 | } |
72 | } |
73 | |
74 | // TODO - move this responsibility out to the searchBar? |
75 | function buildSearchBarTermFromLocation (location) { |
76 | const { page, query } = location |
77 | |
78 | if (page === 'search') return '?' + query |
79 | |
80 | const keys = Object.keys(location) |
81 | if (page && keys.length === 1) return '/' + page |
82 | |
83 | if (location.channels) return location.channels.join('+') |
84 | |
85 | return keys |
86 | .map(k => location[k]) |
87 | .join(', ') |
88 | } |
89 |
Built with git-ssb-web