aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/html/Image.kt
blob: 38b8825adcddecd03fec4b70d30c65de6fe129a8 (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
package pl.treksoft.kvision.html

import com.github.snabbdom.VNode
import pl.treksoft.kvision.core.ResString
import pl.treksoft.kvision.core.Widget
import pl.treksoft.kvision.core.StringBoolPair
import pl.treksoft.kvision.core.StringPair

enum class IMAGESHAPE(val className: String) {
    ROUNDED("img-rounded"),
    CIRCLE("img-circle"),
    THUMBNAIL("img-thumbnail")
}

open class Image(
    src: ResString, alt: String? = null, responsive: Boolean = false, shape: IMAGESHAPE? = null,
    centered: Boolean = false, classes: Set<String> = setOf()
) : Widget(classes) {
    internal var src = src
        set(value) {
            field = value
            refresh()
        }
    private var alt = alt
        set(value) {
            field = value
            refresh()
        }
    private var responsive = responsive
        set(value) {
            field = value
            refresh()
        }
    private var shape = shape
        set(value) {
            field = value
            refresh()
        }
    private var centered = centered
        set(value) {
            field = value
            refresh()
        }

    override fun render(): VNode {
        return kvh("img")
    }

    override fun getSnAttrs(): List<StringPair> {
        val pr = super.getSnAttrs().toMutableList()
        pr.add("src" to src)
        alt?.let {
            pr.add("alt" to it)
        }
        return pr
    }

    override fun getSnClass(): List<StringBoolPair> {
        val cl = super.getSnClass().toMutableList()
        if (responsive) {
            cl.add("img-responsive" to true)
        }
        if (centered) {
            cl.add("center-block" to true)
        }
        shape?.let {
            cl.add(it.className to true)
        }
        return cl
    }
}