Files: 47128cd0a170a7691269a44d297af30a324c3a4a / main-window.js
4806 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 | |
17 | module.exports = function (config) { |
18 | var sockets = combine( |
19 | overrideConfig(config), |
20 | addCommand('app.navigate', setView), |
21 | require('patchwork-gatherings'), |
22 | require('./modules'), |
23 | require('./plugs'), |
24 | require('patchcore'), |
25 | require('./overrides') |
26 | ) |
27 | |
28 | var api = entry(sockets, nest({ |
29 | 'config.sync.load': 'first', |
30 | 'keys.sync.id': 'first', |
31 | 'sbot.obs.connection': 'first', |
32 | 'sbot.async.get': 'first', |
33 | 'blob.sync.url': 'first', |
34 | 'page.html.render': 'first', |
35 | 'app.html.search': '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 | })) |
42 | |
43 | setupContextMenuAndSpellCheck(api.config.sync.load()) |
44 | |
45 | var id = api.keys.sync.id() |
46 | var latestUpdate = LatestUpdate() |
47 | |
48 | // prompt to setup profile on first use |
49 | onceTrue(api.sbot.obs.connection, (sbot) => { |
50 | sbot.latestSequence(sbot.id, (_, key) => { |
51 | if (key == null) { |
52 | api.profile.sheet.edit() |
53 | } |
54 | }) |
55 | }) |
56 | |
57 | var views = api.app.views(api.page.html.render, [ |
58 | '/public', '/private', id, '/mentions' |
59 | ]) |
60 | |
61 | var pendingCount = computed([ |
62 | views.get('/public').pendingUpdates, |
63 | views.get('/private').pendingUpdates |
64 | ], (...counts) => { |
65 | return counts.reduce((a, b) => a + b) |
66 | }) |
67 | |
68 | watch(pendingCount, count => { |
69 | electron.remote.app.setBadgeCount(count) |
70 | }) |
71 | |
72 | |
73 | insertCss(require('./styles')) |
74 | |
75 | var container = h(`MainWindow -${process.platform}`, [ |
76 | h('div.top', [ |
77 | h('span.history', [ |
78 | h('a', { |
79 | 'ev-click': views.goBack, |
80 | classList: [ when(views.canGoBack, '-active') ] |
81 | }), |
82 | h('a', { |
83 | 'ev-click': views.goForward, |
84 | classList: [ when(views.canGoForward, '-active') ] |
85 | }) |
86 | ]), |
87 | h('span.nav', [ |
88 | tab('Public', '/public'), |
89 | tab('Private', '/private') |
90 | ]), |
91 | h('span.appTitle', ['Patchwork']), |
92 | h('span', [ api.app.html.search(api.app.navigate) ]), |
93 | h('span.nav', [ |
94 | tab('Profile', id), |
95 | tab('Mentions', '/mentions') |
96 | ]) |
97 | ]), |
98 | when(latestUpdate, |
99 | h('div.info', [ |
100 | h('a.message -update', { href: 'https://github.com/ssbc/patchwork/releases' }, [ |
101 | h('strong', ['Patchwork ', latestUpdate, ' has been released.']), ' Click here to download and view more info!' |
102 | ]) |
103 | ]) |
104 | ), |
105 | api.app.html.progressNotifier(), |
106 | views.html |
107 | ]) |
108 | |
109 | catchLinks(container, (href, external) => { |
110 | if (external) { |
111 | electron.shell.openExternal(href) |
112 | } else if (ref.isBlob(href)) { |
113 | electron.shell.openExternal(api.blob.sync.url(href)) |
114 | } else if (ref.isMsg(href)) { |
115 | getExternalHandler(href, (err, handler) => { |
116 | if (err) throw err |
117 | if (handler) { |
118 | handler(href) |
119 | } else { |
120 | api.app.navigate(href) |
121 | } |
122 | }) |
123 | } else { |
124 | api.app.navigate(href) |
125 | } |
126 | }) |
127 | |
128 | return container |
129 | |
130 | // scoped |
131 | |
132 | function setView (href) { |
133 | views.setView(href) |
134 | } |
135 | |
136 | function getExternalHandler (key, cb) { |
137 | api.sbot.async.get(key, function (err, value) { |
138 | if (err) return cb(err) |
139 | cb(null, api.app.sync.externalHandler({key, value})) |
140 | }) |
141 | } |
142 | |
143 | function tab (name, view) { |
144 | var instance = views.get(view) |
145 | return h('a', { |
146 | 'ev-click': function (ev) { |
147 | if (instance.pendingUpdates && instance.pendingUpdates() && instance.reload) { |
148 | instance.reload() |
149 | } |
150 | }, |
151 | href: view, |
152 | classList: [ |
153 | when(selected(view), '-selected') |
154 | ] |
155 | }, [ |
156 | name, |
157 | when(instance.pendingUpdates, [ |
158 | ' (', instance.pendingUpdates, ')' |
159 | ]) |
160 | ]) |
161 | } |
162 | |
163 | function selected (view) { |
164 | return computed([views.currentView, view], (currentView, view) => { |
165 | return currentView === view |
166 | }) |
167 | } |
168 | } |
169 | |
170 | function overrideConfig (config) { |
171 | return { |
172 | 'patchwork/config': { |
173 | gives: nest('config.sync.load'), |
174 | create: function (api) { |
175 | return nest('config.sync.load', () => config) |
176 | } |
177 | } |
178 | } |
179 | } |
180 | |
181 | function addCommand (id, cb) { |
182 | return { |
183 | [`patchwork/command/${id}`]: { |
184 | gives: nest(id), |
185 | create: function (api) { |
186 | return nest(id, cb) |
187 | } |
188 | } |
189 | } |
190 | } |
191 |
Built with git-ssb-web