Files: c9b2708a2c9961e39c5c2370d526d929215013df / modules / tabs.js
2353 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 saved |
38 | try { saved = JSON.parse(localStorage.openTabs) } |
39 | catch (_) { saved = ['/public', '/private'] } |
40 | |
41 | saved.forEach(function (path) { |
42 | var el = screen_view(path) |
43 | el.scroll = keyscroll(el.querySelector('.scroller__content')) |
44 | if(el) tabs.add(path, el, true) |
45 | }) |
46 | |
47 | tabs.select(saved[0] || '/public') |
48 | |
49 | tabs.onclick = function (ev) { |
50 | var link = ancestor(ev.target) |
51 | if(!link) return |
52 | var path = link.hash.substring(1) |
53 | |
54 | ev.preventDefault() |
55 | ev.stopPropagation() |
56 | |
57 | //open external links. |
58 | //this ought to be made into something more runcible |
59 | if(/^https?/.test(link.href)) return openExternal(link.href) |
60 | |
61 | if(tabs.has(path)) return tabs.select(path) |
62 | |
63 | var el = screen_view(path) |
64 | if(el) { |
65 | el.scroll = keyscroll(el.querySelector('.scroller__content')) |
66 | tabs.add(path, el, !ev.ctrlKey) |
67 | localStorage.openTabs = JSON.stringify(tabs.tabs) |
68 | } |
69 | } |
70 | |
71 | window.addEventListener('keydown', function (ev) { |
72 | if (ev.target.nodeName === 'INPUT' || ev.target.nodeName === 'TEXTAREA') |
73 | return |
74 | switch(ev.keyCode) { |
75 | // scroll through tabs |
76 | case 72: // h |
77 | return tabs.selectRelative(-1) |
78 | case 76: // l |
79 | return tabs.selectRelative(1) |
80 | // scroll through messages |
81 | case 74: // j |
82 | return tabs.selectedTab.scroll(1) |
83 | case 75: // k |
84 | return tabs.selectedTab.scroll(-1) |
85 | // close a tab |
86 | case 88: // x |
87 | if (tabs.selected !== '/public' && tabs.selected !== '/private') |
88 | return tabs.remove(tabs.selected) |
89 | } |
90 | }) |
91 | |
92 | return tabs |
93 | } |
94 | |
95 | |
96 | |
97 | |
98 | |
99 | |
100 | |
101 | |
102 | |
103 | |
104 | |
105 | |
106 |
Built with git-ssb-web