Files: 9510d7867738dfa7a4aaee17c183c86bb081f55e / app / html / tabs.js
1312 bytesRaw
1 | const nest = require('depnest') |
2 | const { h } = require('mutant') |
3 | const Tabs = require('hypertabs') |
4 | |
5 | exports.gives = nest({ |
6 | app: { |
7 | 'html.tabs': true |
8 | } |
9 | }) |
10 | |
11 | exports.needs = nest({ |
12 | 'app.html': { |
13 | menu: 'first', |
14 | page: 'first', |
15 | searchBar: 'first' |
16 | }, |
17 | 'app.sync.addPage': 'first' |
18 | }) |
19 | |
20 | exports.create = function (api) { |
21 | var _tabs |
22 | |
23 | function tabs (initialTabs = []) { |
24 | if (_tabs) return _tabs |
25 | |
26 | const search = api.app.html.searchBar() |
27 | const menu = api.app.html.menu() |
28 | const onSelect = (indexes) => { |
29 | const { id } = _tabs.get(indexes[0]).content |
30 | |
31 | try { |
32 | const location = JSON.parse(id) |
33 | var locationForSearchBar = Object.keys(location) |
34 | .map(k => location[k]) |
35 | .join(' + ') |
36 | } |
37 | catch (e) { |
38 | throw new Error('app/html/tabs expects all page ids to be stringified location objects') |
39 | var locationForSearchBar = id |
40 | } |
41 | search.input.value = locationForSearchBar |
42 | } |
43 | _tabs = Tabs(onSelect, { |
44 | append: h('div.navExtra', [ search, menu ]) |
45 | }) |
46 | _tabs.getCurrent = () => _tabs.get(_tabs.selected[0]) |
47 | |
48 | // # TODO: review - this works but is strange |
49 | initialTabs.forEach(p => api.app.sync.addPage(p)) |
50 | _tabs.select(0) |
51 | return _tabs |
52 | } |
53 | |
54 | return nest({ |
55 | 'app.html.tabs': tabs |
56 | }) |
57 | } |
58 | |
59 |
Built with git-ssb-web