mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-18 14:23:50 +00:00
commit
31e55d2307
@ -1,5 +1,10 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 0.9.18
|
||||||
|
|
||||||
|
* `Common`
|
||||||
|
* New extensions for `Element`: `Element#onActionOutside` and `Element#onClickOutside`
|
||||||
|
|
||||||
## 0.9.17
|
## 0.9.17
|
||||||
|
|
||||||
* `Common`:
|
* `Common`:
|
||||||
|
@ -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<Element>(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)
|
@ -14,5 +14,5 @@ crypto_js_version=4.1.1
|
|||||||
# Project data
|
# Project data
|
||||||
|
|
||||||
group=dev.inmo
|
group=dev.inmo
|
||||||
version=0.9.17
|
version=0.9.18
|
||||||
android_code_version=107
|
android_code_version=108
|
||||||
|
Loading…
Reference in New Issue
Block a user