-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtodos.js
213 lines (213 loc) · 6.84 KB
/
todos.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
(function() {
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
}, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
$(function() {
/* Todo Model */ var App, AppView, Todo, TodoList, TodoView, Todos;
Todo = (function() {
__extends(Todo, Backbone.Model);
function Todo() {
Todo.__super__.constructor.apply(this, arguments);
}
Todo.prototype.defaults = {
content: "empty todo...",
done: false
};
Todo.prototype.initialize = function() {
if (!this.get("content")) {
return this.set({
"content": this.defaults.content
});
}
};
Todo.prototype.toggle = function() {
return this.save({
done: !this.get("done")
});
};
Todo.prototype.clear = function() {
this.destroy();
return this.view.remove();
};
return Todo;
})();
/* Todo Collection */
TodoList = (function() {
var getDone;
__extends(TodoList, Backbone.Collection);
function TodoList() {
TodoList.__super__.constructor.apply(this, arguments);
}
TodoList.prototype.model = Todo;
TodoList.prototype.localStorage = new Store("todos");
getDone = function(todo) {
return todo.get("done");
};
TodoList.prototype.done = function() {
return this.filter(getDone);
};
TodoList.prototype.remaining = function() {
return this.without.apply(this, this.done());
};
TodoList.prototype.nextOrder = function() {
if (!this.length) {
return 1;
}
return this.last().get('order') + 1;
};
TodoList.prototype.comparator = function(todo) {
return todo.get("order");
};
return TodoList;
})();
/* Todo Item View */
TodoView = (function() {
__extends(TodoView, Backbone.View);
function TodoView() {
this.updateOnEnter = __bind(this.updateOnEnter, this);
this.close = __bind(this.close, this);
this.edit = __bind(this.edit, this);
this.render = __bind(this.render, this);
TodoView.__super__.constructor.apply(this, arguments);
}
TodoView.prototype.tagName = "li";
TodoView.prototype.template = _.template($("#item-template").html());
TodoView.prototype.events = {
"click .check": "toggleDone",
"dblclick div.todo-content": "edit",
"click span.todo-destroy": "clear",
"keypress .todo-input": "updateOnEnter"
};
TodoView.prototype.initialize = function() {
this.model.bind('change', this.render);
return this.model.view = this;
};
TodoView.prototype.render = function() {
this.$(this.el).html(this.template(this.model.toJSON()));
this.setContent();
return this;
};
TodoView.prototype.setContent = function() {
var content;
content = this.model.get("content");
this.$(".todo-content").text(content);
this.input = this.$(".todo-input");
this.input.bind("blur", this.close);
return this.input.val(content);
};
TodoView.prototype.toggleDone = function() {
return this.model.toggle();
};
TodoView.prototype.edit = function() {
this.$(this.el).addClass("editing");
return this.input.focus();
};
TodoView.prototype.close = function() {
this.model.save({
content: this.input.val()
});
return $(this.el).removeClass("editing");
};
TodoView.prototype.updateOnEnter = function(e) {
if (e.keyCode === 13) {
return this.close();
}
};
TodoView.prototype.remove = function() {
return $(this.el).remove();
};
TodoView.prototype.clear = function() {
return this.model.clear();
};
return TodoView;
})();
/* The Application */
AppView = (function() {
var el_tag;
__extends(AppView, Backbone.View);
function AppView() {
this.addAll = __bind(this.addAll, this);
this.addOne = __bind(this.addOne, this);
this.render = __bind(this.render, this);
this.initialize = __bind(this.initialize, this);
AppView.__super__.constructor.apply(this, arguments);
}
el_tag = "#todoapp";
AppView.prototype.el = $(el_tag);
AppView.prototype.statsTemplate = _.template($("#stats-template").html());
AppView.prototype.events = {
"keypress #new-todo": "createOnEnter",
"keyup #new-todo": "showTooltip",
"click .todo-clear a": "clearCompleted"
};
AppView.prototype.initialize = function() {
this.input = this.$("#new-todo");
Todos.bind("add", this.addOne);
Todos.bind("reset", this.addAll);
Todos.bind("all", this.render);
return Todos.fetch();
};
AppView.prototype.render = function() {
return this.$('#todo-stats').html(this.statsTemplate({
total: Todos.length,
done: Todos.done().length,
remaining: Todos.remaining().length
}));
};
AppView.prototype.addOne = function(todo) {
var view;
view = new TodoView({
model: todo
});
return this.$("#todo-list").append(view.render().el);
};
AppView.prototype.addAll = function() {
return Todos.each(this.addOne);
};
AppView.prototype.newAttributes = function() {
return {
content: this.input.val(),
order: Todos.nextOrder(),
done: false
};
};
AppView.prototype.createOnEnter = function(e) {
if (e.keyCode !== 13) {
return;
}
Todos.create(this.newAttributes());
return this.input.val('');
};
AppView.prototype.clearCompleted = function() {
_.each(Todos.done(), function(todo) {
return todo.clear();
});
return false;
};
AppView.prototype.showTooltip = function(e) {
var show, tooltip, val;
tooltip = this.$(".ui-tooltip-top");
val = this.input.val();
tooltip.fadeOut();
if (this.tooltipTimeout) {
clearTimeout(this.tooltipTimeout);
}
if (val === '' || val === this.input.attr("placeholder")) {
return;
}
show = function() {
return tooltip.show().fadeIn();
};
return this.tooltipTimeout = _.delay(show, 1000);
};
return AppView;
})();
Todos = new TodoList;
return App = new AppView();
});
}).call(this);