git ssb

16+

Dominic / patchbay



Commit 9510d7867738dfa7a4aaee17c183c86bb081f55e

object-based routing iterated

mix irving committed on 7/19/2017, 9:19:50 AM
Parent: d3a0c5e302f7edc7a52b5002d4c9ad4f2a2d4df3

Files changed

app/html/app.jschanged
app/html/search-bar.jschanged
app/html/tabs.jschanged
app/page/blob.jschanged
app/page/channel.jschanged
app/page/errors.jschanged
app/page/notifications.jschanged
app/page/private.jschanged
app/page/profile.jschanged
app/page/public.jschanged
app/page/thread.jschanged
app/sync/goTo.jschanged
router/sync/routes.jschanged
app/html/app.jsView
@@ -31,13 +31,10 @@
3131 window = api.app.sync.window(window)
3232 const css = values(api.styles.css()).join('\n')
3333 insertCss(css)
3434
35- const initialTabs = [
36- { page: 'public' },
37- { page: 'private' },
38- { page: 'notifications' },
39- ]
35+ const initialTabs = [ '/public', '/private', '/notifications' ]
36+ // NB router converts these to { page: '/public' }
4037 const tabs = api.app.html.tabs(initialTabs)
4138 const { addPage } = api.app.sync
4239
4340 const App = h('App', tabs)
app/html/search-bar.jsView
@@ -26,10 +26,8 @@
2626 'ev-keyup': ev => {
2727 switch (ev.keyCode) {
2828 case 13: // enter
2929 var location = input.value.trim()
30- if (/^\//.test(location)) location = { page: location.replace(/^\//,'') }
31-
3230 if (goTo(location, !ev.ctrlKey)) input.blur()
3331 return
3432 case 27: // escape
3533 ev.preventDefault()
app/html/tabs.jsView
@@ -25,10 +25,21 @@
2525
2626 const search = api.app.html.searchBar()
2727 const menu = api.app.html.menu()
2828 const onSelect = (indexes) => {
29- const { id, title } = _tabs.get(indexes[0]).content
30- search.input.value = title || id
29+ const { id } = _tabs.get(indexes[0]).content
30+
31+ try {
32+ const location = JSON.parse(id)
33+ var locationForSearchBar = Object.keys(location)
34+ .map(k => location[k])
35+ .join(' + ')
36+ }
37+ catch (e) {
38+ throw new Error('app/html/tabs expects all page ids to be stringified location objects')
39+ var locationForSearchBar = id
40+ }
41+ search.input.value = locationForSearchBar
3142 }
3243 _tabs = Tabs(onSelect, {
3344 append: h('div.navExtra', [ search, menu ])
3445 })
app/page/blob.jsView
@@ -8,10 +8,11 @@
88
99 exports.create = (api) => {
1010 return nest('app.page.blob', blobPage)
1111
12- function blobPage ({ blob }) {
13- return h('Blob', { id: blob, title: blob.slice(0, 9) + '...' }, [
12+ function blobPage (location) {
13+ const { blob } = location
14+ return h('Blob', { id: JSON.stringify(location), title: blob.slice(0, 9) + '...' }, [
1415 h('iframe', {
1516 src: api.blob.sync.url(blob),
1617 sandbox: ''
1718 })
app/page/channel.jsView
@@ -21,9 +21,10 @@
2121
2222 exports.create = function (api) {
2323 return nest('app.page.channel', channelView)
2424
25- function channelView ({ channel }) {
25+ function channelView (location) {
26+ const { channel } = location
2627 const channelName = channel.substr(1)
2728 const composer = api.message.html.compose({ meta: { type: 'post', channelName } })
2829 const { filterMenu, filterDownThrough, filterUpThrough, resetFeed } = api.app.html.filter(draw)
2930 const { container, content } = api.app.html.scroller({ prepend: [composer, filterMenu] })
@@ -46,9 +47,10 @@
4647 )
4748 }
4849 draw()
4950
50- container.id = container.title = channel
51+ container.id = JSON.stringify(location)
52+ container.title = channel
5153 return container
5254 }
5355 }
5456
app/page/errors.jsView
@@ -8,12 +8,12 @@
88
99 exports.create = function (api) {
1010 return nest('app.page.errors', errorsPage)
1111
12- function errorsPage () {
12+ function errorsPage (location) {
1313 var { container, content } = api.app.html.scroller()
1414
15- container.id = '/errors'
15+ container.id = JSON.stringify(location)
1616 container.classList = ['-errors']
1717
1818 // add a dummy entry in the list
1919
app/page/notifications.jsView
@@ -24,9 +24,9 @@
2424 'message.html.render': 'first'
2525 })
2626
2727 exports.create = function (api) {
28- const page = 'notifications'
28+ const page = '/notifications'
2929
3030 return nest({
3131 'app.html.menuItem': menuItem,
3232 'app.page.notifications': notificationsPage
app/page/private.jsView
@@ -25,9 +25,9 @@
2525 }
2626 })
2727
2828 exports.create = function (api) {
29- const page = 'private'
29+ const page = '/private'
3030
3131 return nest({
3232 'app.html.menuItem': menuItem,
3333 'app.page.private': privatePage
app/page/profile.jsView
@@ -35,9 +35,10 @@
3535 'ev-click': () => api.app.sync.goTo(api.keys.sync.id())
3636 }, '/profile')
3737 }
3838
39- function profilePage ({ feed: id }) {
39+ function profilePage (location) {
40+ const { feed: id } = location
4041 const profile = h('Profile', [
4142 h('section.edit', api.about.html.edit(id)),
4243 h('section.relationships', api.contact.html.relationships(id)),
4344 h('section.activity', [
@@ -61,9 +62,9 @@
6162 // pull.through(console.log.bind(console)),
6263 Scroller(container, content, api.message.html.render, false, false)
6364 )
6465
65- container.id = id
66+ container.id = JSON.stringify(location)
6667 watch(api.about.obs.name(id), name => { container.title = '@' + name })
6768 return container
6869 }
6970 }
app/page/public.jsView
@@ -23,9 +23,9 @@
2323 }
2424 })
2525
2626 exports.create = function (api) {
27- const page = 'public'
27+ const page = '/public'
2828
2929 return nest({
3030 'app.html.menuItem': menuItem,
3131 'app.page.public': publicPage,
app/page/thread.jsView
@@ -26,9 +26,10 @@
2626
2727 exports.create = function (api) {
2828 return nest('app.page.thread', threadPage)
2929
30- function threadPage ({ msg }) {
30+ function threadPage (location) {
31+ const { msg } = location
3132 const myId = api.keys.sync.id()
3233 const ImFollowing = api.contact.obs.following(myId)
3334 const { messages, isPrivate, rootId, lastId, channel, recps } = api.feed.obs.thread(msg)
3435 const meta = Struct({
@@ -71,9 +72,9 @@
7172 }))
7273 const { container } = api.app.html.scroller({ prepend: header, content, append: composer })
7374
7475 container.classList.add('Thread')
75- container.id = msg
76+ container.id = JSON.stringify(location)
7677 container.title = msg
7778 api.message.async.name(msg, (err, name) => {
7879 if (err) throw err
7980 container.title = name
app/sync/goTo.jsView
@@ -3,19 +3,21 @@
33 exports.gives = nest({ 'app.sync.goTo': true })
44
55 exports.needs = nest({
66 'app.html.tabs': 'first',
7- 'app.sync.addPage': 'first'
7+ 'app.sync.addPage': 'first',
8+ 'router.sync.normalise': 'first'
89 })
910
1011 exports.create = function (api) {
1112 return nest('app.sync.goTo', function goTo (location, change) {
1213 const tabs = api.app.html.tabs()
1314
14- const locationSignature = JSON.stringify(location)
15+ location = api.router.sync.normalise(location)
16+ const locationId = JSON.stringify(location)
1517
16- if (tabs.has(locationSignature)) {
17- tabs.select(locationSignature)
18+ if (tabs.has(locationId)) {
19+ tabs.select(locationId)
1820 return true
1921 }
2022
2123 api.app.sync.addPage(location, true, false)
router/sync/routes.jsView
@@ -24,13 +24,13 @@
2424 profile, blob, thread
2525 } = api.app.page
2626
2727 const routes = [
28- [ ({ page }) => page === 'public', public ],
29- [ ({ page }) => page === 'private', private ],
30- [ ({ page }) => page === 'notifications', notifications ],
31- [ ({ page }) => page === 'errors', errors ],
32- [ ({ page }) => page === 'profile', () => profile({ id: myId }) ],
28+ [ ({ page }) => page === '/public', public ],
29+ [ ({ page }) => page === '/private', private ],
30+ [ ({ page }) => page === '/notifications', notifications ],
31+ [ ({ page }) => page === '/errors', errors ],
32+ [ ({ page }) => page === '/profile', () => profile({ id: myId }) ],
3333 // TODO - use is-my-json-valid ?
3434 [ ({ blob }) => isPresent(blob), blob ],
3535 [ ({ channel }) => isPresent(channel), channel ],
3636 [ ({ feed }) => isPresent(feed), profile ],

Built with git-ssb-web