Commit 94fb17a34b157954144a3e8817557f2f57547095
add basic routing and pages
mix irving committed on 8/7/2017, 6:27:22 AMParent: d289e19ab01386d51d43203044bfd1a56243f8cb
Files changed
app/html/app.js | changed |
app/index.js | changed |
app/page/group.js | added |
app/page/home.js | added |
app/sync/goTo.js | added |
index.js | changed |
main.js | changed |
package-lock.json | changed |
package.json | changed |
router/index.js | added |
router/sync/routes.js | added |
app/html/app.js | ||
---|---|---|
@@ -1,16 +1,15 @@ | ||
1 | 1 | const nest = require('depnest') |
2 | -const { h } = require('mutant') | |
3 | 2 | |
4 | - | |
5 | 3 | exports.gives = nest('app.html.app') |
6 | 4 | |
5 | +exports.needs = nest({ | |
6 | + 'app.sync.goTo': 'first' | |
7 | +}) | |
8 | + | |
7 | 9 | exports.create = (api) => { |
8 | 10 | return nest('app.html.app', app) |
9 | 11 | |
10 | 12 | function app () { |
11 | - return h('h1', 'Hello!') | |
12 | - | |
13 | - | |
13 | + return api.app.sync.goTo({ page: 'home' }) | |
14 | 14 | } |
15 | 15 | } |
16 | - |
app/index.js | ||
---|---|---|
@@ -1,6 +1,12 @@ | ||
1 | 1 | module.exports = { |
2 | 2 | html: { |
3 | 3 | app: require('./html/app') |
4 | + }, | |
5 | + page: { | |
6 | + group: require('./page/group'), | |
7 | + home: require('./page/home') | |
8 | + }, | |
9 | + sync: { | |
10 | + goTo: require('./sync/goTo') | |
4 | 11 | } |
5 | 12 | } |
6 | - |
app/page/group.js | ||
---|---|---|
@@ -1,0 +1,25 @@ | ||
1 | +const nest = require('depnest') | |
2 | +const { h } = require('mutant') | |
3 | + | |
4 | +exports.gives = nest('app.page.group') | |
5 | + | |
6 | +exports.needs = nest({ | |
7 | + 'app.sync.goTo': 'first' | |
8 | +}) | |
9 | + | |
10 | +exports.create = (api) => { | |
11 | + return nest('app.page.group', group) | |
12 | + | |
13 | + function group (location) { | |
14 | + // location here can be the root message of a group : { type: 'group', key } | |
15 | + // TODO show specific group index described by key | |
16 | + | |
17 | + const { goTo } = api.app.sync | |
18 | + | |
19 | + return h('div', [ | |
20 | + h('h1', 'Group'), | |
21 | + h('a', { 'ev-click': () => goTo({ page: 'home' }) }, 'Home'), | |
22 | + h('p', `key: ${location.key}`) | |
23 | + ]) | |
24 | + } | |
25 | +} |
app/page/home.js | ||
---|---|---|
@@ -1,0 +1,23 @@ | ||
1 | +const nest = require('depnest') | |
2 | +const { h } = require('mutant') | |
3 | + | |
4 | +exports.gives = nest('app.page.home') | |
5 | + | |
6 | +exports.needs = nest({ | |
7 | + 'app.sync.goTo': 'first' | |
8 | +}) | |
9 | + | |
10 | +exports.create = (api) => { | |
11 | + return nest('app.page.home', home) | |
12 | + | |
13 | + function home (location) { | |
14 | + // location here can expected to be: { page: 'home' } | |
15 | + const { goTo } = api.app.sync | |
16 | + | |
17 | + return h('div', [ | |
18 | + h('h1', 'Home'), | |
19 | + h('div', { 'ev-click': () => goTo({ page: 'home' }) }, 'Home'), | |
20 | + h('div', { 'ev-click': () => goTo({ type: 'group', key: '%sadlkjas;lkdjas' }) }, 'Group') | |
21 | + ]) | |
22 | + } | |
23 | +} |
app/sync/goTo.js | ||
---|---|---|
@@ -1,0 +1,22 @@ | ||
1 | +const nest = require('depnest') | |
2 | + | |
3 | +exports.gives = nest('app.sync.goTo') | |
4 | + | |
5 | +exports.needs = nest({ | |
6 | + 'router.sync.router': 'first' | |
7 | +}) | |
8 | + | |
9 | +exports.create = (api) => { | |
10 | + return nest('app.sync.goTo', goTo) | |
11 | + | |
12 | + function goTo (location) { | |
13 | + console.log('goTo', location) | |
14 | + const newView = api.router.sync.router(location) | |
15 | + | |
16 | + // TODO (mix) : change once history in place | |
17 | + const oldView = document.body.firstChild | |
18 | + oldView | |
19 | + ? document.body.replaceChild(newView, oldView) | |
20 | + : document.body.appendChild(newView) | |
21 | + } | |
22 | +} |
index.js | ||
---|---|---|
@@ -1,5 +1,6 @@ | ||
1 | 1 | const ticktack = { |
2 | - app: require('./app') | |
2 | + app: require('./app'), | |
3 | + router: require('./router') | |
3 | 4 | } |
4 | 5 | |
5 | 6 | module.exports = ticktack |
main.js | ||
---|---|---|
@@ -16,6 +16,7 @@ | ||
16 | 16 | |
17 | 17 | const api = entry(sockets, nest('app.html.app', 'first')) |
18 | 18 | |
19 | 19 | const app = api.app.html.app() |
20 | -document.body.appendChild(app) | |
21 | 20 | |
21 | +// TODO (mix) : once goTo/ router is swapping pages, attach the app to the page here | |
22 | +// document.body.appendChild(app) |
package-lock.json | ||
---|---|---|
@@ -991,11 +991,11 @@ | ||
991 | 991 | "pull-through": "1.0.18" |
992 | 992 | } |
993 | 993 | }, |
994 | 994 | "patchcore": { |
995 | - "version": "1.8.2", | |
996 | - "resolved": "https://registry.npmjs.org/patchcore/-/patchcore-1.8.2.tgz", | |
997 | - "integrity": "sha1-9ajmXxqesof1bC1UTEtzzPNM4Xo=", | |
995 | + "version": "1.9.0", | |
996 | + "resolved": "https://registry.npmjs.org/patchcore/-/patchcore-1.9.0.tgz", | |
997 | + "integrity": "sha512-d9bJ7oivSS9uLknOZxnpJMDWTGVSyLmUt38mOTrL0LD1YEAAmThhi+6i/+mPXhm6605DFbYzWtxBVmXQ2t2SjQ==", | |
998 | 998 | "requires": { |
999 | 999 | "bulk-require": "1.0.1", |
1000 | 1000 | "bulkify": "1.4.2", |
1001 | 1001 | "color-hash": "1.0.3", |
package.json | ||
---|---|---|
@@ -17,8 +17,9 @@ | ||
17 | 17 | "license": "GPL-3.0", |
18 | 18 | "dependencies": { |
19 | 19 | "depject": "^4.1.0", |
20 | 20 | "depnest": "^1.3.0", |
21 | - "patchcore": "^1.8.2", | |
21 | + "mutant": "^3.21.2", | |
22 | + "patchcore": "^1.9.0", | |
22 | 23 | "setimmediate": "^1.0.5" |
23 | 24 | } |
24 | 25 | } |
router/index.js | ||
---|---|---|
@@ -1,0 +1,5 @@ | ||
1 | +module.exports = { | |
2 | + sync: { | |
3 | + routes: require('./sync/routes') | |
4 | + } | |
5 | +} |
router/sync/routes.js | ||
---|---|---|
@@ -1,0 +1,22 @@ | ||
1 | +const nest = require('depnest') | |
2 | + | |
3 | +exports.gives = nest('router.sync.routes') | |
4 | + | |
5 | +exports.needs = nest({ | |
6 | + 'app.page.home': 'first', | |
7 | + 'app.page.group': 'first' | |
8 | +}) | |
9 | + | |
10 | +exports.create = (api) => { | |
11 | + return nest('router.sync.routes', (sofar = []) => { | |
12 | + const { home, group } = api.app.page | |
13 | + | |
14 | + // route format: [ routeValidator, routeFunction ] | |
15 | + const routes = [ | |
16 | + [ ({ page }) => page === 'home', home ], | |
17 | + [ ({ type }) => type === 'group', group ] | |
18 | + ] | |
19 | + | |
20 | + return [...sofar, ...routes] | |
21 | + }) | |
22 | +} |
Built with git-ssb-web