From 7924069976b15ee0c535221673d89200ce3d5570 Mon Sep 17 00:00:00 2001 From: lihaoyi Date: Sun, 23 Nov 2014 20:33:33 -0800 Subject: Packaged resources --- book/src/main/scala/book/Book.scala | 26 +++++++++------------- book/src/main/scala/book/Main.scala | 14 +++++++++++- .../main/scalatex/book/handson/WebPage.scalatex | 2 +- .../book/indepth/AdvancedTechniques.scalatex | 2 +- .../main/scalatex/book/indepth/JavaAPIs.scalatex | 8 ++++++- build.sbt | 4 ++-- .../src/main/scala/scrollmenu/Controller.scala | 2 +- .../src/main/scala/scrollmenu/ScrollMenu.scala | 5 +++-- 8 files changed, 39 insertions(+), 24 deletions(-) diff --git a/book/src/main/scala/book/Book.scala b/book/src/main/scala/book/Book.scala index 9323fff..8b5098d 100644 --- a/book/src/main/scala/book/Book.scala +++ b/book/src/main/scala/book/Book.scala @@ -8,7 +8,7 @@ import scalatags.Text.all._ * Created by haoyi on 10/26/14. */ object Book { - val autoResources = Seq( + val autoResources = Set( "META-INF/resources/webjars/highlightjs/8.2-1/highlight.min.js", "META-INF/resources/webjars/highlightjs/8.2-1/styles/idea.min.css", "META-INF/resources/webjars/highlightjs/8.2-1/languages/scala.min.js", @@ -18,38 +18,32 @@ object Book { "META-INF/resources/webjars/highlightjs/8.2-1/languages/xml.min.js", "META-INF/resources/webjars/pure/0.5.0/pure-min.css", "META-INF/resources/webjars/pure/0.5.0/grids-responsive-min.css", - "META-INF/resources/webjars/font-awesome/4.2.0/css/font-awesome.min.css", "META-INF/resources/webjars/font-awesome/4.2.0/fonts/FontAwesome.otf", "META-INF/resources/webjars/font-awesome/4.2.0/fonts/fontawesome-webfont.eot", "META-INF/resources/webjars/font-awesome/4.2.0/fonts/fontawesome-webfont.svg", "META-INF/resources/webjars/font-awesome/4.2.0/fonts/fontawesome-webfont.ttf", "META-INF/resources/webjars/font-awesome/4.2.0/fonts/fontawesome-webfont.woff", - "META-INF/resources/webjars/react/0.11.1/react.min.js", "css/side-menu.css", - "example-fastopt.js", + "example-opt.js", "webpage/weather.js", "favicon.svg", "favicon.png" ) - val manualResources = Seq( + val fontAwesomeCss = + "META-INF/resources/webjars/font-awesome/4.2.0/css/font-awesome.min.css" + + val manualResources = Set( "images/javascript-the-good-parts-the-definitive-guide.jpg", "images/Hello World.png", "images/Hello World White.png", "images/Hello World Console.png", "images/IntelliJ Hello.png", "images/Dropdown.png", - "images/Scalatags Downloads.png" + "images/Scalatags Downloads.png", + fontAwesomeCss ) - val includes = for(res <- autoResources) yield { - if (res.endsWith(".js")) - script(src:=res) - else if (res.endsWith(".css")) - link(rel:="stylesheet", href:=res) - else - raw("") - } val txt = Index() val data = upickle.write(sect.structure) @@ -61,8 +55,10 @@ object Book { meta(charset:="utf-8"), meta(name:="viewport", content:="width=device-width, initial-scale=1.0"), link(rel:="shortcut icon", `type`:="image/png", href:="favicon.png"), + link(rel:="stylesheet", href:=fontAwesomeCss), + link(rel:="stylesheet", href:="styles.css"), tags2.title("Hands-on Scala.js"), - includes + script(src:="scripts.js") ), body( onload:=s"Controller().main($data)", diff --git a/book/src/main/scala/book/Main.scala b/book/src/main/scala/book/Main.scala index da9bace..cb8ef99 100644 --- a/book/src/main/scala/book/Main.scala +++ b/book/src/main/scala/book/Main.scala @@ -22,10 +22,22 @@ object Main { write(Book.site, "output/index.html") - for(res <- Book.autoResources ++ Book.manualResources) { + val jsFiles = Book.autoResources.filter(_.endsWith(".js")).toSet + val cssFiles = Book.autoResources.filter(_.endsWith(".css")).toSet + val miscFiles = Book.autoResources -- cssFiles -- jsFiles + + for(res <- Book.manualResources ++ miscFiles) { copy(getClass.getResourceAsStream("/" + res), "output/" + res) } + for((resources, dest) <- Seq(jsFiles -> "scripts.js", cssFiles -> "styles.css")) { + val blobs = for(res <- resources.iterator) yield { + io.Source.fromInputStream(getClass.getResourceAsStream("/"+res)).mkString + } + + write(blobs.mkString("\n"), "output/"+dest) + } + val allNames = { def rec(n: Tree[String]): Seq[String] = { n.value +: n.children.flatMap(rec) diff --git a/book/src/main/scalatex/book/handson/WebPage.scalatex b/book/src/main/scalatex/book/handson/WebPage.scalatex index 58fceb1..1056460 100644 --- a/book/src/main/scalatex/book/handson/WebPage.scalatex +++ b/book/src/main/scalatex/book/handson/WebPage.scalatex @@ -159,7 +159,7 @@ @li @hl.scala("=>") v.s. @hl.javascript{function} to define the callback. @li - Scalatags' @hl.scala{pre} v.s. @hl.javascript{document.createElement} + Scalatags' @hl.scala{pre} v.s. @hl.javascript{document.createElement("pre")} @p Overall, they're pretty close, which is a common theme in Scala.js: using Javascript APIs in Scala.js is often as seamless and easy as using them in Javascript itself, and it often looks almost identical. diff --git a/book/src/main/scalatex/book/indepth/AdvancedTechniques.scalatex b/book/src/main/scalatex/book/indepth/AdvancedTechniques.scalatex index f368a71..171b2d3 100644 --- a/book/src/main/scalatex/book/indepth/AdvancedTechniques.scalatex +++ b/book/src/main/scalatex/book/indepth/AdvancedTechniques.scalatex @@ -26,7 +26,7 @@ @li @b{Continuous}: Handling of first-class signals, like in @link("Elm", "http://elm-lang.org/learn/What-is-FRP.elm") - @sect{Why FPR} + @sect{Why FRP} @p The value proposition of FRP is that in a "traditional" program, when an event occurs, events and changes propagate throughout the program in an ad-hoc manner. An event-listener may trigger additional events, call some callbacks, or set some mutable variables that subsequent code will read and react to. diff --git a/book/src/main/scalatex/book/indepth/JavaAPIs.scalatex b/book/src/main/scalatex/book/indepth/JavaAPIs.scalatex index cecead8..0ad886d 100644 --- a/book/src/main/scalatex/book/indepth/JavaAPIs.scalatex +++ b/book/src/main/scalatex/book/indepth/JavaAPIs.scalatex @@ -35,4 +35,10 @@ @hl.scala{sun.misc.Unsafe} @p - And other similar APIs will either need to be rewritten to not-use them (e.g. @lnk("AtomicIntegers", "https://github.com/scala-js/scala-js/blob/master/javalib/src/main/scala/java/util/concurrent/atomic/AtomicInteger.scala") can be written without threading/unsafe APIs because Javascript is single-threaded) or can't be ported at all (e.g. @code{java.io.File}) \ No newline at end of file + And other similar APIs will either need to be rewritten to not-use them. For example, @hl.scala{AtomicXXXs} can be written without threading/unsafe APIs because Javascript is single-threaded, making the implementation for e.g. an @hl.scala{AtomicBoolean} pretty trivial: + + @hl.ref("output/scala-js/javalib/src/main/scala/java/util/concurrent/atomic/AtomicBoolean.scala") + + @p + Others can't be ported at all (e.g. @code{java.io.File}) simply because the API capabilities they provide (blocking reads & writes to files) do not exist in the Javascript runtime. + diff --git a/build.sbt b/build.sbt index ece1f30..895c2b9 100644 --- a/build.sbt +++ b/build.sbt @@ -44,8 +44,8 @@ lazy val book = Project( "com.lihaoyi" %%% "upickle" % "0.2.5" ), (resources in Compile) += { - (fastOptJS in (demos, Compile)).value - (artifactPath in (demos, Compile, fastOptJS)).value + (fullOptJS in (demos, Compile)).value + (artifactPath in (demos, Compile, fullOptJS)).value }, (unmanagedResourceDirectories in Compile) ++= (unmanagedResourceDirectories in (demos, Compile)).value, diff --git a/examples/demos/src/main/scala/scrollmenu/Controller.scala b/examples/demos/src/main/scala/scrollmenu/Controller.scala index 80e20eb..d359138 100644 --- a/examples/demos/src/main/scala/scrollmenu/Controller.scala +++ b/examples/demos/src/main/scala/scrollmenu/Controller.scala @@ -34,7 +34,7 @@ object Controller{ scrollSpy.domTrees.map(_.value.frag) ).render - def updateScroll() = scrollSpy(main.scrollTop + main.clientHeight) + def updateScroll() = scrollSpy(main.scrollTop) val expandIcon = i(cls := "fa fa-caret-down").render val expandLink = a( diff --git a/examples/demos/src/main/scala/scrollmenu/ScrollMenu.scala b/examples/demos/src/main/scala/scrollmenu/ScrollMenu.scala index 7c4fb78..29c44a6 100644 --- a/examples/demos/src/main/scala/scrollmenu/ScrollMenu.scala +++ b/examples/demos/src/main/scala/scrollmenu/ScrollMenu.scala @@ -77,7 +77,7 @@ class ScrollSpy(structure: Tree[String], } def walkTree(tree: Tree[MenuNode]): Boolean = { val Tree(MenuNode(menuItem, itemId, start, end), children) = tree - val before = headers(start) < threshold + val before = headers(start) <= threshold val after = (end >= headers.length) || headers(end) > threshold val win = before && after @@ -96,7 +96,8 @@ class ScrollSpy(structure: Tree[String], // This means it's the leaf element, because it won but there // aren't any children which won, so it must be the actual leaf tree.children.foreach(_.value.frag.classList.remove("selected")) - dom.location.hash = itemId + if (dom.location.hash != itemId) + dom.location.hash = itemId scroll(menuItem.children(0)) } -- cgit v1.2.3