aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/modal/Confirm.kt
blob: 05550f345a4587d358bb76994095cde46f30146c (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
package pl.treksoft.kvision.modal

import pl.treksoft.kvision.html.ALIGN
import pl.treksoft.kvision.html.BUTTONSTYLE
import pl.treksoft.kvision.html.Button
import pl.treksoft.kvision.html.TAG
import pl.treksoft.kvision.html.Tag

open class Confirm(
    caption: String? = null, text: String? = null, rich: Boolean = false,
    align: ALIGN? = null, size: MODALSIZE? = null, animation: Boolean = true,
    cancelVisible: Boolean = false,
    private val noCallback: (() -> Unit)? = null,
    private val yesCallback: (() -> Unit)? = null
) : Modal(caption, false, size, animation, false) {
    var text
        get() = content.text
        set(value) {
            content.text = value
        }
    var rich
        get() = content.rich
        set(value) {
            content.rich = value
        }
    var align
        get() = content.align
        set(value) {
            content.align = value
        }
    private var cancelVisible = cancelVisible
        set(value) {
            field = value
            refreshCancelButton()
        }

    private val content = Tag(TAG.SPAN, text, rich, align)
    private val cancelButton = Button("Cancel", "remove")

    init {
        body.add(content)
        cancelButton.setEventListener {
            click = {
                hide()
            }
        }
        this.addButton(cancelButton)
        val noButton = Button("No", "ban-circle")
        noButton.setEventListener {
            click = {
                hide()
                noCallback?.invoke()
            }
        }
        this.addButton(noButton)
        val yesButton = Button("Yes", "ok", BUTTONSTYLE.PRIMARY)
        yesButton.setEventListener {
            click = {
                hide()
                yesCallback?.invoke()
            }
        }
        this.addButton(yesButton)
        refreshCancelButton()
    }

    private fun refreshCancelButton() {
        if (cancelVisible) {
            cancelButton.show()
            closeIcon.show()
        } else {
            cancelButton.hide()
            closeIcon.hide()
        }
    }

    companion object {
        @Suppress("LongParameterList")
        fun show(
            caption: String? = null, text: String? = null, rich: Boolean = false,
            align: ALIGN? = null, size: MODALSIZE? = null, animation: Boolean = true,
            cancelVisible: Boolean = false,
            noCallback: (() -> Unit)? = null, yesCallback: (() -> Unit)? = null
        ) {
            Confirm(caption, text, rich, align, size, animation, cancelVisible, noCallback, yesCallback).show()
        }
    }
}