Files: 9e65f9951f21778ee286834004367aa2b46bc882 / modules / tabs.js
4143 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 | |
18 | exports.message_render = [] |
19 | |
20 | exports.screen_view = function (path) { |
21 | if(path !== 'tabs') |
22 | return |
23 | |
24 | function setSelected (indexes) { |
25 | var ids = indexes.map(function (index) { |
26 | return tabs.get(index).id |
27 | }) |
28 | if(ids.length > 1) |
29 | search.value = 'split('+ids.join(',')+')' |
30 | else |
31 | search.value = ids[0] |
32 | } |
33 | |
34 | var search |
35 | var tabs = Tabs(setSelected) |
36 | // tabs.classList.add('screen') |
37 | |
38 | search = search_box(function (path, change) { |
39 | if(tabs.has(path)) { |
40 | tabs.select(path) |
41 | return true |
42 | } |
43 | var el = screen_view(path) |
44 | if(el) { |
45 | el.scroll = keyscroll(el.querySelector('.scroller__content')) |
46 | tabs.add(el, change) |
47 | // localStorage.openTabs = JSON.stringify(tabs.tabs) |
48 | return change |
49 | } |
50 | }) |
51 | |
52 | tabs.insertBefore(search, tabs.firstChild.nextSibling) |
53 | |
54 | var saved = [] |
55 | // try { saved = JSON.parse(localStorage.openTabs) } |
56 | // catch (_) { } |
57 | |
58 | if(!saved || saved.length < 3) |
59 | saved = ['/public', '/private', '/notifications'] |
60 | |
61 | saved.forEach(function (path) { |
62 | var el = screen_view(path) |
63 | el.id = el.id || path |
64 | if (!el) return |
65 | el.scroll = keyscroll(el.querySelector('.scroller__content')) |
66 | if(el) tabs.add(el, false, false) |
67 | }) |
68 | |
69 | tabs.select(0) |
70 | |
71 | //handle link clicks |
72 | window.onclick = function (ev) { |
73 | var link = ancestor(ev.target) |
74 | if(!link) return |
75 | var path = link.hash.substring(1) |
76 | |
77 | ev.preventDefault() |
78 | ev.stopPropagation() |
79 | |
80 | //open external links. |
81 | //this ought to be made into something more runcible |
82 | if(open.isExternal(link.href)) return open(link.href) |
83 | |
84 | if(tabs.has(path)) |
85 | return tabs.select(path, !ev.ctrlKey, !!ev.shiftKey) |
86 | |
87 | var el = screen_view(path) |
88 | if(el) { |
89 | el.id = el.id || path |
90 | el.scroll = keyscroll(el.querySelector('.scroller__content')) |
91 | tabs.add(el, !ev.ctrlKey, !!ev.shiftKey) |
92 | // localStorage.openTabs = JSON.stringify(tabs.tabs) |
93 | } |
94 | |
95 | return false |
96 | } |
97 | |
98 | window.addEventListener('keydown', function (ev) { |
99 | if (ev.target.nodeName === 'INPUT' || ev.target.nodeName === 'TEXTAREA') |
100 | return |
101 | switch(ev.keyCode) { |
102 | |
103 | // scroll through tabs |
104 | case 72: // h |
105 | return tabs.selectRelative(-1) |
106 | case 76: // l |
107 | return tabs.selectRelative(1) |
108 | |
109 | // scroll through messages |
110 | case 74: // j |
111 | return tabs.get(tabs.selected[0]).scroll(1) |
112 | case 75: // k |
113 | return tabs.get(tabs.selected[0]).scroll(-1) |
114 | |
115 | // close a tab |
116 | case 88: // x |
117 | if (tabs.selected) { |
118 | var sel = tabs.selected |
119 | var i = sel.reduce(function (a, b) { return Math.min(a, b) }) |
120 | tabs.remove(sel) |
121 | tabs.select(Math.max(i-1, 0)) |
122 | } |
123 | return |
124 | |
125 | // activate the search field |
126 | case 191: // / |
127 | search.activate('?', ev) |
128 | return |
129 | |
130 | // navigate to a feed |
131 | case 50: // 2 |
132 | if (ev.shiftKey) |
133 | search.activate('@', ev) |
134 | return |
135 | |
136 | // navigate to a channel |
137 | case 51: // 3 |
138 | if (ev.shiftKey) |
139 | search.activate('#', ev) |
140 | return |
141 | } |
142 | }) |
143 | |
144 | // errors tab |
145 | var errorsContent = h('div.column.scroller__content') |
146 | var errors = h('div.column.scroller', |
147 | {style: {'overflow':'auto'}}, |
148 | h('div.scroller__wrapper', |
149 | errorsContent |
150 | ) |
151 | ) |
152 | |
153 | window.addEventListener('error', function (ev) { |
154 | var err = ev.error || ev |
155 | if(!tabs.has('errors')) |
156 | tabs.add('errors', errors, false) |
157 | var el = h('div.message', |
158 | h('strong', err.message), |
159 | h('pre', err.stack)) |
160 | if (errorsContent.firstChild) |
161 | errorsContent.insertBefore(el, errorsContent.firstChild) |
162 | else |
163 | errorsContent.appendChild(el) |
164 | }) |
165 | |
166 | return tabs |
167 | } |
168 | |
169 |
Built with git-ssb-web