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