Permalink
Browse files

Added simple Bootstrap template.

Updates to changes in Profig to work better at compile-time.
Added testing of Template usage.
  • Loading branch information...
darkfrog26 committed Mar 8, 2018
1 parent 1fabb2e commit ab98be97936e80c2da222ded99ebf52222abd9d4
@@ -189,6 +189,10 @@ trait ServerApplication extends YouIApplication with Server {
includeApplication: Boolean = true): HttpHandler = builder.handle { connection =>
serveHTML(connection, template, deltas, includeApplication)
}
def bootstrap(template: Content = ServerApplication.BootstrapTemplate,
deltas: List[Delta] = Nil,
includeApplication: Boolean = true): HttpHandler = page(template, deltas, includeApplication)
}
def addDeltas(connection: HttpConnection, deltas: List[Delta]): Unit = {
@@ -333,5 +337,30 @@ object ServerApplication {
|</html>
""".stripMargin.trim, ContentType.`text/html`)
/**
* Empty page template with bootstrap setup.
*/
lazy val BootstrapTemplate: Content = Content.string(
"""
|<!doctype html>
|<html lang="en">
| <head>
| <!-- Required meta tags -->
| <meta charset="utf-8">
| <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
| <!-- Bootstrap CSS -->
| <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
|
| <title></title>
| </head>
| <body>
| <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
| <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
| <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
| </body>
|</html>
""".stripMargin.trim, ContentType.`text/html`)
val DeltaKey: String = "deltas"
}
@@ -7,7 +7,7 @@ resolvers in ThisBuild += Resolver.sonatypeRepo("releases")
resolvers in ThisBuild += Resolver.sonatypeRepo("snapshots")
scalacOptions in ThisBuild ++= Seq("-unchecked", "-deprecation")
val profigVersion = "2.0.1"
val profigVersion = "2.1.0-SNAPSHOT"
val scribeVersion = "2.2.0"
val powerScalaVersion = "2.0.5"
val reactifyVersion = "2.3.0"
@@ -120,7 +120,11 @@ lazy val dom = project.in(file("dom"))
.enablePlugins(ScalaJSPlugin)
.settings(
name := "youi-dom",
libraryDependencies += "com.outr" %% "profig" % profigVersion,
libraryDependencies ++= Seq(
"com.outr" %% "profig" % profigVersion,
"org.scalactic" %%% "scalactic" % scalacticVersion,
"org.scalatest" %%% "scalatest" % scalaTestVersion % "test"
),
jsEnv := new org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv
)
.dependsOn(coreJS)
@@ -0,0 +1,7 @@
{
"youi": {
"template": {
"path": "dom"
}
}
}
@@ -4,7 +4,7 @@ import java.io.File
import io.youi.stream.{ByClass, ById, HTMLParser}
import org.scalajs.dom.Element
import profig.Profig
import profig.{ConfigurationPath, JsonParser, Profig}
import scala.annotation.compileTimeOnly
import scala.language.experimental.macros
@@ -22,8 +22,8 @@ object TemplateMacros {
appName: context.Expr[String])(implicit e: context.WeakTypeTag[E]): context.Expr[E] = {
import context.universe._
// Initialize Profig
context.eval(reify(Profig.initMacro(Nil)))
JsonParser.compileTime()
Profig.loadDefaults()
val pathValue = path match {
case Expr(Literal(Constant(value: String))) => value
@@ -66,8 +66,8 @@ object TemplateMacros {
appName: context.Expr[String])(implicit e: context.WeakTypeTag[E]): context.Expr[List[E]] = {
import context.universe._
// Initialize Profig
context.eval(reify(Profig.initMacro(Nil)))
JsonParser.compileTime()
Profig.loadDefaults()
val pathValue = path match {
case Expr(Literal(Constant(value: String))) => value
@@ -0,0 +1,15 @@
package specs
import io.youi.Template
import org.scalatest.{Matchers, WordSpec}
import org.scalajs.dom.html
class TemplateSpec extends WordSpec with Matchers {
"Template" should {
"load from id" in {
val div = Template.byId[html.Div]("template.html", "hello", "youi")
div.innerHTML should be("Hello, World!")
}
}
}
@@ -0,0 +1,5 @@
<html>
<body>
<div id="hello">Hello, World!</div>
</body>
</html>
@@ -2,8 +2,9 @@ package io.youi.example
import io.youi.app.screen.LoadingTransitionSupport
import io.youi.app.{ClientApplication, ClientConnectivity}
import io.youi.dom
import io.youi.dom._
import io.youi.example.screen.{CommunicationScreen, LoginScreen}
import io.youi.example.screen.{CommunicationScreen, ExampleBootstrapScreen, LoginScreen}
import io.youi.example.ui.HelloWorld
//import io.youi.example.ui._
//import io.youi.example.ui.drawable._
@@ -20,6 +21,7 @@ object ClientExampleApplication extends ExampleApplication with ClientApplicatio
val communicationScreen = CommunicationScreen
val login = LoginScreen
val bootstrap = ExampleBootstrapScreen
// val uiExamples = UIExamples
@@ -63,7 +65,7 @@ object ClientExampleApplication extends ExampleApplication with ClientApplicatio
def cc: ClientConnectivity = clientConnectivity(ClientExampleApplication.connectivity)
override protected val loadingElement: html.Element = byId[html.Div]("loading")
override protected val loadingElement: html.Element = getById[html.Div]("loading").getOrElse(dom.create[html.Div]("div"))
@JSExportTopLevel("application")
def main(): Unit = {
@@ -0,0 +1,37 @@
package io.youi.example.screen
import io.youi.app.screen.PathActivation
import io.youi.dom
import org.scalajs.dom.{Event, document, html}
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
object ExampleBootstrapScreen extends PathActivation {
override def path: String = "/bootstrap.html"
implicit def bc2E[E <: html.Element, T <: BootstrapComponent[E, T]](bc: BootstrapComponent[E, T]): E = bc.element
override protected def init(): Future[Unit] = super.init().map { _ =>
document.body.appendChild(bootstrap.button.content("Primary"))
}
}
object bootstrap {
def button: Button = {
val b = dom.create[html.Button]("button")
b.classList.add("btn")
b.classList.add("btn-primary")
new Button(b)
}
}
abstract class BootstrapComponent[E <: html.Element, T <: BootstrapComponent[E, T]](val element: E) {
def content(value: String): T = {
element.innerHTML = value
this.asInstanceOf[T]
}
}
class Button(button: html.Button) extends BootstrapComponent[html.Button, Button](button) {
}
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Template</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<h1 id="heading">Template Heading</h1>
<hr/>
<div id="content"></div>
<footer>
<hr/>
The footer
</footer>
<div id="loading" style="opacity: 0; visibility: hidden;">
<span>Loading...</span>
</div>
</body>
</html>
@@ -16,6 +16,7 @@ object ServerExampleApplication extends ExampleApplication with ServerApplicatio
handler.matcher(path.exact("/hello.txt")).caching(CachingManager.MaxAge(120L)).resource {
Content.string("Hello World!", ContentType.`text/plain`)
}
handler.matcher(path.exact("/bootstrap.html")).bootstrap()
handler.matcher(path.exact("/cookies.html")).wrap(CookiesExample)
handler.matcher(path.exact("/web-socket-example")).wrap(WebSocketExample)
proxies += ProxyHandler(path.exact("/proxy.html")) { url =>
@@ -5,17 +5,18 @@ import java.net
import io.youi.net.URL
import org.powerscala.io._
import profig.{Config, ConfigApplication, JsonUtil}
import profig.{Profig, JsonUtil}
object GoogleFontBuilder extends ConfigApplication {
private lazy val apiKey: String = Config("googleApiKey").as[Option[String]].getOrElse(throw new RuntimeException("No configuration setting present for googleApiKey."))
object GoogleFontBuilder {
private lazy val apiKey: String = Profig("googleApiKey").as[Option[String]].getOrElse(throw new RuntimeException("No configuration setting present for googleApiKey."))
private lazy val url = URL(s"https://www.googleapis.com/webfonts/v1/webfonts?key=$apiKey")
private val base = "http://fonts.gstatic.com/s"
override def main(args: Array[String]): Unit = start(args)
def main(args: Array[String]): Unit = {
Profig.merge(args)
Profig.loadDefaults()
override protected def run(): Unit = {
val jsonString = IO.stream(new net.URL(url.toString), new StringBuilder).toString
val list = JsonUtil.fromJsonString[WebFontList](jsonString)

0 comments on commit ab98be9

Please sign in to comment.