From d964f6beac4cbde710e684cfaa13667eb8dfeb91 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sat, 19 Jan 2019 00:22:09 +0100 Subject: New method for easy access to the content of selected files. --- src/main/kotlin/pl/treksoft/kvision/form/Form.kt | 19 +++++++++++++++++++ .../kotlin/pl/treksoft/kvision/form/FormControl.kt | 8 ++++++++ src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt | 8 ++++++++ 3 files changed, 35 insertions(+) (limited to 'src/main/kotlin/pl/treksoft/kvision') diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt index e2f5d730..983b83e5 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt @@ -32,6 +32,7 @@ import pl.treksoft.kvision.types.DateSerializer import pl.treksoft.kvision.types.KFile import pl.treksoft.kvision.types.toStringF import pl.treksoft.kvision.utils.JSON +import pl.treksoft.kvision.utils.getContent import kotlin.js.Date import kotlin.js.Json import kotlin.reflect.KProperty1 @@ -278,6 +279,24 @@ class Form(private val panel: FormPanel? = null, private val seriali return modelFactory(map.withDefault { null }) } + /** + * Returns current data model with file content read for all KFiles controls. + * @return data model + */ + suspend fun getDataWithFileContent(): K { + val map = fields.entries.associateBy({ it.key }, { + val value = it.value + if (value is KFilesFormControl) { + value.getValue()?.map { + it.copy(content = value.getNativeFile(it)?.getContent()) + } + } else { + value.getValue() + } + }) + return modelFactory(map.withDefault { null }) + } + /** * Returns current data model as JSON. * @return data model as JSON diff --git a/src/main/kotlin/pl/treksoft/kvision/form/FormControl.kt b/src/main/kotlin/pl/treksoft/kvision/form/FormControl.kt index 3af41690..57ce88eb 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/FormControl.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/FormControl.kt @@ -21,6 +21,7 @@ */ package pl.treksoft.kvision.form +import org.w3c.files.File import pl.treksoft.kvision.core.Component import pl.treksoft.kvision.types.KFile import kotlin.js.Date @@ -219,4 +220,11 @@ interface KFilesFormControl : FormControl { } override fun getValueAsString(): String? = value?.joinToString(",") { it.name } + + /** + * Returns the native JavaScript File object. + * @param kFile KFile object + * @return File object + */ + fun getNativeFile(kFile: KFile): File? } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt index c9b33572..10b784e0 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt @@ -375,6 +375,14 @@ open class FormPanel( return form.getData() } + /** + * Returns current data model with file content read for all KFiles controls. + * @return data model + */ + suspend fun getDataWithFileContent(): K { + return form.getDataWithFileContent() + } + /** * Returns current data model as JSON. * @return data model as JSON -- cgit