Files: 567b32727710add1401610208245e6950f200f06 / modules / tabs.js
3681 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 | |
16 | function openExternal (url) { |
17 | var _r = require //fool browserify |
18 | |
19 | //electron@1 |
20 | try {return _r('electron').shell.openExternal(url) } |
21 | catch (err) { } |
22 | |
23 | //electron@0 |
24 | try { return _r('shell').openExternal(url) } |
25 | catch (err) { } |
26 | |
27 | //browser |
28 | window.open(url, '_blank') |
29 | } |
30 | |
31 | exports.message_render = [] |
32 | |
33 | exports.app = function () { |
34 | var tabs = Tabs() |
35 | tabs.classList.add('screen') |
36 | |
37 | var search = h('input.searchprompt', { |
38 | type: 'search', |
39 | style: {'float': 'right'}, |
40 | onkeydown: function (ev) { |
41 | switch (ev.keyCode) { |
42 | case 13: // enter |
43 | var path = search.value |
44 | if(tabs.has(path)) return tabs.select(path) |
45 | var el = screen_view(path) |
46 | if(el) { |
47 | el.scroll = keyscroll(el.querySelector('.scroller__content')) |
48 | tabs.add(path, el, !ev.ctrlKey) |
49 | localStorage.openTabs = JSON.stringify(tabs.tabs) |
50 | search.blur() |
51 | } |
52 | return |
53 | case 27: // escape |
54 | ev.preventDefault() |
55 | search.blur() |
56 | return |
57 | } |
58 | } |
59 | }) |
60 | tabs.insertBefore(search, tabs.querySelector('.hypertabs__content')) |
61 | |
62 | function activateSearch(sigil, ev) { |
63 | search.focus() |
64 | ev.preventDefault() |
65 | if (search.value[0] === sigil) { |
66 | search.selectionStart = 1 |
67 | search.selectionEnd = search.value.length |
68 | } else { |
69 | search.value = sigil |
70 | } |
71 | } |
72 | |
73 | var saved |
74 | try { saved = JSON.parse(localStorage.openTabs) } |
75 | catch (_) { saved = ['/public', '/private'] } |
76 | |
77 | saved.forEach(function (path) { |
78 | var el = screen_view(path) |
79 | if (!el) return |
80 | el.scroll = keyscroll(el.querySelector('.scroller__content')) |
81 | if(el) tabs.add(path, el, true) |
82 | }) |
83 | |
84 | tabs.select(saved[0] || '/public') |
85 | |
86 | tabs.onclick = function (ev) { |
87 | var link = ancestor(ev.target) |
88 | if(!link) return |
89 | var path = link.hash.substring(1) |
90 | |
91 | ev.preventDefault() |
92 | ev.stopPropagation() |
93 | |
94 | //open external links. |
95 | //this ought to be made into something more runcible |
96 | if(/^https?/.test(link.href)) return openExternal(link.href) |
97 | |
98 | if(tabs.has(path)) return tabs.select(path) |
99 | |
100 | var el = screen_view(path) |
101 | if(el) { |
102 | el.scroll = keyscroll(el.querySelector('.scroller__content')) |
103 | tabs.add(path, el, !ev.ctrlKey) |
104 | localStorage.openTabs = JSON.stringify(tabs.tabs) |
105 | } |
106 | } |
107 | |
108 | window.addEventListener('keydown', function (ev) { |
109 | if (ev.target.nodeName === 'INPUT' || ev.target.nodeName === 'TEXTAREA') |
110 | return |
111 | switch(ev.keyCode) { |
112 | |
113 | // scroll through tabs |
114 | case 72: // h |
115 | return tabs.selectRelative(-1) |
116 | case 76: // l |
117 | return tabs.selectRelative(1) |
118 | |
119 | // scroll through messages |
120 | case 74: // j |
121 | return tabs.selectedTab.scroll(1) |
122 | case 75: // k |
123 | return tabs.selectedTab.scroll(-1) |
124 | |
125 | // close a tab |
126 | case 88: // x |
127 | if (tabs.selected !== '/public' && tabs.selected !== '/private') { |
128 | tabs.remove(tabs.selected) |
129 | localStorage.openTabs = JSON.stringify(tabs.tabs) |
130 | } |
131 | return |
132 | |
133 | // activate the search field |
134 | case 191: // / |
135 | activateSearch('?', ev) |
136 | return |
137 | |
138 | // navigate to a channel |
139 | case 51: // 3 |
140 | if (ev.shiftKey) |
141 | activateSearch('#', ev) |
142 | return |
143 | } |
144 | }) |
145 | |
146 | return tabs |
147 | } |
148 | |
149 | |
150 | |
151 | |
152 | |
153 | |
154 | |
155 | |
156 | |
157 | |
158 | |
159 | |
160 |
Built with git-ssb-web