Files: ae12a05c8bad934d6e90d7b524a9a16e76ed5b98 / modules / tabs.js
3428 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('?' + search.value, 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 | var saved |
63 | try { saved = JSON.parse(localStorage.openTabs) } |
64 | catch (_) { saved = ['/public', '/private'] } |
65 | |
66 | saved.forEach(function (path) { |
67 | var el = screen_view(path) |
68 | if (!el) return |
69 | el.scroll = keyscroll(el.querySelector('.scroller__content')) |
70 | if(el) tabs.add(path, el, true) |
71 | }) |
72 | |
73 | tabs.select(saved[0] || '/public') |
74 | |
75 | tabs.onclick = function (ev) { |
76 | var link = ancestor(ev.target) |
77 | if(!link) return |
78 | var path = link.hash.substring(1) |
79 | |
80 | ev.preventDefault() |
81 | ev.stopPropagation() |
82 | |
83 | //open external links. |
84 | //this ought to be made into something more runcible |
85 | if(/^https?/.test(link.href)) return openExternal(link.href) |
86 | |
87 | if(tabs.has(path)) return tabs.select(path) |
88 | |
89 | var el = screen_view(path) |
90 | if(el) { |
91 | el.scroll = keyscroll(el.querySelector('.scroller__content')) |
92 | tabs.add(path, el, !ev.ctrlKey) |
93 | localStorage.openTabs = JSON.stringify(tabs.tabs) |
94 | } |
95 | } |
96 | |
97 | window.addEventListener('keydown', function (ev) { |
98 | if (ev.target.nodeName === 'INPUT' || ev.target.nodeName === 'TEXTAREA') |
99 | return |
100 | switch(ev.keyCode) { |
101 | |
102 | // scroll through tabs |
103 | case 72: // h |
104 | return tabs.selectRelative(-1) |
105 | case 76: // l |
106 | return tabs.selectRelative(1) |
107 | |
108 | // scroll through messages |
109 | case 74: // j |
110 | return tabs.selectedTab.scroll(1) |
111 | case 75: // k |
112 | return tabs.selectedTab.scroll(-1) |
113 | |
114 | // close a tab |
115 | case 88: // x |
116 | if (tabs.selected !== '/public' && tabs.selected !== '/private') { |
117 | tabs.remove(tabs.selected) |
118 | localStorage.openTabs = JSON.stringify(tabs.tabs) |
119 | } |
120 | return |
121 | |
122 | // activate the search field |
123 | case 191: // / |
124 | ev.preventDefault() |
125 | search.focus() |
126 | search.selectionStart = 0 |
127 | search.selectionEnd = search.value.length |
128 | return |
129 | } |
130 | }) |
131 | |
132 | return tabs |
133 | } |
134 | |
135 | |
136 | |
137 | |
138 | |
139 | |
140 | |
141 | |
142 | |
143 | |
144 | |
145 | |
146 |
Built with git-ssb-web