Files: 5ad997b45961f9143176e86d336daddac8c2aa0f / modules_core / tabs.js
5987 bytesRaw
1 | var Tabs = require('hypertabs_bootstrap') |
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.needs = {screen_view: 'first', search_box: 'first', menu: 'first'} |
20 | |
21 | exports.gives = 'screen_view' |
22 | |
23 | exports.create = function (api) { |
24 | return function (path) { |
25 | if(path !== 'tabs') |
26 | return |
27 | |
28 | function setSelected (indexes) { |
29 | var ids = indexes.map(function (index) { |
30 | return tabs.get(index).id |
31 | }) |
32 | if(search) |
33 | if(ids.length > 1) |
34 | search.value = 'split('+ids.join(',')+')' |
35 | else |
36 | search.value = ids[0] |
37 | } |
38 | |
39 | var search |
40 | var tabs = Tabs(setSelected) |
41 | |
42 | search = api.search_box(function (path, change) { |
43 | |
44 | if(tabs.has(path)) { |
45 | tabs.select(path) |
46 | return true |
47 | } |
48 | var el = api.screen_view(path) |
49 | |
50 | if(el) { |
51 | if(!el.title) el.title = path |
52 | el.scroll = keyscroll(el.querySelector('.scroller__content')) |
53 | tabs.add(el, change) |
54 | // localStorage.openTabs = JSON.stringify(tabs.tabs) |
55 | return change |
56 | } |
57 | }) |
58 | |
59 | |
60 | // Somehow get search thing into the menu |
61 | tabs.querySelector('#search_holder').appendChild(search) |
62 | tabs.querySelector('#tabs_holder').appendChild(api.menu()) |
63 | |
64 | //reposition hypertabs menu to inside a container... |
65 | // tabs.insertBefore( |
66 | // tabs.firstChild, //tabs |
67 | // search, api.menu() |
68 | // ) |
69 | // tabs.insertBefore(search, tabs.firstChild.nextSibling) |
70 | |
71 | var saved = [] |
72 | // try { saved = JSON.parse(localStorage.openTabs) } |
73 | // catch (_) { } |
74 | |
75 | if(!saved || saved.length < 3) |
76 | saved = ['/public', '/private', '/notifications', '/data'] |
77 | |
78 | saved.forEach(function (path) { |
79 | var el = api.screen_view(path) |
80 | if(!el) return |
81 | el.id = el.id || path |
82 | if (!el) return |
83 | el.scroll = keyscroll(el.querySelector('.scroller__content')) |
84 | if(el) tabs.add(el, false, false) |
85 | }) |
86 | |
87 | tabs.select(0) |
88 | |
89 | //handle link clicks |
90 | window.onclick = function (ev) { |
91 | var link = ancestor(ev.target) |
92 | if(!link) return |
93 | var path = link.hash.substring(1) |
94 | |
95 | ev.preventDefault() |
96 | ev.stopPropagation() |
97 | |
98 | //let the application handle this link |
99 | if (link.getAttribute('href') === '#') return |
100 | |
101 | //open external links. |
102 | //this ought to be made into something more runcible |
103 | if(open.isExternal(link.href)) return open(link.href) |
104 | |
105 | if(tabs.has(path)) |
106 | return tabs.select(path, !ev.ctrlKey, !!ev.shiftKey) |
107 | |
108 | var el = api.screen_view(path) |
109 | if(el) { |
110 | el.id = el.id || path |
111 | el.scroll = keyscroll(el.querySelector('.scroller__content')) |
112 | tabs.add(el, !ev.ctrlKey, !!ev.shiftKey) |
113 | // localStorage.openTabs = JSON.stringify(tabs.tabs) |
114 | } |
115 | |
116 | return false |
117 | } |
118 | |
119 | window.addEventListener('keydown', function (ev) { |
120 | if (ev.target.nodeName === 'INPUT' || ev.target.nodeName === 'TEXTAREA') |
121 | return |
122 | switch(ev.keyCode) { |
123 | |
124 | // scroll through tabs |
125 | case 72: // h |
126 | return tabs.selectRelative(-1) |
127 | case 76: // l |
128 | return tabs.selectRelative(1) |
129 | |
130 | // scroll through messages |
131 | case 74: // j |
132 | return tabs.get(tabs.selected[0]).scroll(1) |
133 | case 75: // k |
134 | return tabs.get(tabs.selected[0]).scroll(-1) |
135 | |
136 | // close a tab |
137 | case 88: // x |
138 | if (tabs.selected) { |
139 | var sel = tabs.selected |
140 | var i = sel.reduce(function (a, b) { return Math.min(a, b) }) |
141 | tabs.remove(sel) |
142 | tabs.select(Math.max(i-1, 0)) |
143 | } |
144 | return |
145 | |
146 | // activate the search field |
147 | case 191: // / |
148 | if (ev.shiftKey) |
149 | search.activate('?', ev) |
150 | else |
151 | search.activate('/', ev) |
152 | return |
153 | |
154 | // navigate to a feed |
155 | case 50: // 2 |
156 | if (ev.shiftKey) |
157 | search.activate('@', ev) |
158 | return |
159 | |
160 | // navigate to a channel |
161 | case 51: // 3 |
162 | if (ev.shiftKey) |
163 | search.activate('#', ev) |
164 | return |
165 | |
166 | // navigate to a message |
167 | case 53: // 5 |
168 | if (ev.shiftKey) |
169 | search.activate('%', ev) |
170 | return |
171 | } |
172 | }) |
173 | |
174 | // errors tab |
175 | var errorsContent = h('div.column.scroller__content') |
176 | var errors = h('div.column.scroller', { |
177 | id: 'errors', |
178 | style: {'overflow':'auto'} |
179 | }, h('div.scroller__wrapper', |
180 | errorsContent |
181 | ) |
182 | ) |
183 | |
184 | // remove loader error handler |
185 | if (window.onError) { |
186 | window.removeEventListener('error', window.onError) |
187 | delete window.onError |
188 | } |
189 | |
190 | // put errors in a tab |
191 | window.addEventListener('error', function (ev) { |
192 | var err = ev.error || ev |
193 | if(!tabs.has('errors')) |
194 | tabs.add(errors, false) |
195 | var el = h('div.message', |
196 | h('strong', err.message), |
197 | h('pre', err.stack)) |
198 | if (errorsContent.firstChild) |
199 | errorsContent.insertBefore(el, errorsContent.firstChild) |
200 | else |
201 | errorsContent.appendChild(el) |
202 | }) |
203 | |
204 | if (process.versions.electron) { |
205 | window.addEventListener('contextmenu', function (ev) { |
206 | ev.preventDefault() |
207 | var remote = require('electron').remote |
208 | var Menu = remote.Menu |
209 | var MenuItem = remote.MenuItem |
210 | var menu = new Menu() |
211 | menu.append(new MenuItem({ |
212 | label: 'Inspect Element', |
213 | click: function () { |
214 | remote.getCurrentWindow().inspectElement(ev.x, ev.y) |
215 | } |
216 | })) |
217 | menu.popup(remote.getCurrentWindow()) |
218 | }) |
219 | } |
220 | |
221 | return tabs |
222 | } |
223 | |
224 | } |
225 |
Built with git-ssb-web