Files: f2e0672ea2122a0681f222b03f03d0e227303352 / modules_core / tabs.js
5235 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 | }) |
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 | var errors = h('div.errors', {id: 'errors'}) |
172 | |
173 | // put errors in a tab |
174 | window.addEventListener('error', function (ev) { |
175 | var err = ev.error || ev |
176 | if(!tabs.has('errors')) |
177 | tabs.add(errors, false) |
178 | var el = h('div.message', |
179 | h('strong', err.message), |
180 | h('pre', err.stack)) |
181 | if (errorsContent.firstChild) |
182 | errorsContent.insertBefore(el, errorsContent.firstChild) |
183 | else |
184 | errorsContent.appendChild(el) |
185 | }) |
186 | |
187 | if (process.versions.electron) { |
188 | window.addEventListener('contextmenu', function (ev) { |
189 | ev.preventDefault() |
190 | var remote = require('electron').remote |
191 | var Menu = remote.Menu |
192 | var MenuItem = remote.MenuItem |
193 | var menu = new Menu() |
194 | menu.append(new MenuItem({ |
195 | label: 'Inspect Element', |
196 | click: function () { |
197 | remote.getCurrentWindow().inspectElement(ev.x, ev.y) |
198 | } |
199 | })) |
200 | menu.popup(remote.getCurrentWindow()) |
201 | }) |
202 | } |
203 | |
204 | return tabs |
205 | } |
206 | |
207 | |
208 | |
209 | |
210 | |
211 | |
212 | |
213 | |
214 | |
215 | |
216 |
Built with git-ssb-web