Files: 60000ec734ec84dab4b2b19eb518cb6689764bfc / main-window.js
7509 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 | module.exports = function (config) { |
17 | var sockets = combine( |
18 | overrideConfig(config), |
19 | addCommand('app.navigate', setView), |
20 | require('./modules'), |
21 | require('./plugs'), |
22 | require('patch-settings'), |
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 | 'profile.html.preview': 'first', |
41 | 'app.navigate': 'first', |
42 | 'app.linkPreview': 'first', |
43 | 'channel.obs.subscribed': 'first', |
44 | 'settings.obs.get': 'first', |
45 | 'intl.sync.i18n': 'first' |
46 | })) |
47 | |
48 | setupContextMenuAndSpellCheck(api.config.sync.load()) |
49 | |
50 | const i18n = api.intl.sync.i18n |
51 | |
52 | var id = api.keys.sync.id() |
53 | var latestUpdate = LatestUpdate() |
54 | var subscribedChannels = api.channel.obs.subscribed(id) |
55 | |
56 | // prompt to setup profile on first use |
57 | onceTrue(api.sbot.obs.connection, (sbot) => { |
58 | sbot.latestSequence(sbot.id, (_, key) => { |
59 | if (key == null) { |
60 | api.profile.sheet.edit() |
61 | } |
62 | }) |
63 | }) |
64 | |
65 | var views = api.app.views(api.page.html.render, [ |
66 | '/public', '/private', id, '/mentions' |
67 | ]) |
68 | |
69 | var pendingCount = views.get('/mentions').pendingUpdates |
70 | |
71 | watch(pendingCount, count => { |
72 | electron.remote.app.setBadgeCount(count) |
73 | }) |
74 | |
75 | |
76 | watch(api.settings.obs.get('patchwork.theme', 'light'), name => { |
77 | Array.from(document.head.children) |
78 | .filter(c => c.tagName == 'STYLE') |
79 | .forEach(c => c.innerText = '') |
80 | |
81 | var theme = require('./styles')[name] |
82 | if (!theme) theme = require('./styles')['light'] |
83 | insertCss(theme) |
84 | }) |
85 | |
86 | var container = h(`MainWindow -${process.platform}`, [ |
87 | h('div.top', [ |
88 | h('span.history', [ |
89 | h('a', { |
90 | 'ev-click': views.goBack, |
91 | classList: [ when(views.canGoBack, '-active') ] |
92 | }), |
93 | h('a', { |
94 | 'ev-click': views.goForward, |
95 | classList: [ when(views.canGoForward, '-active') ] |
96 | }) |
97 | ]), |
98 | h('span.nav', [ |
99 | tab(i18n("Public"), '/public'), |
100 | tab(i18n("Private"), '/private'), |
101 | dropTab(i18n('More'), [ |
102 | getSubscribedChannelMenu, |
103 | [i18n('Gatherings'), '/gatherings'], |
104 | [i18n('Extended Network'), '/all'], |
105 | {separator: true}, |
106 | [i18n('Settings'), '/settings'] |
107 | ]) |
108 | ]), |
109 | h('span.appTitle', [ |
110 | h('span.title', i18n("Patchwork")), |
111 | api.app.html.progressNotifier() |
112 | ]), |
113 | h('span', [ api.app.html.search(api.app.navigate) ]), |
114 | h('span.nav', [ |
115 | tab(i18n('Profile'), id), |
116 | tab(i18n('Mentions'), '/mentions') |
117 | ]) |
118 | ]), |
119 | when(latestUpdate, |
120 | h('div.info', [ |
121 | h('a.message -update', { href: 'https://github.com/ssbc/patchwork/releases' }, [ |
122 | h('strong', ['Patchwork ', latestUpdate, i18n(' has been released.')]), i18n(' Click here to download and view more info!'), |
123 | h('a.ignore', {'ev-click': latestUpdate.ignore}, 'X') |
124 | ]) |
125 | ]) |
126 | ), |
127 | views.html |
128 | ]) |
129 | |
130 | var previewElement = api.app.linkPreview(container, 500) |
131 | |
132 | catchLinks(container, (href, external, anchor) => { |
133 | if (external) { |
134 | electron.shell.openExternal(href) |
135 | } else if (ref.isBlob(href)) { |
136 | electron.shell.openExternal(api.blob.sync.url(href)) |
137 | } else if (ref.isMsg(href)) { |
138 | getExternalHandler(href, (err, handler) => { |
139 | if (err) throw err |
140 | if (handler) { |
141 | handler(href) |
142 | } else { |
143 | api.app.navigate(href, anchor) |
144 | } |
145 | }) |
146 | } else { |
147 | api.app.navigate(href, anchor) |
148 | } |
149 | }) |
150 | |
151 | return [container, previewElement] |
152 | |
153 | // scoped |
154 | |
155 | function getSubscribedChannelMenu () { |
156 | var channels = Array.from(subscribedChannels()).sort(localeCompare) |
157 | |
158 | if (channels.length) { |
159 | return { |
160 | label: i18n('Channels'), |
161 | submenu: [ |
162 | { label: i18n('Browse All'), |
163 | click () { |
164 | setView('/channels') |
165 | } |
166 | }, |
167 | {type: 'separator'} |
168 | ].concat(channels.map(channel => { |
169 | return { |
170 | label: `#${channel}`, |
171 | click () { |
172 | setView(`#${channel}`) |
173 | } |
174 | } |
175 | })) |
176 | } |
177 | } else { |
178 | return { |
179 | label: i18n('Browse Channels'), |
180 | click () { |
181 | setView('/channels') |
182 | } |
183 | } |
184 | } |
185 | } |
186 | |
187 | function dropTab (title, items) { |
188 | var element = h('a -drop', { |
189 | 'ev-click': (ev) => { |
190 | var rects = element.getBoundingClientRect() |
191 | electron.remote.getCurrentWindow().webContents.getZoomFactor((factor) => { |
192 | var menu = electron.remote.Menu.buildFromTemplate(items.map(item => { |
193 | if (typeof item === 'function') { |
194 | return item() |
195 | } else if (item.separator) { |
196 | return { type: 'separator' } |
197 | } else { |
198 | return { |
199 | label: item[0], |
200 | click () { |
201 | setView(item[1]) |
202 | } |
203 | } |
204 | } |
205 | })) |
206 | menu.popup(electron.remote.getCurrentWindow(), { |
207 | x: Math.round(rects.left * factor), |
208 | y: Math.round(rects.bottom * factor) + 4, |
209 | async: true |
210 | }) |
211 | }) |
212 | } |
213 | }, title) |
214 | return element |
215 | } |
216 | |
217 | function setView (href, anchor) { |
218 | currentHover.cancel() |
219 | previewElement.set(null) |
220 | views.setView(href, anchor) |
221 | } |
222 | |
223 | function getExternalHandler (key, cb) { |
224 | api.sbot.async.get(key, function (err, value) { |
225 | if (err) return cb(err) |
226 | cb(null, api.app.sync.externalHandler({key, value})) |
227 | }) |
228 | } |
229 | |
230 | function tab (name, view) { |
231 | var instance = views.get(view) |
232 | return h('a', { |
233 | 'ev-click': function (ev) { |
234 | if (instance.pendingUpdates && instance.pendingUpdates() && instance.reload) { |
235 | instance.reload() |
236 | } |
237 | }, |
238 | href: view, |
239 | classList: [ |
240 | when(selected(view), '-selected') |
241 | ] |
242 | }, [ |
243 | name, |
244 | when(instance.pendingUpdates, [ |
245 | ' (', instance.pendingUpdates, ')' |
246 | ]) |
247 | ]) |
248 | } |
249 | |
250 | function selected (view) { |
251 | return computed([views.currentView, view], (currentView, view) => { |
252 | return currentView === view |
253 | }) |
254 | } |
255 | } |
256 | |
257 | function overrideConfig (config) { |
258 | return { |
259 | 'patchwork/config': { |
260 | gives: nest('config.sync.load'), |
261 | create: function (api) { |
262 | return nest('config.sync.load', () => config) |
263 | } |
264 | } |
265 | } |
266 | } |
267 | |
268 | function addCommand (id, cb) { |
269 | return { |
270 | [`patchwork/command/${id}`]: { |
271 | gives: nest(id), |
272 | create: function (api) { |
273 | return nest(id, cb) |
274 | } |
275 | } |
276 | } |
277 | } |
278 | |
279 | function localeCompare (a, b) { |
280 | return a.localeCompare(b) |
281 | } |
282 |
Built with git-ssb-web