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