Files: 6a4dd175b0e6195afeec9be9edf7a1e0723dfce0 / main-window.js
4923 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('patchcore'), |
23 | require('./overrides') |
24 | ) |
25 | |
26 | var api = entry(sockets, nest({ |
27 | 'config.sync.load': 'first', |
28 | 'keys.sync.id': 'first', |
29 | 'sbot.obs.connection': 'first', |
30 | 'sbot.async.get': 'first', |
31 | 'blob.sync.url': 'first', |
32 | 'page.html.render': 'first', |
33 | 'app.html.search': 'first', |
34 | 'app.html.channels': 'first', |
35 | 'app.views': 'first', |
36 | 'app.sync.externalHandler': 'first', |
37 | 'app.html.progressNotifier': 'first', |
38 | 'profile.sheet.edit': 'first', |
39 | 'app.navigate': 'first' |
40 | })) |
41 | |
42 | setupContextMenuAndSpellCheck(api.config.sync.load()) |
43 | |
44 | var id = api.keys.sync.id() |
45 | var latestUpdate = LatestUpdate() |
46 | |
47 | // prompt to setup profile on first use |
48 | onceTrue(api.sbot.obs.connection, (sbot) => { |
49 | sbot.latestSequence(sbot.id, (_, key) => { |
50 | if (key == null) { |
51 | api.profile.sheet.edit() |
52 | } |
53 | }) |
54 | }) |
55 | |
56 | var views = api.app.views(api.page.html.render, [ |
57 | '/public', '/private', '/gatherings', id, '/mentions' |
58 | ]) |
59 | |
60 | var pendingCount = computed([ |
61 | views.get('/public').pendingUpdates, |
62 | views.get('/private').pendingUpdates |
63 | ], (...counts) => { |
64 | return counts.reduce((a, b) => a + b) |
65 | }) |
66 | |
67 | watch(pendingCount, count => { |
68 | electron.remote.app.setBadgeCount(count) |
69 | }) |
70 | |
71 | insertCss(require('./styles')) |
72 | |
73 | var container = h(`MainWindow -${process.platform}`, [ |
74 | h('div.top', [ |
75 | h('span.history', [ |
76 | h('a', { |
77 | 'ev-click': views.goBack, |
78 | classList: [ when(views.canGoBack, '-active') ] |
79 | }), |
80 | h('a', { |
81 | 'ev-click': views.goForward, |
82 | classList: [ when(views.canGoForward, '-active') ] |
83 | }) |
84 | ]), |
85 | h('span.nav', [ |
86 | tab('Public', '/public'), |
87 | tab('Private', '/private'), |
88 | tab('Gatherings', '/gatherings') |
89 | ]), |
90 | h('span.appTitle', ['Patchwork']), |
91 | h('span', [ api.app.html.search(api.app.navigate) ]), |
92 | h('span.nav', [ |
93 | tab('Profile', id), |
94 | tab('Mentions', '/mentions') |
95 | ]) |
96 | ]), |
97 | when(latestUpdate, |
98 | h('div.info', [ |
99 | h('a.message -update', { href: 'https://github.com/ssbc/patchwork/releases' }, [ |
100 | h('strong', ['Patchwork ', latestUpdate, ' has been released.']), ' Click here to download and view more info!', |
101 | h('a.ignore', {'ev-click': latestUpdate.ignore}, 'X') |
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