io.javalin.vue.VueStateRenderer.kt Maven / Gradle / Ivy
package io.javalin.vue
import io.javalin.http.Context
import io.javalin.json.toJsonString
import io.javalin.vue.JavalinVueConfig.Companion.VueConfigKey
import org.eclipse.jetty.util.URIUtil
internal object VueStateRenderer {
fun getState(ctx: Context, state: Any?): String {
val cfg = ctx.appData(VueConfigKey)
fun urlEncodedState(state: Any?): String = ctx.jsonMapper()
.toJsonString(
mapOf(
"pathParams" to ctx.pathParamMap(),
"state" to (state ?: cfg.stateFunction(ctx))
)
)
.urlEncodeForJavascript()
val prototypeOrGlobalConfig = if (cfg.vueInstanceNameInJs != null) "${cfg.vueInstanceNameInJs}.config.globalProperties" else "Vue.prototype"
return """
""".trimIndent()
}
// Unfortunately, Java's URLEncoder#encode is slightly different from JavaScript's encodeURIComponent.
// Luckily, Jetty has a util which works exactly the same.
private fun String.urlEncodeForJavascript() = URIUtil.encodePath(this)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy