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