Files
buuh/index.js
T

200 lines
5.6 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-05-11 13:51:17 +07:00
const sendAction = require('send-action')
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
2016-05-11 13:51:17 +07:00
choo.view = 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-05-23 02:45:15 +09:00
function choo () {
2016-05-21 21:59:03 +09:00
const _models = []
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-21 21:59:03 +09:00
function toString (route, state) {
const initialState = {}
_models.forEach(function (model) {
if (model.state) apply(model.name, model.state, initialState)
})
const tree = _router(route, xtend(initialState, state), function () {
throw new Error('send() cannot be called on the server')
})
return tree.toString()
}
2016-05-11 13:51:17 +07:00
// start the application
2016-05-23 02:45:15 +09:00
// obj -> DOMNode
function start (opts) {
opts = opts || {}
const name = opts.name || 'choo'
2016-05-16 04:09:02 +07:00
const initialState = {}
const reducers = {}
const effects = {}
2016-05-21 21:59:03 +09:00
_models.push(appInit(opts))
2016-05-16 04:09:02 +07:00
_models.forEach(function (model) {
2016-05-23 02:45:15 +09:00
if (model.state) apply(model.namespace, model.state, initialState)
if (model.reducers) apply(model.namespace, model.reducers, reducers)
if (model.effects) apply(model.namespace, model.effects, effects)
2016-05-11 13:51:17 +07:00
})
2016-05-23 10:44:32 +09:00
// send() is used to trigger actions inside
// views, effects and subscriptions
2016-05-16 04:09:02 +07:00
const send = sendAction({
onaction: handleAction,
onchange: onchange,
state: initialState
})
2016-05-12 14:45:20 +07:00
2016-05-23 10:44:32 +09:00
// subscriptions are loaded after sendAction() is called
// because they both need access to send() and can't
// react to actions (read-only)
2016-05-16 04:09:02 +07:00
_models.forEach(function (model) {
if (model.subscriptions) {
2016-05-23 10:44:32 +09:00
assert.ok(Array.isArray(model.subscriptions, 'subs must be an array'))
2016-05-16 04:09:02 +07:00
model.subscriptions.forEach(function (sub) {
sub(send)
})
}
})
2016-05-12 14:45:20 +07:00
2016-05-23 10:44:32 +09:00
// the rootId is determined to find the application root
// on update. Since the DOM nodes change between updates,
// we must call document.querySelector() to find the root.
// Use different names when loading multiple choo applications
// on the same page
2016-05-12 14:45:20 +07:00
const rootId = name + '-root'
2016-05-23 02:45:15 +09:00
const tree = _router(send.state().app.location, send.state(), send)
2016-05-12 14:45:20 +07:00
tree.setAttribute('id', rootId)
2016-05-11 14:56:18 +07:00
return tree
2016-05-11 13:51:17 +07:00
2016-05-23 10:44:32 +09:00
// handle an action by either reducers, effects
// or both - return the new state when done
// (obj, obj, fn) -> obj
2016-05-16 04:09:02 +07:00
function handleAction (action, state, send) {
2016-05-23 10:44:32 +09:00
var reducersCalled = false
var effectsCalled = false
2016-05-16 04:09:02 +07:00
2016-05-23 10:44:32 +09:00
// validate if a namespace exists. Namespaces
// are delimited by the first ':'. Perhaps
// we'll allow recursive namespaces in the
// future - who knows
2016-05-23 02:45:15 +09:00
if (/:/.test(action.type)) {
const arr = action.type.split(':')
2016-05-23 10:44:32 +09:00
var ns = arr.shift()
action.type = arr.join(':')
2016-05-23 02:45:15 +09:00
}
2016-05-23 10:44:32 +09:00
const _reducers = ns ? reducers[ns] : reducers
if (_reducers && _reducers[action.type]) {
2016-05-23 02:45:15 +09:00
if (ns) {
2016-05-23 14:40:52 +09:00
const newState = _reducers[action.type](action, state[ns])
2016-05-23 10:44:32 +09:00
state[ns] = xtend(state[ns], newState)
2016-05-23 14:40:52 +09:00
} else state = xtend(state, reducers[action.type](action, state))
2016-05-23 10:44:32 +09:00
reducersCalled = true
2016-05-16 04:09:02 +07:00
}
2016-05-23 10:44:32 +09:00
const _effects = ns ? effects[ns] : effects
if (_effects && _effects[action.type]) {
2016-05-23 14:40:52 +09:00
if (ns) _effects[action.type](action, state[ns], send)
else _effects[action.type](action, state, send)
2016-05-23 10:44:32 +09:00
effectsCalled = true
2016-05-16 04:09:02 +07:00
}
2016-05-23 10:44:32 +09:00
if (!reducersCalled && !effectsCalled) {
2016-05-16 04:09:02 +07:00
throw new Error('Could not find action ' + action.type)
}
2016-05-23 10:44:32 +09:00
return state
2016-05-16 04:09:02 +07:00
}
2016-05-23 10:44:32 +09:00
// update the DOM after every state mutation
// (obj, obj) -> null
2016-05-11 13:51:17 +07:00
function onchange (action, state) {
2016-05-12 14:45:20 +07:00
const oldTree = document.querySelector('#' + rootId)
2016-05-23 02:45:15 +09:00
const newTree = _router(state.app.location, state, send)
2016-05-12 14:45:20 +07:00
newTree.setAttribute('id', rootId)
2016-05-11 14:56:18 +07:00
yo.update(oldTree, newTree)
2016-05-11 13:51:17 +07:00
}
}
2016-05-23 10:44:32 +09:00
// register all routes on the router
2016-05-11 13:51:17 +07:00
// [obj|fn] -> null
function router (cb) {
_router = sheetRouter(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-05-11 13:51:17 +07:00
_models.push(model)
}
}
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) {
const model = {
2016-05-23 02:45:15 +09:00
namespace: 'app',
2016-05-22 20:54:39 +09:00
state: { location: document.location.href },
2016-05-23 10:44:32 +09:00
subscriptions: [],
reducers: {
// handle href links
location: function setLocation (action, state) {
return {
location: action.location.replace(/#.*/, '')
}
}
}
2016-05-11 13:51:17 +07:00
}
2016-05-23 10:44:32 +09:00
// enable catching <href a=""></href> links
// enable HTML5 history API
if (opts.history !== false) pushLocationSub(href)
if (opts.history !== false) pushLocationSub(history)
2016-05-16 04:09:02 +07:00
return model
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-05-23 10:44:32 +09:00
// fn -> null
function pushLocationSub (cb) {
model.subscriptions.push(function (send) {
cb(function (href) {
send('app:location', { location: href })
})
})
2016-05-11 13:51:17 +07:00
}
}
2016-05-12 14:45:20 +07:00
// compose an object conditionally
2016-05-23 10:44:32 +09:00
// optionally contains a namespace
// which is used to nest properties.
2016-05-12 14:45:20 +07:00
// (str, obj, obj) -> null
2016-05-23 10:44:32 +09:00
function apply (ns, source, target) {
2016-05-12 14:45:20 +07:00
Object.keys(source).forEach(function (key) {
2016-05-23 10:44:32 +09:00
if (ns) {
if (!target[ns]) target[ns] = {}
target[ns][key] = source[key]
2016-05-23 02:45:15 +09:00
} else target[key] = source[key]
2016-05-12 14:45:20 +07:00
})
}