-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.ts
71 lines (61 loc) · 1.79 KB
/
index.ts
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
import { html, View, on } from 'rune-ts';
import { CheckView } from '../lib/CheckView';
import { Toggled } from '../lib/events/Toggled';
import { ListView } from '../lib/ListView';
import { CheckAllController } from '../lib/CheckAllController';
interface Todo {
title: string;
completed: boolean;
}
class TodoItemView extends View<Todo> {
private _checkView = new CheckView({ on: this.data.completed });
override template({ title, completed }: Todo) {
return html`
<div class="${completed ? 'completed' : ''}">
${this._checkView}
<span class="title">${title}</span>
</div>
`;
}
@on(Toggled)
private _syncCompleted() {
this.data.completed = this._checkView.data.on;
this.element().classList.toggle('completed', this.data.completed);
}
setCompleted(completed: boolean) {
this._checkView.setOn(completed);
this._syncCompleted();
}
}
class TodoListView extends ListView<Todo, TodoItemView> {
createItemView(itemData: Todo) {
return new TodoItemView(itemData);
}
}
class TodoPage extends View<Todo[]> {
private _checkAllController = new CheckAllController(
new CheckView(),
new TodoListView(this.data),
(itemView) => itemView.data.completed,
(itemView, bool) => itemView.setCompleted(bool),
);
override template() {
return html`
<div>
<div class="header">
${this._checkAllController.checkAllView}
<input type="text" />
</div>
<div class="body">${this._checkAllController.listView}</div>
</div>
`;
}
}
export function main() {
const todos: Todo[] = [
{ title: 'Coding', completed: false },
{ title: 'Dinner', completed: true },
{ title: 'Test', completed: false },
];
document.querySelector('#body')!.append(new TodoPage(todos).render());
}