aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/window/Window.kt
blob: f2ed2ec8ec76b5e6116374b582ad2511ba16ef38 (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
/*
 * 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.window

import com.github.snabbdom.VNode
import org.w3c.dom.events.Event
import org.w3c.dom.events.MouseEvent
import pl.treksoft.kvision.KVManager
import pl.treksoft.kvision.core.Component
import pl.treksoft.kvision.core.Container
import pl.treksoft.kvision.core.CssSize
import pl.treksoft.kvision.core.Overflow
import pl.treksoft.kvision.core.Position
import pl.treksoft.kvision.core.Resize
import pl.treksoft.kvision.core.UNIT
import pl.treksoft.kvision.html.TAG
import pl.treksoft.kvision.html.Tag
import pl.treksoft.kvision.modal.CloseIcon
import pl.treksoft.kvision.panel.SimplePanel
import pl.treksoft.kvision.utils.px

internal const val DEFAULT_Z_INDEX = 1000
internal const val WINDOW_HEADER_HEIGHT = 40
internal const val WINDOW_CONTENT_MARGIN_BOTTOM = 11

/**
 * Floating window container.
 *
 * @constructor
 * @param caption window title
 * @param contentWidth window content width
 * @param contentHeight window content height
 * @param isResizable determines if the window is resizable
 * @param isDraggable determines if the window is draggable
 * @param closeButton determines if Close button is visible
 * @param classes a set of CSS class names
 * @param init an initializer extension function
 */
open class Window(
    caption: String? = null,
    contentWidth: CssSize? = CssSize(0, UNIT.auto),
    contentHeight: CssSize? = CssSize(0, UNIT.auto),
    isResizable: Boolean = true,
    isDraggable: Boolean = true,
    closeButton: Boolean = false,
    classes: Set<String> = setOf(),
    init: (Window.() -> Unit)? = null
) :
    SimplePanel(classes + setOf("modal-content", "kv-window")) {

    /**
     * Window caption text.
     */
    var caption
        get() = captionTag.text
        set(value) {
            captionTag.text = value
            checkHeaderVisibility()
        }
    /**
     * Window content width.
     */
    var contentWidth
        get() = width
        set(value) {
            width = value
        }
    /**
     * Window content height.
     */
    var contentHeight
        get() = content.height
        set(value) {
            content.height = value
        }
    /**
     * Determines if the window is resizable.
     */
    var isResizable by refreshOnUpdate(isResizable, { checkIsResizable() })
    /**
     * Determines if the window is draggable.
     */
    var isDraggable by refreshOnUpdate(isDraggable, { checkIsDraggable(); })
    /**
     * Determines if Close button is visible.
     */
    var closeButton
        get() = closeIcon.visible
        set(value) {
            closeIcon.visible = value
            checkHeaderVisibility()
        }

    private val header = SimplePanel(setOf("modal-header"))

    /**
     * @suppress
     * Internal property.
     */
    protected val content = SimplePanel().apply {
        this.height = contentHeight
        this.overflow = Overflow.AUTO
    }
    private val closeIcon = CloseIcon()
    private val captionTag = Tag(TAG.H4, caption, classes = setOf("modal-title"))

    private var isResizeEvent = false

    init {
        position = Position.ABSOLUTE
        overflow = Overflow.HIDDEN
        @Suppress("LeakingThis")
        width = contentWidth
        zIndex = DEFAULT_Z_INDEX
        closeIcon.visible = closeButton
        closeIcon.setEventListener {
            click = {
                hide()
            }
        }
        header.add(closeIcon)
        header.add(captionTag)
        checkHeaderVisibility()
        addInternal(header)
        addInternal(content)
        checkIsDraggable()
        if (isResizable) {
            resize = Resize.BOTH
            content.marginBottom = WINDOW_CONTENT_MARGIN_BOTTOM.px
        }
        @Suppress("LeakingThis")
        init?.invoke(this)
    }

    private fun checkHeaderVisibility() {
        if (!closeButton && caption == null && !isDraggable) {
            header.hide()
        } else {
            header.show()
        }
    }

    private fun checkIsDraggable() {
        var isDrag: Boolean
        if (isDraggable) {
            header.setEventListener<SimplePanel> {
                mousedown = { e ->
                    isDrag = true
                    val dragStartX = this@Window.getElementJQuery()?.position()?.left?.toInt() ?: 0
                    val dragStartY = this@Window.getElementJQuery()?.position()?.top?.toInt() ?: 0
                    val dragMouseX = e.pageX
                    val dragMouseY = e.pageY
                    val moveCallback = { me: Event ->
                        if (isDrag) {
                            this@Window.left = (dragStartX + (me as MouseEvent).pageX - dragMouseX).toInt().px
                            this@Window.top = (dragStartY + (me).pageY - dragMouseY).toInt().px
                        }
                    }
                    kotlin.browser.window.addEventListener("mousemove", moveCallback)
                    var upCallback: ((Event) -> Unit)? = null
                    upCallback = { _ ->
                        isDrag = false
                        kotlin.browser.window.removeEventListener("mousemove", moveCallback)
                        kotlin.browser.window.removeEventListener("mouseup", upCallback)
                    }
                    kotlin.browser.window.addEventListener("mouseup", upCallback)
                }
            }
        } else {
            isDrag = false
            header.removeEventListeners()
        }
    }

    private fun checkIsResizable() {
        checkResizablEventHandler()
        if (isResizable) {
            resize = Resize.BOTH
            val intHeight = (getElementJQuery()?.height()?.toInt() ?: 0) + 2
            this.height = (intHeight + WINDOW_CONTENT_MARGIN_BOTTOM - 1).px
            content.marginBottom = WINDOW_CONTENT_MARGIN_BOTTOM.px
        } else {
            resize = Resize.NONE
            val intHeight = (getElementJQuery()?.height()?.toInt() ?: 0) + 2
            this.height = (intHeight - WINDOW_CONTENT_MARGIN_BOTTOM - 1).px
            content.marginBottom = 0.px
        }
    }

    private fun checkResizablEventHandler() {
        if (isResizable) {
            isResizeEvent = true
            KVManager.setResizeEvent(this) {
                val intWidth = (getElementJQuery()?.width()?.toInt() ?: 0) + 2
                val intHeight = (getElementJQuery()?.height()?.toInt() ?: 0) + 2
                width = intWidth.px
                height = intHeight.px
                content.width = (intWidth - 2).px
                content.height = (intHeight - WINDOW_HEADER_HEIGHT - WINDOW_CONTENT_MARGIN_BOTTOM - 1 - 2).px
            }
        } else if (isResizeEvent) {
            KVManager.clearResizeEvent(this)
        }
    }

    override fun add(child: Component): SimplePanel {
        content.add(child)
        return this
    }

    override fun addAll(children: List<Component>): SimplePanel {
        content.addAll(children)
        return this
    }

    override fun remove(child: Component): SimplePanel {
        content.remove(child)
        return this
    }

    override fun removeAll(): SimplePanel {
        content.removeAll()
        return this
    }

    override fun getChildren(): List<Component> {
        return content.getChildren()
    }

    override fun afterCreate(node: VNode) {
        checkResizablEventHandler()
    }

    companion object {
        /**
         * DSL builder extension function.
         *
         * It takes the same parameters as the constructor of the built component.
         */
        fun Container.window(
            caption: String? = null,
            width: CssSize? = CssSize(0, UNIT.auto),
            height: CssSize? = CssSize(0, UNIT.auto),
            resizable: Boolean = true,
            draggable: Boolean = true,
            closeButton: Boolean = false,
            classes: Set<String> = setOf(),
            init: (Window.() -> Unit)? = null
        ): Window {
            val window = Window(caption, width, height, resizable, draggable, closeButton, classes, init)
            this.add(window)
            return window
        }
    }
}