-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.go
219 lines (204 loc) · 5.48 KB
/
app.go
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
// +build js
package main
import (
"bytes"
"github.com/gopherjs/jquery"
"github.com/gopherjs/todomvc/utils"
"html/template"
)
var jQuery = jquery.NewJQuery //for convenience
const (
KEY = "TodoMVC4GopherJS"
ENTER_KEY = 13
ESCAPE_KEY = 27
)
func main() {
app := NewApp()
app.bindEvents()
app.initRouter()
app.render()
}
type ToDo struct {
Id string
Text string
Completed bool
}
type App struct {
todos []ToDo
todoTmpl *template.Template
footerTmpl *template.Template
todoAppJQuery jquery.JQuery
headerJQuery jquery.JQuery
mainJQuery jquery.JQuery
footerJQuery jquery.JQuery
newTodoJQuery jquery.JQuery
toggleAllJQuery jquery.JQuery
todoListJQuery jquery.JQuery
countJQuery jquery.JQuery
clearBtnJQuery jquery.JQuery
filter string
}
func NewApp() *App {
somethingToDo := make([]ToDo, 0)
utils.Retrieve(KEY, &somethingToDo)
todoHtml := jQuery("#todo-template").Html()
todoTmpl := template.Must(template.New("todo").Parse(todoHtml))
footerHtml := jQuery("#footer-template").Html()
footerTmpl := template.Must(template.New("footer").Parse(footerHtml))
todoAppJQuery := jQuery("#todoapp")
headerJQuery := todoAppJQuery.Find("#header")
mainJQuery := todoAppJQuery.Find("#main")
footerJQuery := todoAppJQuery.Find("#footer")
newTodoJQuery := headerJQuery.Find("#new-todo")
toggleAllJQuery := mainJQuery.Find("#toggle-all")
todoListJQuery := mainJQuery.Find("#todo-list")
countJQuery := footerJQuery.Find("#todo-count")
clearBtnJQuery := footerJQuery.Find("#clear-completed")
filter := "all"
return &App{somethingToDo, todoTmpl, footerTmpl, todoAppJQuery, headerJQuery, mainJQuery, footerJQuery, newTodoJQuery, toggleAllJQuery, todoListJQuery, countJQuery, clearBtnJQuery, filter}
}
func (a *App) bindEvents() {
a.newTodoJQuery.On(jquery.KEYUP, a.create)
a.toggleAllJQuery.On(jquery.CHANGE, a.toggleAll)
a.footerJQuery.On(jquery.CLICK, "#clear-completed", a.destroyCompleted)
a.todoListJQuery.On(jquery.CHANGE, ".toggle", a.toggle)
a.todoListJQuery.On(jquery.DBLCLICK, "label", a.edit)
a.todoListJQuery.On(jquery.KEYUP, ".edit", a.blurOnEnter)
a.todoListJQuery.On(jquery.FOCUSOUT, ".edit", a.update)
a.todoListJQuery.On(jquery.CLICK, ".destroy", a.destroy)
}
func (a *App) initRouter() {
router := utils.NewRouter()
router.On("/:filter", func(filter string) {
a.filter = filter
a.render()
})
router.Init("/all")
}
func (a *App) render() {
todos := a.getFilteredTodos()
var b bytes.Buffer
a.todoTmpl.Execute(&b, todos)
strtodoTmpl := b.String()
a.todoListJQuery.SetHtml(strtodoTmpl)
a.mainJQuery.Toggle(len(a.todos) > 0)
a.toggleAllJQuery.SetProp("checked", len(a.getActiveTodos()) != 0)
a.renderfooter()
a.newTodoJQuery.Focus()
utils.Store(KEY, a.todos)
}
func (a *App) renderfooter() {
activeTodoCount := len(a.getActiveTodos())
activeTodoWord := utils.Pluralize(activeTodoCount, "item")
completedTodos := len(a.todos) - activeTodoCount
filter := a.filter
footerData := struct {
ActiveTodoCount int
ActiveTodoWord string
CompletedTodos int
Filter string
}{
activeTodoCount, activeTodoWord, completedTodos, filter,
}
var b bytes.Buffer
a.footerTmpl.Execute(&b, footerData)
footerJQueryStr := b.String()
a.footerJQuery.Toggle(len(a.todos) > 0).SetHtml(footerJQueryStr)
}
func (a *App) toggleAll(e jquery.Event) {
checked := !a.toggleAllJQuery.Prop("checked").(bool)
for idx := range a.todos {
a.todos[idx].Completed = checked
}
a.render()
}
func (a *App) getActiveTodos() []ToDo {
todosTmp := make([]ToDo, 0)
for _, val := range a.todos {
if !val.Completed {
todosTmp = append(todosTmp, val)
}
}
return todosTmp
}
func (a *App) getCompletedTodos() []ToDo {
todosTmp := make([]ToDo, 0)
for _, val := range a.todos {
if val.Completed {
todosTmp = append(todosTmp, val)
}
}
return todosTmp
}
func (a *App) getFilteredTodos() []ToDo {
switch a.filter {
case "active":
return a.getActiveTodos()
case "completed":
return a.getCompletedTodos()
default:
return a.todos
}
}
func (a *App) destroyCompleted(e jquery.Event) {
a.todos = a.getActiveTodos()
a.filter = "all"
a.render()
}
func (a *App) indexFromEl(e jquery.Event) int {
id := jQuery(e.Target).Closest("li").Data("id")
for idx, val := range a.todos {
if val.Id == id {
return idx
}
}
return -1
}
func (a *App) create(e jquery.Event) {
val := jquery.Trim(a.newTodoJQuery.Val())
if len(val) == 0 || e.KeyCode != ENTER_KEY {
return
}
newToDo := ToDo{Id: utils.Uuid(), Text: val, Completed: false}
a.todos = append(a.todos, newToDo)
a.newTodoJQuery.SetVal("")
a.render()
}
func (a *App) toggle(e jquery.Event) {
idx := a.indexFromEl(e)
a.todos[idx].Completed = !a.todos[idx].Completed
a.render()
}
func (a *App) edit(e jquery.Event) {
input := jQuery(e.Target).Closest("li").AddClass("editing").Find(".edit")
input.SetVal(input.Val()).Focus()
}
func (a *App) blurOnEnter(e jquery.Event) {
switch e.KeyCode {
case ENTER_KEY:
jQuery(e.Target).Blur()
case ESCAPE_KEY:
jQuery(e.Target).SetData("abort", "true").Blur()
}
}
func (a *App) update(e jquery.Event) {
thisJQuery := jQuery(e.Target)
val := jquery.Trim(thisJQuery.Val())
if thisJQuery.Data("abort") == "true" {
thisJQuery.SetData("abort", "false")
a.render()
return
}
idx := a.indexFromEl(e)
if len(val) > 0 {
a.todos[idx].Text = val
} else {
a.todos = append(a.todos[:idx], a.todos[idx+1:]...)
}
a.render()
}
func (a *App) destroy(e jquery.Event) {
idx := a.indexFromEl(e)
a.todos = append(a.todos[:idx], a.todos[idx+1:]...)
a.render()
}