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