-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feature/custom colors in interactions #420
base: develop
Are you sure you want to change the base?
Changes from 16 commits
963d70f
22acdea
483d188
a537998
c01e263
5756494
cd49b58
90eb6e2
fa33240
9c728a3
883ef03
25725e5
769eea5
089ea95
e2f280c
5957b6d
b28b1da
fed3625
fb95d67
2d69b53
88591b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
body { | ||
background-color: red; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ import org.corespring.container.client.views.txt.js.{ ComponentServerWrapper, Co | |
import org.corespring.container.components.model._ | ||
import org.corespring.container.components.model.dependencies.ComponentTypeFilter | ||
import org.corespring.container.components.model.packaging.ClientSideDependency | ||
import play.api.libs.json.JsObject | ||
import play.api.libs.json.{ Json, JsValue, JsObject } | ||
|
||
object SourceGenerator { | ||
object Keys { | ||
|
@@ -26,6 +26,8 @@ trait SourceGenerator | |
|
||
def css(components: Seq[Component]): String | ||
|
||
def less(components: Seq[Component], customColors: JsObject = Json.obj()): String | ||
|
||
protected def wrapComponent(moduleName: String, directiveName: String, src: String) = { | ||
ComponentWrapper(moduleName, directiveName, src).toString | ||
} | ||
|
@@ -119,6 +121,29 @@ abstract class BaseGenerator | |
""".stripMargin | ||
} | ||
|
||
override def less(components: Seq[Component], customColors: JsObject = Json.obj()): String = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it alot of this is static content - try and isolate it and generate it once There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what do you mean? |
||
val commonLessSource = scala.io.Source.fromURL(this.getClass().getResource("/public/components-common.less")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should only load this once in the lifetime of the app and we need to be careful that it's not a memory leak.. have a look at other places where are reading resources I think we mostly use IOUtils There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you give me a hint where we are doing that? |
||
val commonLess = commonLessSource.mkString | ||
val (libraries, uiComps, layoutComps, widgets) = splitComponents(components) | ||
val uiLess = uiComps.map(_.client.less.getOrElse("")).mkString("\n") | ||
val widgetLess = widgets.map(_.client.less.getOrElse("")).mkString("\n") | ||
val layoutLess = layoutComps.map(_.less.getOrElse("")).mkString("\n") | ||
val libraryLess = libraries.map(_.less.getOrElse("")).mkString("\n") | ||
val dynamicColors = customColors.asOpt[Map[String, String]] match { | ||
case Some(cols) => cols.map { case (a, b) => s"@$a: $b;" }.mkString("\n") | ||
case _ => "" | ||
} | ||
val res = s""" | ||
|$commonLess | ||
|$dynamicColors | ||
|$uiLess | ||
|$widgetLess | ||
|$layoutLess | ||
|$libraryLess | ||
""".stripMargin | ||
res | ||
} | ||
|
||
override def js(components: Seq[Component]): String = { | ||
val (libs, uiComps, layoutComps, widgets) = splitComponents(components) | ||
val uiJs = uiComps.map(interactionToJs).mkString("\n") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,15 @@ import org.corespring.container.components.model.dependencies.DependencyResolver | |
import org.corespring.container.logging.ContainerLogger | ||
import play.api.http.ContentTypes | ||
import play.api.mvc._ | ||
import org.lesscss.LessCompiler | ||
import play.api.libs.json.{ JsObject, Json } | ||
|
||
trait ComponentSets extends Controller with ComponentUrls { | ||
|
||
private lazy val logger = ContainerLogger.getLogger("ComponentSets") | ||
|
||
private val lessCompiler = new LessCompiler() | ||
|
||
def allComponents: Seq[Component] | ||
|
||
def playerGenerator: SourceGenerator | ||
|
@@ -23,15 +27,18 @@ trait ComponentSets extends Controller with ComponentUrls { | |
|
||
def dependencyResolver: DependencyResolver | ||
|
||
def resource[A >: EssentialAction](context: String, directive: String, suffix: String): A | ||
def resource[A >: EssentialAction](context: String, directive: String, suffix: String, resourceToken: String = "default"): A | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
def singleResource[A >: EssentialAction](context: String, componentType: String, suffix: String): A | ||
def singleResource[A >: EssentialAction](context: String, componentType: String, suffix: String, resourceToken: String = "default"): A | ||
|
||
protected final def generate(context: String, resolvedComponents: Seq[Component], suffix: String): (String, String) = { | ||
protected final def generate(context: String, resolvedComponents: Seq[Component], suffix: String, parameters: JsObject = Json.obj()): (String, String) = { | ||
|
||
def gen(generator: SourceGenerator): String = suffix match { | ||
case "js" => generator.js(resolvedComponents) | ||
case "css" => generator.css(resolvedComponents) | ||
case "less" => | ||
val res = generator.less(resolvedComponents, (parameters \ "colors").asOpt[JsObject].getOrElse(Json.obj())) | ||
lessCompiler.compile(res) | ||
case _ => "" | ||
} | ||
|
||
|
@@ -46,37 +53,40 @@ trait ComponentSets extends Controller with ComponentUrls { | |
val contentType = suffix match { | ||
case "js" => ContentTypes.JAVASCRIPT | ||
case "css" => ContentTypes.CSS | ||
case "less" => ContentTypes.CSS | ||
case _ => throw new RuntimeException(s"Unknown suffix: $suffix") | ||
} | ||
|
||
(out, contentType) | ||
} | ||
|
||
protected final def generateBodyAndContentType(context: String, directive: String, suffix: String): (String, String) = { | ||
protected final def generateBodyAndContentType(context: String, directive: String, suffix: String, parameters: JsObject = Json.obj()): (String, String) = { | ||
val types: Seq[String] = ComponentUrlDirective(directive, allComponents) | ||
val usedComponents = types.map { t => allComponents.find(_.componentType == t) }.flatten | ||
val components = dependencyResolver.resolveComponents(usedComponents.map(_.id), Some(context)) | ||
logger.trace(s"context: $context, comps: ${components.map(_.componentType).mkString(",")}") | ||
generate(context, components, suffix) | ||
generate(context, components, suffix, parameters) | ||
} | ||
|
||
override def cssUrl(context: String, components: Seq[Component], separatePaths: Boolean): Seq[String] = url(context, components, "css", separatePaths) | ||
|
||
override def lessUrl(context: String, components: Seq[Component], separatePaths: Boolean, encodedCustomColors: String): Seq[String] = url(context, components, "less", separatePaths, encodedCustomColors) | ||
|
||
override def jsUrl(context: String, components: Seq[Component], separatePaths: Boolean): Seq[String] = url(context, components, "js", separatePaths) | ||
|
||
private def url(context: String, components: Seq[Component], suffix: String, separatePaths: Boolean): Seq[String] = { | ||
private def url(context: String, components: Seq[Component], suffix: String, separatePaths: Boolean, resourceToken: String = "default"): Seq[String] = { | ||
|
||
require(allComponents.length > 0, "Can't load components") | ||
|
||
if (separatePaths) { | ||
val resolvedComponents = dependencyResolver.resolveComponents(components.map(_.id), Some(context)) | ||
resolvedComponents.map { c => routes.ComponentSets.singleResource(context, c.componentType, suffix).url } | ||
resolvedComponents.map { c => routes.ComponentSets.singleResource(context, c.componentType, suffix, resourceToken).url } | ||
} else { | ||
components match { | ||
case Nil => Seq.empty | ||
case _ => { | ||
ComponentUrlDirective.unapply(components.map(_.componentType), allComponents).map { path => | ||
routes.ComponentSets.resource(context, path, suffix).url | ||
routes.ComponentSets.resource(context, path, suffix, resourceToken).url | ||
}.toSeq | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
duplication? line 65?