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