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
|
package com.example
import com.lightningkite.kotlin.observable.list.observableListOf
import org.w3c.dom.get
import org.w3c.dom.set
import pl.treksoft.kvision.ApplicationBase
import pl.treksoft.kvision.core.Root
import pl.treksoft.kvision.data.DataComponent
import pl.treksoft.kvision.data.DataContainer
import pl.treksoft.kvision.form.FieldLabel
import pl.treksoft.kvision.form.check.CHECKINPUTTYPE
import pl.treksoft.kvision.form.check.CheckInput
import pl.treksoft.kvision.form.text.TextInput
import pl.treksoft.kvision.html.Button
import pl.treksoft.kvision.html.LIST
import pl.treksoft.kvision.html.Link
import pl.treksoft.kvision.html.ListTag
import pl.treksoft.kvision.html.TAG
import pl.treksoft.kvision.html.Tag
import pl.treksoft.kvision.routing.routing
import kotlin.browser.localStorage
const val ENTER_KEY = 13
const val ESCAPE_KEY = 27
class Todo(completed: Boolean, title: String, hidden: Boolean) : DataComponent() {
var completed: Boolean by obs(completed)
var title: String by obs(title)
var hidden: Boolean by obs(hidden)
}
enum class LISTMODE {
ALL,
ACTIVE,
COMPLETED
}
class Todomvc : ApplicationBase() {
private val model = observableListOf<Todo>()
private val checkAllInput = CheckInput(classes = setOf("toggle-all")).apply {
id = "toggle-all"
onClick {
val value = this.value
model.forEach { it.completed = value }
}
}
private val allLink = Link("All", "#!/", classes = setOf("selected"))
private val activeLink = Link("Active", "#!/active")
private val completedLink = Link("Completed", "#!/completed")
private val clearCompletedButton = Button("Clear completed", classes = setOf("clear-completed")).onClick {
model.filter { it.completed }.forEach { model.remove(it) }
}
private val countTag = Tag(TAG.STRONG, "0")
private val itemsLeftTag = Tag(TAG.SPAN, " items left", classes = setOf("todo-count")).apply {
add(countTag)
}
private var mode: LISTMODE = LISTMODE.ALL
private val header = genHeader()
private val main = genMain()
private val footer = genFooter()
override fun start(state: Map<String, Any>) {
val root = Root("todomvc")
val section = Tag(TAG.SECTION, classes = setOf("todoapp"))
section.add(this.header)
section.add(this.main)
section.add(this.footer)
root.add(section)
loadModel()
checkModel()
routing.on("/", { _ -> all() })
.on("/active", { _ -> active() })
.on("/completed", { _ -> completed() })
.resolve()
}
private fun loadModel() {
localStorage.get("todos-kvision")?.let {
JSON.parse<Array<dynamic>>(it).map { Todo(it.completed, it.title, false) }.forEach {
model.add(it)
}
}
}
private fun saveModel() {
val jsonString = model.map {
val array = listOf("title" to it.title, "completed" to it.completed).toTypedArray()
JSON.stringify(kotlin.js.json(*array))
}.toString()
localStorage.set("todos-kvision", jsonString)
}
private fun checkModel() {
val countActive = model.filter { !it.completed }.size
val countCompleted = model.filter { it.completed }.size
this.main.visible = model.isNotEmpty()
this.footer.visible = model.isNotEmpty()
this.countTag.text = countActive.toString()
this.itemsLeftTag.text = when (countActive) {
1 -> " item left"
else -> " items left"
}
this.checkAllInput.value = (countActive == 0)
this.clearCompletedButton.visible = countCompleted > 0
saveModel()
}
private fun all() {
this.mode = LISTMODE.ALL
this.allLink.addCssClass("selected")
this.activeLink.removeCssClass("selected")
this.completedLink.removeCssClass("selected")
this.model.forEach { it.hidden = false }
}
private fun active() {
this.mode = LISTMODE.ACTIVE
this.allLink.removeCssClass("selected")
this.activeLink.addCssClass("selected")
this.completedLink.removeCssClass("selected")
this.model.forEach { it.hidden = it.completed }
}
private fun completed() {
this.mode = LISTMODE.COMPLETED
this.allLink.removeCssClass("selected")
this.activeLink.removeCssClass("selected")
this.completedLink.addCssClass("selected")
this.model.forEach { it.hidden = !it.completed }
}
private fun genHeader(): Tag {
return Tag(TAG.HEADER, classes = setOf("header")).apply {
add(Tag(TAG.H1, "todos"))
add(TextInput(classes = setOf("new-todo")).apply {
placeholder = "What needs to be done?"
autofocus = true
setEventListener<TextInput> {
keydown = { e ->
if (e.keyCode == ENTER_KEY) {
addTodo(self.value)
self.value = null
}
}
}
})
}
}
private fun addTodo(value: String?) {
val v = value?.trim() ?: ""
if (v.isNotEmpty()) {
model.add(Todo(false, v, mode == LISTMODE.COMPLETED))
}
}
private fun editTodo(index: Int, value: String?) {
val v = value?.trim() ?: ""
if (v.isNotEmpty()) {
model[index].title = v
} else {
model.removeAt(index)
}
}
private fun genMain(): Tag {
return Tag(TAG.SECTION, classes = setOf("main")).apply {
add(checkAllInput)
add(FieldLabel("toggle-all", "Mark all as complete"))
add(DataContainer(model, { index ->
val li = Tag(TAG.LI)
li.apply {
if (model[index].completed) addCssClass("completed")
if (model[index].hidden) addCssClass("hidden")
val edit = TextInput(classes = setOf("edit"))
val view = Tag(TAG.DIV, classes = setOf("view")).apply {
add(CheckInput(
CHECKINPUTTYPE.CHECKBOX, model[index].completed, classes = setOf("toggle")
).onClick {
model[index].completed = this.value
model[index].hidden =
mode == LISTMODE.ACTIVE && this.value || mode == LISTMODE.COMPLETED && !this.value
})
add(Tag(TAG.LABEL, model[index].title).apply {
setEventListener<Tag> {
dblclick = {
li.getElementJQuery()?.addClass("editing")
edit.value = model[index].title
edit.getElementJQuery()?.focus()
}
}
})
add(Button("", classes = setOf("destroy")).onClick {
model.removeAt(index)
})
}
edit.setEventListener<TextInput> {
blur = {
if (li.getElementJQuery()?.hasClass("editing") == true) {
li.getElementJQuery()?.removeClass("editing")
editTodo(index, self.value)
}
}
keydown = { e ->
if (e.keyCode == ENTER_KEY) {
li.getElementJQuery()?.removeClass("editing")
editTodo(index, self.value)
}
if (e.keyCode == ESCAPE_KEY) {
li.getElementJQuery()?.removeClass("editing")
}
}
}
add(view)
add(edit)
}
}, Tag(TAG.UL, classes = setOf("todo-list"))).onUpdate {
checkModel()
})
}
}
private fun genFooter(): Tag {
return Tag(TAG.FOOTER, classes = setOf("footer")).apply {
add(itemsLeftTag)
add(ListTag(LIST.UL, classes = setOf("filters")).apply {
add(allLink)
add(activeLink)
add(completedLink)
})
add(clearCompletedButton)
}
}
override fun dispose(): Map<String, Any> {
return mapOf()
}
}
|