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