git ssb

10+

Matt McKegg / patchwork



Commit 74f49434445c5d9e01f6946c68205bfb4d8f1af1

extract view/rendering navigation, catchLinks from main-window

Matt McKegg committed on 2/21/2017, 2:07:18 AM
Parent: 81be424f72e758de508ff828259c7c30cea65236

Files changed

lib/catch-links.jsadded
main-window.jschanged
modules/app/views.jsadded
lib/catch-links.jsView
@@ -1,0 +1,32 @@
1 +var Url = require('url')
2 +
3 +module.exports = function (root, cb) {
4 + root.addEventListener('click', (ev) => {
5 + if (ev.altKey || ev.ctrlKey || ev.metaKey || ev.shiftKey || ev.defaultPrevented) {
6 + return true
7 + }
8 +
9 + var anchor = null
10 + for (var n = ev.target; n.parentNode; n = n.parentNode) {
11 + if (n.nodeName === 'A') {
12 + anchor = n
13 + break
14 + }
15 + }
16 + if (!anchor) return true
17 +
18 + var href = anchor.getAttribute('href')
19 +
20 + if (href) {
21 + var url = Url.parse(href)
22 + if (url.host) {
23 + cb(href, true)
24 + } else if (href !== '#') {
25 + cb(href, false)
26 + }
27 + }
28 +
29 + ev.preventDefault()
30 + ev.stopPropagation()
31 + })
32 +}
main-window.jsView
@@ -1,15 +1,11 @@
11 var combine = require('depject')
22 var entry = require('depject/entry')
33 var electron = require('electron')
44 var h = require('mutant/h')
5-var Value = require('mutant/value')
65 var when = require('mutant/when')
76 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')
128 var insertCss = require('insert-css')
139 var nest = require('depnest')
1410 var LatestUpdate = require('./lib/latest-update')
1511
@@ -24,79 +20,42 @@
2420 require('./overrides')
2521 )
2622
2723 var api = entry(sockets, nest({
28- 'page.html.render': 'first',
2924 'keys.sync.id': 'first',
3025 'blob.sync.url': 'first',
31- 'app.html.search': 'first'
26 + 'page.html.render': 'first',
27 + 'app.html.search': 'first',
28 + 'app.views': 'first'
3229 }))
3330
34- var renderPage = api.page.html.render
3531 var id = api.keys.sync.id()
3632 var latestUpdate = LatestUpdate()
3733
38- var forwardHistory = []
39- var backHistory = []
34 + var views = api.app.views(api.page.html.render, [
35 + '/public', '/private', id, '/mentions'
36 + ])
4037
41- var views = MutantDict({
42- // preload tabs (and subscribe to update notifications)
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- // delete cached view after 5 mins of last seeing
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-
7538 insertCss(require('./styles'))
7639
77- var container = h(`MainWindow -${process.platform}`, {
78- events: {
79- click: catchLinks
80- }
81- }, [
40 + var container = h(`MainWindow -${process.platform}`, [
8241 h('div.top', [
8342 h('span.history', [
8443 h('a', {
85- 'ev-click': goBack,
86- classList: [ when(canGoBack, '-active') ]
44 + 'ev-click': views.goBack,
45 + classList: [ when(views.canGoBack, '-active') ]
8746 }, '<'),
8847 h('a', {
89- 'ev-click': goForward,
90- classList: [ when(canGoForward, '-active') ]
48 + 'ev-click': views.goForward,
49 + classList: [ when(views.canGoForward, '-active') ]
9150 }, '>')
9251 ]),
9352 h('span.nav', [
9453 tab('Public', '/public'),
9554 tab('Private', '/private')
9655 ]),
9756 h('span.appTitle', ['Patchwork']),
98- h('span', [ api.app.html.search(setView) ]),
57 + h('span', [ api.app.html.search(views.setView) ]),
9958 h('span.nav', [
10059 tab('Profile', id),
10160 tab('Mentions', '/mentions')
10261 ])
@@ -107,49 +66,27 @@
10766 h('strong', ['Patchwork ', latestUpdate, ' has been released.']), ' Click here for more info!'
10867 ])
10968 ])
11069 ),
111- mainElement
70 + views.html
11271 ])
11372
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 +
11483 return container
11584
11685 // scoped
11786
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-
14987 function tab (name, view) {
15088 var instance = views.get(view)
151- lastViewed[view] = true
15289 return h('a', {
15390 'ev-click': function (ev) {
15491 if (instance.pendingUpdates && instance.pendingUpdates() && instance.reload) {
15592 instance.reload()
@@ -166,62 +103,10 @@
166103 ])
167104 ])
168105 }
169106
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-
222107 function selected (view) {
223- return computed([currentView, view], (currentView, view) => {
108 + return computed([views.currentView, view], (currentView, view) => {
224109 return currentView === view
225110 })
226111 }
227112 }
modules/app/views.jsView
@@ -1,0 +1,112 @@
1 +var { h, Value, Dict, dictToCollection, map, computed } = require('mutant')
2 +var nest = require('depnest')
3 +
4 +exports.gives = nest('app.views')
5 +
6 +exports.create = function (api) {
7 + return nest('app.views', function (renderPage, defaultViews) {
8 + var views = Dict({})
9 +
10 + var forwardHistory = []
11 + var backHistory = []
12 +
13 + if (defaultViews) {
14 + defaultViews.forEach((view) => {
15 + views.put(view, renderPage(view))
16 + })
17 + }
18 +
19 + var lastViewed = {}
20 +
21 + // delete cached view after 5 mins of last seeing
22 + setInterval(() => {
23 + views.keys().forEach((view) => {
24 + if (!defaultViews.includes(view)) {
25 + if (lastViewed[view] !== true && Date.now() - lastViewed[view] > (5 * 60e3) && view !== currentView()) {
26 + views.delete(view)
27 + }
28 + }
29 + })
30 + }, 60e3)
31 +
32 + var canGoForward = Value(false)
33 + var canGoBack = Value(false)
34 + var currentView = Value(defaultViews && defaultViews[0] || null)
35 +
36 + var viewCollection = dictToCollection(views)
37 + var html = h('div.main', map(viewCollection, (item) => {
38 + return h('div.view', {
39 + attributes: {
40 + 'data-href': item.key
41 + },
42 + hidden: computed([item.key, currentView], (a, b) => a !== b)
43 + }, [ item.value ])
44 + }))
45 +
46 + return {
47 + get: views.get,
48 + defaultViews,
49 + canGoForward,
50 + canGoBack,
51 + currentView,
52 + setView,
53 + goBack,
54 + goForward,
55 + html
56 + }
57 +
58 + // scoped
59 +
60 + function goBack () {
61 + if (backHistory.length) {
62 + canGoForward.set(true)
63 + forwardHistory.push(currentView())
64 +
65 + var view = backHistory.pop()
66 + if (!views.has(view)) {
67 + views.put(view, renderPage(view))
68 + }
69 +
70 + currentView.set(view)
71 + canGoBack.set(backHistory.length > 0)
72 + }
73 + }
74 +
75 + function goForward () {
76 + if (forwardHistory.length) {
77 + backHistory.push(currentView())
78 +
79 + var view = forwardHistory.pop()
80 + if (!views.has(view)) {
81 + views.put(view, renderPage(view))
82 + }
83 +
84 + currentView.set(view)
85 + canGoForward.set(forwardHistory.length > 0)
86 + canGoBack.set(true)
87 + }
88 + }
89 +
90 + function setView (view) {
91 + if (!views.has(view)) {
92 + views.put(view, renderPage(view))
93 + }
94 +
95 + if (lastViewed[view] !== true) {
96 + lastViewed[view] = Date.now()
97 + }
98 +
99 + if (currentView() && lastViewed[currentView()] !== true) {
100 + lastViewed[currentView()] = Date.now()
101 + }
102 +
103 + if (view !== currentView()) {
104 + canGoForward.set(false)
105 + canGoBack.set(true)
106 + forwardHistory.length = 0
107 + backHistory.push(currentView())
108 + currentView.set(view)
109 + }
110 + }
111 + })
112 +}

Built with git-ssb-web