added the dom events API to the kotlin standard library and get it compiling as JS too (not unit tested yet mind you ;)
This commit is contained in:
@@ -38,12 +38,8 @@ native public trait Event {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
native public trait EventListener {
|
|
||||||
public fun handleEvent(arg1: Event): Unit = js.noImpl
|
|
||||||
}
|
|
||||||
|
|
||||||
native public trait EventTarget {
|
native public trait EventTarget {
|
||||||
public fun dispatchEvent(arg1: Event): Boolean = js.noImpl
|
public fun dispatchEvent(arg1: Event?): Boolean = js.noImpl
|
||||||
public fun addEventListener(arg1: String?, arg2: EventListener, arg3: Boolean): Unit = js.noImpl
|
public fun addEventListener(arg1: String?, arg2: EventListener, arg3: Boolean): Unit = js.noImpl
|
||||||
public fun removeEventListener(arg1: String?, arg2: EventListener, arg3: Boolean): Unit = js.noImpl
|
public fun removeEventListener(arg1: String?, arg2: EventListener, arg3: Boolean): Unit = js.noImpl
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package org.w3c.dom.events
|
||||||
|
|
||||||
|
import org.w3c.dom.*
|
||||||
|
import org.w3c.dom.views.*
|
||||||
|
|
||||||
|
/*
|
||||||
|
TODO we should maybe update GeneratedJavaScriptStubs.kt to auto-create this file
|
||||||
|
too so that we can have the implementation code generated for JS
|
||||||
|
|
||||||
|
See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
|
||||||
|
*/
|
||||||
|
|
||||||
|
public trait EventListener {
|
||||||
|
public fun handleEvent(arg1: Event?): Unit = js.noImpl
|
||||||
|
}
|
||||||
@@ -1,17 +1,33 @@
|
|||||||
package kotlin.dom
|
package kotlin.dom
|
||||||
|
|
||||||
import org.w3c.dom.Document
|
import java.io.Closeable
|
||||||
import org.w3c.dom.Node
|
import org.w3c.dom.Node
|
||||||
import org.w3c.dom.events.*
|
import org.w3c.dom.events.*
|
||||||
import java.io.Closeable
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turns an event handler function into an [EventListener]
|
||||||
|
*/
|
||||||
|
fun eventHandler(handler: (Event) -> Unit): EventListener {
|
||||||
|
return EventListenerHandler(handler)
|
||||||
|
}
|
||||||
|
|
||||||
fun <T: Event> eventHandler(eventType: Class<T>, handler: (T) -> Unit): EventListener {
|
private class EventListenerHandler(val handler: (Event) -> Unit): EventListener {
|
||||||
return object : EventListener {
|
public override fun handleEvent(e: Event?) {
|
||||||
public override fun handleEvent(e: Event?) {
|
if (e != null) {
|
||||||
if (e != null && eventType.isInstance(e)) {
|
handler(e)
|
||||||
handler(e as T)
|
}
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
TODO: needs KT-2507 fixed
|
||||||
|
|
||||||
|
public override fun toString(): String? = "EventListenerHandler($handler)"
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
fun mouseEventHandler(handler: (MouseEvent) -> Unit): EventListener {
|
||||||
|
return eventHandler { e ->
|
||||||
|
if (e is MouseEvent) {
|
||||||
|
handler(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,31 +36,37 @@ fun <T: Event> eventHandler(eventType: Class<T>, handler: (T) -> Unit): EventLis
|
|||||||
* Registers a handler on the named event
|
* Registers a handler on the named event
|
||||||
*/
|
*/
|
||||||
public fun Node?.on(name: String, capture: Boolean, handler: (Event) -> Unit): Closeable? {
|
public fun Node?.on(name: String, capture: Boolean, handler: (Event) -> Unit): Closeable? {
|
||||||
return on(name, capture, javaClass<Event>(), handler)
|
return on(name, capture, eventHandler(handler))
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun <T: Event> Node?.on(name: String, capture: Boolean, eventType: Class<T>, handler: (T) -> Unit): Closeable? {
|
/**
|
||||||
|
* Registers an [EventListener] on the named event
|
||||||
|
*/
|
||||||
|
public fun Node?.on(name: String, capture: Boolean, listener: EventListener): Closeable? {
|
||||||
return if (this is EventTarget) {
|
return if (this is EventTarget) {
|
||||||
val target: EventTarget = this
|
addEventListener(name, listener, capture)
|
||||||
val listener = eventHandler(eventType, handler)
|
CloseableEventListener(this, listener, name, capture)
|
||||||
target.addEventListener(name, listener, capture)
|
|
||||||
object: Closeable {
|
|
||||||
public override fun close() {
|
|
||||||
target.removeEventListener(name, listener, capture)
|
|
||||||
}
|
|
||||||
|
|
||||||
public override fun toString(): String? = "CloseableEventListener($target, $name)"
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class CloseableEventListener(val target: EventTarget, val listener: EventListener, val name: String, val capture: Boolean): Closeable {
|
||||||
|
public override fun close() {
|
||||||
|
target.removeEventListener(name, listener, capture)
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
TODO: needs KT-2507 fixed
|
||||||
|
|
||||||
|
public override fun toString(): String? = "CloseableEventListener($target, $name)"
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
public fun Node?.onClick(capture: Boolean = false, handler: (MouseEvent) -> Unit): Closeable? {
|
public fun Node?.onClick(capture: Boolean = false, handler: (MouseEvent) -> Unit): Closeable? {
|
||||||
return on("click", capture, javaClass<MouseEvent>(), handler)
|
return on("click", capture, mouseEventHandler(handler))
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun Node?.onDoubleClick(capture: Boolean = false, handler: (MouseEvent) -> Unit): Closeable? {
|
public fun Node?.onDoubleClick(capture: Boolean = false, handler: (MouseEvent) -> Unit): Closeable? {
|
||||||
return on("dblclick", capture, javaClass<MouseEvent>(), handler)
|
return on("dblclick", capture, mouseEventHandler(handler))
|
||||||
}
|
}
|
||||||
@@ -43,7 +43,11 @@ fun generateDomEventsAPI(file: File): Unit {
|
|||||||
|
|
||||||
|
|
||||||
val classes: List<Class<*>> = arrayList(javaClass<DocumentEvent>(), javaClass<Event>(),
|
val classes: List<Class<*>> = arrayList(javaClass<DocumentEvent>(), javaClass<Event>(),
|
||||||
javaClass<EventListener>(), javaClass<EventTarget>(),
|
// TODO see domEventsCode.kt we manually hand craft this for now
|
||||||
|
// to get the implementation in JS
|
||||||
|
//
|
||||||
|
// javaClass<EventListener>(),
|
||||||
|
javaClass<EventTarget>(),
|
||||||
javaClass<MouseEvent>(), javaClass<MutationEvent>(),
|
javaClass<MouseEvent>(), javaClass<MutationEvent>(),
|
||||||
javaClass<UIEvent>())
|
javaClass<UIEvent>())
|
||||||
|
|
||||||
@@ -77,7 +81,7 @@ import js.noImpl
|
|||||||
|
|
||||||
fun parameterTypeName(klass: Class<out Any?>?): String {
|
fun parameterTypeName(klass: Class<out Any?>?): String {
|
||||||
val answer = simpleTypeName(klass)
|
val answer = simpleTypeName(klass)
|
||||||
return if (answer == "String" || answer.endsWith("DocumentType")) {
|
return if (answer == "String" || answer == "Event" || answer.endsWith("DocumentType")) {
|
||||||
answer + "?"
|
answer + "?"
|
||||||
} else answer
|
} else answer
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,9 +44,6 @@
|
|||||||
<fileset dir="${basedir}/../../stdlib/src/kotlin/dom">
|
<fileset dir="${basedir}/../../stdlib/src/kotlin/dom">
|
||||||
<include name="**/*.kt"/>
|
<include name="**/*.kt"/>
|
||||||
<exclude name="**/*JVM.kt"/>
|
<exclude name="**/*JVM.kt"/>
|
||||||
|
|
||||||
<!-- TODO get compiling ASAP when we can use Class<T> in JavaScript -->
|
|
||||||
<exclude name="**/DomEvents.kt"/>
|
|
||||||
</fileset>
|
</fileset>
|
||||||
<fileset dir="${basedir}/../../stdlib/src/kotlin/support">
|
<fileset dir="${basedir}/../../stdlib/src/kotlin/support">
|
||||||
<include name="**/*.kt"/>
|
<include name="**/*.kt"/>
|
||||||
|
|||||||
Reference in New Issue
Block a user