Files: cb96c21f9190d81e56e129d5b7def3e8300c8cbe / app / html / tabs.js
1287 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 | sync: { |
9 | 'goTo': true, |
10 | 'addPage': true |
11 | } |
12 | } |
13 | }) |
14 | |
15 | exports.needs = nest({ |
16 | 'app.html': { |
17 | menu: 'first', |
18 | page: 'first', |
19 | searchBar: 'first' |
20 | } |
21 | }) |
22 | |
23 | exports.create = function (api) { |
24 | |
25 | var _tabs |
26 | |
27 | function tabs (initialTabs = []) { |
28 | if (_tabs) return _tabs |
29 | |
30 | const search = api.app.html.searchBar(goTo) |
31 | const menu = api.app.html.menu(goTo) |
32 | const onSelect = (indexes) => { |
33 | search.input.value = _tabs.get(indexes[0]).content.id |
34 | } |
35 | _tabs = Tabs(onSelect, { |
36 | append: h('div.navExtra', [ search, menu ]) |
37 | }) |
38 | |
39 | initialTabs.forEach(p => addPage(p)) |
40 | _tabs.select(0) |
41 | return _tabs |
42 | } |
43 | |
44 | function goTo (path, change) { |
45 | tabs() |
46 | if (_tabs.has(path)) { |
47 | _tabs.select(path) |
48 | return true |
49 | } |
50 | |
51 | addPage(path, true, false) |
52 | return change |
53 | } |
54 | |
55 | function addPage (link, change, split) { |
56 | tabs() |
57 | const page = api.app.html.page(link) |
58 | if (!page) return |
59 | |
60 | page.id = page.id || link |
61 | _tabs.add(page, change, split) |
62 | } |
63 | |
64 | return nest({ |
65 | app: { |
66 | 'html.tabs': tabs, |
67 | sync: { |
68 | goTo, |
69 | addPage |
70 | } |
71 | } |
72 | }) |
73 | } |
74 | |
75 |
Built with git-ssb-web