Files: a32527dad991ff7ed922f6452c9d20e532a1e5a8 / app / html / tabs.js
1423 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.addPage': 'first' |
13 | }) |
14 | |
15 | exports.create = function (api) { |
16 | var _tabs |
17 | |
18 | function tabs (initialTabs = []) { |
19 | if (_tabs) return _tabs |
20 | |
21 | const search = api.app.html.searchBar() |
22 | const menu = api.app.html.menu() |
23 | const onSelect = (indexes) => { |
24 | const { id } = _tabs.get(indexes[0]).content |
25 | |
26 | try { |
27 | var location = JSON.parse(id) |
28 | } catch (e) { |
29 | throw new Error('app.html.tabs expects all page ids to be stringified location objects') |
30 | } |
31 | |
32 | search.input.value = buildSearchBarTermFromLocation(location) |
33 | } |
34 | _tabs = Tabs(onSelect, { |
35 | append: h('div.navExtra', [ search, menu ]) |
36 | }) |
37 | _tabs.getCurrent = () => _tabs.get(_tabs.selected[0]) |
38 | |
39 | // # TODO: review - this works but is strange |
40 | initialTabs.forEach(p => api.app.sync.addPage(p)) |
41 | _tabs.select(0) |
42 | return _tabs |
43 | } |
44 | |
45 | return nest({ |
46 | 'app.html.tabs': tabs |
47 | }) |
48 | } |
49 | |
50 | function buildSearchBarTermFromLocation (location) { |
51 | const { page, query } = location |
52 | |
53 | if (page === 'search') return '?' + query |
54 | |
55 | const keys = Object.keys(location) |
56 | if (page && keys.length === 1) return '/' + page |
57 | |
58 | return keys |
59 | .map(k => location[k]) |
60 | .join(', ') |
61 | } |
62 |
Built with git-ssb-web