Files: 1934f63b1eb1fb4cfafe583543b63fbacdf7e1f3 / modules / tabs.js
4721 bytesRaw
1 | var Tabs = require('hypertabs') |
2 | var h = require('hyperscript') |
3 | var pull = require('pull-stream') |
4 | var u = require('../util') |
5 | var keyscroll = require('../keyscroll') |
6 | var open = require('open-external') |
7 | |
8 | function ancestor (el) { |
9 | if(!el) return |
10 | if(el.tagName !== 'A') return ancestor(el.parentElement) |
11 | return el |
12 | } |
13 | |
14 | var plugs = require('../plugs') |
15 | var screen_view = plugs.first(exports._screen_view = []) |
16 | var search_box = plugs.first(exports.search_box = []) |
17 | var menu = plugs.first(exports.menu = []) |
18 | |
19 | exports.message_render = [] |
20 | |
21 | exports.screen_view = function (path) { |
22 | if(path !== 'tabs') |
23 | return |
24 | |
25 | function setSelected (indexes) { |
26 | var ids = indexes.map(function (index) { |
27 | return tabs.get(index).id |
28 | }) |
29 | if(ids.length > 1) |
30 | search.value = 'split('+ids.join(',')+')' |
31 | else |
32 | search.value = ids[0] |
33 | } |
34 | |
35 | var search |
36 | var tabs = Tabs(setSelected) |
37 | |
38 | search = search_box(function (path, change) { |
39 | |
40 | if(tabs.has(path)) { |
41 | tabs.select(path) |
42 | return true |
43 | } |
44 | var el = screen_view(path) |
45 | |
46 | if(el) { |
47 | if(!el.title) el.title = path |
48 | el.scroll = keyscroll(el.querySelector('.scroller__content')) |
49 | tabs.add(el, change) |
50 | // localStorage.openTabs = JSON.stringify(tabs.tabs) |
51 | return change |
52 | } |
53 | }) |
54 | |
55 | //reposition hypertabs menu to inside a container... |
56 | tabs.insertBefore(h('div.header.row', |
57 | h('div.header__tabs.row', tabs.firstChild), //tabs |
58 | h('div.header__search.row.end', h('div', search), menu()) |
59 | ), tabs.firstChild) |
60 | // tabs.insertBefore(search, tabs.firstChild.nextSibling) |
61 | |
62 | var saved = [] |
63 | // try { saved = JSON.parse(localStorage.openTabs) } |
64 | // catch (_) { } |
65 | |
66 | if(!saved || saved.length < 3) |
67 | // saved = ['/public', '/private', '/notifications'] |
68 | saved = ['/public', '/private'] |
69 | |
70 | saved.forEach(function (path) { |
71 | var el = screen_view(path) |
72 | el.id = el.id || path |
73 | if (!el) return |
74 | el.scroll = keyscroll(el.querySelector('.scroller__content')) |
75 | if(el) tabs.add(el, false, false) |
76 | }) |
77 | |
78 | tabs.select(0) |
79 | |
80 | //handle link clicks |
81 | window.onclick = function (ev) { |
82 | var link = ancestor(ev.target) |
83 | if(!link) return |
84 | var path = link.hash.substring(1) |
85 | |
86 | ev.preventDefault() |
87 | ev.stopPropagation() |
88 | |
89 | //open external links. |
90 | //this ought to be made into something more runcible |
91 | if(open.isExternal(link.href)) return open(link.href) |
92 | |
93 | if(tabs.has(path)) |
94 | return tabs.select(path, !ev.ctrlKey, !!ev.shiftKey) |
95 | |
96 | var el = screen_view(path) |
97 | if(el) { |
98 | el.id = el.id || path |
99 | el.scroll = keyscroll(el.querySelector('.scroller__content')) |
100 | tabs.add(el, !ev.ctrlKey, !!ev.shiftKey) |
101 | // localStorage.openTabs = JSON.stringify(tabs.tabs) |
102 | } |
103 | |
104 | return false |
105 | } |
106 | |
107 | window.addEventListener('keydown', function (ev) { |
108 | if (ev.target.nodeName === 'INPUT' || ev.target.nodeName === 'TEXTAREA') |
109 | return |
110 | switch(ev.keyCode) { |
111 | |
112 | // scroll through tabs |
113 | case 72: // h |
114 | return tabs.selectRelative(-1) |
115 | case 76: // l |
116 | return tabs.selectRelative(1) |
117 | |
118 | // scroll through messages |
119 | case 74: // j |
120 | return tabs.get(tabs.selected[0]).scroll(1) |
121 | case 75: // k |
122 | return tabs.get(tabs.selected[0]).scroll(-1) |
123 | |
124 | // close a tab |
125 | case 88: // x |
126 | if (tabs.selected) { |
127 | var sel = tabs.selected |
128 | var i = sel.reduce(function (a, b) { return Math.min(a, b) }) |
129 | tabs.remove(sel) |
130 | tabs.select(Math.max(i-1, 0)) |
131 | } |
132 | return |
133 | |
134 | // activate the search field |
135 | case 191: // / |
136 | if (ev.shiftKey) |
137 | search.activate('?', ev) |
138 | else |
139 | search.activate('/', ev) |
140 | return |
141 | |
142 | // navigate to a feed |
143 | case 50: // 2 |
144 | if (ev.shiftKey) |
145 | search.activate('@', ev) |
146 | return |
147 | |
148 | // navigate to a channel |
149 | case 51: // 3 |
150 | if (ev.shiftKey) |
151 | search.activate('#', ev) |
152 | return |
153 | } |
154 | }) |
155 | |
156 | // errors tab |
157 | var errorsContent = h('div.column.scroller__content') |
158 | var errors = h('div.column.scroller', |
159 | {style: {'overflow':'auto'}}, |
160 | h('div.scroller__wrapper', |
161 | errorsContent |
162 | ) |
163 | ) |
164 | |
165 | // remove loader error handler |
166 | if (window.onError) { |
167 | window.removeEventListener('error', window.onError) |
168 | delete window.onError |
169 | } |
170 | |
171 | // put errors in a tab |
172 | window.addEventListener('error', function (ev) { |
173 | var err = ev.error || ev |
174 | if(!tabs.has('errors')) |
175 | tabs.add('errors', errors, false) |
176 | var el = h('div.message', |
177 | h('strong', err.message), |
178 | h('pre', err.stack)) |
179 | if (errorsContent.firstChild) |
180 | errorsContent.insertBefore(el, errorsContent.firstChild) |
181 | else |
182 | errorsContent.appendChild(el) |
183 | }) |
184 | |
185 | return tabs |
186 | } |
187 | |
188 | |
189 | |
190 | |
191 | |
192 | |
193 | |
194 | |
195 | |
196 |
Built with git-ssb-web