Files: 09d18ee84670767e89b5afcc9d5de810a6174aa5 / main-window.js
4860 bytesRaw
1 | var combine = require('depject') |
2 | var entry = require('depject/entry') |
3 | var electron = require('electron') |
4 | var h = require('mutant/h') |
5 | var when = require('mutant/when') |
6 | var onceTrue = require('mutant/once-true') |
7 | var computed = require('mutant/computed') |
8 | var catchLinks = require('./lib/catch-links') |
9 | var insertCss = require('insert-css') |
10 | var nest = require('depnest') |
11 | var LatestUpdate = require('./lib/latest-update') |
12 | var ref = require('ssb-ref') |
13 | var setupContextMenuAndSpellCheck = require('./lib/context-menu-and-spellcheck') |
14 | var watch = require('mutant/watch') |
15 | |
16 | |
17 | module.exports = function (config) { |
18 | var sockets = combine( |
19 | overrideConfig(config), |
20 | addCommand('app.navigate', setView), |
21 | require('./modules'), |
22 | require('./plugs'), |
23 | require('patchcore'), |
24 | require('./overrides') |
25 | ) |
26 | |
27 | var api = entry(sockets, nest({ |
28 | 'config.sync.load': 'first', |
29 | 'keys.sync.id': 'first', |
30 | 'sbot.obs.connection': 'first', |
31 | 'sbot.async.get': 'first', |
32 | 'blob.sync.url': 'first', |
33 | 'page.html.render': 'first', |
34 | 'app.html.search': 'first', |
35 | 'app.html.channels': 'first', |
36 | 'app.views': 'first', |
37 | 'app.sync.externalHandler': 'first', |
38 | 'app.html.progressNotifier': 'first', |
39 | 'profile.sheet.edit': 'first', |
40 | 'app.navigate': 'first' |
41 | })) |
42 | |
43 | setupContextMenuAndSpellCheck(api.config.sync.load()) |
44 | |
45 | var id = api.keys.sync.id() |
46 | var latestUpdate = LatestUpdate() |
47 | |
48 | // prompt to setup profile on first use |
49 | onceTrue(api.sbot.obs.connection, (sbot) => { |
50 | sbot.latestSequence(sbot.id, (_, key) => { |
51 | if (key == null) { |
52 | api.profile.sheet.edit() |
53 | } |
54 | }) |
55 | }) |
56 | |
57 | var views = api.app.views(api.page.html.render, [ |
58 | '/public', '/private', '/gatherings', id, '/mentions' |
59 | ]) |
60 | |
61 | var pendingCount = computed([ |
62 | views.get('/public').pendingUpdates, |
63 | views.get('/private').pendingUpdates |
64 | ], (...counts) => { |
65 | return counts.reduce((a, b) => a + b) |
66 | }) |
67 | |
68 | watch(pendingCount, count => { |
69 | electron.remote.app.setBadgeCount(count) |
70 | }) |
71 | |
72 | |
73 | insertCss(require('./styles')) |
74 | |
75 | var container = h(`MainWindow -${process.platform}`, [ |
76 | h('div.top', [ |
77 | h('span.history', [ |
78 | h('a', { |
79 | 'ev-click': views.goBack, |
80 | classList: [ when(views.canGoBack, '-active') ] |
81 | }), |
82 | h('a', { |
83 | 'ev-click': views.goForward, |
84 | classList: [ when(views.canGoForward, '-active') ] |
85 | }) |
86 | ]), |
87 | h('span.nav', [ |
88 | tab('Public', '/public'), |
89 | tab('Private', '/private'), |
90 | tab('Gatherings', '/gatherings') |
91 | ]), |
92 | h('span.appTitle', ['Patchwork']), |
93 | h('span', [ api.app.html.search(api.app.navigate) ]), |
94 | h('span.nav', [ |
95 | tab('Profile', id), |
96 | tab('Mentions', '/mentions') |
97 | ]) |
98 | ]), |
99 | when(latestUpdate, |
100 | h('div.info', [ |
101 | h('a.message -update', { href: 'https://github.com/ssbc/patchwork/releases' }, [ |
102 | h('strong', ['Patchwork ', latestUpdate, ' has been released.']), ' Click here to download and view more info!' |
103 | ]) |
104 | ]) |
105 | ), |
106 | api.app.html.progressNotifier(), |
107 | views.html |
108 | ]) |
109 | |
110 | catchLinks(container, (href, external) => { |
111 | if (external) { |
112 | electron.shell.openExternal(href) |
113 | } else if (ref.isBlob(href)) { |
114 | electron.shell.openExternal(api.blob.sync.url(href)) |
115 | } else if (ref.isMsg(href)) { |
116 | getExternalHandler(href, (err, handler) => { |
117 | if (err) throw err |
118 | if (handler) { |
119 | handler(href) |
120 | } else { |
121 | api.app.navigate(href) |
122 | } |
123 | }) |
124 | } else { |
125 | api.app.navigate(href) |
126 | } |
127 | }) |
128 | |
129 | return container |
130 | |
131 | // scoped |
132 | |
133 | function setView (href) { |
134 | views.setView(href) |
135 | } |
136 | |
137 | function getExternalHandler (key, cb) { |
138 | api.sbot.async.get(key, function (err, value) { |
139 | if (err) return cb(err) |
140 | cb(null, api.app.sync.externalHandler({key, value})) |
141 | }) |
142 | } |
143 | |
144 | function tab (name, view) { |
145 | var instance = views.get(view) |
146 | return h('a', { |
147 | 'ev-click': function (ev) { |
148 | if (instance.pendingUpdates && instance.pendingUpdates() && instance.reload) { |
149 | instance.reload() |
150 | } |
151 | }, |
152 | href: view, |
153 | classList: [ |
154 | when(selected(view), '-selected') |
155 | ] |
156 | }, [ |
157 | name, |
158 | when(instance.pendingUpdates, [ |
159 | ' (', instance.pendingUpdates, ')' |
160 | ]) |
161 | ]) |
162 | } |
163 | |
164 | function selected (view) { |
165 | return computed([views.currentView, view], (currentView, view) => { |
166 | return currentView === view |
167 | }) |
168 | } |
169 | } |
170 | |
171 | function overrideConfig (config) { |
172 | return { |
173 | 'patchwork/config': { |
174 | gives: nest('config.sync.load'), |
175 | create: function (api) { |
176 | return nest('config.sync.load', () => config) |
177 | } |
178 | } |
179 | } |
180 | } |
181 | |
182 | function addCommand (id, cb) { |
183 | return { |
184 | [`patchwork/command/${id}`]: { |
185 | gives: nest(id), |
186 | create: function (api) { |
187 | return nest(id, cb) |
188 | } |
189 | } |
190 | } |
191 | } |
192 |
Built with git-ssb-web