├── .gitignore ├── README.md ├── build.sbt ├── project ├── build.properties └── plugins.sbt ├── scalajs-tutorial-fastopt.html ├── scalajs-tutorial.html └── src ├── main └── scala │ └── tutorial │ └── webapp │ └── TutorialApp.scala └── test └── scala └── tutorial └── webapp └── TutorialTest.scala /.gitignore: -------------------------------------------------------------------------------- 1 | # sbt target folders 2 | target/ 3 | 4 | # eclipse project files 5 | /.settings 6 | /.classpath 7 | /.project 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # The Scala.js tutorial 2 | 3 | This repository contains the code accompanying the [Scala.js tutorial](http://www.scala-js.org/doc/tutorial.html) 4 | -------------------------------------------------------------------------------- /build.sbt: -------------------------------------------------------------------------------- 1 | enablePlugins(ScalaJSPlugin) 2 | 3 | name := "Scala.js Tutorial" 4 | scalaVersion := "2.13.13" 5 | 6 | // This is an application with a main method 7 | scalaJSUseMainModuleInitializer := true 8 | 9 | libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "2.8.0" 10 | 11 | // Add support for the DOM in `run` and `test` 12 | jsEnv := new org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv() 13 | 14 | // uTest settings 15 | libraryDependencies += "com.lihaoyi" %%% "utest" % "0.8.2" % "test" 16 | testFrameworks += new TestFramework("utest.runner.Framework") 17 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=1.9.9 2 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0") 2 | 3 | libraryDependencies += "org.scala-js" %% "scalajs-env-jsdom-nodejs" % "1.1.0" 4 | -------------------------------------------------------------------------------- /scalajs-tutorial-fastopt.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | The Scala.js Tutorial 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /scalajs-tutorial.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | The Scala.js Tutorial 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/main/scala/tutorial/webapp/TutorialApp.scala: -------------------------------------------------------------------------------- 1 | package tutorial.webapp 2 | 3 | import org.scalajs.dom 4 | import org.scalajs.dom.document 5 | 6 | object TutorialApp { 7 | def main(args: Array[String]): Unit = { 8 | document.addEventListener("DOMContentLoaded", { (e: dom.Event) => 9 | setupUI() 10 | }) 11 | } 12 | 13 | def setupUI(): Unit = { 14 | val button = document.createElement("button") 15 | button.textContent = "Click me!" 16 | button.addEventListener("click", { (e: dom.MouseEvent) => 17 | addClickedMessage() 18 | }) 19 | document.body.appendChild(button) 20 | 21 | appendPar(document.body, "Hello World") 22 | } 23 | 24 | def appendPar(targetNode: dom.Node, text: String): Unit = { 25 | val parNode = document.createElement("p") 26 | parNode.textContent = text 27 | targetNode.appendChild(parNode) 28 | } 29 | 30 | def addClickedMessage(): Unit = { 31 | appendPar(document.body, "You clicked the button!") 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/scala/tutorial/webapp/TutorialTest.scala: -------------------------------------------------------------------------------- 1 | package tutorial.webapp 2 | 3 | import utest._ 4 | 5 | import scala.scalajs.js 6 | 7 | import org.scalajs.dom 8 | import org.scalajs.dom.document 9 | import org.scalajs.dom.ext._ 10 | 11 | object TutorialTest extends TestSuite { 12 | 13 | // Initialize App 14 | TutorialApp.setupUI() 15 | 16 | def tests = Tests { 17 | test("HelloWorld") { 18 | assert(document.querySelectorAll("p").count(_.textContent == "Hello World") == 1) 19 | } 20 | 21 | test("ButtonClick") { 22 | def messageCount = 23 | document.querySelectorAll("p").count(_.textContent == "You clicked the button!") 24 | 25 | val button = document.querySelector("button").asInstanceOf[dom.html.Button] 26 | assert(button != null && button.textContent == "Click me!") 27 | assert(messageCount == 0) 28 | 29 | for (c <- 1 to 5) { 30 | button.click() 31 | assert(messageCount == c) 32 | } 33 | } 34 | } 35 | } 36 | --------------------------------------------------------------------------------