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