Files: a41e049ca3755ddfd97e7f53c9da830711e636f1 / app / html / tabs.js
1870 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 | 'history.obs.store': 'first', |
14 | 'history.sync.push': 'first' |
15 | }) |
16 | |
17 | exports.create = function (api) { |
18 | var _tabs |
19 | |
20 | return nest({ |
21 | 'app.html.tabs': tabs |
22 | }) |
23 | |
24 | function tabs (initialTabs = []) { |
25 | if (_tabs) return _tabs |
26 | |
27 | const onSelect = (indexes) => { |
28 | const { id } = _tabs.get(indexes[0]).content |
29 | |
30 | try { |
31 | var location = JSON.parse(id) |
32 | } catch (e) { |
33 | debugger |
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 JSON.stringify(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 | |
51 | _tabs = Tabs({ |
52 | onSelect, |
53 | onClose, |
54 | append: h('div.navExtra', [ search, menu ]) |
55 | }) |
56 | _tabs.currentPage = () => _tabs.get(_tabs.selected[0]).firstChild |
57 | |
58 | // # TODO: review - this works but is strange |
59 | initialTabs.forEach(p => api.app.sync.goTo(p)) |
60 | api.app.sync.goTo(initialTabs[0]) |
61 | return _tabs |
62 | } |
63 | } |
64 | |
65 | // TODO - move this responsibility out to the searchBar? |
66 | function buildSearchBarTermFromLocation (location) { |
67 | const { page, query } = location |
68 | |
69 | if (page === 'search') return '?' + query |
70 | |
71 | const keys = Object.keys(location) |
72 | if (page && keys.length === 1) return '/' + page |
73 | |
74 | return keys |
75 | .map(k => location[k]) |
76 | .join(', ') |
77 | } |
78 |
Built with git-ssb-web