git ssb

16+

Dominic / patchbay



Tree: 560b4eeaebdc81616f23b3384a8c6ac9c0126e09

Files: 560b4eeaebdc81616f23b3384a8c6ac9c0126e09 / modules_core / tabs.js

5426 bytesRaw
1var Tabs = require('hypertabs')
2var h = require('hyperscript')
3var pull = require('pull-stream')
4var u = require('../util')
5var keyscroll = require('../keyscroll')
6var open = require('open-external')
7
8function ancestor (el) {
9 if(!el) return
10 if(el.tagName !== 'A') return ancestor(el.parentElement)
11 return el
12}
13
14var plugs = require('../plugs')
15var screen_view = plugs.first(exports._screen_view = [])
16var search_box = plugs.first(exports.search_box = [])
17var menu = plugs.first(exports.menu = [])
18
19exports.message_render = []
20
21exports.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 //let the application handle this link
90 if (link.getAttribute('href') === '#') return
91
92 //open external links.
93 //this ought to be made into something more runcible
94 if(open.isExternal(link.href)) return open(link.href)
95
96 if(tabs.has(path))
97 return tabs.select(path, !ev.ctrlKey, !!ev.shiftKey)
98
99 var el = screen_view(path)
100 if(el) {
101 el.id = el.id || path
102 el.scroll = keyscroll(el.querySelector('.scroller__content'))
103 tabs.add(el, !ev.ctrlKey, !!ev.shiftKey)
104// localStorage.openTabs = JSON.stringify(tabs.tabs)
105 }
106
107 return false
108 }
109
110 window.addEventListener('keydown', function (ev) {
111 if (ev.target.nodeName === 'INPUT' || ev.target.nodeName === 'TEXTAREA')
112 return
113 switch(ev.keyCode) {
114
115 // scroll through tabs
116 case 72: // h
117 return tabs.selectRelative(-1)
118 case 76: // l
119 return tabs.selectRelative(1)
120
121 // scroll through messages
122 case 74: // j
123 return tabs.get(tabs.selected[0]).scroll(1)
124 case 75: // k
125 return tabs.get(tabs.selected[0]).scroll(-1)
126
127 // close a tab
128 case 88: // x
129 if (tabs.selected) {
130 var sel = tabs.selected
131 var i = sel.reduce(function (a, b) { return Math.min(a, b) })
132 tabs.remove(sel)
133 tabs.select(Math.max(i-1, 0))
134 }
135 return
136
137 // activate the search field
138 case 191: // /
139 if (ev.shiftKey)
140 search.activate('?', ev)
141 else
142 search.activate('/', ev)
143 return
144
145 // navigate to a feed
146 case 50: // 2
147 if (ev.shiftKey)
148 search.activate('@', ev)
149 return
150
151 // navigate to a channel
152 case 51: // 3
153 if (ev.shiftKey)
154 search.activate('#', ev)
155 return
156
157 // navigate to a message
158 case 53: // 5
159 if (ev.shiftKey)
160 search.activate('%', ev)
161 return
162 }
163 })
164
165 // errors tab
166 var errorsContent = h('div.column.scroller__content')
167 var errors = h('div.column.scroller', {
168 id: 'errors',
169 style: {'overflow':'auto'}
170 }, h('div.scroller__wrapper',
171 errorsContent
172 )
173 )
174
175 // remove loader error handler
176 if (window.onError) {
177 window.removeEventListener('error', window.onError)
178 delete window.onError
179 }
180
181 // put errors in a tab
182 window.addEventListener('error', function (ev) {
183 var err = ev.error || ev
184 if(!tabs.has('errors'))
185 tabs.add(errors, false)
186 var el = h('div.message',
187 h('strong', err.message),
188 h('pre', err.stack))
189 if (errorsContent.firstChild)
190 errorsContent.insertBefore(el, errorsContent.firstChild)
191 else
192 errorsContent.appendChild(el)
193 })
194
195 if (process.versions.electron) {
196 window.addEventListener('contextmenu', function (ev) {
197 ev.preventDefault()
198 var remote = require('electron').remote
199 var Menu = remote.Menu
200 var MenuItem = remote.MenuItem
201 var menu = new Menu()
202 menu.append(new MenuItem({
203 label: 'Inspect Element',
204 click: function () {
205 remote.getCurrentWindow().inspectElement(ev.x, ev.y)
206 }
207 }))
208 menu.popup(remote.getCurrentWindow())
209 })
210 }
211
212 return tabs
213}
214
215
216
217
218
219
220
221
222
223
224

Built with git-ssb-web