PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/container-client-web/app/org/corespring/container/client/controllers/launcher/JsBuilder.scala

https://github.com/corespring/corespring-container
Scala | 57 lines | 45 code | 12 blank | 0 comment | 0 complexity | 0a5ad08e87aa8815ff7f8382233cff24 MD5 | raw file
Possible License(s): 0BSD
  1. package org.corespring.container.client.controllers.launcher
  2. import org.corespring.container.client.io.ResourcePath
  3. import org.corespring.container.client.views.txt.js.ServerLibraryWrapper
  4. import play.api.libs.json.{ JsObject, Json }
  5. import play.api.templates.TxtFormat
  6. private[corespring] class JsBuilder(val resourcePath : ResourcePath) extends JsResource {
  7. override def load: (String) => Option[String] = resourcePath.loadPath(_)
  8. private def lib(n: String) = s"container-client/js/player-launcher/$n"
  9. lazy val coreJs: String = {
  10. val corePaths = {
  11. Some("container-client/bower_components/msgr.js/dist/msgr.js") ++
  12. Seq("logger", "callback-utils", "error-codes", "instance", "client-launcher", "url-builder", "object-id", "draft-id")
  13. .map(s => s"$s.js")
  14. .map(lib)
  15. }
  16. val rawJs = pathToNameAndContents("container-client/js/corespring/core-library.js")._2
  17. val wrapped = corePaths.map(pathToNameAndContents).map(t => ServerLibraryWrapper(t._1, t._2))
  18. val bootstrap =
  19. s"""
  20. |window.org = window.org || {};
  21. |org.corespring = org.corespring || {};
  22. |org.corespring.players = org.corespring.players || {};
  23. |org.corespring.editors = org.corespring.editors || {};
  24. """.stripMargin
  25. s"""$bootstrap
  26. $rawJs
  27. ${wrapped.mkString("\n")}
  28. """
  29. }
  30. def buildJs(corespringUrl: String, files: Seq[String], options: JsObject, bootstrapLine: String, queryParams: Map[String, String]): String = {
  31. val additionalJsNameAndSrc = files.map(lib(_)).map(pathToNameAndContents)
  32. val fullConfig = Json.obj(
  33. "corespringUrl" -> corespringUrl,
  34. "queryParams" -> queryParams) ++ options
  35. val fullConfigJs = ("launch-config" -> s"module.exports = ${Json.stringify(fullConfig)}")
  36. val wrappedNameAndContents = fullConfigJs +: additionalJsNameAndSrc
  37. val wrappedContents: Seq[TxtFormat.Appendable] = wrappedNameAndContents.map {
  38. case (name, content) => ServerLibraryWrapper(name, content)
  39. }
  40. s"""
  41. $coreJs
  42. ${wrappedContents.mkString("\n")}
  43. $bootstrapLine"""
  44. }
  45. }