Files: e79d34134f281a91928cfc9729fcc40cdc3a88d4 / main-window.js
7463 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 && handler) { |
140 | handler(href) |
141 | } else { |
142 | api.app.navigate(href, anchor) |
143 | } |
144 | }) |
145 | } else { |
146 | api.app.navigate(href, anchor) |
147 | } |
148 | }) |
149 | |
150 | return [container, previewElement] |
151 | |
152 | // scoped |
153 | |
154 | function getSubscribedChannelMenu () { |
155 | var channels = Array.from(subscribedChannels()).sort(localeCompare) |
156 | |
157 | if (channels.length) { |
158 | return { |
159 | label: i18n('Channels'), |
160 | submenu: [ |
161 | { label: i18n('Browse All'), |
162 | click () { |
163 | setView('/channels') |
164 | } |
165 | }, |
166 | {type: 'separator'} |
167 | ].concat(channels.map(channel => { |
168 | return { |
169 | label: `#${channel}`, |
170 | click () { |
171 | setView(`#${channel}`) |
172 | } |
173 | } |
174 | })) |
175 | } |
176 | } else { |
177 | return { |
178 | label: i18n('Browse Channels'), |
179 | click () { |
180 | setView('/channels') |
181 | } |
182 | } |
183 | } |
184 | } |
185 | |
186 | function dropTab (title, items) { |
187 | var element = h('a -drop', { |
188 | 'ev-click': (ev) => { |
189 | var rects = element.getBoundingClientRect() |
190 | electron.remote.getCurrentWindow().webContents.getZoomFactor((factor) => { |
191 | var menu = electron.remote.Menu.buildFromTemplate(items.map(item => { |
192 | if (typeof item === 'function') { |
193 | return item() |
194 | } else if (item.separator) { |
195 | return { type: 'separator' } |
196 | } else { |
197 | return { |
198 | label: item[0], |
199 | click () { |
200 | setView(item[1]) |
201 | } |
202 | } |
203 | } |
204 | })) |
205 | menu.popup(electron.remote.getCurrentWindow(), { |
206 | x: Math.round(rects.left * factor), |
207 | y: Math.round(rects.bottom * factor) + 4, |
208 | async: true |
209 | }) |
210 | }) |
211 | } |
212 | }, title) |
213 | return element |
214 | } |
215 | |
216 | function setView (href, anchor) { |
217 | previewElement.cancel() |
218 | views.setView(href, anchor) |
219 | } |
220 | |
221 | function getExternalHandler (key, cb) { |
222 | api.sbot.async.get(key, function (err, value) { |
223 | if (err) return cb(err) |
224 | cb(null, api.app.sync.externalHandler({key, value})) |
225 | }) |
226 | } |
227 | |
228 | function tab (name, view) { |
229 | var instance = views.get(view) |
230 | return h('a', { |
231 | 'ev-click': function (ev) { |
232 | if (instance.pendingUpdates && instance.pendingUpdates() && instance.reload) { |
233 | instance.reload() |
234 | } |
235 | }, |
236 | href: view, |
237 | classList: [ |
238 | when(selected(view), '-selected') |
239 | ] |
240 | }, [ |
241 | name, |
242 | when(instance.pendingUpdates, [ |
243 | ' (', instance.pendingUpdates, ')' |
244 | ]) |
245 | ]) |
246 | } |
247 | |
248 | function selected (view) { |
249 | return computed([views.currentView, view], (currentView, view) => { |
250 | return currentView === view |
251 | }) |
252 | } |
253 | } |
254 | |
255 | function overrideConfig (config) { |
256 | return { |
257 | 'patchwork/config': { |
258 | gives: nest('config.sync.load'), |
259 | create: function (api) { |
260 | return nest('config.sync.load', () => config) |
261 | } |
262 | } |
263 | } |
264 | } |
265 | |
266 | function addCommand (id, cb) { |
267 | return { |
268 | [`patchwork/command/${id}`]: { |
269 | gives: nest(id), |
270 | create: function (api) { |
271 | return nest(id, cb) |
272 | } |
273 | } |
274 | } |
275 | } |
276 | |
277 | function localeCompare (a, b) { |
278 | return a.localeCompare(b) |
279 | } |
280 |
Built with git-ssb-web