added first cut of the DOM events API (its not all compiling to JS yet though due to Class<T> not being supported)

This commit is contained in:
James Strachan
2012-07-24 11:10:50 +01:00
parent a3cbd37e5f
commit b25c27bfed
10 changed files with 249 additions and 21 deletions
@@ -0,0 +1,50 @@
package kotlin.dom
import org.w3c.dom.Document
import org.w3c.dom.Node
import org.w3c.dom.events.*
import java.io.Closeable
fun <T: Event> eventHandler(eventType: Class<T>, handler: (T) -> Unit): EventListener {
return object : EventListener {
public override fun handleEvent(e: Event?) {
if (e != null && eventType.isInstance(e)) {
handler(e as T)
}
}
}
}
/**
* Registers a handler on the named event
*/
public fun Node?.on(name: String, capture: Boolean, handler: (Event) -> Unit): Closeable? {
return on(name, capture, javaClass<Event>(), handler)
}
public fun <T: Event> Node?.on(name: String, capture: Boolean, eventType: Class<T>, handler: (T) -> Unit): Closeable? {
return if (this is EventTarget) {
val target: EventTarget = this
val listener = eventHandler(eventType, handler)
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 {
null
}
}
public fun Node?.onClick(capture: Boolean = false, handler: (MouseEvent) -> Unit): Closeable? {
return on("click", capture, javaClass<MouseEvent>(), handler)
}
public fun Node?.onDoubleClick(capture: Boolean = false, handler: (MouseEvent) -> Unit): Closeable? {
return on("dblclick", capture, javaClass<MouseEvent>(), handler)
}
@@ -0,0 +1,58 @@
package kotlin.dom
import org.w3c.dom.Node
import org.w3c.dom.events.*
// JavaScript style properties for JVM : TODO could auto-generate these
val Event.bubbles: Boolean
get() = getBubbles()
val Event.cancelable: Boolean
get() = getCancelable()
val Event.getCurrentTarget: EventTarget?
get() = getCurrentTarget()
val Event.eventPhase: Short
get() = getEventPhase()
val Event.target: EventTarget?
get() = getTarget()
val Event.timeStamp: Long
get() = getTimeStamp()
// TODO we can't use 'type' as the property name in Kotlin so we should fix it in JS
val Event.eventType: String
get() = getType()!!
val MouseEvent.altKey: Boolean
get() = getAltKey()
val MouseEvent.button: Short
get() = getButton()
val MouseEvent.clientX: Int
get() = getClientX()
val MouseEvent.clientY: Int
get() = getClientY()
val MouseEvent.ctrlKey: Boolean
get() = getCtrlKey()
val MouseEvent.metaKey: Boolean
get() = getMetaKey()
val MouseEvent.relatedTarget: EventTarget?
get() = getRelatedTarget()
val MouseEvent.screenX: Int
get() = getScreenX()
val MouseEvent.screenY: Int
get() = getScreenY()
val MouseEvent.shiftKey: Boolean
get() = getShiftKey()
+2 -1
View File
@@ -1,5 +1,5 @@
/**
* JVM specific API implementations using JAXB and so forth which would not be used when compiling to JS
* JVM specific API implementations using JAXP and so forth which would not be used when compiling to JS
*/
package kotlin.dom
@@ -19,6 +19,7 @@ import javax.xml.transform.stream.StreamResult
import org.w3c.dom.*
import org.xml.sax.InputSource
// JavaScript style properties - TODO could auto-generate these
val Node.nodeName: String
get() = getNodeName() ?: ""