summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorLi Haoyi <haoyi@dropbox.com>2014-11-09 17:10:11 -0800
committerLi Haoyi <haoyi@dropbox.com>2014-11-09 17:10:11 -0800
commit52306f10ce3d1e462b171688de04b37174c0b74a (patch)
tree82dcc4d155f9701174528f6857b9de349b2b6bc9 /examples
parent978a138c02c07822ef71f31f71e552a9659a0a53 (diff)
downloadhands-on-scala-js-52306f10ce3d1e462b171688de04b37174c0b74a.tar.gz
hands-on-scala-js-52306f10ce3d1e462b171688de04b37174c0b74a.tar.bz2
hands-on-scala-js-52306f10ce3d1e462b171688de04b37174c0b74a.zip
update
Diffstat (limited to 'examples')
-rw-r--r--examples/crossBuilds/clientserver/build.sbt25
-rw-r--r--examples/crossBuilds/clientserver/js/shared/main/scala/simple/Simple.scala7
-rw-r--r--examples/crossBuilds/clientserver/js/shared/test/scala/simple/SimpleTest.scala13
-rw-r--r--examples/crossBuilds/clientserver/js/src/main/scala/simple/Platform.scala31
-rw-r--r--examples/crossBuilds/clientserver/jvm/shared/main/scala/simple/Simple.scala7
-rw-r--r--examples/crossBuilds/clientserver/jvm/shared/test/scala/simple/SimpleTest.scala13
-rw-r--r--examples/crossBuilds/clientserver/jvm/src/main/scala/simple/Platform.scala59
-rw-r--r--examples/crossBuilds/clientserver/project/build.sbt4
-rw-r--r--examples/crossBuilds/clientserver2/build.sbt28
-rw-r--r--examples/crossBuilds/clientserver2/js/shared/main/scala/simple/Simple.scala7
-rw-r--r--examples/crossBuilds/clientserver2/js/shared/test/scala/simple/SimpleTest.scala13
-rw-r--r--examples/crossBuilds/clientserver2/js/src/main/scala/simple/Platform.scala34
-rw-r--r--examples/crossBuilds/clientserver2/jvm/shared/main/scala/simple/Simple.scala7
-rw-r--r--examples/crossBuilds/clientserver2/jvm/shared/test/scala/simple/SimpleTest.scala13
-rw-r--r--examples/crossBuilds/clientserver2/jvm/src/main/scala/simple/Platform.scala61
-rw-r--r--examples/crossBuilds/clientserver2/project/build.sbt4
16 files changed, 326 insertions, 0 deletions
diff --git a/examples/crossBuilds/clientserver/build.sbt b/examples/crossBuilds/clientserver/build.sbt
new file mode 100644
index 0000000..434fb42
--- /dev/null
+++ b/examples/crossBuilds/clientserver/build.sbt
@@ -0,0 +1,25 @@
+import utest.jsrunner.JsCrossBuild
+import scala.scalajs.sbtplugin.ScalaJSPlugin._
+import ScalaJSKeys._
+
+val cross = new JsCrossBuild(
+ // Shared settings here
+)
+
+lazy val js = cross.js.settings(
+ libraryDependencies ++= Seq(
+ "org.scala-lang.modules.scalajs" %%% "scalajs-dom" % "0.6"
+ )
+)
+
+lazy val jvm = cross.jvm.settings(
+ libraryDependencies ++= Seq(
+ "io.spray" %% "spray-can" % "1.3.2",
+ "io.spray" %% "spray-routing" % "1.3.2",
+ "com.typesafe.akka" %% "akka-actor" % "2.3.6"
+ ),
+ (resources in Compile) += {
+ (fastOptJS in (js, Compile)).value
+ (artifactPath in (js, Compile, fastOptJS)).value
+ }
+)
diff --git a/examples/crossBuilds/clientserver/js/shared/main/scala/simple/Simple.scala b/examples/crossBuilds/clientserver/js/shared/main/scala/simple/Simple.scala
new file mode 100644
index 0000000..d3b0278
--- /dev/null
+++ b/examples/crossBuilds/clientserver/js/shared/main/scala/simple/Simple.scala
@@ -0,0 +1,7 @@
+/*shared/main/scala/simple/Simple.scala*/
+package simple
+object Simple{
+ def formatTimes(timestamps: Seq[Long]): String = {
+ timestamps.map(Platform.format).mkString("\n")
+ }
+} \ No newline at end of file
diff --git a/examples/crossBuilds/clientserver/js/shared/test/scala/simple/SimpleTest.scala b/examples/crossBuilds/clientserver/js/shared/test/scala/simple/SimpleTest.scala
new file mode 100644
index 0000000..ec6b29f
--- /dev/null
+++ b/examples/crossBuilds/clientserver/js/shared/test/scala/simple/SimpleTest.scala
@@ -0,0 +1,13 @@
+/*js/shared/test/scala/simple/SimpleTest.scala*/
+/*jvm/shared/test/scala/simple/SimpleTest.scala*/
+package simple
+import utest._
+object SimpleTest extends TestSuite{
+ val tests = TestSuite{
+ 'format{
+ 'nil - assert(Simple.formatTimes(Nil) == "")
+ 'timeZero - assert(
+ Simple.formatTimes(Seq(0)) == "December 31, 1969 4:00:00 PM PST")
+ }
+ }
+} \ No newline at end of file
diff --git a/examples/crossBuilds/clientserver/js/src/main/scala/simple/Platform.scala b/examples/crossBuilds/clientserver/js/src/main/scala/simple/Platform.scala
new file mode 100644
index 0000000..c82a112
--- /dev/null
+++ b/examples/crossBuilds/clientserver/js/src/main/scala/simple/Platform.scala
@@ -0,0 +1,31 @@
+//js/src/main/scala/simple/Platform.scala
+package simple
+
+import org.scalajs.dom.XMLHttpRequest
+
+import scala.scalajs.js
+import org.scalajs.dom
+
+import scala.scalajs.js.annotation.JSExport
+
+@JSExport
+object Platform extends{
+ def format(ts: Long) = {
+ new js.Date(ts).toLocaleString()
+ }
+ @JSExport
+ def main(container: dom.HTMLDivElement) = {
+ container.innerHTML +=
+ "<h1>Hello from Scala.js!</h1>" +
+ s"<p>${Simple.formatTimes(Seq(0, 1 << 30))}</p>"
+
+ val xhr = new XMLHttpRequest()
+ xhr.open("POST", "/formatDates")
+ xhr.onload = (e: dom.Event) => {
+ container.innerHTML +=
+ "<h1>Hello from Ajax!</h1>" +
+ s"<p>${xhr.responseText}</p>"
+ }
+ xhr.send(Seq(0, 1 << 30).mkString(","))
+ }
+} \ No newline at end of file
diff --git a/examples/crossBuilds/clientserver/jvm/shared/main/scala/simple/Simple.scala b/examples/crossBuilds/clientserver/jvm/shared/main/scala/simple/Simple.scala
new file mode 100644
index 0000000..d3b0278
--- /dev/null
+++ b/examples/crossBuilds/clientserver/jvm/shared/main/scala/simple/Simple.scala
@@ -0,0 +1,7 @@
+/*shared/main/scala/simple/Simple.scala*/
+package simple
+object Simple{
+ def formatTimes(timestamps: Seq[Long]): String = {
+ timestamps.map(Platform.format).mkString("\n")
+ }
+} \ No newline at end of file
diff --git a/examples/crossBuilds/clientserver/jvm/shared/test/scala/simple/SimpleTest.scala b/examples/crossBuilds/clientserver/jvm/shared/test/scala/simple/SimpleTest.scala
new file mode 100644
index 0000000..ec6b29f
--- /dev/null
+++ b/examples/crossBuilds/clientserver/jvm/shared/test/scala/simple/SimpleTest.scala
@@ -0,0 +1,13 @@
+/*js/shared/test/scala/simple/SimpleTest.scala*/
+/*jvm/shared/test/scala/simple/SimpleTest.scala*/
+package simple
+import utest._
+object SimpleTest extends TestSuite{
+ val tests = TestSuite{
+ 'format{
+ 'nil - assert(Simple.formatTimes(Nil) == "")
+ 'timeZero - assert(
+ Simple.formatTimes(Seq(0)) == "December 31, 1969 4:00:00 PM PST")
+ }
+ }
+} \ No newline at end of file
diff --git a/examples/crossBuilds/clientserver/jvm/src/main/scala/simple/Platform.scala b/examples/crossBuilds/clientserver/jvm/src/main/scala/simple/Platform.scala
new file mode 100644
index 0000000..61fdc58
--- /dev/null
+++ b/examples/crossBuilds/clientserver/jvm/src/main/scala/simple/Platform.scala
@@ -0,0 +1,59 @@
+//jvm/src/main/scala/simple/Platform.scala
+package simple
+
+import java.text.SimpleDateFormat
+
+import akka.actor.ActorSystem
+import spray.http.{HttpEntity, MediaTypes}
+import spray.routing.SimpleRoutingApp
+
+object Platform extends SimpleRoutingApp{
+ def format(ts: Long) = {
+ val fmt =
+ "MMMM d, yyyy h:mm:ss aaa z"
+ new SimpleDateFormat(fmt).format(
+ new java.util.Date(ts)
+ )
+ }
+
+ def main(args: Array[String]): Unit = {
+ implicit val system = ActorSystem()
+ startServer("localhost", port = 8080){
+ get{
+ pathSingleSlash{
+ complete{
+ val msg = Simple.formatTimes(Seq(0, 1 << 30))
+ val boot =
+ "Platform().main(document.getElementById('contents'))"
+ HttpEntity(
+ MediaTypes.`text/html`,
+ s"""
+ <html>
+ <head>
+ <script src="/js-fastopt.js"></script>
+ </head>
+ <body onload="$boot">
+ <div id="contents">
+ <h1>Hello from Scala-JVM!</h1>
+ <p>$msg</p>
+ </div>
+ </body>
+ </html>
+ """
+ )
+ }
+ } ~
+ getFromResourceDirectory("")
+ } ~
+ post{
+ path("formatDates"){
+ extract(_.request.entity.asString) { e =>
+ complete {
+ Simple.formatTimes(e.split(",").map(_.toLong))
+ }
+ }
+ }
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/examples/crossBuilds/clientserver/project/build.sbt b/examples/crossBuilds/clientserver/project/build.sbt
new file mode 100644
index 0000000..5bd83ce
--- /dev/null
+++ b/examples/crossBuilds/clientserver/project/build.sbt
@@ -0,0 +1,4 @@
+/*project/build.sbt*/
+addSbtPlugin("org.scala-lang.modules.scalajs" % "scalajs-sbt-plugin" % "0.5.5")
+
+addSbtPlugin("com.lihaoyi" % "utest-js-plugin" % "0.2.4") \ No newline at end of file
diff --git a/examples/crossBuilds/clientserver2/build.sbt b/examples/crossBuilds/clientserver2/build.sbt
new file mode 100644
index 0000000..7e5bddb
--- /dev/null
+++ b/examples/crossBuilds/clientserver2/build.sbt
@@ -0,0 +1,28 @@
+import utest.jsrunner.JsCrossBuild
+import scala.scalajs.sbtplugin.ScalaJSPlugin._
+import ScalaJSKeys._
+
+val cross = new JsCrossBuild(
+ libraryDependencies ++= Seq(
+ "com.scalatags" %%% "scalatags" % "0.4.2",
+ "com.lihaoyi" %%% "upickle" % "0.2.5"
+ )
+)
+
+lazy val js = cross.js.settings(
+ libraryDependencies ++= Seq(
+ "org.scala-lang.modules.scalajs" %%% "scalajs-dom" % "0.6"
+ )
+)
+
+lazy val jvm = cross.jvm.settings(
+ libraryDependencies ++= Seq(
+ "io.spray" %% "spray-can" % "1.3.2",
+ "io.spray" %% "spray-routing" % "1.3.2",
+ "com.typesafe.akka" %% "akka-actor" % "2.3.6"
+ ),
+ (resources in Compile) += {
+ (fastOptJS in (js, Compile)).value
+ (artifactPath in (js, Compile, fastOptJS)).value
+ }
+)
diff --git a/examples/crossBuilds/clientserver2/js/shared/main/scala/simple/Simple.scala b/examples/crossBuilds/clientserver2/js/shared/main/scala/simple/Simple.scala
new file mode 100644
index 0000000..d3b0278
--- /dev/null
+++ b/examples/crossBuilds/clientserver2/js/shared/main/scala/simple/Simple.scala
@@ -0,0 +1,7 @@
+/*shared/main/scala/simple/Simple.scala*/
+package simple
+object Simple{
+ def formatTimes(timestamps: Seq[Long]): String = {
+ timestamps.map(Platform.format).mkString("\n")
+ }
+} \ No newline at end of file
diff --git a/examples/crossBuilds/clientserver2/js/shared/test/scala/simple/SimpleTest.scala b/examples/crossBuilds/clientserver2/js/shared/test/scala/simple/SimpleTest.scala
new file mode 100644
index 0000000..ec6b29f
--- /dev/null
+++ b/examples/crossBuilds/clientserver2/js/shared/test/scala/simple/SimpleTest.scala
@@ -0,0 +1,13 @@
+/*js/shared/test/scala/simple/SimpleTest.scala*/
+/*jvm/shared/test/scala/simple/SimpleTest.scala*/
+package simple
+import utest._
+object SimpleTest extends TestSuite{
+ val tests = TestSuite{
+ 'format{
+ 'nil - assert(Simple.formatTimes(Nil) == "")
+ 'timeZero - assert(
+ Simple.formatTimes(Seq(0)) == "December 31, 1969 4:00:00 PM PST")
+ }
+ }
+} \ No newline at end of file
diff --git a/examples/crossBuilds/clientserver2/js/src/main/scala/simple/Platform.scala b/examples/crossBuilds/clientserver2/js/src/main/scala/simple/Platform.scala
new file mode 100644
index 0000000..906329e
--- /dev/null
+++ b/examples/crossBuilds/clientserver2/js/src/main/scala/simple/Platform.scala
@@ -0,0 +1,34 @@
+//js/src/main/scala/simple/Platform.scala
+package simple
+
+import scala.scalajs.js
+import org.scalajs.dom
+import dom.extensions.Ajax
+import scala.scalajs.concurrent.JSExecutionContext.Implicits.runNow
+import scala.scalajs.js.annotation.JSExport
+import scalatags.JsDom.all._
+@JSExport
+object Platform extends{
+ def format(ts: Long) = {
+ new js.Date(ts).toLocaleString()
+ }
+ @JSExport
+ def main(container: dom.HTMLDivElement) = {
+ container.appendChild(
+ div(
+ h1("Hello from Scala.js!"),
+ p(Simple.formatTimes(Seq(0, 1 << 30)))
+ ).render
+ )
+ val payload = upickle.write(Seq(0L, 1L << 30))
+ Ajax.post("/formatDates", payload).foreach{ xhr =>
+ container.appendChild(
+ div(
+ h1("Hello from Ajax!"),
+ p(xhr.responseText)
+ ).render
+ )
+ }
+
+ }
+} \ No newline at end of file
diff --git a/examples/crossBuilds/clientserver2/jvm/shared/main/scala/simple/Simple.scala b/examples/crossBuilds/clientserver2/jvm/shared/main/scala/simple/Simple.scala
new file mode 100644
index 0000000..d3b0278
--- /dev/null
+++ b/examples/crossBuilds/clientserver2/jvm/shared/main/scala/simple/Simple.scala
@@ -0,0 +1,7 @@
+/*shared/main/scala/simple/Simple.scala*/
+package simple
+object Simple{
+ def formatTimes(timestamps: Seq[Long]): String = {
+ timestamps.map(Platform.format).mkString("\n")
+ }
+} \ No newline at end of file
diff --git a/examples/crossBuilds/clientserver2/jvm/shared/test/scala/simple/SimpleTest.scala b/examples/crossBuilds/clientserver2/jvm/shared/test/scala/simple/SimpleTest.scala
new file mode 100644
index 0000000..ec6b29f
--- /dev/null
+++ b/examples/crossBuilds/clientserver2/jvm/shared/test/scala/simple/SimpleTest.scala
@@ -0,0 +1,13 @@
+/*js/shared/test/scala/simple/SimpleTest.scala*/
+/*jvm/shared/test/scala/simple/SimpleTest.scala*/
+package simple
+import utest._
+object SimpleTest extends TestSuite{
+ val tests = TestSuite{
+ 'format{
+ 'nil - assert(Simple.formatTimes(Nil) == "")
+ 'timeZero - assert(
+ Simple.formatTimes(Seq(0)) == "December 31, 1969 4:00:00 PM PST")
+ }
+ }
+} \ No newline at end of file
diff --git a/examples/crossBuilds/clientserver2/jvm/src/main/scala/simple/Platform.scala b/examples/crossBuilds/clientserver2/jvm/src/main/scala/simple/Platform.scala
new file mode 100644
index 0000000..f4ef986
--- /dev/null
+++ b/examples/crossBuilds/clientserver2/jvm/src/main/scala/simple/Platform.scala
@@ -0,0 +1,61 @@
+//jvm/src/main/scala/simple/Platform.scala
+package simple
+
+import java.text.SimpleDateFormat
+import akka.actor.ActorSystem
+import spray.http.{HttpEntity, MediaTypes}
+import spray.routing.SimpleRoutingApp
+import scalatags.Text.all._
+
+object Static{
+ val msg = Simple.formatTimes(Seq(0, 1 << 30))
+ val boot =
+ "Platform().main(document.getElementById('contents'))"
+ val page = html(
+ head(
+ script(src:="/js-fastopt.js")
+ ),
+ body(
+ onload:=boot,
+ div(id:="contents")(
+ h1("Hello from Scala-JVM!"),
+ p(msg)
+ )
+ )
+ )
+}
+object Platform extends SimpleRoutingApp{
+ def format(ts: Long) = {
+ val fmt =
+ "MMMM d, yyyy h:mm:ss aaa z"
+ new SimpleDateFormat(fmt).format(
+ new java.util.Date(ts)
+ )
+ }
+
+ def main(args: Array[String]): Unit = {
+ implicit val system = ActorSystem()
+ startServer("localhost", port = 8080){
+ get{
+ pathSingleSlash{
+ complete{
+ HttpEntity(
+ MediaTypes.`text/html`,
+ Static.page.render
+ )
+ }
+ } ~
+ getFromResourceDirectory("")
+ } ~
+ post{
+ path("formatDates"){
+ extract(_.request.entity.asString) { e =>
+ complete {
+ Simple.formatTimes(upickle.read[Seq[Long]](e))
+ }
+ }
+ }
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/examples/crossBuilds/clientserver2/project/build.sbt b/examples/crossBuilds/clientserver2/project/build.sbt
new file mode 100644
index 0000000..5bd83ce
--- /dev/null
+++ b/examples/crossBuilds/clientserver2/project/build.sbt
@@ -0,0 +1,4 @@
+/*project/build.sbt*/
+addSbtPlugin("org.scala-lang.modules.scalajs" % "scalajs-sbt-plugin" % "0.5.5")
+
+addSbtPlugin("com.lihaoyi" % "utest-js-plugin" % "0.2.4") \ No newline at end of file