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