Files
buuh/index.js
T

139 lines
4.0 KiB
JavaScript
Raw Normal View History

2016-05-12 14:45:20 +07:00
const history = require('sheet-router/history')
2016-05-11 13:51:17 +07:00
const sheetRouter = require('sheet-router')
const document = require('global/document')
2016-05-12 14:45:20 +07:00
const href = require('sheet-router/href')
2016-06-04 15:03:56 -04:00
const hash = require('sheet-router/hash')
const hashMatch = require('hash-match')
2016-06-27 01:04:21 +02:00
const barracks = require('barracks')
2016-05-23 10:44:32 +09:00
const assert = require('assert')
2016-05-11 13:51:17 +07:00
const xtend = require('xtend')
const yo = require('yo-yo')
2016-05-10 23:23:33 +07:00
module.exports = choo
2016-05-23 10:44:32 +09:00
// framework for creating sturdy web applications
2016-05-11 13:51:17 +07:00
// null -> fn
2016-06-27 01:04:21 +02:00
function choo (opts) {
opts = opts || {}
const _store = barracks(xtend(opts, { onState: render }))
var _rootNode = null
2016-05-11 13:51:17 +07:00
var _router = null
2016-05-21 21:59:03 +09:00
start.toString = toString
2016-05-11 13:51:17 +07:00
start.router = router
start.model = model
start.start = start
return start
2016-05-21 21:59:03 +09:00
// render the application to a string
2016-05-23 10:44:32 +09:00
// (str, obj) -> str
2016-05-23 23:39:22 +09:00
function toString (route, serverState) {
2016-06-27 01:04:21 +02:00
serverState = serverState || {}
assert.equal(typeof route, 'string', 'choo.app.toString: route must be a string')
assert.equal(typeof serverState, 'object', 'choo.app.toString: serverState must be an object')
_store.start({ noSubscriptions: true, noReducers: true, noEffects: true })
const state = _store.state({ state: serverState })
2016-05-23 23:39:22 +09:00
const tree = _router(route, state, function () {
2016-06-27 01:04:21 +02:00
assert.fail('choo: send() cannot be called from Node')
2016-05-21 21:59:03 +09:00
})
return tree.toString()
}
2016-05-11 13:51:17 +07:00
// start the application
2016-05-23 23:39:22 +09:00
// (str?, obj?) -> DOMNode
2016-06-27 01:04:21 +02:00
function start (selector, startOpts) {
if (!startOpts && typeof selector !== 'string') {
startOpts = selector
selector = null
}
2016-06-27 01:04:21 +02:00
startOpts = startOpts || {}
2016-05-16 04:09:02 +07:00
2016-06-27 01:04:21 +02:00
_store.model(appInit(startOpts))
const createSend = _store.start(startOpts)
const send = createSend('view', true)
const state = _store.state()
2016-05-11 13:51:17 +07:00
2016-06-27 01:04:21 +02:00
if (!selector) {
const tree = _router(state.app.location, state, send)
_rootNode = tree
2016-05-23 23:39:22 +09:00
return tree
2016-06-27 01:04:21 +02:00
} else {
document.addEventListener('DOMContentLoaded', function (event) {
const oldTree = document.querySelector(selector)
assert.ok(oldTree, 'could not query selector: ' + selector)
const newTree = _router(state.app.location, state, send)
_rootNode = yo.update(oldTree, newTree)
})
2016-05-23 23:39:22 +09:00
}
2016-06-27 01:04:21 +02:00
}
2016-05-11 13:51:17 +07:00
2016-06-27 01:04:21 +02:00
// update the DOM after every state mutation
// (obj, obj, obj, str, fn) -> null
function render (action, state, prev, name, createSend) {
if (opts.onState) opts.onState(action, state, prev, name, createSend)
if (state === prev) return
2016-05-16 04:09:02 +07:00
2016-06-27 01:04:21 +02:00
// note(yw): only here till sheet-router supports custom constructors
const send = createSend('view', true)
const newTree = _router(state.app.location, state, send, prev)
_rootNode = yo.update(_rootNode, newTree)
2016-05-11 13:51:17 +07:00
}
2016-05-23 10:44:32 +09:00
// register all routes on the router
2016-06-23 16:44:45 +02:00
// (str?, [fn|[fn]]) -> obj
function router (defaultRoute, cb) {
_router = sheetRouter(defaultRoute, cb)
2016-05-16 03:37:19 +07:00
return _router
2016-05-11 13:51:17 +07:00
}
// create a new model
// (str?, obj) -> null
2016-05-23 02:45:15 +09:00
function model (model) {
2016-06-27 01:04:21 +02:00
_store.model(model)
2016-05-11 13:51:17 +07:00
}
}
2016-05-12 14:45:20 +07:00
// initial application state model
2016-05-16 04:09:02 +07:00
// obj -> obj
function appInit (opts) {
2016-06-27 01:04:21 +02:00
const loc = document.location
const state = { location: (opts.hash) ? hashMatch(loc.hash) : loc.href }
const reducers = {
location: function setLocation (action, state) {
return { location: action.location.replace(/#.*/, '') }
2016-05-23 10:44:32 +09:00
}
2016-05-11 13:51:17 +07:00
}
2016-06-04 19:53:05 -04:00
// if hash routing explicitly enabled, subscribe to it
2016-06-27 01:04:21 +02:00
const subs = {}
2016-06-04 19:53:05 -04:00
if (opts.hash === true) {
2016-06-04 15:03:56 -04:00
pushLocationSub(function (navigate) {
hash(function (fragment) {
navigate(hashMatch(fragment))
})
2016-06-27 01:04:21 +02:00
}, 'handleHash', subs)
2016-06-04 19:53:05 -04:00
} else {
2016-06-27 01:04:21 +02:00
if (opts.history !== false) pushLocationSub(history, 'setLocation', subs)
if (opts.href !== false) pushLocationSub(href, 'handleHref', subs)
2016-06-04 15:03:56 -04:00
}
2016-05-16 04:09:02 +07:00
2016-06-27 01:04:21 +02:00
return {
namespace: 'app',
subscriptions: subs,
reducers: reducers,
state: state
}
2016-05-16 04:09:02 +07:00
2016-05-23 10:44:32 +09:00
// create a new subscription that modifies
2016-05-23 14:40:52 +09:00
// 'app:location' and push it to be loaded
2016-06-27 01:04:21 +02:00
// (fn, obj) -> null
function pushLocationSub (cb, key, model) {
model[key] = function (send, done) {
cb(function navigate (href) {
send('app:location', { location: href }, done)
2016-05-23 10:44:32 +09:00
})
2016-06-27 01:04:21 +02:00
}
2016-05-11 13:51:17 +07:00
}
}