-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodolist.go
449 lines (399 loc) · 11.8 KB
/
todolist.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package main
import (
"encoding/json"
"fmt"
"io"
"strings"
"github.com/mbertschler/guiapi"
"github.com/mbertschler/html"
"github.com/mbertschler/html/attr"
)
type Page struct {
*guiapi.PageCtx
Sess *Session
}
type Action struct {
*guiapi.ActionCtx
Sess *Session
State TodoListState
}
type ActionFunc[T any] func(c *Action, args *T) (*guiapi.Update, error)
func ContextAction[T any](db *DB, fn ActionFunc[T]) guiapi.ActionFunc {
return func(c *guiapi.ActionCtx) (*guiapi.Update, error) {
var input T
if c.Args != nil {
err := json.Unmarshal(c.Args, &input)
if err != nil {
return nil, err
}
}
ctx := &Action{
ActionCtx: c,
Sess: db.Session(c.Writer, c.Request),
}
err := json.Unmarshal(c.State, &ctx.State)
if err != nil {
return nil, err
}
return fn(ctx, &input)
}
}
type PageFunc func(c *Page) (guiapi.Page, error)
func PageWrapper(db *DB) func(PageFunc) guiapi.PageFunc {
return func(pf PageFunc) guiapi.PageFunc {
return func(ctx *guiapi.PageCtx) (guiapi.Page, error) {
sess := db.Session(ctx.Writer, ctx.Request)
return pf(&Page{PageCtx: ctx, Sess: sess})
}
}
}
type TodoPage struct {
Content html.Block
State TodoListState
}
func (t *TodoPage) WriteHTML(w io.Writer) error {
stateJSON, err := json.Marshal(t.State)
if err != nil {
return err
}
block := html.Blocks{
html.Doctype("html"),
html.Html(attr.Lang("en"),
html.Head(nil,
html.Meta(attr.Charset("utf-8")),
html.Meta(attr.Name("viewport").Content("width=device-width, initial-scale=1")),
html.Title(nil, html.Text("Guiapi • TodoMVC")),
html.Link(attr.Rel("stylesheet").Href("https://cdn.jsdelivr.net/npm/[email protected]/index.min.css")),
html.Link(attr.Rel("stylesheet").Href("/dist/bundle.css")),
),
html.Body(nil,
html.Main(attr.Id("page"), t.Content),
html.Elem("footer", attr.Class("info"),
html.P(nil, html.Text("Double-click to edit a todo")),
html.P(nil, html.Text("Template by "), html.A(attr.Href("http://sindresorhus.com"), html.Text("Sindre Sorhus"))),
html.P(nil, html.Text("Created by "), html.A(attr.Href("https://github.com/mbertschler"), html.Text("Martin Bertschler"))),
html.P(nil, html.Text("Part of "), html.A(attr.Href("http://todomvc.com"), html.Text("TodoMVC"))),
html.Hr(nil),
html.P(attr.Class("biglink"), html.A(attr.Href("/counter"), html.Text("Counter Example"))),
html.P(attr.Class("biglink"), html.A(attr.Href("/reports"), html.Text("Reports Example"))),
),
html.Script(nil, html.JS("var state = "+string(stateJSON)+";")),
html.Script(attr.Src("/dist/bundle.js")),
),
),
}
return html.RenderMinified(w, block)
}
func (t *TodoPage) Update() (*guiapi.Update, error) {
out, err := html.RenderMinifiedString(t.Content)
if err != nil {
return nil, err
}
res := guiapi.ReplaceContent("#page", out)
res.State = t.State
return res, nil
}
type TodoList struct {
*DB
}
func (t *TodoList) Register(s *guiapi.Server) {
s.AddPage("/", t.RenderFullPage(TodoListPageAll))
s.AddPage("/active", t.RenderFullPage(TodoListPageActive))
s.AddPage("/completed", t.RenderFullPage(TodoListPageCompleted))
s.AddAction("TodoList.NewTodo", ContextAction(t.DB, t.NewTodo))
s.AddAction("TodoList.ToggleItem", ContextAction(t.DB, t.ToggleItem))
s.AddAction("TodoList.ToggleAll", ContextAction(t.DB, t.ToggleAll))
s.AddAction("TodoList.DeleteItem", ContextAction(t.DB, t.DeleteItem))
s.AddAction("TodoList.ClearCompleted", ContextAction(t.DB, t.ClearCompleted))
s.AddAction("TodoList.EditItem", ContextAction(t.DB, t.EditItem))
s.AddAction("TodoList.UpdateItem", ContextAction(t.DB, t.UpdateItem))
}
const (
TodoListPageAll = "all"
TodoListPageActive = "active"
TodoListPageCompleted = "completed"
)
type TodoListState struct {
Page string
}
type TodoListProps struct {
Page string
Todos *StoredTodo
EditItemID int
}
func (t *TodoList) RenderFullPage(page string) guiapi.PageFunc {
wrap := PageWrapper(t.DB)
return wrap(func(ctx *Page) (guiapi.Page, error) {
content, err := t.renderPageContent(ctx, page)
if err != nil {
return nil, err
}
return &TodoPage{
Content: content,
State: TodoListState{
Page: page,
}}, nil
})
}
func (t *TodoList) renderPageContent(ctx *Page, page string) (html.Block, error) {
props, err := t.todoListProps(ctx.Sess, page)
if err != nil {
return nil, err
}
return t.renderBlock(props)
}
type PageArgs struct {
Page string
}
func (t *TodoList) renderBlock(props *TodoListProps) (html.Block, error) {
var main, footer html.Block
if len(props.Todos.Items) > 0 {
var err error
main, err = t.renderMainBlock(props.Todos, props.Page, props.EditItemID)
if err != nil {
return nil, err
}
footer, err = t.renderFooterBlock(props.Todos, props.Page)
if err != nil {
return nil, err
}
}
block := html.Elem("section", attr.Class("todoapp"),
html.Elem("header", attr.Class("header"),
html.H1(nil, html.Text("todos")),
html.Input(attr.Class("new-todo ga").Name("new-todo").Placeholder("What needs to be done?").
Autofocus("").Attr("ga-on", "keydown").Attr("ga-func", "newTodoKeydown")),
),
main,
footer,
)
return block, nil
}
func (t *TodoList) renderMainBlock(todos *StoredTodo, page string, editItemID int) (html.Block, error) {
items := html.Blocks{}
for _, item := range todos.Items {
if page == TodoListPageActive && item.Done {
continue
}
if page == TodoListPageCompleted && !item.Done {
continue
}
items.Add(t.renderItem(&item, editItemID))
}
main := html.Elem("section", attr.Class("main"),
html.Input(attr.Id("toggle-all").Class("toggle-all").Type("checkbox")),
html.Label(attr.Class("ga").For("toggle-all").Attr("ga-on", "click").Attr("ga-action", "TodoList.ToggleAll"),
html.Text("Mark all as complete")),
html.Ul(attr.Class("todo-list"),
items,
),
)
return main, nil
}
func (t *TodoList) renderItem(item *StoredTodoItem, editItemID int) html.Block {
if item.ID == editItemID {
return t.renderItemEdit(item, editItemID)
}
liAttrs := attr.Attr("ga-on", "dblclick").
Attr("ga-action", "TodoList.EditItem").
Attr("ga-args", fmt.Sprintf(`{"id":%d}`, item.ID))
inputAttrs := attr.Class("toggle ga").Type("checkbox").
Attr("ga-on", "click").Attr("ga-action", "TodoList.ToggleItem").
Attr("ga-args", fmt.Sprintf(`{"id":%d}`, item.ID))
if item.Done {
liAttrs = liAttrs.Class("completed ga")
inputAttrs = inputAttrs.Checked("")
} else {
liAttrs = liAttrs.Class("active ga")
}
id := fmt.Sprintf("todo-%d", item.ID)
li := html.Li(liAttrs,
html.Div(attr.Class("view"),
html.Input(inputAttrs.Id(id)),
html.Label(attr.For(id), html.Text(item.Text)),
html.Button(attr.Class("destroy ga").
Attr("ga-on", "click").Attr("ga-action", "TodoList.DeleteItem").
Attr("ga-args", fmt.Sprintf(`{"id":%d}`, item.ID))),
),
)
return li
}
func (t *TodoList) renderItemEdit(item *StoredTodoItem, editItemID int) html.Block {
li := html.Li(attr.Class("editing"),
html.Div(attr.Class("view"),
html.Input(attr.Class("edit ga").Attr("ga-init", "initEdit").
Attr("ga-args", fmt.Sprintf(`{"id":%d}`, item.ID)).Value(item.Text)),
),
)
return li
}
func (t *TodoList) renderFooterBlock(todos *StoredTodo, page string) (html.Block, error) {
var allClass, activeClass, completedClass string
switch page {
case TodoListPageAll:
allClass = "selected"
case TodoListPageActive:
activeClass = "selected"
case TodoListPageCompleted:
completedClass = "selected"
default:
allClass = "selected"
}
leftCount := 0
someDone := false
for _, item := range todos.Items {
if !item.Done {
leftCount++
} else {
someDone = true
}
}
itemsLeftText := " items left"
if leftCount == 1 {
itemsLeftText = " item left"
}
var clearCompletedButton html.Block
if someDone {
clearCompletedButton = html.Button(attr.Class("clear-completed ga").Attr("ga-on", "click").Attr("ga-action", "TodoList.ClearCompleted"),
html.Text("Clear completed"))
}
footer := html.Elem("footer", attr.Class("footer"),
html.Span(attr.Class("todo-count"),
html.Strong(nil, html.Text(fmt.Sprint(leftCount))),
html.Text(itemsLeftText),
),
html.Ul(attr.Class("filters"),
html.Li(nil,
html.A(attr.Class(allClass+" ga").Href("/").Attr("ga-link", nil), html.Text("All")),
),
html.Li(nil,
html.A(attr.Class(activeClass+" ga").Href("/active").Attr("ga-link", nil), html.Text("Active")),
),
html.Li(nil,
html.A(attr.Class(completedClass+" ga").Href("/completed").Attr("ga-link", nil), html.Text("Completed")),
),
),
clearCompletedButton,
)
return footer, nil
}
type NewTodoArgs struct {
Text string `json:"text"`
}
func (t *TodoList) NewTodo(ctx *Action, input *NewTodoArgs) (*guiapi.Update, error) {
return t.updateTodoList(ctx, func(props *TodoListProps, todos *StoredTodo) error {
var highestID int
for _, item := range todos.Items {
if item.ID > highestID {
highestID = item.ID
}
}
input.Text = strings.TrimSpace(input.Text)
todos.Items = append(todos.Items, StoredTodoItem{ID: highestID + 1, Text: input.Text})
return t.DB.SetTodo(todos)
})
}
func (t *TodoList) ToggleItem(ctx *Action, args *IDArgs) (*guiapi.Update, error) {
return t.updateTodoList(ctx, func(props *TodoListProps, todos *StoredTodo) error {
for i, item := range todos.Items {
if item.ID == args.ID {
todos.Items[i].Done = !todos.Items[i].Done
}
}
return t.DB.SetTodo(todos)
})
}
func (t *TodoList) ToggleAll(ctx *Action, args *NoArgs) (*guiapi.Update, error) {
return t.updateTodoList(ctx, func(props *TodoListProps, todos *StoredTodo) error {
allDone := true
for _, item := range todos.Items {
if !item.Done {
allDone = false
break
}
}
for i := range todos.Items {
todos.Items[i].Done = !allDone
}
return t.DB.SetTodo(todos)
})
}
func (t *TodoList) DeleteItem(ctx *Action, args *IDArgs) (*guiapi.Update, error) {
return t.updateTodoList(ctx, func(props *TodoListProps, todos *StoredTodo) error {
var newItems []StoredTodoItem
for _, item := range todos.Items {
if item.ID == args.ID {
continue
}
newItems = append(newItems, item)
}
todos.Items = newItems
return t.DB.SetTodo(todos)
})
}
type NoArgs struct{}
func (t *TodoList) ClearCompleted(ctx *Action, _ *NoArgs) (*guiapi.Update, error) {
return t.updateTodoList(ctx, func(props *TodoListProps, todos *StoredTodo) error {
var newItems []StoredTodoItem
for _, item := range todos.Items {
if item.Done {
continue
}
newItems = append(newItems, item)
}
todos.Items = newItems
return t.DB.SetTodo(todos)
})
}
type IDArgs struct {
ID int `json:"id"`
}
func (t *TodoList) EditItem(ctx *Action, args *IDArgs) (*guiapi.Update, error) {
return t.updateTodoList(ctx, func(props *TodoListProps, _ *StoredTodo) error {
props.EditItemID = args.ID
return nil
})
}
type UpdateItemArgs struct {
ID int `json:"id"`
Text string `json:"text"`
}
func (t *TodoList) UpdateItem(ctx *Action, args *UpdateItemArgs) (*guiapi.Update, error) {
return t.updateTodoList(ctx, func(props *TodoListProps, todos *StoredTodo) error {
for i, item := range todos.Items {
if item.ID == args.ID {
todos.Items[i].Text = strings.TrimSpace(args.Text)
}
}
return t.DB.SetTodo(todos)
})
}
func (t *TodoList) updateTodoList(ctx *Action, fn func(*TodoListProps, *StoredTodo) error) (*guiapi.Update, error) {
props, err := t.todoListProps(ctx.Sess, ctx.State.Page)
if err != nil {
return nil, err
}
err = fn(props, props.Todos)
if err != nil {
return nil, err
}
appBlock, err := t.renderBlock(props)
if err != nil {
return nil, err
}
out, err := html.RenderMinifiedString(appBlock)
if err != nil {
return nil, err
}
return guiapi.ReplaceContent(".todoapp", out), nil
}
func (t *TodoList) todoListProps(sess *Session, page string) (*TodoListProps, error) {
todos, err := t.DB.GetTodo(sess.ID)
if err != nil {
return nil, err
}
return &TodoListProps{
Page: page,
Todos: todos,
}, nil
}