diff options
Diffstat (limited to 'examples/scala-js/examples/helloworld')
6 files changed, 170 insertions, 0 deletions
diff --git a/examples/scala-js/examples/helloworld/HelloWorld.scala b/examples/scala-js/examples/helloworld/HelloWorld.scala new file mode 100644 index 0000000..fd33060 --- /dev/null +++ b/examples/scala-js/examples/helloworld/HelloWorld.scala @@ -0,0 +1,86 @@ +/* Scala.js example code + * Public domain + * @author Sébastien Doeraene + */ + +package helloworld + +import scala.scalajs.js +import js.annotation.JSName + +object HelloWorld extends js.JSApp { + def main() { + if (!(!js.Dynamic.global.document) && + !(!js.Dynamic.global.document.getElementById("playground"))) { + sayHelloFromDOM() + sayHelloFromTypedDOM() + sayHelloFromJQuery() + sayHelloFromTypedJQuery() + } else { + println("Hello world!") + } + } + + def sayHelloFromDOM() { + val document = js.Dynamic.global.document + val playground = document.getElementById("playground") + + val newP = document.createElement("p") + newP.innerHTML = "Hello world! <i>-- DOM</i>" + playground.appendChild(newP) + } + + def sayHelloFromTypedDOM() { + val document = window.document + val playground = document.getElementById("playground") + + val newP = document.createElement("p") + newP.innerHTML = "Hello world! <i>-- typed DOM</i>" + playground.appendChild(newP) + } + + def sayHelloFromJQuery() { + // val $ is fine too, but not very recommended in Scala code + val jQuery = js.Dynamic.global.jQuery + val newP = jQuery("<p>").html("Hello world! <i>-- jQuery</i>") + newP.appendTo(jQuery("#playground")) + } + + def sayHelloFromTypedJQuery() { + val jQuery = helloworld.JQuery + val newP = jQuery("<p>").html("Hello world! <i>-- typed jQuery</i>") + newP.appendTo(jQuery("#playground")) + } +} + +object window extends js.GlobalScope { + val document: DOMDocument = js.native + + def alert(msg: String): Unit = js.native +} + +trait DOMDocument extends js.Object { + def getElementById(id: String): DOMElement = js.native + def createElement(tag: String): DOMElement = js.native +} + +trait DOMElement extends js.Object { + var innerHTML: String = js.native + + def appendChild(child: DOMElement): Unit = js.native +} + +@JSName("jQuery") +object JQuery extends js.Object { + def apply(selector: String): JQuery = js.native +} + +trait JQuery extends js.Object { + def text(value: String): JQuery = js.native + def text(): String = js.native + + def html(value: String): JQuery = js.native + def html(): String = js.native + + def appendTo(parent: JQuery): JQuery = js.native +} diff --git a/examples/scala-js/examples/helloworld/helloworld-2.10-fastopt.html b/examples/scala-js/examples/helloworld/helloworld-2.10-fastopt.html new file mode 100644 index 0000000..98b2705 --- /dev/null +++ b/examples/scala-js/examples/helloworld/helloworld-2.10-fastopt.html @@ -0,0 +1,19 @@ +<!DOCTYPE html> +<html> +<head> + <title>Hello world - Scala.js example</title> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> +</head> +<body> + +<div id="playground"> +</div> + +<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script> + +<script type="text/javascript" src="./target/scala-2.10/helloworld-fastopt.js"></script> + +<script type="text/javascript" src="./target/scala-2.10/helloworld-launcher.js"></script> + +</body> +</html> diff --git a/examples/scala-js/examples/helloworld/helloworld-2.10.html b/examples/scala-js/examples/helloworld/helloworld-2.10.html new file mode 100644 index 0000000..80b00b9 --- /dev/null +++ b/examples/scala-js/examples/helloworld/helloworld-2.10.html @@ -0,0 +1,19 @@ +<!DOCTYPE html> +<html> +<head> + <title>Hello world - Scala.js example</title> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> +</head> +<body> + +<div id="playground"> +</div> + +<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script> + +<script type="text/javascript" src="./target/scala-2.10/helloworld-opt.js"></script> + +<script type="text/javascript" src="./target/scala-2.10/helloworld-launcher.js"></script> + +</body> +</html> diff --git a/examples/scala-js/examples/helloworld/helloworld-2.11-fastopt.html b/examples/scala-js/examples/helloworld/helloworld-2.11-fastopt.html new file mode 100644 index 0000000..dbf5598 --- /dev/null +++ b/examples/scala-js/examples/helloworld/helloworld-2.11-fastopt.html @@ -0,0 +1,19 @@ +<!DOCTYPE html> +<html> +<head> + <title>Hello world - Scala.js example</title> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> +</head> +<body> + +<div id="playground"> +</div> + +<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script> + +<script type="text/javascript" src="./target/scala-2.11/helloworld-fastopt.js"></script> + +<script type="text/javascript" src="./target/scala-2.11/helloworld-launcher.js"></script> + +</body> +</html> diff --git a/examples/scala-js/examples/helloworld/helloworld-2.11.html b/examples/scala-js/examples/helloworld/helloworld-2.11.html new file mode 100644 index 0000000..9c9a3a1 --- /dev/null +++ b/examples/scala-js/examples/helloworld/helloworld-2.11.html @@ -0,0 +1,19 @@ +<!DOCTYPE html> +<html> +<head> + <title>Hello world - Scala.js example</title> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> +</head> +<body> + +<div id="playground"> +</div> + +<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script> + +<script type="text/javascript" src="./target/scala-2.11/helloworld-opt.js"></script> + +<script type="text/javascript" src="./target/scala-2.11/helloworld-launcher.js"></script> + +</body> +</html> diff --git a/examples/scala-js/examples/helloworld/startup.js b/examples/scala-js/examples/helloworld/startup.js new file mode 100644 index 0000000..f45e4cb --- /dev/null +++ b/examples/scala-js/examples/helloworld/startup.js @@ -0,0 +1,8 @@ +/* Scala.js example code + * Public domain + * Author: Sébastien Doeraene + */ + +$(function() { + ScalaJS.modules.helloworld_HelloWorld().main(); +}); |