git ssb

16+

Dominic / patchbay



Commit d3a0c5e302f7edc7a52b5002d4c9ad4f2a2d4df3

switch to object-based routing

mix irving committed on 7/16/2017, 10:34:42 AM
Parent: f10b724969a4b81ac7e05057e6f753c165549cee

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/notifications.jschanged
app/page/private.jschanged
app/page/profile.jschanged
app/page/public.jschanged
app/page/thread.jschanged
app/sync/addPage.jschanged
app/sync/goTo.jschanged
router/sync/routes.jschanged
app/html/app.jsView
@@ -31,9 +31,13 @@
3131 window = api.app.sync.window(window)
3232 const css = values(api.styles.css()).join('\n')
3333 insertCss(css)
3434
35- const initialTabs = ['/public', '/private', '/notifications']
35+ const initialTabs = [
36+ { page: 'public' },
37+ { page: 'private' },
38+ { page: 'notifications' },
39+ ]
3640 const tabs = api.app.html.tabs(initialTabs)
3741 const { addPage } = api.app.sync
3842
3943 const App = h('App', tabs)
app/html/search-bar.jsView
@@ -25,11 +25,12 @@
2525 placeholder: '?search, @name, #channel',
2626 'ev-keyup': ev => {
2727 switch (ev.keyCode) {
2828 case 13: // enter
29- if (goTo(input.value.trim(), !ev.ctrlKey)) {
30- input.blur()
31- }
29+ var location = input.value.trim()
30+ if (/^\//.test(location)) location = { page: location.replace(/^\//,'') }
31+
32+ if (goTo(location, !ev.ctrlKey)) input.blur()
3233 return
3334 case 27: // escape
3435 ev.preventDefault()
3536 input.blur()
app/html/tabs.jsView
@@ -25,9 +25,10 @@
2525
2626 const search = api.app.html.searchBar()
2727 const menu = api.app.html.menu()
2828 const onSelect = (indexes) => {
29- search.input.value = _tabs.get(indexes[0]).content.id
29+ const { id, title } = _tabs.get(indexes[0]).content
30+ search.input.value = title || id
3031 }
3132 _tabs = Tabs(onSelect, {
3233 append: h('div.navExtra', [ search, menu ])
3334 })
app/page/blob.jsView
@@ -8,12 +8,12 @@
88
99 exports.create = (api) => {
1010 return nest('app.page.blob', blobPage)
1111
12- function blobPage () {
13- return h('Blob', { id: path, title: path.slice(0, 9) + '...' }, [
12+ function blobPage ({ blob }) {
13+ return h('Blob', { id: blob, title: blob.slice(0, 9) + '...' }, [
1414 h('iframe', {
15- src: api.blob.sync.url(path),
15+ src: api.blob.sync.url(blob),
1616 sandbox: ''
1717 })
1818 ])
1919 }
app/page/channel.jsView
@@ -21,18 +21,18 @@
2121
2222 exports.create = function (api) {
2323 return nest('app.page.channel', channelView)
2424
25- function channelView (path) {
26- const channel = path.substr(1)
27- const composer = api.message.html.compose({ meta: { type: 'post', channel } })
25+ function channelView ({ channel }) {
26+ const channelName = channel.substr(1)
27+ const composer = api.message.html.compose({ meta: { type: 'post', channelName } })
2828 const { filterMenu, filterDownThrough, filterUpThrough, resetFeed } = api.app.html.filter(draw)
2929 const { container, content } = api.app.html.scroller({ prepend: [composer, filterMenu] })
3030
3131 function draw () {
3232 resetFeed({ container, content })
3333
34- const openChannelSource = api.feed.pull.channel(channel)
34+ const openChannelSource = api.feed.pull.channel(channelName)
3535
3636 pull(
3737 openChannelSource({old: false}),
3838 filterUpThrough(),
@@ -46,8 +46,9 @@
4646 )
4747 }
4848 draw()
4949
50+ container.id = container.title = channel
5051 return container
5152 }
5253 }
5354
app/page/notifications.jsView
@@ -24,9 +24,9 @@
2424 'message.html.render': 'first'
2525 })
2626
2727 exports.create = function (api) {
28- const route = '/notifications'
28+ const page = 'notifications'
2929
3030 return nest({
3131 'app.html.menuItem': menuItem,
3232 'app.page.notifications': notificationsPage
@@ -34,13 +34,13 @@
3434
3535 function menuItem () {
3636 return h('a', {
3737 style: { order: 3 },
38- 'ev-click': () => api.app.sync.goTo(route)
39- }, route)
38+ 'ev-click': () => api.app.sync.goTo({ page })
39+ }, page)
4040 }
4141
42- function notificationsPage () {
42+ function notificationsPage (location) {
4343 const id = api.keys.sync.id()
4444
4545 const { filterMenu, filterDownThrough, filterUpThrough, resetFeed } = api.app.html.filter(draw)
4646 const { container, content } = api.app.html.scroller({ prepend: [ filterMenu ] })
@@ -61,8 +61,10 @@
6161 )
6262 }
6363 draw()
6464
65+ container.title = page
66+ container.id = JSON.stringify(location)
6567 return container
6668 }
6769 }
6870
app/page/private.jsView
@@ -25,9 +25,9 @@
2525 }
2626 })
2727
2828 exports.create = function (api) {
29- const route = '/private'
29+ const page = 'private'
3030
3131 return nest({
3232 'app.html.menuItem': menuItem,
3333 'app.page.private': privatePage
@@ -35,13 +35,13 @@
3535
3636 function menuItem () {
3737 return h('a', {
3838 style: { order: 2 },
39- 'ev-click': () => api.app.sync.goTo(route)
40- }, route)
39+ 'ev-click': () => api.app.sync.goTo({ page })
40+ }, page)
4141 }
4242
43- function privatePage () {
43+ function privatePage (location) {
4444 const id = api.keys.sync.id()
4545
4646 const composer = api.message.html.compose({
4747 meta: { type: 'post' },
@@ -71,8 +71,10 @@
7171 )
7272 }
7373 draw()
7474
75+ container.title = page
76+ container.id = JSON.stringify(location)
7577 return container
7678 }
7779 }
7880
app/page/profile.jsView
@@ -35,9 +35,9 @@
3535 'ev-click': () => api.app.sync.goTo(api.keys.sync.id())
3636 }, '/profile')
3737 }
3838
39- function profilePage (id) {
39+ function profilePage ({ feed: id }) {
4040 const profile = h('Profile', [
4141 h('section.edit', api.about.html.edit(id)),
4242 h('section.relationships', api.contact.html.relationships(id)),
4343 h('section.activity', [
@@ -47,11 +47,8 @@
4747 ])
4848
4949 var { container, content } = api.app.html.scroller({ prepend: profile })
5050
51- const name = api.about.obs.name(id)
52- watch(name, function (name) { container.title = '@' + name })
53- container.id = id
5451
5552 pull(
5653 api.sbot.pull.userFeed({id: id, old: false, live: true}),
5754 Scroller(container, content, api.message.html.render, true, false)
@@ -64,8 +61,10 @@
6461 // pull.through(console.log.bind(console)),
6562 Scroller(container, content, api.message.html.render, false, false)
6663 )
6764
65+ container.id = id
66+ watch(api.about.obs.name(id), name => { container.title = '@' + name })
6867 return container
6968 }
7069 }
7170
app/page/public.jsView
@@ -23,9 +23,9 @@
2323 }
2424 })
2525
2626 exports.create = function (api) {
27- const route = '/public'
27+ const page = 'public'
2828
2929 return nest({
3030 'app.html.menuItem': menuItem,
3131 'app.page.public': publicPage,
@@ -33,13 +33,13 @@
3333
3434 function menuItem () {
3535 return h('a', {
3636 style: { order: 1 },
37- 'ev-click': () => api.app.sync.goTo(route)
38- }, route)
37+ 'ev-click': () => api.app.sync.goTo({ page })
38+ }, page)
3939 }
4040
41- function publicPage () {
41+ function publicPage (location) {
4242 const composer = api.message.html.compose({
4343 meta: { type: 'post' },
4444 placeholder: 'Write a public message'
4545 })
@@ -70,8 +70,10 @@
7070 )
7171 }
7272 draw()
7373
74+ container.id = JSON.stringify(location)
75+ container.title = page
7476 return container
7577 }
7678 }
7779
app/page/thread.jsView
@@ -26,12 +26,12 @@
2626
2727 exports.create = function (api) {
2828 return nest('app.page.thread', threadPage)
2929
30- function threadPage () {
30+ function threadPage ({ msg }) {
3131 const myId = api.keys.sync.id()
3232 const ImFollowing = api.contact.obs.following(myId)
33- const { messages, isPrivate, rootId, lastId, channel, recps } = api.feed.obs.thread(id)
33+ const { messages, isPrivate, rootId, lastId, channel, recps } = api.feed.obs.thread(msg)
3434 const meta = Struct({
3535 type: 'post',
3636 root: rootId,
3737 branch: lastId,
@@ -66,14 +66,16 @@
6666 placeholder: 'Write a reply',
6767 shrink: false
6868 })
6969 const content = h('section.content', map(messages, m => {
70- return api.message.html.render(resolve(m), {pageId: id})
70+ return api.message.html.render(resolve(m), {pageId: msg})
7171 }))
7272 const { container } = api.app.html.scroller({ prepend: header, content, append: composer })
7373
7474 container.classList.add('Thread')
75- api.message.async.name(id, (err, name) => {
75+ container.id = msg
76+ container.title = msg
77+ api.message.async.name(msg, (err, name) => {
7678 if (err) throw err
7779 container.title = name
7880 })
7981
app/sync/addPage.jsView
@@ -12,20 +12,16 @@
1212 'app.sync': { addPage }
1313 })
1414
1515 // TODO : make it so error catching doesn't need this, move it into goTo
16- function addPage (path, change, split) {
16+ function addPage (location, change, split) {
1717 const tabs = api.app.html.tabs()
1818
19-
20- // TOD (mix) : gross sheet-router hack
21- if (path[0] !== '/') path = '/'+path
22- console.log(path)
23-
24- const page = api.router.sync.router(path)
19+ const page = api.router.sync.router(location)
2520 if (!page) return
2621
27- page.id = page.id || path
22+ // TODO - review unique page id + naming system
23+ page.id = page.id || location
2824 tabs.add(page, change, split)
2925 }
3026 }
3127
app/sync/goTo.jsView
@@ -7,17 +7,19 @@
77 'app.sync.addPage': 'first'
88 })
99
1010 exports.create = function (api) {
11- return nest('app.sync.goTo', function goTo (path, change) {
11+ return nest('app.sync.goTo', function goTo (location, change) {
1212 const tabs = api.app.html.tabs()
1313
14- if (tabs.has(path)) {
15- tabs.select(path)
14+ const locationSignature = JSON.stringify(location)
15+
16+ if (tabs.has(locationSignature)) {
17+ tabs.select(locationSignature)
1618 return true
1719 }
1820
19- api.app.sync.addPage(path, true, false)
21+ api.app.sync.addPage(location, true, false)
2022 return change
2123 })
2224 }
2325
router/sync/routes.jsView
@@ -1,6 +1,5 @@
11 const nest = require('depnest')
2-const { isBlob, isFeed, isMsg } = require('ssb-ref')
32
43 exports.gives = nest('router.sync.routes')
54
65 exports.needs = nest({
@@ -25,27 +24,26 @@
2524 profile, blob, thread
2625 } = api.app.page
2726
2827 const routes = [
29- ['/', () => public()],
30- ['/errors', () => errors()],
31- ['/public', () => public()],
32- ['/private', () => private()],
33- ['/notifications', () => notifications()],
34- ['/profile', () => profile({ id: myId })],
35- ['/:key', (params) => {
36- const { key } = params
37- if (isFeed(key)) return profile(params)
38- if (isBlob(key)) return blob(params)
39- if (isMsg(key)) return thread(params)
40- if (isChannel(key)) return channel(params)
41- }]
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 }) ],
33+ // TODO - use is-my-json-valid ?
34+ [ ({ blob }) => isPresent(blob), blob ],
35+ [ ({ channel }) => isPresent(channel), channel ],
36+ [ ({ feed }) => isPresent(feed), profile ],
37+ [ ({ msg }) => isPresent(msg), thread ]
4238 ]
4339
4440 return [...sofar, ...routes]
4541 })
4642 }
4743
48-function isChannel (str) {
49- typeof str === 'string' && str[0] === '#' && str.length > 1
44+function isPresent (content) {
45+ return typeof content === 'string' && content.length > 1
5046 }
5147
48+
49+

Built with git-ssb-web