-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtodos.coffee
332 lines (262 loc) · 11.4 KB
/
todos.coffee
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# An example Backbone application contributed by
# [Jérôme Gravel-Niquet](http://jgn.me/). This demo uses a simple
# [LocalStorage adapter](backbone-localstorage.html)
# to persist Backbone models within your browser.
#
# This [CoffeeScript](http://jashkenas.github.com/coffee-script/) variation has been provided by [Jason Giedymin](http://jasongiedymin.com/).
#
# Note: two things you will notice with my CoffeeScript are that I prefer to
# use four space inde:nts and prefer to use `()` for all functions.
# Load the application once the DOM is ready, using a `jQuery.ready` shortcut.
$ ->
### Todo Model ###
# Our basic **Todo** model has `content`, `order`, and `done` attributes.
class Proposition extends StackMob.Model
# Default attributes for the todo.
defaults:
content: "empty todo..."
done: false
agree_votes: 0
disagree_votes: 0
abstain_votes: 0
negative: []
positive: []
# i: 1
vote: (vote_type) ->
if vote_type is "agree"
@save (agree_votes: @get("agree_votes") + 1)
else if vote_type is "disagree"
@save (disagree_votes: @get("disagree_votes") + 1)
else if vote_type is "abstain"
@save (abstain_votes: @get("abstain_votes") + 1)
# Ensure that each todo created has `content`.
initialize: ->
if !@get("content")
@set({ "content": @defaults.content })
# Toggle the `done` state of this todo item.
toggle: ->
@save({ done: !@get("done") })
# Remove this Todo from *localStorage* and delete its view.
clear: ->
@destroy()
@view.remove()
savepos: (el) ->
a = @get('positive')
a.push(el)
@save(positive: a)
saveneg: (el) ->
a = (@get('negative'))
a.push(el)
@save(negative: a)
### Todo Collection ###
# The collection of todos is backed by *localStorage* instead of a remote
# server.
class PropositionList extends StackMob.Collection
# Reference to this collection's model.
model: Proposition
# Save all of the todo items under the `"todos"` namespace.
# localStorage: new Store("todos")
# Attribute getter/setter
getDone = (todo) ->
return todo.get("done")
# Filter down the list of all todo items that are finished.
done: ->
return @filter( getDone )
# Filter down the list to only todo items that are still not finished.
remaining: ->
return @without.apply( this, @done() )
# We keep the Todos in sequential order, despite being saved by unordered
# GUID in the database. This generates the next order number for new items.
nextOrder: ->
return 1 if !@length
return @last().get('order') + 1
# Todos are sorted by their original insertion order.
comparator: (todo) ->
return todo.get("order")
### Todo Item View ###
# The DOM element for a todo item...
class PropositionView extends Backbone.View
#... is a list tag.
tagName: "li"
# Cache the template function for a single item.
template: _.template( $("#item-template").html() )
# The DOM events specific to an item.
events:
"click .check" : "toggleDone",
"dblclick div.todo-content" : "edit",
"click span.todo-destroy" : "clear",
"keypress .todo-input" : "updateOnEnter"
"click .agree" : "voteAgree"
"click .disagree" : "voteDisagree"
"click .abstained" : "voteAbstained"
"keypress .positive" : "pushPosOnEnter"
"keypress .negative" : "pushNegOnEnter"
# "keypress .positive" : "createOnEnterP" ,
# "keypress .negative" : "createOnEnterN"
# The TodoView listens for changes to its model, re-rendering. Since there's
# a one-to-one correspondence between a **Todo** and a **TodoView** in this
# app, we set a direct reference on the model for convenience.
initialize: ->
@model.bind('change', this.render);
@model.view = this;
@inputp = this.$(".positive")
@inputn = this.$(".negative")
# Re-render the contents of the todo item.
render: =>
content = "<div id=" + @model.get('content') + "></div>"
positivelist = "<ul>" + @model.get('positive') + "</ul>"
negativelist = "<ul>" + @model.get('negative') + "</ul>"
agree_votes = @model.get('agree_votes')
disagree_votes = @model.get('disagree_votes')
abstain_votes = @model.get('abstain_votes')
agv = " agree votes: <font color = \"green\">"
dgv = "</font>disagree votes: <font color = \"red\">"
abv = "</font> abstain votes: <font color = \"gold\">"
sop = "</font> <ul>supporting propositions:</ul>"
opp = "<ul>opposing propositions:</ul>"
this.$(@el).html( content + @template(@model.toJSON()) + agv + agree_votes + dgv + disagree_votes + abv + abstain_votes + sop + positivelist + opp + negativelist )
@setContent()
return this
# To avoid XSS (not that it would be harmful in this particular app),
# we use `jQuery.text` to set the contents of the todo item.
setContent: ->
content = @model.get("content")
this.$(".todo-content").text(content)
@input = this.$(".todo-input");
@input.bind("blur", @close);
@input.val(content);
@inputp = this.$(".positive";)
@inputp.bind("blur", @pushPosOnEnter);
@inputn = this.$(".negative";)
@inputn.bind("blur", @pushNegOnEnter);
# Toggle the `"done"` state of the model.
toggleDone: ->
@model.toggle()
# Switch this view into `"editing"` mode, displaying the input field.
edit: =>
this.$(@el).addClass("editing")
@input.focus()
# Close the `"editing"` mode, saving changes to the todo.
close: =>
@model.save({ content: @input.val() })
$(@el).removeClass("editing")
voteAgree: =>
@model.vote("agree")
voteDisagree: =>
@model.vote("disagree")
voteAbstained: =>
@model.vote("abstain")
# If you hit `enter`, we're through editing the item.
updateOnEnter: (e) =>
@close() if e.keyCode is 13
pushPosOnEnter: (e) ->
if e.keyCode != 13
return
Propositions.create( @newAttributesP() )
@model.savepos ("<a href =\"#" + @inputp.val() + "\"><ul>" + @inputp.val() + "</a></ul>")
@inputp.val('')
# alert ((i) for i in @model.get ("positive"))
pushNegOnEnter: (e) =>
if e.keyCode != 13
return
Propositions.create( @newAttributesN() )
@model.saveneg ("<a href=\"#" + @inputn.val() + "\"><ul>" + @inputn.val() + "</a></ul>")
@inputn.val('')
# alert ((i) for i in @model.get ("negative"))
# Remove this view from the DOM.
remove: ->
$(@el).remove()
# Remove the item, destroy the model.
clear: () ->
@model.clear()
# createOnEnterP: (e) ->
# return if (e.keyCode != 13)
# createOnEnterN: (e) ->
# return if (e.keyCode != 13)
newAttributesP: ->
#that was added to observe @inputp alert @inputp.get("val()")
return {
content: @inputp.val(),
order: Propositions.nextOrder(),
done: false
}
newAttributesN: ->
return {
content: @inputn.val(),
order: Propositions.nextOrder(),
done: false
}
### The Application ###
# Our overall **AppView** is the top-level piece of UI.
class AppView extends Backbone.View
# Instead of generating a new element, bind to the existing skeleton of
# the App already present in the HTML.
el_tag = "#todoapp"
el: $(el_tag)
# Our template for the line of statistics at the bottom of the app.
statsTemplate: _.template( $("#stats-template").html() )
# Delegated events for creating new items, and clearing completed ones.
events:
"keypress #new-todo" : "createOnEnter",
"keyup #new-todo" : "showTooltip",
"click .todo-clear a" : "clearCompleted" ,
# At initialization we bind to the relevant events on the `Todos`
# collection, when items are added or changed. Kick things off by
# loading any preexisting todos that might be saved in *localStorage*.
initialize: ->
@input = this.$("#new-todo")
Propositions.bind("add", @addOne)
Propositions.bind("reset", @addAll)
Propositions.bind("all", @render)
Propositions.fetch()
# Re-rendering the App just means refreshing the statistics -- the rest
# of the app doesn't change.
render: =>
this.$('#todo-stats').html( @statsTemplate({
total: Propositions.length,
done: Propositions.done().length,
remaining: Propositions.remaining().length
}))
# Add a single todo item to the list by creating a view for it, and
# appending its element to the `<ul>`.
addOne: (todo) =>
view = new PropositionView( {model: todo} )
this.$("#todo-list").append( view.render().el )
# Add all items in the **Todos** collection at once.
addAll: =>
Propositions.each(@addOne);
# Generate the attributes for a new Todo item.
newAttributes: ->
return {
content: @input.val(),
order: Propositions.nextOrder(),
done: false
}
# If you hit return in the main input field, create new **Todo** model,
# persisting it to *localStorage*.
createOnEnter: (e) ->
return if (e.keyCode != 13)
Propositions.create( @newAttributes() )
@input.val('')
# Clear all done todo items, destroying their models.
clearCompleted: ->
_.each(Propositions.done(), (todo) ->
todo.clear()
)
return false
# Lazily show the tooltip that tells you to press `enter` to save
# a new todo item, after one second.
showTooltip: (e) ->
tooltip = this.$(".ui-tooltip-top")
val = @input.val()
tooltip.fadeOut()
clearTimeout(@tooltipTimeout) if (@tooltipTimeout)
return if (val is '' || val is @input.attr("placeholder"))
show = () ->
tooltip.show().fadeIn()
@tooltipTimeout = _.delay(show, 1000)
# Create our global collection of **Todos**.
# Note: I've actually chosen not to export globally to `window`.
# Original documentation has been left intact.
Propositions = new PropositionList
App = new AppView()