Files: 944aabb813d4b39e34dc79a025bb30fa8db414ec / modules_core / tabs.js
5362 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 | |
69 | saved.forEach(function (path) { |
70 | var el = screen_view(path) |
71 | if(!el) return |
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 | // navigate to a message |
155 | case 53: // 5 |
156 | if (ev.shiftKey) |
157 | search.activate('%', ev) |
158 | return |
159 | } |
160 | }) |
161 | |
162 | // errors tab |
163 | var errorsContent = h('div.column.scroller__content') |
164 | var errors = h('div.column.scroller', |
165 | {style: {'overflow':'auto'}}, |
166 | h('div.scroller__wrapper', |
167 | errorsContent |
168 | ) |
169 | ) |
170 | |
171 | // remove loader error handler |
172 | if (window.onError) { |
173 | window.removeEventListener('error', window.onError) |
174 | delete window.onError |
175 | } |
176 | |
177 | var errors = h('div.errors', {id: 'errors'}) |
178 | |
179 | // put errors in a tab |
180 | window.addEventListener('error', function (ev) { |
181 | var err = ev.error || ev |
182 | if(!tabs.has('errors')) |
183 | tabs.add(errors, false) |
184 | var el = h('div.message', |
185 | h('strong', err.message), |
186 | h('pre', err.stack)) |
187 | if (errorsContent.firstChild) |
188 | errorsContent.insertBefore(el, errorsContent.firstChild) |
189 | else |
190 | errorsContent.appendChild(el) |
191 | }) |
192 | |
193 | if (process.versions.electron) { |
194 | window.addEventListener('contextmenu', function (ev) { |
195 | ev.preventDefault() |
196 | var remote = require('electron').remote |
197 | var Menu = remote.Menu |
198 | var MenuItem = remote.MenuItem |
199 | var menu = new Menu() |
200 | menu.append(new MenuItem({ |
201 | label: 'Inspect Element', |
202 | click: function () { |
203 | remote.getCurrentWindow().inspectElement(ev.x, ev.y) |
204 | } |
205 | })) |
206 | menu.popup(remote.getCurrentWindow()) |
207 | }) |
208 | } |
209 | |
210 | return tabs |
211 | } |
212 | |
213 | |
214 | |
215 | |
216 | |
217 | |
218 | |
219 | |
220 | |
221 | |
222 |
Built with git-ssb-web