diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d4c555406c..025a0fe4719 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 0.9.18 + +* `Common` + * New extensions for `Element`: `Element#onActionOutside` and `Element#onClickOutside` + ## 0.9.17 * `Common`: diff --git a/common/src/jsMain/kotlin/dev/inmo/micro_utils/common/OnClickOutside.kt b/common/src/jsMain/kotlin/dev/inmo/micro_utils/common/OnClickOutside.kt new file mode 100644 index 00000000000..b5b4840f99d --- /dev/null +++ b/common/src/jsMain/kotlin/dev/inmo/micro_utils/common/OnClickOutside.kt @@ -0,0 +1,38 @@ +package dev.inmo.micro_utils.common + +import kotlinx.browser.document +import org.w3c.dom.* +import org.w3c.dom.events.Event +import org.w3c.dom.events.EventListener + +fun Element.onActionOutside(type: String, options: dynamic = null, callback: (Event) -> Unit): EventListener { + lateinit var observer: MutationObserver + val listener = EventListener { + val elementsToCheck = mutableListOf(this@onActionOutside) + while (it.target != this@onActionOutside && elementsToCheck.isNotEmpty()) { + val childrenGettingElement = elementsToCheck.removeFirst() + for (i in 0 until childrenGettingElement.childElementCount) { + elementsToCheck.add(childrenGettingElement.children[i] ?: continue) + } + } + if (elementsToCheck.isEmpty()) { + callback(it) + } + } + if (options == null) { + document.addEventListener(type, listener) + } else { + document.addEventListener(type, listener, options) + } + observer = onRemoved { + if (options == null) { + document.removeEventListener(type, listener) + } else { + document.removeEventListener(type, listener, options) + } + observer.disconnect() + } + return listener +} + +fun Element.onClickOutside(options: dynamic = null, callback: (Event) -> Unit) = onActionOutside("click", options, callback) diff --git a/gradle.properties b/gradle.properties index bc19ff604f3..9991b235ec9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,5 +14,5 @@ crypto_js_version=4.1.1 # Project data group=dev.inmo -version=0.9.17 -android_code_version=107 +version=0.9.18 +android_code_version=108