Files: d170e66ba2918935e031c59b154e8a2b42afb08e / main-window.js
7801 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 themes = require('./styles') |
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 | document.head.appendChild( |
76 | h('style', { |
77 | innerHTML: computed(api.settings.obs.get('patchwork.theme', 'light'), themeName => { |
78 | return themes[themeName] || themes['light'] |
79 | }) |
80 | }) |
81 | ) |
82 | |
83 | document.head.appendChild( |
84 | h('style', { |
85 | innerHTML: computed(api.settings.obs.get('patchwork.fontSize'), size => { |
86 | if (size) { |
87 | return 'html, body {font-size: ' + size + ';}' |
88 | } |
89 | }) |
90 | }) |
91 | ) |
92 | |
93 | var container = h(`MainWindow -${process.platform}`, [ |
94 | h('div.top', [ |
95 | h('span.history', [ |
96 | h('a', { |
97 | 'ev-click': views.goBack, |
98 | classList: [ when(views.canGoBack, '-active') ] |
99 | }), |
100 | h('a', { |
101 | 'ev-click': views.goForward, |
102 | classList: [ when(views.canGoForward, '-active') ] |
103 | }) |
104 | ]), |
105 | h('span.nav', [ |
106 | tab(i18n('Public'), '/public'), |
107 | tab(i18n('Private'), '/private'), |
108 | dropTab(i18n('More'), [ |
109 | getSubscribedChannelMenu, |
110 | [i18n('Gatherings'), '/gatherings'], |
111 | [i18n('Extended Network'), '/all'], |
112 | {separator: true}, |
113 | [i18n('Settings'), '/settings'] |
114 | ]) |
115 | ]), |
116 | h('span.appTitle', [ |
117 | h('span.title', i18n('Patchwork')), |
118 | api.app.html.progressNotifier() |
119 | ]), |
120 | h('span', [ api.app.html.search(api.app.navigate) ]), |
121 | h('span.nav', [ |
122 | tab(i18n('Profile'), id), |
123 | tab(i18n('Mentions'), '/mentions') |
124 | ]) |
125 | ]), |
126 | when(latestUpdate, |
127 | h('div.info', [ |
128 | h('a.message -update', { href: 'https://github.com/ssbc/patchwork/releases' }, [ |
129 | h('strong', ['Patchwork ', latestUpdate, i18n(' has been released.')]), i18n(' Click here to download and view more info!'), |
130 | h('a.ignore', {'ev-click': latestUpdate.ignore}, 'X') |
131 | ]) |
132 | ]) |
133 | ), |
134 | views.html |
135 | ]) |
136 | |
137 | var previewElement = api.app.linkPreview(container, 500) |
138 | |
139 | catchLinks(container, (href, external, anchor) => { |
140 | if (external) { |
141 | electron.shell.openExternal(href) |
142 | } else if (ref.isBlob(href)) { |
143 | electron.shell.openExternal(api.blob.sync.url(href)) |
144 | } else if (ref.isMsg(href)) { |
145 | getExternalHandler(href, (err, handler) => { |
146 | if (!err && handler) { |
147 | handler(href) |
148 | } else { |
149 | api.app.navigate(href, anchor) |
150 | } |
151 | }) |
152 | } else { |
153 | api.app.navigate(href, anchor) |
154 | } |
155 | }) |
156 | |
157 | return [container, previewElement] |
158 | |
159 | // scoped |
160 | |
161 | function getSubscribedChannelMenu () { |
162 | var channels = Array.from(subscribedChannels()).sort(localeCompare) |
163 | |
164 | if (channels.length) { |
165 | return { |
166 | label: i18n('Channels'), |
167 | submenu: [ |
168 | { label: i18n('Browse All'), |
169 | click () { |
170 | setView('/channels') |
171 | } |
172 | }, |
173 | {type: 'separator'} |
174 | ].concat(channels.map(channel => { |
175 | return { |
176 | label: `#${channel}`, |
177 | click () { |
178 | setView(`#${channel}`) |
179 | } |
180 | } |
181 | })) |
182 | } |
183 | } else { |
184 | return { |
185 | label: i18n('Browse Channels'), |
186 | click () { |
187 | setView('/channels') |
188 | } |
189 | } |
190 | } |
191 | } |
192 | |
193 | function dropTab (title, items) { |
194 | var element = h('a -drop', { |
195 | 'ev-click': (ev) => { |
196 | var rects = element.getBoundingClientRect() |
197 | electron.remote.getCurrentWindow().webContents.getZoomFactor((factor) => { |
198 | var menu = electron.remote.Menu.buildFromTemplate(items.map(item => { |
199 | if (typeof item === 'function') { |
200 | return item() |
201 | } else if (item.separator) { |
202 | return { type: 'separator' } |
203 | } else { |
204 | return { |
205 | label: item[0], |
206 | click () { |
207 | setView(item[1]) |
208 | } |
209 | } |
210 | } |
211 | })) |
212 | menu.popup(electron.remote.getCurrentWindow(), { |
213 | x: Math.round(rects.left * factor), |
214 | y: Math.round(rects.bottom * factor) + 4, |
215 | async: true |
216 | }) |
217 | }) |
218 | } |
219 | }, title) |
220 | return element |
221 | } |
222 | |
223 | function setView (href, anchor) { |
224 | previewElement.cancel() |
225 | views.setView(href, anchor) |
226 | } |
227 | |
228 | function getExternalHandler (key, cb) { |
229 | api.sbot.async.get(key, function (err, value) { |
230 | if (err) return cb(err) |
231 | cb(null, api.app.sync.externalHandler({key, value})) |
232 | }) |
233 | } |
234 | |
235 | function tab (name, view) { |
236 | var instance = views.get(view) |
237 | return h('a', { |
238 | 'ev-click': function (ev) { |
239 | var isSelected = views.currentView() === view |
240 | var needsRefresh = instance.pendingUpdates && instance.pendingUpdates() |
241 | |
242 | // refresh if tab is clicked when there are pending items or the page is already selected |
243 | if ((needsRefresh || isSelected) && instance.reload) { |
244 | instance.reload() |
245 | } |
246 | }, |
247 | href: view, |
248 | classList: [ |
249 | when(selected(view), '-selected') |
250 | ] |
251 | }, [ |
252 | name, |
253 | when(instance.pendingUpdates, [ |
254 | ' (', instance.pendingUpdates, ')' |
255 | ]) |
256 | ]) |
257 | } |
258 | |
259 | function selected (view) { |
260 | return computed([views.currentView, view], (currentView, view) => { |
261 | return currentView === view |
262 | }) |
263 | } |
264 | } |
265 | |
266 | function overrideConfig (config) { |
267 | return { |
268 | 'patchwork/config': { |
269 | gives: nest('config.sync.load'), |
270 | create: function (api) { |
271 | return nest('config.sync.load', () => config) |
272 | } |
273 | } |
274 | } |
275 | } |
276 | |
277 | function addCommand (id, cb) { |
278 | return { |
279 | [`patchwork/command/${id}`]: { |
280 | gives: nest(id), |
281 | create: function (api) { |
282 | return nest(id, cb) |
283 | } |
284 | } |
285 | } |
286 | } |
287 | |
288 | function localeCompare (a, b) { |
289 | return a.localeCompare(b) |
290 | } |
291 |
Built with git-ssb-web