@@ -28,6 +28,11 @@ import (
28
28
// todolist.js: 38 lines
29
29
// total: 511 lines
30
30
31
+ // Code stats after refactoring (excluding comments and imports):
32
+ // todolist.go: 405 lines -15%
33
+ // todolist.js: 38 lines
34
+ // total: 443 lines -14%
35
+
31
36
func registerTodoList (server * Server , db * DB ) {
32
37
tl := & TodoList {DB : db }
33
38
server .RegisterComponent (tl )
@@ -88,7 +93,7 @@ func todoLayout(todoApp html.Block, state TodoListState) (html.Block, error) {
88
93
html .Meta (html .Name ("viewport" ).Content ("width=device-width, initial-scale=1" )),
89
94
html .Title (nil , html .Text ("Guiapi • TodoMVC" )),
90
95
html .
Link (
html .
Rel (
"stylesheet" ).
Href (
"https://cdn.jsdelivr.net/npm/[email protected] /index.min.css" )),
91
- html .Link (html .Rel ("stylesheet" ).Href ("/css/main .css" )),
96
+ html .Link (html .Rel ("stylesheet" ).Href ("/dist/bundle .css" )),
92
97
),
93
98
html .Body (nil ,
94
99
todoApp ,
@@ -98,9 +103,8 @@ func todoLayout(todoApp html.Block, state TodoListState) (html.Block, error) {
98
103
html .P (nil , html .Text ("Created by " ), html .A (html .Href ("https://github.com/mbertschler" ), html .Text ("Martin Bertschler" ))),
99
104
html .P (nil , html .Text ("Part of " ), html .A (html .Href ("http://todomvc.com" ), html .Text ("TodoMVC" ))),
100
105
),
101
- html .Script (html .Src ("/js/guiapi.js" )),
102
106
html .Script (nil , html .JS ("var state = " + string (stateJSON )+ ";" )),
103
- html .Script (html .Src ("/js/todolist .js" )),
107
+ html .Script (html .Src ("/dist/bundle .js" )),
104
108
),
105
109
),
106
110
}, nil
@@ -195,30 +199,30 @@ func (t *TodoList) renderMainBlock(todos *StoredTodo, page string, editItemID in
195
199
if page == TodoListPageCompleted && ! item .Done {
196
200
continue
197
201
}
198
- items .Add (t .renderItem (& item , page , editItemID ))
202
+ items .Add (t .renderItem (& item , editItemID ))
199
203
}
200
204
main := html .Elem ("section" , html .Class ("main" ),
201
205
html .Input (html .Class ("toggle-all" ).Attr ("type" , "checkbox" )),
202
- html .Label (html .Class ("ga" ).Attr ("for" , "toggle-all" ).Attr ("ga-on" , "click" ).Attr ("ga-action" , "TodoList.ToggleAll" ).
203
- Attr ( "ga-args" , fmt . Sprintf ( `{"page":%q}` , page )), html .Text ("Mark all as complete" )),
206
+ html .Label (html .Class ("ga" ).Attr ("for" , "toggle-all" ).Attr ("ga-on" , "click" ).Attr ("ga-action" , "TodoList.ToggleAll" ),
207
+ html .Text ("Mark all as complete" )),
204
208
html .Ul (html .Class ("todo-list" ),
205
209
items ,
206
210
),
207
211
)
208
212
return main , nil
209
213
}
210
214
211
- func (t * TodoList ) renderItem (item * StoredTodoItem , page string , editItemID int ) html.Block {
215
+ func (t * TodoList ) renderItem (item * StoredTodoItem , editItemID int ) html.Block {
212
216
if item .ID == editItemID {
213
- return t .renderItemEdit (item , page , editItemID )
217
+ return t .renderItemEdit (item , editItemID )
214
218
}
215
219
216
220
liAttrs := html .Attr ("ga-on" , "dblclick" ).
217
221
Attr ("ga-action" , "TodoList.EditItem" ).
218
- Attr ("ga-args" , fmt .Sprintf (`{"id":%d,"page":%q }` , item .ID , page ))
222
+ Attr ("ga-args" , fmt .Sprintf (`{"id":%d}` , item .ID ))
219
223
inputAttrs := html .Class ("toggle ga" ).Attr ("type" , "checkbox" ).
220
224
Attr ("ga-on" , "click" ).Attr ("ga-action" , "TodoList.ToggleItem" ).
221
- Attr ("ga-args" , fmt .Sprintf (`{"id":%d,"page":%q }` , item .ID , page ))
225
+ Attr ("ga-args" , fmt .Sprintf (`{"id":%d}` , item .ID ))
222
226
if item .Done {
223
227
liAttrs = liAttrs .Class ("completed ga" )
224
228
inputAttrs = inputAttrs .Attr ("checked" , "" )
@@ -232,17 +236,17 @@ func (t *TodoList) renderItem(item *StoredTodoItem, page string, editItemID int)
232
236
html .Label (nil , html .Text (item .Text )),
233
237
html .Button (html .Class ("destroy ga" ).
234
238
Attr ("ga-on" , "click" ).Attr ("ga-action" , "TodoList.DeleteItem" ).
235
- Attr ("ga-args" , fmt .Sprintf (`{"id":%d,"page":%q }` , item .ID , page ))),
239
+ Attr ("ga-args" , fmt .Sprintf (`{"id":%d}` , item .ID ))),
236
240
),
237
241
)
238
242
return li
239
243
}
240
244
241
- func (t * TodoList ) renderItemEdit (item * StoredTodoItem , page string , editItemID int ) html.Block {
245
+ func (t * TodoList ) renderItemEdit (item * StoredTodoItem , editItemID int ) html.Block {
242
246
li := html .Li (html .Class ("editing" ),
243
247
html .Div (html .Class ("view" ),
244
248
html .Input (html .Class ("edit ga" ).Attr ("ga-init" , "initEdit" ).
245
- Attr ("ga-args" , fmt .Sprintf (`{"id":%d, "page":%q }` , item .ID , page )).Attr ("value" , item .Text )),
249
+ Attr ("ga-args" , fmt .Sprintf (`{"id":%d}` , item .ID )).Attr ("value" , item .Text )),
246
250
),
247
251
)
248
252
return li
@@ -277,8 +281,8 @@ func (t *TodoList) renderFooterBlock(todos *StoredTodo, page string) (html.Block
277
281
278
282
var clearCompletedButton html.Block
279
283
if someDone {
280
- clearCompletedButton = html .Button (html .Class ("clear-completed ga" ).Attr ("ga-on" , "click" ).Attr ("ga-action" , "TodoList.ClearCompleted" ).
281
- Attr ( "ga-args" , fmt . Sprintf ( `{"page":%q}` , page )), html .Text ("Clear completed" ))
284
+ clearCompletedButton = html .Button (html .Class ("clear-completed ga" ).Attr ("ga-on" , "click" ).Attr ("ga-action" , "TodoList.ClearCompleted" ),
285
+ html .Text ("Clear completed" ))
282
286
}
283
287
284
288
footer := html .Elem ("footer" , html .Class ("footer" ),
@@ -316,7 +320,6 @@ func (t *TodoList) NewTodo(ctx *Context, input *NewTodoArgs) (*Response, error)
316
320
}
317
321
input .Text = strings .TrimSpace (input .Text )
318
322
todos .Items = append (todos .Items , StoredTodoItem {ID : highestID + 1 , Text : input .Text })
319
-
320
323
return t .DB .SetTodo (todos )
321
324
})
322
325
}
0 commit comments