add support of notifications

This commit is contained in:
InsanusMokrassar 2022-02-28 23:55:09 +06:00
parent 7c33efa121
commit 2e28334b41
6 changed files with 115 additions and 2 deletions

View File

@ -2,6 +2,8 @@
## 0.0.39
* Add support of `Notifications`
## 0.0.38
* Add support of `Toggle`

View File

@ -0,0 +1,15 @@
package dev.inmo.jsuikit.elements
import dev.inmo.jsuikit.modifiers.UIKit
import dev.inmo.jsuikit.types.UIKitNotificationParameter
import dev.inmo.jsuikit.types.NotificationsGroup
import dev.inmo.jsuikit.types.invoke
import dev.inmo.jsuikit.utils.Milliseconds
fun Notification(
message: String,
status: UIKitNotificationParameter.Style? = null,
timeout: Milliseconds? = null,
group: NotificationsGroup? = null,
position: UIKitNotificationParameter.Position? = null
) = UIKit.notification.invoke(message, status, timeout, group, position)

View File

@ -1,4 +1,6 @@
package dev.inmo.jsuikit.modifiers
inline val UIKit
get() = js("UIkit")
import dev.inmo.jsuikit.types.UIKit
inline val UIKit: UIKit
get() = js("UIkit").unsafeCast<UIKit>()

View File

@ -0,0 +1,5 @@
package dev.inmo.jsuikit.types
external interface UIKit {
val notification: UIKitNotifications
}

View File

@ -0,0 +1,39 @@
package dev.inmo.jsuikit.types
sealed class UIKitNotificationParameter {
abstract val parameterName: String
abstract val parameterValue: String
sealed class Style(override val parameterValue: String) : UIKitNotificationParameter() {
override val parameterName: String
get() = "status"
object Primary : Style("primary")
object Success : Style("success")
object Warning : Style("warning")
object Danger : Style("danger")
}
sealed class Position(override val parameterValue: String) : UIKitNotificationParameter() {
override val parameterName: String
get() = "pos"
sealed class Top(parameterValue: String) : Position("top-$parameterValue") {
object Left : Top("left")
object Center : Top("center")
object Right : Top("right")
}
sealed class Bottom(parameterValue: String) : Position("bottom-$parameterValue") {
object Left : Bottom("left")
object Center : Bottom("center")
object Right : Bottom("right")
}
}
}

View File

@ -0,0 +1,50 @@
package dev.inmo.jsuikit.types
import dev.inmo.jsuikit.utils.Milliseconds
import org.w3c.dom.Element
import kotlin.js.Json
import kotlin.js.json
typealias NotificationsGroup = String
external interface UIKitNotifications {
fun closeAll(group: NotificationsGroup)
operator fun invoke(message: String, parameters: Json)
operator fun invoke(element: Element): UIKitNotification?
}
external interface UIKitNotification {
fun close(immediate: Boolean)
}
data class UIKitNotificationsParameters(
val status: UIKitNotificationParameter.Style? = null,
val timeout: Milliseconds? = null,
val group: NotificationsGroup? = null,
val position: UIKitNotificationParameter.Position? = null
) {
fun parametersJson() = json(
*listOfNotNull(
status ?.let { it.parameterName to it.parameterValue },
timeout ?.let { "timeout" to timeout.toString() },
group ?.let { "group" to it },
position ?.let { it.parameterName to it.parameterValue },
).toTypedArray()
)
}
operator fun UIKitNotifications.invoke(
message: String,
parameters: UIKitNotificationsParameters
) = invoke(message, parameters.parametersJson())
operator fun UIKitNotifications.invoke(
message: String,
status: UIKitNotificationParameter.Style? = null,
timeout: Milliseconds? = null,
group: NotificationsGroup? = null,
position: UIKitNotificationParameter.Position? = null
) = invoke(message, UIKitNotificationsParameters(status, timeout, group, position))