Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Components #606

Closed
wants to merge 12 commits into from
Closed
47 changes: 47 additions & 0 deletions component/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var assert = require('assert')

module.exports = ChooComponentCache

function ChooComponentCache (state, emit) {
assert.equal(typeof state, 'object', 'ChooComponentCache: state should be type object')
assert.equal(typeof emit, 'function', 'ChooComponentCache: state should be type function')

this.state = state
this.emit = emit
this.cache = {}
}

ChooComponentCache.prototype.prune = function () {
var keys = Object.keys(this.cache)
for (var id, i = 0, len = keys.length; i < len; i++) {
id = keys[i]
if (!this.cache[id].element) delete this.cache[id]
}
}

ChooComponentCache.prototype.render = function (Component) {
assert.equal(typeof Component, 'function', 'ChooComponentCache.render: Component should be type function')
var args = []
for (var i = 1, len = arguments.length; i < len; i++) {
args.push(arguments[i])
}

assert.equal(typeof Component.identity, 'function', 'ChooComponentCache.render: Component.identity should be type function')
var id = Component.identity.apply(Component, args)
assert.equal(typeof id, 'string', 'ChooComponentCache.render: Component.identity should return type string')

var el = this.cache[id]
if (!el) {
var ext = args.slice(0)
ext.unshift(Component, id, this.state, this.emit)
el = newCall.apply(newCall, ext)
this.cache[id] = el
}
return el.render.apply(el, args)
}

// Because you can't call `new` and `.apply()` at the same time. This is a mad
// hack, but hey it works so we gonna go for it. Whoop.
function newCall (Cls) {
return new (Cls.bind.apply(Cls, arguments)) // eslint-disable-line
}
1 change: 1 addition & 0 deletions component/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('nanocomponent')
15 changes: 15 additions & 0 deletions example/components/footer/clear-button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var html = require('bel')

module.exports = deleteCompleted

function deleteCompleted (emit) {
return html`
<button class="clear-completed" onclick=${deleteAllCompleted}>
Clear completed
</button>
`

function deleteAllCompleted () {
emit('todos:deleteCompleted')
}
}
18 changes: 18 additions & 0 deletions example/components/footer/filter-button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var html = require('bel')

module.exports = filterButton

function filterButton (name, filter, currentFilter, emit) {
var filterClass = filter === currentFilter
? 'selected'
: ''

var uri = '#' + name.toLowerCase()
if (uri === '#all') uri = '/'

return html`<li>
<a href=${uri} class=${filterClass}>
${name}
</a>
</li>`
}
54 changes: 54 additions & 0 deletions example/components/footer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var Component = require('../../../component')
var html = require('bel')

var clearButton = require('./clear-button')
var filterButton = require('./filter-button')

module.exports = class Footer extends Component {
static identity () {
return 'footer'
}

constructor (name, state, emit) {
super(name)
this.state = state
this.emit = emit

this.local = this.state.components.footer = {}
this.setState()
}

setState () {
this.local.rawTodos = this.state.todos.clock
this.local.rawHref = this.state.href

this.local.filter = this.state.href.replace(/^\//, '') || ''
this.local.activeCount = this.state.todos.active.length
this.local.hasDone = this.state.todos.done.length || null
}

update () {
if (this.local.rawTodos !== this.state.todos.clock ||
this.local.rawHref !== this.state.href) {
this.setState()
return true
} else {
return false
}
}

createElement () {
return html`<footer class="footer">
<span class="todo-count">
<strong>${this.local.activeCount}</strong>
item${this.state.todos.all === 1 ? '' : 's'} left
</span>
<ul class="filters">
${filterButton('All', '', this.local.filter, this.emit)}
${filterButton('Active', 'active', this.local.filter, this.emit)}
${filterButton('Completed', 'completed', this.local.filter, this.emit)}
</ul>
${this.local.hasDone && clearButton(this.emit)}
</footer>`
}
}
36 changes: 36 additions & 0 deletions example/components/header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var Component = require('../../component')
var html = require('bel')

module.exports = class Header extends Component {
static identity () {
return 'header'
}

constructor (name, state, emit) {
super(name)
this.state = state
this.emit = emit
}

update () {
return false
}

createElement () {
return html`<header class="header">
<h1>todos</h1>
<input class="new-todo"
autofocus
placeholder="What needs to be done?"
onkeydown=${this.createTodo.bind(this)} />
</header>`
}

createTodo (e) {
var value = e.target.value
if (e.keyCode === 13) {
e.target.value = ''
this.emit('todos:create', value)
}
}
}
20 changes: 20 additions & 0 deletions example/components/info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var Component = require('../../component')
var html = require('bel')

module.exports = class Info extends Component {
static identity () {
return 'Info'
}

update () {
return false
}

createElement () {
return html`<footer class="info">
<p>Double-click to edit a todo</p>
<p>choo by <a href="https://yoshuawuyts.com/">Yoshua Wuyts</a></p>
<p>Created by <a href="http://shuheikagawa.com">Shuhei Kagawa</a></p>
</footer>`
}
}
69 changes: 69 additions & 0 deletions example/components/todos/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
var Component = require('../../../component')
var html = require('bel')

var Todo = require('./todo')

module.exports = class Header extends Component {
static identity () {
return 'todos'
}

constructor (name, state, emit) {
super(name)
this.state = state
this.emit = emit
this.local = this.state.components.todos = {}
this.setState()
}

setState () {
this.local.rawTodos = this.state.todos.clock
this.local.rawHref = this.state.href

this.local.allDone = this.state.todos.done.length === this.state.todos.all.length
this.local.filter = this.state.href.replace(/^\//, '') || ''
this.local.todos = this.local.filter === 'completed'
? this.state.todos.done
: this.local.filter === 'active'
? this.state.todos.active
: this.state.todos.all
}

update () {
if (this.local.rawTodos !== this.state.todos.clock ||
this.local.rawHref !== this.state.href) {
this.setState()
return true
} else {
return false
}
}

createElement () {
return html`<section class="main">
<input
class="toggle-all"
type="checkbox"
checked=${this.local.allDone}
onchange=${() => this.toggleAll()}/>
<label for="toggle-all" style="display: none;">
Mark all as done
</label>
<ul class="todo-list">
${this.local.todos.map(todo => Todo(todo, this.emit))}
</ul>
</section>`
}

createTodo (e) {
var value = e.target.value
if (e.keyCode === 13) {
e.target.value = ''
this.emit('todos:create', value)
}
}

toggleAll () {
this.emit('todos:toggleAll')
}
}
64 changes: 64 additions & 0 deletions example/components/todos/todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
var html = require('bel')

module.exports = Todo

function Todo (todo, emit) {
var clx = classList({ completed: todo.done, editing: todo.editing })
return html`
<li id=${todo.id} class=${clx}>
<div class="view">
<input
type="checkbox"
class="toggle"
checked="${todo.done}"
onchange=${toggle} />
<label ondblclick=${edit}>${todo.name}</label>
<button
class="destroy"
onclick=${destroy}
></button>
</div>
<input
class="edit"
value=${todo.name}
onkeydown=${handleEditKeydown}
onblur=${update} />
</li>
`

function toggle (e) {
emit('todos:toggle', todo.id)
}

function edit (e) {
emit('todos:edit', todo.id)
}

function destroy (e) {
emit('todos:delete', todo.id)
}

function update (e) {
emit('todos:update', {
id: todo.id,
editing: false,
name: e.target.value
})
}

function handleEditKeydown (e) {
if (e.keyCode === 13) update(e) // Enter
else if (e.code === 27) emit('todos:unedit') // Escape
}

function classList (classes) {
var str = ''
var keys = Object.keys(classes)
for (var i = 0, len = keys.length; i < len; i++) {
var key = keys[i]
var val = classes[key]
if (val) str += (key + ' ')
}
return str
}
}
10 changes: 5 additions & 5 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ var app = choo()
if (process.env.NODE_ENV !== 'production') {
app.use(require('choo-devtools')())
}
app.use(require('./store'))
app.use(require('./stores/todos'))

app.route('/', require('./view'))
app.route('#active', require('./view'))
app.route('#completed', require('./view'))
app.route('*', require('./view'))
app.route('/', require('./views/main'))
app.route('#active', require('./views/main'))
app.route('#completed', require('./views/main'))
app.route('*', require('./views/main'))

module.exports = app.mount('body')
Loading