-
Notifications
You must be signed in to change notification settings - Fork 0
/
Views.scala
161 lines (147 loc) · 3.92 KB
/
Views.scala
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
package sjs.diffless.demo
import sjs.diffless.*
import sjs.diffless.imports.{ given, * }
/** all our views in one place */
object Views {
lazy val mainView:View[Model,Action,Handle] =
div(
className := "main",
headerView,
taskListView(_.visible),
footerView
)
lazy val headerView:View[Model,Action,Handle] =
header(
className := "header",
completeView(_.tasks),
createView
)
lazy val completeView:View[Vector[Task],Action,Handle] =
input(
className := "complete",
`type` := "checkbox",
visible ~= (_.nonEmpty),
checked ~= (_.forall(_.data.completed)),
onInput |= { (target, event) => Action.Complete }
)
lazy val createView:View[Model,Action,Handle] =
input(
className := "create",
`type` := "text",
placeholder := "what needs to be done?",
value ~= (_.creating),
onInput |= { (target, event) => Action.Creating(target.value) },
onKeyUp |= { (target, event) => if (event.key == "Enter") Action.Create else Action.Skip },
attach { self =>
Handle.CreateText(
focus = () => self.focus()
)
}
)
lazy val taskListView:View[Vector[Task],Action,Handle] =
ul(
className := "task-list",
taskItemViewEmbed
)
lazy val taskItemViewEmbed:View[Vector[Task],Action,Handle] = {
val keyify:Task=>(String,Task) = it => it.id.value -> it
keyed(taskItemEmbeddedView)(_.map(keyify))
}
lazy val taskItemEmbeddedView:View[Task,Action,Handle] =
taskItemView
.contextual (
actionFunc = Action.Task.apply,
handleFunc = Handle.Task.apply
)
.adaptModel { task =>
task.id -> task.data
}
lazy val taskItemView:View[TaskData,TaskAction,TaskHandle] =
li(
className := "task-item",
onDblClick |= { (target, model) => TaskAction.Edit },
input(
className := "task-item-active",
`type` := "checkbox",
visible ~= (!_.editing),
checked ~= (_.completed),
onInput |= { (target, event) => TaskAction.Toggle }
),
div(
classSet ~= { task =>
if (task.completed) Set("task-item-text", "task-item-text-completed")
else Set("task-item-text")
},
displayed ~= (!_.editing),
text(_.text)
),
button(
className := "task-item-remove",
displayed ~= (!_.editing),
onClick |= { (target, event) => TaskAction.Remove },
literal("🗙")
),
input(
className := "task-item-editor",
`type` := "text",
displayed ~= (_.editing),
value ~= (_.preview),
onInput |= { (target, event) => TaskAction.Change(target.value) },
onBlur |= { (target, event) => TaskAction.Commit },
onKeyUp |= { (target, event) =>
if (event.key == "Enter") TaskAction.Commit
else if (event.key == "Escape") TaskAction.Rollback
else TaskAction.Skip
},
attach { self =>
TaskHandle.Editor(
() => self.focus()
)
}
)
)
lazy val footerView:View[Model,Action,Nothing] =
footer(
className := "footer",
displayed ~= (_.tasks.nonEmpty),
countView(_.incompleteCount),
filterListView(_.filter),
clearView(_.hasCompleted)
)
lazy val countView:View[Int,Action,Nothing] =
div(
className := "count",
strong(
text(_.toString)
),
literal(" "),
text(it =>
if (it == 1) "item" else "items"
),
literal(" left")
)
lazy val filterListView:View[Option[Boolean],Action,Nothing] =
div(
className := "filter-list",
filterItemView("all", None),
filterItemView("active", Some(false)),
filterItemView("completed", Some(true))
)
def filterItemView(labelText:String, state:Option[Boolean]):View[Option[Boolean],Action,Nothing] =
label(
className := "filter-item",
input(
`type` := "radio",
checked ~= (_ == state),
onInput |= { (target, event) => Action.Filter(state) }
),
literal(labelText)
)
lazy val clearView:View[Boolean,Action,Nothing] =
button(
className := "clear",
visible ~= identity,
onClick |= { (target, event) => Action.Clear },
literal("clear completed")
)
}