aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/panel/GridPanel.kt
blob: 1f5efbb4edb9271c05b625e4a2e89daa98b0b395 (plain)
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
/*
 * 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.panel

import pl.treksoft.kvision.core.Component
import pl.treksoft.kvision.core.Container
import pl.treksoft.kvision.core.StringPair
import pl.treksoft.kvision.core.WidgetWrapper

/**
 * CSS grid justification options.
 */
enum class GridJustify(internal val justify: String) {
    START("start"),
    END("end"),
    CENTER("center"),
    STRETCH("stretch")
}

/**
 * CSS grid alignment options.
 */
enum class GridAlign(internal val align: String) {
    START("start"),
    END("end"),
    CENTER("center"),
    STRETCH("stretch")
}

/**
 * CSS grid content justification options.
 */
enum class GridJustifyContent(internal val justifyContent: String) {
    START("start"),
    END("end"),
    CENTER("center"),
    STRETCH("stretch"),
    SPACEAROUND("space-around"),
    SPACEBETWEEN("space-between"),
    SPACEEVENLY("space-evenly")
}

/**
 * CSS grid content alignment options.
 */
enum class GridAlignContent(internal val alignContent: String) {
    START("start"),
    END("end"),
    CENTER("center"),
    STRETCH("stretch"),
    SPACEAROUND("space-around"),
    SPACEBETWEEN("space-between"),
    SPACEEVENLY("space-evenly")
}

/**
 * CSS grid flow options.
 */
enum class GridFlow(internal val flow: String) {
    ROW("row"),
    COLUMN("column"),
    ROWDENSE("row dense"),
    COLUMNDENSE("column dense")
}

/**
 * The container with CSS grid layout support.
 *
 * @constructor
 * @param autoColumns grid auto columns
 * @param autoRows grid auto rows
 * @param autoFlow grid auto flow
 * @param templateColumns grid columns template
 * @param templateRows grid rows template
 * @param templateAreas grid areas template
 * @param columnGap grid column gap
 * @param rowGap grid row gap
 * @param justifyItems grid items justification
 * @param alignItems grid items alignment
 * @param justifyContent flexbox content justification
 * @param alignContent flexbox content alignment
 * @param classes a set of CSS class names
 * @param init an initializer extension function
 */
open class GridPanel(
    autoColumns: String? = null, autoRows: String? = null, autoFlow: GridFlow? = null,
    templateColumns: String? = null, templateRows: String? = null, templateAreas: List<String>? = null,
    columnGap: Int? = null, rowGap: Int? = null, justifyItems: GridJustify? = null,
    alignItems: GridAlign? = null, justifyContent: GridJustifyContent? = null,
    alignContent: GridAlignContent? = null, classes: Set<String> = setOf(), init: (GridPanel.() -> Unit)? = null
) : SimplePanel(classes) {

    /**
     * CSS grid auto columns.
     */
    var autoColumns by refreshOnUpdate(autoColumns)
    /**
     * CSS grid auto rows.
     */
    var autoRows by refreshOnUpdate(autoRows)
    /**
     * CSS grid auto flow.
     */
    var autoFlow by refreshOnUpdate(autoFlow)
    /**
     * CSS grid columns template.
     */
    var templateColumns by refreshOnUpdate(templateColumns)
    /**
     * CSS grid rows template.
     */
    var templateRows by refreshOnUpdate(templateRows)
    /**
     * CSS grid areas template.
     */
    var templateAreas by refreshOnUpdate(templateAreas)
    /**
     * CSS grid column gap.
     */
    var columnGap by refreshOnUpdate(columnGap)
    /**
     * CSS grid row gap.
     */
    var rowGap by refreshOnUpdate(rowGap)
    /**
     * CSS grid items justification.
     */
    var justifyItems by refreshOnUpdate(justifyItems)
    /**
     * CSS grid items alignment.
     */
    var alignItems by refreshOnUpdate(alignItems)
    /**
     * CSS grid content justification.
     */
    var justifyContent by refreshOnUpdate(justifyContent)
    /**
     * CSS grid content alignment.
     */
    var alignContent by refreshOnUpdate(alignContent)

    init {
        @Suppress("LeakingThis")
        init?.invoke(this)
    }

    /**
     * Adds a component to the grid container.
     * @param child child component
     * @param columnStart number of starting column
     * @param rowStart number of starting row
     * @param columnEnd number of ending column
     * @param rowEnd number of ending row
     * @param area grid area
     * @param justifySelf child self justification
     * @param alignSelf child self alignment
     * @param classes a set of CSS class names
     * @return current container
     */
    @Suppress("LongParameterList")
    fun add(
        child: Component, columnStart: Int? = null, rowStart: Int? = null,
        columnEnd: String? = null, rowEnd: String? = null, area: String? = null, justifySelf: GridJustify? = null,
        alignSelf: GridAlign? = null, classes: Set<String> = setOf()
    ): GridPanel {
        addInternal(GridWrapper(child, columnStart, rowStart, columnEnd, rowEnd, area, justifySelf, alignSelf, classes))
        return this
    }

    override fun add(child: Component): GridPanel {
        return add(child, null, null)
    }

    override fun addAll(children: List<Component>): GridPanel {
        children.forEach { add(it, null, null) }
        return this
    }

    override fun remove(child: Component): GridPanel {
        children.find { (it as GridWrapper).wrapped == child }?.let {
            super.remove(it)
            it.dispose()
        }
        return this
    }

    override fun removeAll(): GridPanel {
        children.map {
            it.clearParent()
            it.dispose()
        }
        children.clear()
        refresh()
        return this
    }

    @Suppress("ComplexMethod")
    override fun getSnStyle(): List<StringPair> {
        val snstyle = super.getSnStyle().toMutableList()
        snstyle.add("display" to "grid")
        autoColumns?.let {
            snstyle.add("grid-auto-columns" to it)
        }
        autoRows?.let {
            snstyle.add("grid-auto-rows" to it)
        }
        autoFlow?.let {
            snstyle.add("grid-auto-flow" to it.flow)
        }
        templateColumns?.let {
            snstyle.add("grid-template-columns" to it)
        }
        templateRows?.let {
            snstyle.add("grid-template-rows" to it)
        }
        templateAreas?.let {
            snstyle.add("grid-template-areas" to it.joinToString("\n"))
        }
        columnGap?.let {
            snstyle.add("grid-column-gap" to "${it}px")
        }
        rowGap?.let {
            snstyle.add("grid-row-gap" to "${it}px")
        }
        justifyItems?.let {
            snstyle.add("justify-items" to it.justify)
        }
        alignItems?.let {
            snstyle.add("align-items" to it.align)
        }
        justifyContent?.let {
            snstyle.add("justify-content" to it.justifyContent)
        }
        alignContent?.let {
            snstyle.add("align-content" to it.alignContent)
        }
        return snstyle
    }

    companion object {
        /**
         * DSL builder extension function.
         *
         * It takes the same parameters as the constructor of the built component.
         */
        fun Container.gridPanel(
            autoColumns: String? = null, autoRows: String? = null, autoFlow: GridFlow? = null,
            templateColumns: String? = null, templateRows: String? = null, templateAreas: List<String>? = null,
            columnGap: Int? = null, rowGap: Int? = null, justifyItems: GridJustify? = null,
            alignItems: GridAlign? = null, justifyContent: GridJustifyContent? = null,
            alignContent: GridAlignContent? = null, classes: Set<String> = setOf(), init: (GridPanel.() -> Unit)? = null
        ): GridPanel {
            val gridPanel = GridPanel(
                autoColumns, autoRows, autoFlow, templateColumns, templateRows, templateAreas,
                columnGap, rowGap, justifyItems, alignItems, justifyContent, alignContent, classes, init
            )
            this.add(gridPanel)
            return gridPanel
        }
    }
}

class GridWrapper(
    delegate: Component, private val columnStart: Int? = null, private val rowStart: Int? = null,
    private val columnEnd: String? = null, private val rowEnd: String? = null,
    private val area: String? = null, private val justifySelf: GridJustify? = null,
    private val alignSelf: GridAlign? = null,
    classes: Set<String> = setOf()
) : WidgetWrapper(delegate, classes) {

    override fun getSnStyle(): List<StringPair> {
        val snstyle = super.getSnStyle().toMutableList()
        columnStart?.let {
            snstyle.add("grid-column-start" to "$it")
        }
        rowStart?.let {
            snstyle.add("grid-row-start" to "$it")
        }
        columnEnd?.let {
            snstyle.add("grid-column-end" to it)
        }
        rowEnd?.let {
            snstyle.add("grid-row-end" to it)
        }
        area?.let {
            snstyle.add("grid-area" to it)
        }
        justifySelf?.let {
            snstyle.add("justify-self" to it.justify)
        }
        alignSelf?.let {
            snstyle.add("align-self" to it.align)
        }
        return snstyle
    }

}