-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapp.js
145 lines (124 loc) · 4.15 KB
/
app.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
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
134
135
136
137
138
139
140
141
142
143
144
145
(function () {
const { Component } = owl;
const { xml } = owl.tags;
const { whenReady } = owl.utils;
const { useRef, useSubEnv } = owl.hooks;
// -------------------------------------------------------------------------
// Model
// -------------------------------------------------------------------------
class TaskModel extends owl.core.EventBus {
nextId = 1
tasks = [];
constructor(tasks) {
super()
for (let task of tasks) {
this.tasks.push(task);
this.nextId = Math.max(this.nextId, task.id + 1);
}
}
addTask(title) {
const newTask = {
id: this.nextId++,
title: title,
isCompleted: false,
};
this.tasks.unshift(newTask);
this.trigger('update');
}
toggleTask(id) {
const task = this.tasks.find(t => t.id === id);
task.isCompleted = !task.isCompleted;
this.tasks.sort(function (a,b) {
if (a.isCompleted) {
if (b.isCompleted) {
a.title.localeCompare(b.title)
} else {
return 1;
}
} else {
if (b.isCompleted) {
return -1;
} else {
a.title.localeCompare(b.title)
}
}
});
this.trigger('update')
}
deleteTask(id) {
const index = this.tasks.findIndex(t => t.id === id);
this.tasks.splice(index, 1);
this.trigger('update');
}
}
class StoredTaskModel extends TaskModel {
constructor(storage) {
const tasks = storage.getItem("todoapp");
super(tasks ? JSON.parse(tasks) : []);
this.on('update', this, () => {
storage.setItem("todoapp", JSON.stringify(this.tasks))
});
}
}
// -------------------------------------------------------------------------
// Task Component
// -------------------------------------------------------------------------
const TASK_TEMPLATE = xml /* xml */`
<div class="task" t-att-class="props.task.isCompleted ? 'done' : ''">
<input type="checkbox" t-att-checked="props.task.isCompleted" t-on-click="toggleTask"/>
<span><t t-esc="props.task.title"/></span>
<span class="delete" t-on-click="deleteTask">🗑</span>
</div>`;
class Task extends Component {
static template = TASK_TEMPLATE;
toggleTask() {
this.env.model.toggleTask(this.props.task.id);
}
deleteTask() {
this.env.model.deleteTask(this.props.task.id);
}
}
// -------------------------------------------------------------------------
// App Component
// -------------------------------------------------------------------------
const APP_TEMPLATE = xml /* xml */`
<div class="todo-app">
<input placeholder="Enter a new task" t-on-keyup="addTask" t-ref="add-input"/>
<div class="task-list">
<t t-foreach="env.model.tasks" t-as="task" t-key="task.id">
<Task task="task"/>
</t>
</div>
</div>`;
class App extends Component {
static template = APP_TEMPLATE;
static components = { Task };
inputRef = useRef("add-input");
constructor() {
super();
const model = new StoredTaskModel(this.env.localStorage);
model.on('update', this, this.render);
useSubEnv({model});
}
mounted() {
this.inputRef.el.focus();
}
addTask(ev) {
// 13 is keycode for ENTER
if (ev.keyCode === 13) {
const title = ev.target.value.trim();
ev.target.value = "";
if (title) {
this.env.model.addTask(title);
}
}
}
}
// Setup code
function setup() {
App.env.localStorage = window.localStorage;
const app = new App();
app.mount(document.body);
}
whenReady(setup);
})();