main-window.jsView |
---|
1 | 1 … | var combine = require('depject') |
2 | 2 … | var entry = require('depject/entry') |
3 | 3 … | var electron = require('electron') |
4 | 4 … | var h = require('mutant/h') |
5 | | -var Value = require('mutant/value') |
6 | 5 … | var when = require('mutant/when') |
7 | 6 … | var computed = require('mutant/computed') |
8 | | -var toCollection = require('mutant/dict-to-collection') |
9 | | -var MutantDict = require('mutant/dict') |
10 | | -var MutantMap = require('mutant/map') |
11 | | -var Url = require('url') |
| 7 … | +var catchLinks = require('./lib/catch-links') |
12 | 8 … | var insertCss = require('insert-css') |
13 | 9 … | var nest = require('depnest') |
14 | 10 … | var LatestUpdate = require('./lib/latest-update') |
15 | 11 … | |
24 | 20 … | require('./overrides') |
25 | 21 … | ) |
26 | 22 … | |
27 | 23 … | var api = entry(sockets, nest({ |
28 | | - 'page.html.render': 'first', |
29 | 24 … | 'keys.sync.id': 'first', |
30 | 25 … | 'blob.sync.url': 'first', |
31 | | - 'app.html.search': 'first' |
| 26 … | + 'page.html.render': 'first', |
| 27 … | + 'app.html.search': 'first', |
| 28 … | + 'app.views': 'first' |
32 | 29 … | })) |
33 | 30 … | |
34 | | - var renderPage = api.page.html.render |
35 | 31 … | var id = api.keys.sync.id() |
36 | 32 … | var latestUpdate = LatestUpdate() |
37 | 33 … | |
38 | | - var forwardHistory = [] |
39 | | - var backHistory = [] |
| 34 … | + var views = api.app.views(api.page.html.render, [ |
| 35 … | + '/public', '/private', id, '/mentions' |
| 36 … | + ]) |
40 | 37 … | |
41 | | - var views = MutantDict({ |
42 | | - |
43 | | - '/public': renderPage('/public'), |
44 | | - '/private': renderPage('/private'), |
45 | | - [id]: renderPage(id), |
46 | | - '/mentions': renderPage('/mentions') |
47 | | - }) |
48 | | - |
49 | | - var lastViewed = {} |
50 | | - var defaultViews = views.keys() |
51 | | - |
52 | | - |
53 | | - setInterval(() => { |
54 | | - views.keys().forEach((view) => { |
55 | | - if (!defaultViews.includes(view)) { |
56 | | - if (lastViewed[view] !== true && Date.now() - lastViewed[view] > (5 * 60e3) && view !== currentView()) { |
57 | | - views.delete(view) |
58 | | - } |
59 | | - } |
60 | | - }) |
61 | | - }, 60e3) |
62 | | - |
63 | | - var canGoForward = Value(false) |
64 | | - var canGoBack = Value(false) |
65 | | - var currentView = Value('/public') |
66 | | - |
67 | | - var viewCollection = toCollection(views) |
68 | | - |
69 | | - var mainElement = h('div.main', MutantMap(viewCollection, (item) => { |
70 | | - return h('div.view', { |
71 | | - hidden: computed([item.key, currentView], (a, b) => a !== b) |
72 | | - }, [ item.value ]) |
73 | | - })) |
74 | | - |
75 | 38 … | insertCss(require('./styles')) |
76 | 39 … | |
77 | | - var container = h(`MainWindow -${process.platform}`, { |
78 | | - events: { |
79 | | - click: catchLinks |
80 | | - } |
81 | | - }, [ |
| 40 … | + var container = h(`MainWindow -${process.platform}`, [ |
82 | 41 … | h('div.top', [ |
83 | 42 … | h('span.history', [ |
84 | 43 … | h('a', { |
85 | | - 'ev-click': goBack, |
86 | | - classList: [ when(canGoBack, '-active') ] |
| 44 … | + 'ev-click': views.goBack, |
| 45 … | + classList: [ when(views.canGoBack, '-active') ] |
87 | 46 … | }, '<'), |
88 | 47 … | h('a', { |
89 | | - 'ev-click': goForward, |
90 | | - classList: [ when(canGoForward, '-active') ] |
| 48 … | + 'ev-click': views.goForward, |
| 49 … | + classList: [ when(views.canGoForward, '-active') ] |
91 | 50 … | }, '>') |
92 | 51 … | ]), |
93 | 52 … | h('span.nav', [ |
94 | 53 … | tab('Public', '/public'), |
95 | 54 … | tab('Private', '/private') |
96 | 55 … | ]), |
97 | 56 … | h('span.appTitle', ['Patchwork']), |
98 | | - h('span', [ api.app.html.search(setView) ]), |
| 57 … | + h('span', [ api.app.html.search(views.setView) ]), |
99 | 58 … | h('span.nav', [ |
100 | 59 … | tab('Profile', id), |
101 | 60 … | tab('Mentions', '/mentions') |
102 | 61 … | ]) |
107 | 66 … | h('strong', ['Patchwork ', latestUpdate, ' has been released.']), ' Click here for more info!' |
108 | 67 … | ]) |
109 | 68 … | ]) |
110 | 69 … | ), |
111 | | - mainElement |
| 70 … | + views.html |
112 | 71 … | ]) |
113 | 72 … | |
| 73 … | + catchLinks(container, (href, external) => { |
| 74 … | + if (external) { |
| 75 … | + electron.shell.openExternal(href) |
| 76 … | + } else if (href[0] === '&') { |
| 77 … | + electron.shell.openExternal(api.blob.sync.url(href)) |
| 78 … | + } else { |
| 79 … | + views.setView(href) |
| 80 … | + } |
| 81 … | + }) |
| 82 … | + |
114 | 83 … | return container |
115 | 84 … | |
116 | 85 … | |
117 | 86 … | |
118 | | - function catchLinks (ev) { |
119 | | - if (ev.altKey || ev.ctrlKey || ev.metaKey || ev.shiftKey || ev.defaultPrevented) { |
120 | | - return true |
121 | | - } |
122 | | - |
123 | | - var anchor = null |
124 | | - for (var n = ev.target; n.parentNode; n = n.parentNode) { |
125 | | - if (n.nodeName === 'A') { |
126 | | - anchor = n |
127 | | - break |
128 | | - } |
129 | | - } |
130 | | - if (!anchor) return true |
131 | | - |
132 | | - var href = anchor.getAttribute('href') |
133 | | - |
134 | | - if (href) { |
135 | | - var url = Url.parse(href) |
136 | | - if (url.host) { |
137 | | - electron.shell.openExternal(href) |
138 | | - } else if (href.charAt(0) === '&') { |
139 | | - electron.shell.openExternal(api.blob.sync.url(href)) |
140 | | - } else if (href !== '#') { |
141 | | - setView(href) |
142 | | - } |
143 | | - } |
144 | | - |
145 | | - ev.preventDefault() |
146 | | - ev.stopPropagation() |
147 | | - } |
148 | | - |
149 | 87 … | function tab (name, view) { |
150 | 88 … | var instance = views.get(view) |
151 | | - lastViewed[view] = true |
152 | 89 … | return h('a', { |
153 | 90 … | 'ev-click': function (ev) { |
154 | 91 … | if (instance.pendingUpdates && instance.pendingUpdates() && instance.reload) { |
155 | 92 … | instance.reload() |
166 | 103 … | ]) |
167 | 104 … | ]) |
168 | 105 … | } |
169 | 106 … | |
170 | | - function goBack () { |
171 | | - if (backHistory.length) { |
172 | | - canGoForward.set(true) |
173 | | - forwardHistory.push(currentView()) |
174 | | - |
175 | | - var view = backHistory.pop() |
176 | | - if (!views.has(view)) { |
177 | | - views.put(view, renderPage(view)) |
178 | | - } |
179 | | - |
180 | | - currentView.set(view) |
181 | | - canGoBack.set(backHistory.length > 0) |
182 | | - } |
183 | | - } |
184 | | - |
185 | | - function goForward () { |
186 | | - if (forwardHistory.length) { |
187 | | - backHistory.push(currentView()) |
188 | | - |
189 | | - var view = forwardHistory.pop() |
190 | | - if (!views.has(view)) { |
191 | | - views.put(view, renderPage(view)) |
192 | | - } |
193 | | - |
194 | | - currentView.set(view) |
195 | | - canGoForward.set(forwardHistory.length > 0) |
196 | | - canGoBack.set(true) |
197 | | - } |
198 | | - } |
199 | | - |
200 | | - function setView (view) { |
201 | | - if (!views.has(view)) { |
202 | | - views.put(view, renderPage(view)) |
203 | | - } |
204 | | - |
205 | | - if (lastViewed[view] !== true) { |
206 | | - lastViewed[view] = Date.now() |
207 | | - } |
208 | | - |
209 | | - if (currentView() && lastViewed[currentView()] !== true) { |
210 | | - lastViewed[currentView()] = Date.now() |
211 | | - } |
212 | | - |
213 | | - if (view !== currentView()) { |
214 | | - canGoForward.set(false) |
215 | | - canGoBack.set(true) |
216 | | - forwardHistory.length = 0 |
217 | | - backHistory.push(currentView()) |
218 | | - currentView.set(view) |
219 | | - } |
220 | | - } |
221 | | - |
222 | 107 … | function selected (view) { |
223 | | - return computed([currentView, view], (currentView, view) => { |
| 108 … | + return computed([views.currentView, view], (currentView, view) => { |
224 | 109 … | return currentView === view |
225 | 110 … | }) |
226 | 111 … | } |
227 | 112 … | } |