-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtodo-list.html
More file actions
133 lines (108 loc) · 3.74 KB
/
todo-list.html
File metadata and controls
133 lines (108 loc) · 3.74 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.6/handlebars.min.js"></script>
<script src="../dist/fronty.js"></script>
<!-- templates -->
<script id="todo-list-template" type="text/x-handlebars-template">
<div>
<ul class="list-group">
{{#each items}}
<li fronty-component="TodoItemComponent" id="item-{{id}}" key="item-{{id}}" model="items[{{@index}}]"></li>
{{/each}}
</ul>
<div>
Add item: <input class="form-control" id="itemname" type="text"></input><button type="button" id="addbutton" class="btn btn-success">Add</button>
</div>
</div>
</script>
<script id="todo-item" type="text/x-handlebars-template">
<li class="{{#if done}}disabled {{/if}}list-group-item" key="item-{{id}}">
{{description}}
{{#if done}}
<button class="reopenbutton btn btn-default" type="button">reopen</button>
{{else}}
<button class="donebutton btn btn-default" type="button">done</button>
{{/if}}
<button class="removebutton btn btn-default" type="button">remove</button>
</li>
</script>
<script>
// Model
class TodoList extends Fronty.Model {
constructor() {
super('todolist-model');
this.items = [];
}
addItem(item) {
this.items.unshift(item);
}
}
class TodoItem extends Fronty.Model {
constructor(description, done, list) {
super(description);
this.id = TodoItem.autoincrement++;
this.description = description;
this.done = done;
this.list = list;
}
done() {
this.done = true;
}
reopen() {
this.done = false;
}
}
TodoItem.autoincrement = 0; // a simple key generator
// Components
class TodoListComponent extends Fronty.ModelComponent {
constructor(id, items) {
super(Handlebars.compile(document.getElementById('todo-list-template').innerHTML), items, id);
this.addEventListener('click', '#addbutton', () => {
items.set(() => {
items.addItem(new TodoItem(document.getElementById('itemname').value, false, items));
});
});
}
}
class TodoItemComponent extends Fronty.ModelComponent {
constructor(id, item) {
super(Handlebars.compile(document.getElementById('todo-item').innerHTML), item, id);
this.item = item;
this.addEventListener('click', '.donebutton', () => {
this.item.set(() => {
this.item.done = true
});
});
this.addEventListener('click', '.reopenbutton', () => {
this.item.set(() => {
this.item.done = false
});
});
this.addEventListener('click', '.removebutton', () => {
var todoList = this.item.list;
todoList.set( () => {
todoList.items.splice(todoList.items.indexOf(this.item), 1);
});
});
}
}
var todos = new TodoList();
var todoListComponent = new TodoListComponent('todo-list-app', todos);
todos.set(() => {
todos.addItem(new TodoItem('lunch', false, todos));
todos.addItem(new TodoItem('dinner', false, todos));
});
</script>
</head>
<body>
<h1>Todo list with <a href="https://github.com/lipido/fronty.js">fronty.js</a></h1>
<div id="todo-list-app">Loading</div>
<script>
todoListComponent.start();
</script>
</body>
</html>