diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | examples/scalajs-plain-example/README.md | 9 | ||||
-rw-r--r-- | examples/scalajs-plain-example/js/App.scala | 27 | ||||
-rw-r--r-- | examples/scalajs-plain-example/js/Plotly.scala | 8 | ||||
-rw-r--r-- | examples/scalajs-plain-example/js/build/build.scala | 18 | ||||
-rw-r--r-- | examples/scalajs-plain-example/js/build/build/build.scala | 4 | ||||
-rw-r--r-- | examples/scalajs-plain-example/jvm/build/build.scala | 6 | ||||
-rw-r--r-- | examples/scalajs-plain-example/server/public/index.html | 17 | ||||
-rw-r--r-- | examples/scalajs-plain-example/shared/SomeSharedClass.scala | 2 | ||||
-rw-r--r-- | examples/scalajs-react-example/server/app.js | 28 |
10 files changed, 106 insertions, 14 deletions
@@ -13,3 +13,4 @@ node_modules *fastopt* *fullopt* examples/dotty-example/_site +examples/scalajs-plain-example/server/public/generated diff --git a/examples/scalajs-plain-example/README.md b/examples/scalajs-plain-example/README.md new file mode 100644 index 0000000..280a555 --- /dev/null +++ b/examples/scalajs-plain-example/README.md @@ -0,0 +1,9 @@ + +Compilation instructions +------------------------------------------- +1. `cd js` +2. `cbt compile` or `cbt fullOpt.compile` + +Execution instructions +------------------------------------------- +1. open `server/public/index.html` in a browser diff --git a/examples/scalajs-plain-example/js/App.scala b/examples/scalajs-plain-example/js/App.scala new file mode 100644 index 0000000..71a285e --- /dev/null +++ b/examples/scalajs-plain-example/js/App.scala @@ -0,0 +1,27 @@ +package prototype + +import org.scalajs.dom + +import scala.scalajs.js +import scala.scalajs.js.JSON +import scala.scalajs.js.JSApp +import scala.scalajs.js.annotation.JSExport + +@JSExport("App") +object App extends JSApp { + def main(): Unit = { + val trace1 = """{ + | "x": [1, 2, 3, 4], + | "y": [10, 15, 13, 17], + | "type": "scatter" + |} """.stripMargin + val trace2 = """{ + | "x": [1, 2, 3, 4], + | "y": [16, 5, 11, 9], + | "type": "scatter" + |} """.stripMargin + val data = js.Array(trace1, trace2).map(trace => JSON.parse(trace)) + val doc = dom.document + Plotly.newPlot("plotly", data) + } +} diff --git a/examples/scalajs-plain-example/js/Plotly.scala b/examples/scalajs-plain-example/js/Plotly.scala new file mode 100644 index 0000000..a4623c6 --- /dev/null +++ b/examples/scalajs-plain-example/js/Plotly.scala @@ -0,0 +1,8 @@ +package prototype + +import scala.scalajs.js + +@js.native +object Plotly extends js.Object { + def newPlot(id: String, data: js.Array[js.Dynamic]): js.Any = js.native +} diff --git a/examples/scalajs-plain-example/js/build/build.scala b/examples/scalajs-plain-example/js/build/build.scala new file mode 100644 index 0000000..06e4876 --- /dev/null +++ b/examples/scalajs-plain-example/js/build/build.scala @@ -0,0 +1,18 @@ +import cbt._ +class Build(val context: Context) extends ScalaJsBuild{ + override def projectName = "my-project" + + override def sources = super.sources ++ Seq( + projectDirectory.getParentFile ++ "/shared" + ) + + override def dependencies = ( + super.dependencies ++ + Resolver( mavenCentral ).bind( + "org.scala-js" %%% "scalajs-dom" % "0.9.0" + ) + ) + + override def scalaJsTargetFile = + projectDirectory.getParentFile ++ ("/server/public/generated/" ++ projectName ++ ".js") +} diff --git a/examples/scalajs-plain-example/js/build/build/build.scala b/examples/scalajs-plain-example/js/build/build/build.scala new file mode 100644 index 0000000..b30e005 --- /dev/null +++ b/examples/scalajs-plain-example/js/build/build/build.scala @@ -0,0 +1,4 @@ +import cbt._ +class Build(val context: Context) extends BuildBuild{ + override def dependencies = super.dependencies :+ plugins.scalaJs +} diff --git a/examples/scalajs-plain-example/jvm/build/build.scala b/examples/scalajs-plain-example/jvm/build/build.scala new file mode 100644 index 0000000..327d705 --- /dev/null +++ b/examples/scalajs-plain-example/jvm/build/build.scala @@ -0,0 +1,6 @@ +import cbt._ +class Build(val context: Context) extends BaseBuild{ + override def sources = super.sources ++ Seq( + projectDirectory.getParentFile ++ "/shared" + ) +} diff --git a/examples/scalajs-plain-example/server/public/index.html b/examples/scalajs-plain-example/server/public/index.html new file mode 100644 index 0000000..a06fc3a --- /dev/null +++ b/examples/scalajs-plain-example/server/public/index.html @@ -0,0 +1,17 @@ +<!DOCTYPE html> +<html> +<head> + <title>Prototype</title> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> + <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.16.1/vis.min.css"> +</head> +<body> + execute <pre style="display: inline">cbt fastOptJs</pre> in <pre style="display: inline">js/</pre> to generate/update the .js file + <div id="plotly"></div> + <script src="https://cdn.plot.ly/plotly-latest.min.js"></script> + <script type="text/javascript" src="./generated/my-project.js"></script> + <script type="text/javascript"> + App().main(); + </script> +</body> +</html> diff --git a/examples/scalajs-plain-example/shared/SomeSharedClass.scala b/examples/scalajs-plain-example/shared/SomeSharedClass.scala new file mode 100644 index 0000000..44d4ca8 --- /dev/null +++ b/examples/scalajs-plain-example/shared/SomeSharedClass.scala @@ -0,0 +1,2 @@ +package prototype +case class SomeSharedClass(i: Int) diff --git a/examples/scalajs-react-example/server/app.js b/examples/scalajs-react-example/server/app.js index 620c26a..d44adcc 100644 --- a/examples/scalajs-react-example/server/app.js +++ b/examples/scalajs-react-example/server/app.js @@ -9,20 +9,20 @@ app.get('/data', function(req, res){ //'https://api.instagram.com/v1/media/popular?client_id=642176ece1e7445e99244cec26f4de1f&callback=?', 'https://pixabay.com/api/?key=2741116-9706ac6d4a58f2b5416225505&q=yellow+flowers&image_type=photo', function(response) { - - var body = ""; - response.on('data', function(data) { - body += data; - }); - response.on('end', function() { - console.log(body); - try { - res.send(JSON.parse(body)); - } catch (e) { - return console.error(e); - } - }); - }); + var body = ""; + response.on('data', function(data) { + body += data; + }); + response.on('end', function() { + console.log(body); + try { + res.send(JSON.parse(body)); + } catch (e) { + return console.error(e); + } + }); + } + ); request.on('error', function(e) { console.log('Problem with request: ' + e.message); }); |