From 5b9535e9964816eb228d3797f4c8a3e7676d1f53 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Wed, 25 Apr 2018 08:12:25 +0200 Subject: Multiplatform kvision-common and kvision-server modules. Support for automatic remote bindings (work in progress). --- .../pl/treksoft/kvision/form/upload/Upload.kt | 15 +++++++++++-- .../pl/treksoft/kvision/form/upload/UploadInput.kt | 25 ++++++++++++++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) (limited to 'src/main/kotlin/pl/treksoft/kvision/form/upload') diff --git a/src/main/kotlin/pl/treksoft/kvision/form/upload/Upload.kt b/src/main/kotlin/pl/treksoft/kvision/form/upload/Upload.kt index e6b397eb..5e3ac0df 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/upload/Upload.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/upload/Upload.kt @@ -3,13 +3,15 @@ */ package pl.treksoft.kvision.form.upload +import org.w3c.files.File import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.StringBoolPair import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.form.FieldLabel -import pl.treksoft.kvision.form.FilesFormControl import pl.treksoft.kvision.form.HelpBlock +import pl.treksoft.kvision.form.KFilesFormControl import pl.treksoft.kvision.panel.SimplePanel +import pl.treksoft.kvision.types.KFile import pl.treksoft.kvision.utils.SnOn /** @@ -25,7 +27,7 @@ import pl.treksoft.kvision.utils.SnOn open class Upload( uploadUrl: String? = null, multiple: Boolean = false, label: String? = null, rich: Boolean = false -) : SimplePanel(setOf("form-group")), FilesFormControl { +) : SimplePanel(setOf("form-group")), KFilesFormControl { /** * File input value. @@ -227,6 +229,15 @@ open class Upload( return input.getValueAsString() } + /** + * Returns the native JavaScript File object. + * @param kFile KFile object + * @return File object + */ + fun getNativeFile(kFile: KFile): File? { + return input.getNativeFile(kFile) + } + /** * Resets the file input control. */ diff --git a/src/main/kotlin/pl/treksoft/kvision/form/upload/UploadInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/upload/UploadInput.kt index d77a9b9b..645cc69b 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/upload/UploadInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/upload/UploadInput.kt @@ -11,6 +11,7 @@ import pl.treksoft.kvision.core.StringPair import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.form.FormInput import pl.treksoft.kvision.form.InputSize +import pl.treksoft.kvision.types.KFile import pl.treksoft.kvision.utils.obj /** @@ -28,7 +29,7 @@ open class UploadInput(uploadUrl: String? = null, multiple: Boolean = false, cla /** * File input value. */ - var value: List? + var value: List? get() = getValue() set(value) { if (value == null) resetInput() @@ -112,6 +113,8 @@ open class UploadInput(uploadUrl: String? = null, multiple: Boolean = false, cla */ override var size: InputSize? by refreshOnUpdate() + private val nativeFiles: MutableMap = mutableMapOf() + override fun render(): VNode { return render("input") } @@ -139,7 +142,7 @@ open class UploadInput(uploadUrl: String? = null, multiple: Boolean = false, cla return sn } - private fun getValue(): List? { + private fun getValue(): List? { val v = getFiles() return if (v.isNotEmpty()) v else null } @@ -217,8 +220,22 @@ open class UploadInput(uploadUrl: String? = null, multiple: Boolean = false, cla getElementJQueryD()?.fileinput("unlock") } - private fun getFiles(): List { - return (getElementJQueryD()?.fileinput("getFileStack") as Array).toList() + /** + * Returns the native JavaScript File object. + * @param kFile KFile object + * @return File object + */ + fun getNativeFile(kFile: KFile): File? { + return nativeFiles[kFile] + } + + private fun getFiles(): List { + nativeFiles.clear() + return (getElementJQueryD()?.fileinput("getFileStack") as Array).toList().map { + val kFile = KFile(it.name, it.size, null) + nativeFiles[kFile] = it + kFile + } } /** -- cgit