Files: cb26626721ed310376d6b1cfac574cc58e616db4 / modules / tabs.js
3171 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 | |
7 | function ancestor (el) { |
8 | if(!el) return |
9 | if(el.tagName !== 'A') return ancestor(el.parentElement) |
10 | return el |
11 | } |
12 | |
13 | var plugs = require('../plugs') |
14 | var screen_view = plugs.first(exports.screen_view = []) |
15 | var search_box = plugs.first(exports.search_box = []) |
16 | |
17 | function openExternal (url) { |
18 | var _r = require //fool browserify |
19 | |
20 | //electron@1 |
21 | try {return _r('electron').shell.openExternal(url) } |
22 | catch (err) { } |
23 | |
24 | //electron@0 |
25 | try { return _r('shell').openExternal(url) } |
26 | catch (err) { } |
27 | |
28 | //browser |
29 | window.open(url, '_blank') |
30 | } |
31 | |
32 | exports.message_render = [] |
33 | |
34 | exports.app = function () { |
35 | var tabs = Tabs() |
36 | tabs.classList.add('screen') |
37 | |
38 | var 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(path, el, change) |
47 | localStorage.openTabs = JSON.stringify(tabs.tabs) |
48 | return change |
49 | } |
50 | }) |
51 | tabs.insertBefore(search, tabs.querySelector('.hypertabs__content')) |
52 | |
53 | var saved |
54 | try { saved = JSON.parse(localStorage.openTabs) } |
55 | catch (_) { saved = ['/public', '/private'] } |
56 | |
57 | saved.forEach(function (path) { |
58 | var el = screen_view(path) |
59 | if (!el) return |
60 | el.scroll = keyscroll(el.querySelector('.scroller__content')) |
61 | if(el) tabs.add(path, el, true) |
62 | }) |
63 | |
64 | tabs.select(saved[0] || '/public') |
65 | |
66 | tabs.onclick = function (ev) { |
67 | var link = ancestor(ev.target) |
68 | if(!link) return |
69 | var path = link.hash.substring(1) |
70 | |
71 | ev.preventDefault() |
72 | ev.stopPropagation() |
73 | |
74 | //open external links. |
75 | //this ought to be made into something more runcible |
76 | if(/^https?/.test(link.href)) return openExternal(link.href) |
77 | |
78 | if(tabs.has(path)) return tabs.select(path) |
79 | |
80 | var el = screen_view(path) |
81 | if(el) { |
82 | el.scroll = keyscroll(el.querySelector('.scroller__content')) |
83 | tabs.add(path, el, !ev.ctrlKey) |
84 | localStorage.openTabs = JSON.stringify(tabs.tabs) |
85 | } |
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 !== '/public' && tabs.selected !== '/private') { |
108 | tabs.remove(tabs.selected) |
109 | localStorage.openTabs = JSON.stringify(tabs.tabs) |
110 | } |
111 | return |
112 | |
113 | // activate the search field |
114 | case 191: // / |
115 | search.activate('?', ev) |
116 | return |
117 | |
118 | // navigate to a channel |
119 | case 51: // 3 |
120 | if (ev.shiftKey) |
121 | search.activate('#', ev) |
122 | return |
123 | } |
124 | }) |
125 | |
126 | return tabs |
127 | } |
128 | |
129 | |
130 | |
131 | |
132 | |
133 | |
134 | |
135 | |
136 | |
137 | |
138 | |
139 | |
140 |
Built with git-ssb-web