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