git ssb

16+

Dominic / patchbay



Tree: 2a898c6bb5c44df0a373a3f46046dcf94e618dda

Files: 2a898c6bb5c44df0a373a3f46046dcf94e618dda / modules_core / tabs.js

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

Built with git-ssb-web