This repository has been archived by the owner on Jan 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
176 lines (156 loc) · 4.27 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//The TodoService handles access to persistent Todo items
class TodoService {
//load all Todos ordered by completion and name
static all(listId) {
return DB.Todo.find()
.equal("listId", listId)
.ascending("done")
.ascending("name")
.resultList();
}
//load unfinished Todos
static unfinished(listId) {
return DB.Todo.find()
.equal("listId", listId)
.notEqual("done", true)
.resultList();
}
//load finished Todos
static done(listId) {
return DB.Todo.find()
.equal("listId", listId)
.equal("done", true)
.resultList();
}
//delete a Todo
static delete(todo) {
todo.delete();
}
//save a Todo
static save(todo) {
todo.save();
}
}
//The controller does event handling and rendering, passing todos between view and service
class TodoCtrl {
constructor() {
this.todos = {};
this.isUnfinished = false;
this.template = null;
this.listId = null;
}
//Updates the list of loaded Todos
updateLocal(toSave) {
this.todos = {};
toSave.forEach((todo) => this.todos[todo.id] = todo);
this.render();
}
//Shows all Todos
showAll() {
this.isUnfinished = false;
TodoService.all(this.listId).then((todos) => this.updateLocal(todos));
}
//Shows unfinished Todos
showUnfinished() {
this.isUnfinished = true;
TodoService.unfinished(this.listId).then((todos) => this.updateLocal(todos));
}
//Shows completed Todos
showDone() {
this.isUnfinished = false;
TodoService.done(this.listId).then((todos) => this.updateLocal(todos));
}
//Deletes a Todo by id
delete(id) {
TodoService.delete(this.todos[id]);
delete this.todos[id];
this.render();
}
//Marks a Todo as done
done(id) {
this.todos[id].done = true;
TodoService.save(this.todos[id]);
if (this.isUnfinished)
delete this.todos[id];
this.render();
}
//Adds a new Todo by name
add(name) {
var todo = new DB.Todo({
activities: new DB.List(),
name: name,
listId: this.listId
});
TodoService.save(todo);
this.todos[todo.id] = todo;
this.render();
}
//Starts or stops work on a Todo
toggleWork(id) {
var todo = this.todos[id];
todo.active = !todo.active;
if (todo.active) {
todo.activities.unshift(new DB.Activity({
start: new Date()
}));
} else {
todo.activities[0].end = new Date();
}
TodoService.save(todo);
this.render();
}
//Repaints the TodoList
render() {
var open = $('.in').attr('id');
$('#todos').html(this.template({
todos: this.todos,
isUnfinished: this.isUnfinished
}));
$('#' + open).addClass('in');
}
//Sets up the template
onReady() {
var hash = location.hash;
if (hash != '' && hash != '#') {
hash = hash.substring(1);
if (hash == this.listId)
return;
this.listId = hash;
} else if (localStorage["listId"]) {
this.listId = localStorage["listId"];
} else {
this.listId = DB.util.uuid();
localStorage["listId"] = this.listId;
}
//replace the location, so the back button works properly
location.replace('#' + this.listId);
//Ignore the first hash change
setTimeout(() => $(window).on('hashchange', () => this.onReady()));
$("#shareURL").val(location);
var source = $('#todo-template').html();
this.template = Handlebars.compile(source);
this.showUnfinished();
}
}
var TodoController = new TodoCtrl({}, false, null, null);
//Change "toodle" to your own app name here!
DB.connect("toodle").then(() => TodoController.onReady());
//Boilerplate helper code for date formatting
Handlebars.registerHelper('pretty', function(date) {
return date == null ? "" : date.toLocaleString();
});
Handlebars.registerHelper('diff', function(from, to) {
return (from == null || to == null) ? "" : Math.round((to - from) / 1000) + " s";
});
Handlebars.registerHelper('sum', function(activities) {
if (activities == null || activities.length == 0 ) {
return "";
} else {
var sum = Math.round(activities
.map((a) => a.end ? (a.end - a.start) : 0)
.reduce((a, b) => a + b, 0) / 1000);
return sum != 0 ? sum + " s" : "";
}
});
//Fixes an iOS bug that causes click delay
FastClick.attach(document.body);