Skip to content

Commit 21a7db7

Browse files
committed
added TouchEvent wrappers, and extensions/ package
1 parent cad0bbb commit 21a7db7

File tree

9 files changed

+165
-0
lines changed

9 files changed

+165
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ scala-js-dom
33

44
Provides a nice statically typed interface to the DOM such that it can be called from Scala code without resorting to `js.Dynamic`. Initially translated using the [scala-js-ts-importer](https://github.com/sjrd/scala-js-ts-importer) from Typescript definition files, but it has since been heavily edited to clean up various rough edges and to make it a more pleasant experience to use from Scala.
55

6+
Also contains useful extension methods in `scala/scalajs/extensions`, which allow the DOM api to be used in a more idiomatic and fluent way.
7+
68
See [scala-js-games](https://github.com/lihaoyi/scala-js-games) for an example of its use. There remain lots more cleanup to do, but this should be a reasonable place to start from. Pull requests/forks are welcome!
79

810
License
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package scala.scalajs.extensions
2+
3+
import scala.scalajs.js
4+
5+
6+
class EasySeq[T](jsLength: js.Number, jsApply: js.Number => T) extends Seq[T]{
7+
def length = jsLength.toInt
8+
def apply(x: Int) = jsApply(x)
9+
def iterator = new Iterator[T]{
10+
var index = 0
11+
def hasNext: scala.Boolean = index < jsLength
12+
def next() = {
13+
val res = jsApply(index)
14+
index += 1
15+
res
16+
}
17+
18+
}
19+
}
20+
21+
22+
object Color{
23+
def rgb(r: Int, g: Int, b: Int) = s"rgb($r, $g, $b)"
24+
val White = rgb(255, 255, 255)
25+
val Red = rgb(255, 0, 0)
26+
val Green = rgb(0, 255, 0)
27+
val Blue = rgb(0, 0, 255)
28+
val Cyan = rgb(0, 255, 255)
29+
val Magenta = rgb(255, 0, 255)
30+
val Yellow = rgb(255, 255, 0)
31+
val Black = rgb(0, 0, 0)
32+
val all = Seq(
33+
White,
34+
Red,
35+
Green,
36+
Blue,
37+
Cyan,
38+
Magenta,
39+
Yellow,
40+
Black
41+
)
42+
}
43+
object KeyCode{
44+
val backspace = 8
45+
val tab = 9
46+
val enter = 13
47+
val shift = 16
48+
val ctrl = 17
49+
val alt = 18
50+
val pause = 19
51+
val capsLock = 20
52+
val escape = 27
53+
val pageUp = 33
54+
val pageDown = 34
55+
val end = 35
56+
val home = 36
57+
val left = 37
58+
val up = 38
59+
val right = 39
60+
val down = 40
61+
val insert = 45
62+
val delete = 46
63+
val num0 = 48
64+
val num1 = 49
65+
val num2 = 50
66+
val num3 = 51
67+
val num4 = 52
68+
val num5 = 53
69+
val num6 = 54
70+
val num7 = 55
71+
val num8 = 56
72+
val num9 = 57
73+
val a = 65
74+
val b = 66
75+
val c = 67
76+
val d = 68
77+
val e = 69
78+
val f = 70
79+
val g = 71
80+
val h = 72
81+
val i = 73
82+
val j = 74
83+
val k = 75
84+
val l = 76
85+
val m = 77
86+
val n = 78
87+
val o = 79
88+
val p = 80
89+
val q = 81
90+
val r = 82
91+
val s = 83
92+
val t = 84
93+
val u = 85
94+
val v = 86
95+
val w = 87
96+
val x = 88
97+
val y = 89
98+
val z = 90
99+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package scala.scalajs
2+
3+
import scala.scalajs.js.CanvasRenderingContext2D
4+
5+
package object extensions {
6+
implicit class PimpedNodeList(nodes: js.NodeList) extends EasySeq[js.Node](nodes.length, nodes.apply)
7+
implicit class PimpedTouchList(nodes: js.TouchList) extends EasySeq[js.TouchEvent](nodes.length, nodes.apply)
8+
implicit class PimpedHtmlCollection(coll: js.HTMLCollection) extends EasySeq[js.Element](coll.length, coll.apply)
9+
implicit class Castable(x: js.Dynamic){
10+
def to[T] = x.asInstanceOf[T]
11+
}
12+
implicit class pimpedContext(val ctx: CanvasRenderingContext2D){
13+
def prepCircle(x: Double, y: Double, r: Double) = {
14+
ctx.beginPath()
15+
ctx.arc(x, y, r, 0, math.Pi * 2)
16+
}
17+
def fillCircle(x: Double, y: Double, r: Double) = {
18+
prepCircle(x, y, r)
19+
ctx.fill()
20+
}
21+
def strokeCircle(x: Double, y: Double, r: Double) = {
22+
prepCircle(x, y, r)
23+
ctx.fill()
24+
}
25+
def prepPath(points: (js.Number, js.Number)*) = {
26+
ctx.beginPath()
27+
ctx.moveTo(points.last._1, points.last._2)
28+
for(p <- points){
29+
ctx.lineTo(p._1, p._2)
30+
}
31+
}
32+
def fillPath(points: (js.Number, js.Number)*) = {
33+
prepPath(points:_*)
34+
ctx.fill()
35+
}
36+
def strokePath(points: (js.Number, js.Number)*) = {
37+
prepPath(points:_*)
38+
ctx.stroke()
39+
}
40+
}
41+
}

src/main/scala/scala/js/lib.scala renamed to src/main/scala/scala/scalajs/js/lib.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,27 @@ object CanvasGradient extends js.Object {
636636
/* ??? ConstructorMember(FunSignature(List(),List(),Some(TypeRef(TypeName(CanvasGradient),List())))) */
637637
}
638638

639+
trait TouchEvent{
640+
val altKey: Boolean = ???
641+
val changedTouches: TouchList = ???
642+
val ctrlKey: Boolean = ???
643+
val metaKey: Boolean = ???
644+
val shiftKey: Boolean = ???
645+
val targetTouches: TouchList = ???
646+
val touches: TouchList = ???
647+
val `type`: String = ???
648+
val target: js.Any = ???
649+
}
650+
651+
trait TouchList extends js.Object {
652+
var length: js.Number = _
653+
def item(index: js.Number): TouchEvent = ???
654+
@scala.scalajs.js.annotation.JSBracketAccess
655+
def apply(index: js.Number): TouchEvent = ???
656+
@scala.scalajs.js.annotation.JSBracketAccess
657+
def update(index: js.Number, v: Node): Unit = ???
658+
}
659+
639660
trait KeyboardEvent extends UIEvent {
640661
var location: js.Number = _
641662
var keyCode: js.Number = _
@@ -2451,6 +2472,8 @@ object Worker extends js.Object {
24512472
}
24522473
package js{
24532474
object JsGlobals extends js.GlobalScope {
2475+
def parseInt(string: js.String, radix: js.Number): js.Number = ???
2476+
def parseFloat(string: js.String, radix: js.Number): js.Number = ???
24542477
var ondragend: js.Function1[DragEvent, js.Any] = _
24552478
var onkeydown: js.Function1[KeyboardEvent, js.Any] = _
24562479
var ondragover: js.Function1[DragEvent, js.Any] = _

0 commit comments

Comments
 (0)