aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/html/Button.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/html/Button.kt')
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/html/Button.kt70
1 files changed, 66 insertions, 4 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Button.kt b/src/main/kotlin/pl/treksoft/kvision/html/Button.kt
index bb48558f..874c4182 100644
--- a/src/main/kotlin/pl/treksoft/kvision/html/Button.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/html/Button.kt
@@ -1,13 +1,37 @@
+/*
+ * 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.html
import com.github.snabbdom.VNode
import org.w3c.dom.events.MouseEvent
import pl.treksoft.kvision.core.ResString
-import pl.treksoft.kvision.core.Widget
import pl.treksoft.kvision.core.StringBoolPair
import pl.treksoft.kvision.core.StringPair
+import pl.treksoft.kvision.core.Widget
-enum class BUTTONSTYLE(val className: String) {
+/**
+ * Button styles.
+ */
+enum class BUTTONSTYLE(internal val className: String) {
DEFAULT("btn-default"),
PRIMARY("btn-primary"),
SUCCESS("btn-success"),
@@ -17,46 +41,81 @@ enum class BUTTONSTYLE(val className: String) {
LINK("btn-link")
}
-enum class BUTTONSIZE(val className: String) {
+/**
+ * Button sizes.
+ */
+enum class BUTTONSIZE(internal val className: String) {
LARGE("btn-lg"),
SMALL("btn-sm"),
XSMALL("btn-xs")
}
+/**
+ * Button component.
+ *
+ * @constructor
+ * @param text button label
+ * @param icon button icon
+ * @param style button style
+ * @param disabled button state
+ * @param classes a set of CSS class names
+ */
open class Button(
text: String, icon: String? = null, style: BUTTONSTYLE = BUTTONSTYLE.DEFAULT,
disabled: Boolean = false, classes: Set<String> = setOf()
) : Widget(classes) {
+
+ /**
+ * Button label.
+ */
var text = text
set(value) {
field = value
refresh()
}
+ /**
+ * Button icon.
+ */
var icon = icon
set(value) {
field = value
refresh()
}
+ /**
+ * Button style.
+ */
var style = style
set(value) {
field = value
refresh()
}
+ /**
+ * Determines if button is disabled.
+ */
var disabled = disabled
set(value) {
field = value
refresh()
}
+ /**
+ * Button image.
+ */
var image: ResString? = null
set(value) {
field = value
refresh()
}
+ /**
+ * Button size.
+ */
var size: BUTTONSIZE? = null
set(value) {
field = value
refresh()
}
+ /**
+ * Determines if the button takes all the space horizontally.
+ */
var block = false
set(value) {
field = value
@@ -65,7 +124,7 @@ open class Button(
override fun render(): VNode {
val t = createLabelWithIcon(text, icon, image)
- return kvh("button", t)
+ return render("button", t)
}
override fun getSnClass(): List<StringBoolPair> {
@@ -88,6 +147,9 @@ open class Button(
return super.getSnAttrs() + ("type" to "button")
}
+ /**
+ * A convenient helper for easy setting onClick event handler.
+ */
open fun onClick(handler: Button.(MouseEvent) -> Unit): Button {
this.setEventListener<Button> {
click = { e ->