Skip to content

Commit b25c27b

Browse files
committed
added first cut of the DOM events API (its not all compiling to JS yet though due to Class<T> not being supported)
1 parent a3cbd37 commit b25c27b

File tree

10 files changed

+249
-21
lines changed

10 files changed

+249
-21
lines changed

.idea/compiler.xml

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/js.libraries/src/core/dom.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
package org.w3c.dom
33

4+
5+
46
//
57
// NOTE THIS FILE IS AUTO-GENERATED by the GeneratedJavaScriptStubs.kt
68
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib

js/js.libraries/src/core/domEvents.kt

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
package org.w3c.dom.events
3+
4+
import org.w3c.dom.*
5+
import org.w3c.dom.views.*
6+
7+
8+
//
9+
// NOTE THIS FILE IS AUTO-GENERATED by the GeneratedJavaScriptStubs.kt
10+
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
11+
//
12+
13+
import js.noImpl
14+
15+
// Contains stub APIs for the W3C DOM API so we can delegate to the platform DOM instead
16+
17+
18+
native public trait DocumentEvent {
19+
public fun createEvent(arg1: String?): Event = js.noImpl
20+
}
21+
22+
native public trait Event {
23+
public val `type`: String
24+
public val bubbles: Boolean
25+
public val cancelable: Boolean
26+
public val currentTarget: EventTarget
27+
public val eventPhase: Short
28+
public val target: EventTarget
29+
public val timeStamp: Long
30+
public fun stopPropagation(): Unit = js.noImpl
31+
public fun preventDefault(): Unit = js.noImpl
32+
public fun initEvent(arg1: String?, arg2: Boolean, arg3: Boolean): Unit = js.noImpl
33+
34+
public class object {
35+
public val CAPTURING_PHASE: Short = 1
36+
public val AT_TARGET: Short = 2
37+
public val BUBBLING_PHASE: Short = 3
38+
}
39+
}
40+
41+
native public trait EventListener {
42+
public fun handleEvent(arg1: Event): Unit = js.noImpl
43+
}
44+
45+
native public trait EventTarget {
46+
public fun dispatchEvent(arg1: Event): Boolean = js.noImpl
47+
public fun addEventListener(arg1: String?, arg2: EventListener, arg3: Boolean): Unit = js.noImpl
48+
public fun removeEventListener(arg1: String?, arg2: EventListener, arg3: Boolean): Unit = js.noImpl
49+
}
50+
51+
native public trait MouseEvent: UIEvent {
52+
public val altKey: Boolean
53+
public val button: Short
54+
public val clientX: Int
55+
public val clientY: Int
56+
public val ctrlKey: Boolean
57+
public val metaKey: Boolean
58+
public val relatedTarget: EventTarget
59+
public val screenX: Int
60+
public val screenY: Int
61+
public val shiftKey: Boolean
62+
public fun initMouseEvent(arg1: String?, arg2: Boolean, arg3: Boolean, arg4: AbstractView, arg5: Int, arg6: Int, arg7: Int, arg8: Int, arg9: Int, arg10: Boolean, arg11: Boolean, arg12: Boolean, arg13: Boolean, arg14: Short, arg15: EventTarget): Unit = js.noImpl
63+
}
64+
65+
native public trait MutationEvent: Event {
66+
public val attrChange: Short
67+
public val attrName: String
68+
public val newValue: String
69+
public val prevValue: String
70+
public val relatedNode: Node
71+
public fun initMutationEvent(arg1: String?, arg2: Boolean, arg3: Boolean, arg4: Node, arg5: String?, arg6: String?, arg7: String?, arg8: Short): Unit = js.noImpl
72+
73+
public class object {
74+
public val MODIFICATION: Short = 1
75+
public val ADDITION: Short = 2
76+
public val REMOVAL: Short = 3
77+
}
78+
}
79+
80+
native public trait UIEvent: Event {
81+
public val detail: Int
82+
public val view: AbstractView
83+
public fun initUIEvent(arg1: String?, arg2: Boolean, arg3: Boolean, arg4: AbstractView, arg5: Int): Unit = js.noImpl
84+
}
85+

js/js.libraries/src/core/domViews.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.w3c.dom.views
2+
3+
import js.noImpl
4+
5+
native public trait AbstractView {
6+
}
7+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package kotlin.dom
2+
3+
import org.w3c.dom.Document
4+
import org.w3c.dom.Node
5+
import org.w3c.dom.events.*
6+
import java.io.Closeable
7+
8+
9+
fun <T: Event> eventHandler(eventType: Class<T>, handler: (T) -> Unit): EventListener {
10+
return object : EventListener {
11+
public override fun handleEvent(e: Event?) {
12+
if (e != null && eventType.isInstance(e)) {
13+
handler(e as T)
14+
}
15+
}
16+
}
17+
}
18+
19+
/**
20+
* Registers a handler on the named event
21+
*/
22+
public fun Node?.on(name: String, capture: Boolean, handler: (Event) -> Unit): Closeable? {
23+
return on(name, capture, javaClass<Event>(), handler)
24+
}
25+
26+
public fun <T: Event> Node?.on(name: String, capture: Boolean, eventType: Class<T>, handler: (T) -> Unit): Closeable? {
27+
return if (this is EventTarget) {
28+
val target: EventTarget = this
29+
val listener = eventHandler(eventType, handler)
30+
target.addEventListener(name, listener, capture)
31+
object: Closeable {
32+
public override fun close() {
33+
target.removeEventListener(name, listener, capture)
34+
}
35+
36+
public override fun toString(): String? = "CloseableEventListener($target, $name)"
37+
}
38+
} else {
39+
null
40+
}
41+
}
42+
43+
44+
public fun Node?.onClick(capture: Boolean = false, handler: (MouseEvent) -> Unit): Closeable? {
45+
return on("click", capture, javaClass<MouseEvent>(), handler)
46+
}
47+
48+
public fun Node?.onDoubleClick(capture: Boolean = false, handler: (MouseEvent) -> Unit): Closeable? {
49+
return on("dblclick", capture, javaClass<MouseEvent>(), handler)
50+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package kotlin.dom
2+
3+
import org.w3c.dom.Node
4+
import org.w3c.dom.events.*
5+
6+
// JavaScript style properties for JVM : TODO could auto-generate these
7+
val Event.bubbles: Boolean
8+
get() = getBubbles()
9+
10+
val Event.cancelable: Boolean
11+
get() = getCancelable()
12+
13+
val Event.getCurrentTarget: EventTarget?
14+
get() = getCurrentTarget()
15+
16+
val Event.eventPhase: Short
17+
get() = getEventPhase()
18+
19+
val Event.target: EventTarget?
20+
get() = getTarget()
21+
22+
val Event.timeStamp: Long
23+
get() = getTimeStamp()
24+
25+
// TODO we can't use 'type' as the property name in Kotlin so we should fix it in JS
26+
val Event.eventType: String
27+
get() = getType()!!
28+
29+
30+
val MouseEvent.altKey: Boolean
31+
get() = getAltKey()
32+
33+
val MouseEvent.button: Short
34+
get() = getButton()
35+
36+
val MouseEvent.clientX: Int
37+
get() = getClientX()
38+
39+
val MouseEvent.clientY: Int
40+
get() = getClientY()
41+
42+
val MouseEvent.ctrlKey: Boolean
43+
get() = getCtrlKey()
44+
45+
val MouseEvent.metaKey: Boolean
46+
get() = getMetaKey()
47+
48+
val MouseEvent.relatedTarget: EventTarget?
49+
get() = getRelatedTarget()
50+
51+
val MouseEvent.screenX: Int
52+
get() = getScreenX()
53+
54+
val MouseEvent.screenY: Int
55+
get() = getScreenY()
56+
57+
val MouseEvent.shiftKey: Boolean
58+
get() = getShiftKey()

libraries/stdlib/src/kotlin/dom/DomJVM.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* JVM specific API implementations using JAXB and so forth which would not be used when compiling to JS
2+
* JVM specific API implementations using JAXP and so forth which would not be used when compiling to JS
33
*/
44
package kotlin.dom
55

@@ -19,6 +19,7 @@ import javax.xml.transform.stream.StreamResult
1919
import org.w3c.dom.*
2020
import org.xml.sax.InputSource
2121

22+
// JavaScript style properties - TODO could auto-generate these
2223
val Node.nodeName: String
2324
get() = getNodeName() ?: ""
2425

libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateJavaScriptStubs.kt

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,55 @@ import java.lang.reflect.Modifier
88
import java.util.ArrayList
99
import java.util.TreeMap
1010
import org.w3c.dom.*
11+
import org.w3c.dom.events.*
12+
import java.util.List
1113

1214
/**
1315
* This tool generates JavaScript stubs for classes available in the JDK which are already available in the browser environment
1416
* such as the W3C DOM
1517
*/
1618
fun generateDomAPI(file: File): Unit {
19+
val packageName = "org.w3c.dom"
20+
val imports = ""
21+
22+
val classes: List<Class<*>> = arrayList(javaClass<Attr>(), javaClass<CDATASection>(),
23+
javaClass<CharacterData>(), javaClass<Comment>(),
24+
javaClass<Document>(), javaClass<DocumentFragment>(), javaClass<DocumentType>(),
25+
javaClass<DOMConfiguration>(),
26+
javaClass<DOMError>(), javaClass<DOMErrorHandler>(),
27+
javaClass<DOMImplementation>(), javaClass<DOMImplementationList>(),
28+
javaClass<DOMLocator>(),
29+
javaClass<DOMStringList>(),
30+
javaClass<Element>(),
31+
javaClass<Entity>(), javaClass<EntityReference>(),
32+
javaClass<NameList>(), javaClass<NamedNodeMap>(), javaClass<Node>(), javaClass<NodeList>(),
33+
javaClass<Notation>(), javaClass<ProcessingInstruction>(),
34+
javaClass<Text>(), javaClass<TypeInfo>(),
35+
javaClass<UserDataHandler>())
36+
37+
writeClassesFile(file, packageName, imports, classes)
38+
}
39+
40+
fun generateDomEventsAPI(file: File): Unit {
41+
val packageName = "org.w3c.dom.events"
42+
val imports = "import org.w3c.dom.*\nimport org.w3c.dom.views.*\n"
43+
44+
45+
val classes: List<Class<*>> = arrayList(javaClass<DocumentEvent>(), javaClass<Event>(),
46+
javaClass<EventListener>(), javaClass<EventTarget>(),
47+
javaClass<MouseEvent>(), javaClass<MutationEvent>(),
48+
javaClass<UIEvent>())
49+
50+
writeClassesFile(file, packageName, imports, classes)
51+
}
52+
53+
private fun writeClassesFile(file: File, packageName: String, imports: String, classes: List<Class<*>>): Unit {
1754
write(file) {
1855

1956
println("""
20-
package org.w3c.dom
57+
package $packageName
58+
59+
$imports
2160
2261
//
2362
// NOTE THIS FILE IS AUTO-GENERATED by the GeneratedJavaScriptStubs.kt
@@ -30,20 +69,6 @@ import js.noImpl
3069
3170
""")
3271

33-
val classes = arrayList(javaClass<Attr>(), javaClass<CDATASection>(),
34-
javaClass<CharacterData>(), javaClass<Comment>(),
35-
javaClass<Document>(), javaClass<DocumentFragment>(), javaClass<DocumentType>(),
36-
javaClass<DOMConfiguration>(),
37-
javaClass<DOMError>(), javaClass<DOMErrorHandler>(),
38-
javaClass<DOMImplementation>(), javaClass<DOMImplementationList>(),
39-
javaClass<DOMLocator>(),
40-
javaClass<DOMStringList>(),
41-
javaClass<Element>(),
42-
javaClass<Entity>(), javaClass<EntityReference>(),
43-
javaClass<NameList>(), javaClass<NamedNodeMap>(), javaClass<Node>(), javaClass<NodeList>(),
44-
javaClass<Notation>(), javaClass<ProcessingInstruction>(),
45-
javaClass<Text>(), javaClass<TypeInfo>(),
46-
javaClass<UserDataHandler>())
4772

4873
fun simpleTypeName(klass: Class<out Any?>?): String {
4974
val answer = klass?.getSimpleName()?.capitalize() ?: "Unit"

libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateStandardLib.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ fun main(args: Array<String>) {
6969
val jsCoreDir = File(srcDir, "../../../../js/js.libraries/src/core")
7070
require(jsCoreDir.exists())
7171
generateDomAPI(File(jsCoreDir, "dom.kt"))
72+
generateDomEventsAPI(File(jsCoreDir, "domEvents.kt"))
7273

7374
val otherArrayNames = arrayList("Boolean", "Byte", "Char", "Short", "Int", "Long", "Float", "Double")
7475

libraries/tools/kotlin-js-library/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
<fileset dir="${basedir}/../../stdlib/src/kotlin/dom">
4545
<include name="**/*.kt"/>
4646
<exclude name="**/*JVM.kt"/>
47+
48+
<!-- TODO get compiling ASAP when we can use Class<T> in JavaScript -->
49+
<exclude name="**/DomEvents.kt"/>
4750
</fileset>
4851
<fileset dir="${basedir}/../../stdlib/src/kotlin/support">
4952
<include name="**/*.kt"/>

0 commit comments

Comments
 (0)