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