-
Notifications
You must be signed in to change notification settings - Fork 0
/
pageview.go
348 lines (305 loc) · 7.72 KB
/
pageview.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
package components
import (
"encoding/json"
"fmt"
"strconv"
"syscall/js"
"github.com/octoberswimmer/masc"
"github.com/octoberswimmer/masc/elem"
"github.com/octoberswimmer/masc/event"
"github.com/octoberswimmer/masc/prop"
"github.com/octoberswimmer/masc/style"
)
// PageView is a masc.Component which represents the entire page.
type PageView struct {
masc.Core
Items []*Item `masc:"prop"`
newItemTitle string
// Filter represents the active viewing filter for items.
Filter FilterState
}
// Item represents a single Todo item in the store.
type Item struct {
Title string
Completed bool
editing bool
editTitle string
}
type ItemList []*Item
// FilterState represents a viewing filter for Todo items in the store.
type FilterState int
const (
// All is a FilterState which shows all items.
All FilterState = iota
// Active is a FilterState which shows only non-completed items.
Active
// Completed is a FilterState which shows only completed items.
Completed
)
func (m *PageView) Init() masc.Cmd {
fmt.Println("Initializing PageView")
masc.AddStylesheet("https://rawgit.com/tastejs/todomvc-common/master/base.css")
masc.AddStylesheet("https://rawgit.com/tastejs/todomvc-app-css/master/index.css")
return attachLocalStorage
}
func (m *PageView) Update(msg masc.Msg) (masc.Model, masc.Cmd) {
var (
cmds []masc.Cmd
)
switch msg := msg.(type) {
case ItemList:
m.Items = msg
return m, nil
case AddItemMsg:
m.Items = append(m.Items, &Item{Title: m.newItemTitle})
m.newItemTitle = ""
cmds = append(cmds, m.updateLocalStorage)
case NewItemTitleMsg:
m.newItemTitle = msg.Title
case ClearCompleted:
var activeItems []*Item
for _, item := range m.Items {
if !item.Completed {
activeItems = append(activeItems, item)
}
}
m.Items = activeItems
cmds = append(cmds, m.updateLocalStorage)
case SetAllCompleted:
for _, item := range m.Items {
item.Completed = msg.Completed
}
cmds = append(cmds, m.updateLocalStorage)
case SetFilter:
m.Filter = msg.Filter
case StartEdit:
m.Items[msg.Index].editing = true
m.Items[msg.Index].editTitle = m.Items[msg.Index].Title
case EditInput:
m.Items[msg.Index].editTitle = msg.Title
case StopEdit:
m.Items[msg.Index].editing = false
m.Items[msg.Index].Title = m.Items[msg.Index].editTitle
m.Items[msg.Index].editTitle = ""
cmds = append(cmds, m.updateLocalStorage)
case UpdateCompleted:
m.Items[msg.Index].Completed = msg.Completed
cmds = append(cmds, m.updateLocalStorage)
case Destroy:
m.Items = append(m.Items[:msg.Index], m.Items[msg.Index+1:]...)
cmds = append(cmds, m.updateLocalStorage)
}
return m, masc.Batch(cmds...)
}
func (m *PageView) updateLocalStorage() masc.Msg {
fmt.Printf("Marshalling %+v", m.Items)
data, err := json.Marshal(m.Items)
if err != nil {
fmt.Println("failed to store items: " + err.Error())
}
js.Global().Get("localStorage").Set("items", string(data))
fmt.Println("Updated local storage", string(data))
return nil
}
func attachLocalStorage() masc.Msg {
if data := js.Global().Get("localStorage").Get("items"); !data.IsUndefined() {
fmt.Println("Got items from localStorage", data.String())
var items ItemList
if err := json.Unmarshal([]byte(data.String()), &items); err != nil {
println("failed to load items: " + err.Error())
}
return items
}
return nil
}
func (m *PageView) onNewItemTitleInput(send func(masc.Msg)) func(*masc.Event) {
return func(event *masc.Event) {
send(NewItemTitleMsg{Title: event.Target.Get("value").String()})
}
}
func (m *PageView) onAdd(send func(masc.Msg)) func(*masc.Event) {
return func(event *masc.Event) {
send(AddItemMsg{})
}
}
func (m *PageView) onClearCompleted(send func(masc.Msg)) func(*masc.Event) {
return func(event *masc.Event) {
send(ClearCompleted{})
}
}
func (m *PageView) onToggleAllCompleted(send func(masc.Msg)) func(*masc.Event) {
return func(event *masc.Event) {
send(SetAllCompleted{
Completed: event.Target.Get("checked").Bool(),
})
}
}
// Render implements the masc.Component interface.
func (m *PageView) Render(send func(masc.Msg)) masc.ComponentOrHTML {
return elem.Div(
elem.Section(
masc.Markup(
masc.Class("todoapp"),
),
m.renderHeader(send),
masc.If(len(m.Items) > 0,
m.renderItemList(send),
m.renderFooter(send),
),
),
m.renderInfo(),
)
}
func (m *PageView) renderHeader(send func(masc.Msg)) *masc.HTML {
return elem.Header(
masc.Markup(
masc.Class("header"),
),
elem.Heading1(
masc.Text("todos"),
),
elem.Form(
masc.Markup(
style.Margin(style.Px(0)),
event.Submit(m.onAdd(send)).PreventDefault(),
),
elem.Input(
masc.Markup(
masc.Class("new-todo"),
prop.Placeholder("What needs to be done?"),
prop.Autofocus(true),
prop.Value(m.newItemTitle),
event.Input(m.onNewItemTitleInput(send)),
),
),
),
)
}
// ActiveItemCount returns the current number of items that are not completed.
func (m *PageView) ActiveItemCount() int {
return m.count(false)
}
// CompletedItemCount returns the current number of items that are completed.
func (m *PageView) CompletedItemCount() int {
return m.count(true)
}
func (m *PageView) count(completed bool) int {
count := 0
for _, item := range m.Items {
if item.Completed == completed {
count++
}
}
return count
}
func (m *PageView) renderFooter(send func(masc.Msg)) *masc.HTML {
count := m.ActiveItemCount()
itemsLeftText := " items left"
if count == 1 {
itemsLeftText = " item left"
}
return elem.Footer(
masc.Markup(
masc.Class("footer"),
),
elem.Span(
masc.Markup(
masc.Class("todo-count"),
),
elem.Strong(
masc.Text(strconv.Itoa(count)),
),
masc.Text(itemsLeftText),
),
elem.UnorderedList(
masc.Markup(
masc.Class("filters"),
),
&FilterButton{Label: "All", Filter: All, ActiveFilter: m.Filter == All},
masc.Text(" "),
&FilterButton{Label: "Active", Filter: Active, ActiveFilter: m.Filter == Active},
masc.Text(" "),
&FilterButton{Label: "Completed", Filter: Completed, ActiveFilter: m.Filter == Completed},
),
masc.If(m.CompletedItemCount() > 0,
elem.Button(
masc.Markup(
masc.Class("clear-completed"),
event.Click(m.onClearCompleted(send)),
),
masc.Text("Clear completed ("+strconv.Itoa(m.CompletedItemCount())+")"),
),
),
)
}
func (m *PageView) renderInfo() *masc.HTML {
return elem.Footer(
masc.Markup(
masc.Class("info"),
),
elem.Paragraph(
masc.Text("Double-click to edit a todo"),
),
elem.Paragraph(
masc.Text("Created by "),
elem.Anchor(
masc.Markup(
prop.Href("http://github.com/neelance"),
),
masc.Text("Richard Musiol"),
),
),
elem.Paragraph(
masc.Text("Part of "),
elem.Anchor(
masc.Markup(
prop.Href("http://todomvc.com"),
),
masc.Text("TodoMVC"),
),
),
)
}
func (m *PageView) renderItemList(send func(masc.Msg)) *masc.HTML {
var items masc.List
for i, item := range m.Items {
if (m.Filter == Active && item.Completed) || (m.Filter == Completed && !item.Completed) {
continue
}
iv := &ItemView{Index: i, Title: item.Title, Completed: item.Completed, Editing: item.editing}
if iv.Editing {
iv.EditTitle = item.editTitle
}
items = append(items, iv)
}
return elem.Section(
masc.Markup(
masc.Class("main"),
),
elem.Input(
masc.Markup(
masc.Class("toggle-all"),
prop.ID("toggle-all"),
prop.Type(prop.TypeCheckbox),
prop.Checked(m.CompletedItemCount() == len(m.Items)),
event.Change(m.onToggleAllCompleted(send)),
),
),
elem.Label(
masc.Markup(
prop.For("toggle-all"),
),
masc.Text("Mark all as complete"),
),
elem.UnorderedList(
masc.Markup(
masc.Class("todo-list"),
),
items,
),
)
}
func (m *PageView) Copy() masc.Component {
cpy := *m
return &cpy
}