Skip to content
Permalink
Browse files

Updated default ContentScreen show and hide functionality to not requ…

…ire Bootstrap and initial hiding functionality to utilize it properly

Added Template.byTag macro support
Updated CommunicationScreen to utilize PreloadedContentScreen
  • Loading branch information...
darkfrog26 committed Aug 20, 2019
1 parent 3c2049b commit 4fe6cb385a71e14b159f230fbf9bc86d95f04661
@@ -70,23 +70,25 @@ trait ContentScreen extends Screen with PathActivation {
}
span.appendChild(child)
}
span.classList.add("d-none")

hideContent(span)
pageTag.appendChild(span)
contentOptionVar := Some(span)
}

protected def showContent(): Unit = contentOption.foreach { _ =>
content.classList.remove("d-none")
protected def showContent(content: html.Element): Unit = {
content.style.removeProperty("display")
}
protected def hideContent(): Unit = contentOption.foreach { _ =>
content.classList.add("d-none")

protected def hideContent(content: html.Element): Unit = {
content.style.display = "none"
}

override protected def activate(): Future[Unit] = super.activate().map { _ =>
showContent()
contentOption.foreach(showContent)
}

override protected def deactivate(): Future[Unit] = super.deactivate().map { _ =>
hideContent()
contentOption.foreach(hideContent)
}
}
@@ -2,7 +2,7 @@ package io.youi

import java.io.File

import io.youi.stream.{ByClass, ById, HTMLParser}
import io.youi.stream.{ByClass, ById, ByTag, HTMLParser}
import org.scalajs.dom.Element
import profig.Profig

@@ -13,6 +13,7 @@ import scala.reflect.macros.blackbox
object Template {
def byId[E <: Element](path: String, id: String, appName: String): E = macro TemplateMacros.existingById[E]
def byClass[E <: Element](path: String, className: String, appName: String): List[E] = macro TemplateMacros.existingByClass[E]
def byTag[E <: Element](path: String, tagName: String, appName: String): List[E] = macro TemplateMacros.existingByTag[E]
}

@compileTimeOnly("Enable Macros for expansion")
@@ -98,4 +99,45 @@ object TemplateMacros {
}
""")
}

def existingByTag[E <: Element](context: blackbox.Context)(path: context.Expr[String],
tagName: context.Expr[String],
appName: context.Expr[String])(implicit e: context.WeakTypeTag[E]): context.Expr[List[E]] = {
import context.universe._

Profig.loadDefaultsMacro()

val pathValue = path match {
case Expr(Literal(Constant(value: String))) => value
}
val tagValue = tagName match {
case Expr(Literal(Constant(value: String))) => value
}
val pathKey = appName match {
case Expr(Literal(Constant(value: String))) => s"$value.template.path"
}
val templatePath = Profig(pathKey).opt[String]
val file = templatePath match {
case Some(basePath) => new File(basePath, pathValue)
case None => new File(pathValue)
}
if (!file.exists()) {
context.warning(context.enclosingPosition, s"No configuration defined for $pathKey.")
context.abort(context.enclosingPosition, s"Unable to find path for ${file.getAbsolutePath}.")
}
val parser = HTMLParser(file)
val template = parser.stream(Nil, selector = Some(ByTag(tagValue)), includeAllMatches = true)
if (template.trim.isEmpty) {
context.abort(context.enclosingPosition, s"No content found for $tagValue in ${file.getAbsolutePath}")
}

context.Expr[List[E]](
q"""
try {
io.youi.dom.fromString[$e]($template)
} catch {
case t: Throwable => throw new RuntimeException("Error parsing HTML [" + $template + "] byTag " + $tagValue, t)
}
""")
}
}
@@ -1,14 +1,15 @@
package io.youi.example.screen

import io.youi.Template
import io.youi.app.screen.PreloadedContentScreen
import io.youi.dom._
import io.youi.example.ClientExampleApplication._
import io.youi.net._
import org.scalajs.dom.{Event, html, window}

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

object CommunicationScreen extends ExampleScreen {
object CommunicationScreen extends ExampleScreen with PreloadedContentScreen {
def connectedInput: html.Input = content.byId[html.Input]("communicationConnected")
def connectedButton: html.Button = content.byId[html.Button]("communicationConnectButton")
def reverseInput: html.Input = content.byId[html.Input]("communicationReverse")
@@ -24,6 +25,12 @@ object CommunicationScreen extends ExampleScreen {

override def path: Path = path"/communication.html"

override protected def preloadScreen: html.Element = Template.byTag[html.Element](
"example/jvm/src/main/resources/content/templates/communication.html",
"screen",
"youi"
).head

override protected def load(): Future[Unit] = super.load().map { _ =>
configure()
}

0 comments on commit 4fe6cb3

Please sign in to comment.
You can’t perform that action at this time.