Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Scala JS for the main scripts #930

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ vendor/
/.idea
.ruby-version
.sass-cache/
.ivy2/
.sbt/
target
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ bin/serve
bundle exec jekyll serve --incremental
```

### For ScalaJs Development
```
sbt ~fastOptJS
```

## Viewing the site

Regardless of your method of running Jekyll, the generated site is available at `http://localhost:4000`.
Expand Down
3 changes: 3 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ highlighter:
exclude:
- bundle-vendor/
- bin/
- js-src/
- project/
- blog/README.md
- training/README.md
- README.md
Expand All @@ -19,6 +21,7 @@ exclude:
- vendor
- Procfile
- Rakefile
- docker-compose.yml

future: true

Expand Down
1 change: 1 addition & 0 deletions _includes/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

<!-- Custom javascript -->
<script src="{{ site.baseurl }}/resources/js/functions.js" type="text/javascript"></script>
<script src="{{ site.baseurl }}/resources/js/scala-functions.js" type="text/javascript"></script>
<script src="{{ site.baseurl }}/resources/js/analytics.js" type="text/javascript"></script>
</body>
</html>
3 changes: 3 additions & 0 deletions bin/build-js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash -x
command -v docker-compose >/dev/null 2>&1 || { echo >&2 "Please install Docker Compose: https://docs.docker.com/compose/install/"; exit 1; }
docker-compose run js-build sbt fullOptJS
3 changes: 2 additions & 1 deletion bin/serve
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash
command -v docker-compose >/dev/null 2>&1 || { echo >&2 "Please install Docker Compose: https://docs.docker.com/compose/install/"; exit 1; }
docker-compose run --service-ports scala-lang jekyll serve --incremental
docker-compose up
docker-compose stop
14 changes: 14 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name := "scala-lang"

organization in ThisBuild := "org.scalalang"
version in ThisBuild := "0.1"
scalaVersion in ThisBuild := "2.12.6"
scalacOptions in ThisBuild ++= Seq(
"-feature",
"-deprecation"
)

lazy val functions = project in file("js-src/functions")

lazy val root: Project = project.in(file("."))
.aggregate(functions)
8 changes: 7 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
version: "2"
services:
js-build:
image: hseeberger/scala-sbt:latest
volumes:
- $PWD:/root
command: sbt ~fastOptJS
scala-lang:
image: jekyll/jekyll:latest
volumes:
Expand All @@ -9,4 +14,5 @@ services:
- 4000:4000
- 35729:35729
- 3000:3000
- 80:4000
- 80:4000
command: jekyll serve --incremental
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming that docker-compose is only used for development, but if this isn't the case I could be breaking things here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct it's optional dev-only stuff

18 changes: 18 additions & 0 deletions js-src/functions/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import org.scalajs.core.tools.linker.backend.OutputMode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import is unused.


enablePlugins(ScalaJSPlugin)

name := "functions"

libraryDependencies ++= Seq(
"org.scala-js" %%% "scalajs-dom" % Versions.Dom,
"org.querki" %%% "jquery-facade" % Versions.JQuery
)

// move our output folder to static
artifactPath in(Compile, fastOptJS) := baseDirectory.value / ".." / ".." / "resources" / "js" / s"scala-${name.value}.js"
artifactPath in(Compile, fullOptJS) := baseDirectory.value / ".." / ".." / "resources" / "js" / s"scala-${name.value}.js"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't configure fastOptJS and fullOptJS to produce the same target file. It will confuse caches (fullOptJS after fastOptJS will not recreate the file, for example).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good to know. What's the recommended way to setup a build like this? (Where I'd like my html file to be static and to just import the compiled output regardless of compilation target)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. I think it depends on the web server and/or templating mechanisms that one use. The folks at https://gitter.im/scala-js/scala-js or https://stackoverflow.com/questions/tagged/scala.js might have more experience with that than I do.

scalaJSOptimizerOptions in (Compile, fullOptJS) ~= { _.withUseClosureCompiler(true) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This setting is redundant, as it is the default.


scalaJSUseMainModuleInitializer := true
emitSourceMaps := false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer the more future-proof

scalaJSLinkerConfig ~= { _.withSourceMap(false) }

45 changes: 45 additions & 0 deletions js-src/functions/src/main/scala/org/scalalang/DownloadLinks.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.scalalang

import org.querki.jquery._
import org.scalajs.dom.{Element, document}
import org.scalalang.utils.{JsUtils, Logger, OS}

/**
* This updates our download links based on the OS of the client
*/
object DownloadLinks {

def apply(): Unit = {
setupBinariesElement()
setupMainDownload()
}

private def setupBinariesElement(): Unit = JsUtils.findElement("#download-binaries").foreach((binariesElmnt: JQuery) => {
val os: OS = OS()
val osLabel: String = os.label

var anchor: Element = document.getElementById("#link-main-unixsys")
if (os == OS.Windows) {
anchor = document.getElementById("#link-main-windows")
}
if (anchor == null) {
anchor = document.getElementById("#link-main-one4all")
}
val link: String = anchor.getAttribute("href")

binariesElmnt.attr("href", link).addClass(osLabel)
$("#users-os").text(osLabel)
})

private def setupMainDownload(): Unit = JsUtils.findElement(".main-download").foreach(_ => {
val osLabel: String = OS().label

val intelliJlink: String = $("#intellij-" + osLabel).text()
val sbtLink: String = $("#sbt-" + osLabel).text()
val stepOneContent: String = $("#stepOne-" + osLabel).html()

$("#download-intellij-link").attr("href", intelliJlink)
$("#download-sbt-link").attr("href", sbtLink)
$("#download-step-one").html(stepOneContent)
})
}
24 changes: 24 additions & 0 deletions js-src/functions/src/main/scala/org/scalalang/FunctionsApp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.scalalang

import org.querki.jquery._
import org.scalajs.dom.document
import org.scalalang.utils.{Logger, RootLogger}

/**
* This is the main entry point for our application
*/
object FunctionsApp {
RootLogger.setTrace()

private val logger: Logger = Logger[FunctionsApp.type]

def main(args: Array[String]): Unit = {
$(document).ready(() => {
logger.trace("Dom Ready")

Tooltip()
DownloadLinks()
PositionMarker()
})
}
}
37 changes: 37 additions & 0 deletions js-src/functions/src/main/scala/org/scalalang/PositionMarker.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.scalalang

import org.querki.jquery._
import org.scalajs.dom.window
import org.scalalang.utils.JsUtils

/**
* This renders / updates the position marker on the front page
*/
object PositionMarker {

private val imageWidth: Int = 1680

// where our position should be on the image
private val targetX: Int = 1028
private val targetY: Int = 290

def apply(): Unit = JsUtils.findElement("#position-marker")
.foreach((pointer: JQuery) => {
val updater: () => Unit = updatePointer(pointer)

// register on window resize
$(window).resize(updater)

// initialize
pointer.css("top", targetY)
updater()
})

private def updatePointer(pointer: JQuery): () => Unit = () => {
val windowWidth: Double = $(window).width()

val xScale: Double = windowWidth / imageWidth

pointer.css("left", (targetX * xScale).toInt)
}
}
40 changes: 40 additions & 0 deletions js-src/functions/src/main/scala/org/scalalang/Tooltip.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.scalalang

import org.querki.jquery._
import org.scalajs.dom.Element

import scala.scalajs.js

/**
* This renders tooltips for any .masterTooltip class element
*/
object Tooltip {
def apply(): Unit = {
$(".masterTooltip")
.hover(
(tooltip: Element) => {
// make sure we have a title
$(tooltip).attr("title").toOption.foreach((title: String) => {
// create our tooltip and place it on the body
$("<p class=\"tooltip\"></p>")
.text(title)
.appendTo("body")
.fadeIn("slow")
})
},
() => {
// remove our tooltip when we mouse off
$(".tooltip").remove()
}
)
.mousemove((e: JQueryEventObject) => {
val mouseX: Int = e.pageX + 20 //Get X coordinates
val mouseY: Int = e.pageY + 10 //Get Y coordinates

$(".tooltip").css(js.Dictionary[js.Any](
"left" -> mouseX,
"top" -> mouseY
))
})
}
}
14 changes: 14 additions & 0 deletions js-src/functions/src/main/scala/org/scalalang/utils/JsUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.scalalang.utils

import org.querki.jquery._

object JsUtils {
/**
* @param element the element we wish to find
* @return the returned jquery element will have > 1 elements
*/
@inline
def findElement(element: ElementDesc): Option[JQuery] = {
Option($(element)).filter(_.length > 0)
}
}
Loading