This repository has been archived by the owner on Jun 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathtodo-item.js
107 lines (89 loc) · 2.74 KB
/
todo-item.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict';
var hg = require('../../index.js');
var h = require('../../index.js').h;
var cuid = require('cuid');
var WeakmapEvent = require('../lib/weakmap-event.js');
var FocusHook = require('../lib/focus-hook.js');
var DestroyEvent = WeakmapEvent();
var ESCAPE = 27;
TodoItem.onDestroy = DestroyEvent.listen;
TodoItem.setCompleted = function setCompleted(s, x) {
s.completed.set(x);
};
TodoItem.isCompleted = function isCompleted(s) {
return s.completed();
};
module.exports = TodoItem;
function TodoItem(item) {
item = item || {};
return hg.state({
id: hg.value(item.id || cuid()),
title: hg.value(item.title || ''),
editing: hg.value(item.editing || false),
completed: hg.value(item.completed || false),
channels: {
toggle: toggle,
startEdit: startEdit,
cancelEdit: cancelEdit,
finishEdit: finishEdit
}
});
}
function toggle(state, data) {
state.completed.set(data.completed);
}
function startEdit(state) {
state.editing.set(true);
}
function finishEdit(state, data) {
if (state.editing() === false) {
return;
}
state.editing.set(false);
state.title.set(data.title);
if (data.title.trim() === '') {
DestroyEvent.broadcast(state, {
id: state.id()
});
}
}
function cancelEdit(state) {
state.editing.set(false);
}
var sneakyGlobal = {};
TodoItem.render = function render(todo, parentHandles) {
var className = (todo.completed ? 'completed ' : '') +
(todo.editing ? 'editing' : '');
sneakyGlobal[todo.id] = todo;
return h('li', { className: className, key: todo.id }, [
h('.view', [
h('input.toggle', {
type: 'checkbox',
checked: todo.completed,
'ev-change': hg.send(todo.channels.toggle, {
completed: !todo.completed
})
}),
h('label', {
'ev-dblclick': hg.send(todo.channels.startEdit)
}, todo.title),
h('button.destroy', {
'ev-click': hg.send(parentHandles.destroy, {
id: todo.id
})
})
]),
h('input.edit', {
value: todo.title,
name: 'title',
// when we need an RPC invocation we add a
// custom mutable operation into the tree to be
// invoked at patch time
'ev-focus': todo.editing ? FocusHook() : null,
'ev-keydown': hg.sendKey(
todo.channels.cancelEdit, null, {key: ESCAPE}),
'ev-event': hg.sendSubmit(todo.channels.finishEdit),
'ev-blur': hg.sendValue(todo.channels.finishEdit)
})
]);
};