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
|
/*
* Copyright (c) 2017-present Robert Jaros
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package pl.treksoft.kvision.table
import com.github.snabbdom.VNode
import com.github.snabbdom.h
import pl.treksoft.kvision.core.Component
import pl.treksoft.kvision.core.Container
import pl.treksoft.kvision.core.StringBoolPair
import pl.treksoft.kvision.html.TAG
import pl.treksoft.kvision.html.Tag
import pl.treksoft.kvision.panel.SimplePanel
import pl.treksoft.kvision.utils.snClasses
import pl.treksoft.kvision.utils.snOpt
/**
* HTML table types.
*/
enum class TableType(internal val type: String) {
STRIPED("table-striped"),
BORDERED("table-bordered"),
HOVER("table-hover"),
CONDENSED("table-condensed")
}
/**
* HTML table component.
*
* @constructor
* @param headerNames a list of table headers names
* @param types a set of table types
* @param caption table caption
* @param responsive determines if the table is responsive
* @param classes a set of CSS class names
* @param init an initializer extension function
*/
@Suppress("TooManyFunctions")
open class Table(
headerNames: List<String>? = null,
types: Set<TableType> = setOf(), caption: String? = null, responsive: Boolean = false,
classes: Set<String> = setOf(), init: (Table.() -> Unit)? = null
) : SimplePanel(classes + "table") {
/**
* Table headers names.
*/
var headerNames by refreshOnUpdate(headerNames) { refreshHeaders() }
/**
* Table types.
*/
var types by refreshOnUpdate(types)
/**
* Table caption.
*/
var caption by refreshOnUpdate(caption)
/**
* Determines if the table is responsive.
*/
var responsive by refreshOnUpdate(responsive)
private val theadRow = Tag(TAG.TR)
private val thead = Tag(TAG.THEAD).add(theadRow)
private val tbody = Tag(TAG.TBODY)
init {
refreshHeaders()
@Suppress("LeakingThis")
init?.invoke(this)
}
private fun refreshHeaders() {
theadRow.removeAll()
headerNames?.forEach {
theadRow.add(HeaderCell(it))
}
}
/**
* Adds new header cell to the table.
* @param cell header cell
* @return this table
*/
fun addHeaderCell(cell: HeaderCell): Table {
theadRow.add(cell)
return this
}
/**
* Removes given header cell from the table.
* @param cell header cell
* @return this table
*/
fun removeHeaderCell(cell: HeaderCell): Table {
theadRow.remove(cell)
return this
}
/**
* Removes all header cells from table.
* @return this table
*/
fun removeHeaderCells(): Table {
theadRow.removeAll()
return this
}
override fun render(): VNode {
return if (responsive) {
val opt = snOpt {
`class` = snClasses(listOf("table-responsive" to true))
}
h("div", opt, arrayOf(render("table", childrenVNodes())))
} else {
render("table", childrenVNodes())
}
}
override fun childrenVNodes(): Array<VNode> {
val captionElement = caption?.let {
Tag(TAG.CAPTION, it)
}
return listOf(captionElement, thead, tbody).mapNotNull { it?.renderVNode() }.toTypedArray()
}
override fun getSnClass(): List<StringBoolPair> {
val cl = super.getSnClass().toMutableList()
types.forEach {
cl.add(it.type to true)
}
return cl
}
override fun add(child: Component): SimplePanel {
tbody.add(child)
return this
}
override fun addAll(children: List<Component>): SimplePanel {
tbody.addAll(children)
return this
}
override fun remove(child: Component): SimplePanel {
tbody.remove(child)
return this
}
override fun removeAll(): SimplePanel {
tbody.removeAll()
return this
}
override fun getChildren(): List<Component> {
return tbody.getChildren()
}
companion object {
/**
* DSL builder extension function.
*
* It takes the same parameters as the constructor of the built component.
*/
fun Container.table(
headerNames: List<String>? = null,
types: Set<TableType> = setOf(), caption: String? = null, responsive: Boolean = false,
classes: Set<String> = setOf(), init: (Table.() -> Unit)? = null
): Table {
val table =
Table(headerNames, types, caption, responsive, classes, init)
this.add(table)
return table
}
}
}
|