aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/html/Image.kt
blob: a03d673a0c8acca78882bbc8401045ab88b9baba (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
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.snabbdom.StringBoolPair
import pl.treksoft.kvision.snabbdom.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) {
    var src = src
        set(value) {
            field = value
            refresh()
        }
    var alt = alt
        set(value) {
            field = value
            refresh()
        }
    var responsive = responsive
        set(value) {
            field = value
            refresh()
        }
    var shape = shape
        set(value) {
            field = value
            refresh()
        }
    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)
        if (alt != null) pr.add("alt" to alt.orEmpty())
        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)
        }
        if (shape != null) {
            cl.add(shape?.className.orEmpty() to true)
        }
        return cl
    }
}