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