From 0d3e7c87bf74948f83ce006a1d340b0f3f5e68b0 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Mon, 8 Oct 2018 13:40:04 +0200 Subject: Refactoring to modules --- src/main/kotlin/pl/treksoft/kvision/KVManager.kt | 203 +----------- .../pl/treksoft/kvision/core/StyledComponent.kt | 8 +- src/main/kotlin/pl/treksoft/kvision/core/Widget.kt | 6 +- src/main/kotlin/pl/treksoft/kvision/form/Form.kt | 18 - .../kotlin/pl/treksoft/kvision/form/FormPanel.kt | 14 - .../pl/treksoft/kvision/form/check/RadioGroup.kt | 3 +- .../pl/treksoft/kvision/form/select/AjaxOptions.kt | 128 -------- .../pl/treksoft/kvision/form/select/Select.kt | 285 ---------------- .../pl/treksoft/kvision/form/select/SelectInput.kt | 363 --------------------- .../treksoft/kvision/form/select/SelectOptGroup.kt | 91 ------ .../treksoft/kvision/form/select/SelectOption.kt | 103 ------ .../pl/treksoft/kvision/form/spinner/Spinner.kt | 263 --------------- .../treksoft/kvision/form/spinner/SpinnerInput.kt | 323 ------------------ .../pl/treksoft/kvision/form/text/RichText.kt | 79 ----- .../pl/treksoft/kvision/form/text/RichTextInput.kt | 133 -------- .../pl/treksoft/kvision/form/time/DateTime.kt | 246 -------------- .../pl/treksoft/kvision/form/time/DateTimeInput.kt | 291 ----------------- .../pl/treksoft/kvision/form/upload/Upload.kt | 333 ------------------- .../pl/treksoft/kvision/form/upload/UploadInput.kt | 327 ------------------- src/main/kotlin/pl/treksoft/kvision/html/Button.kt | 2 +- src/main/kotlin/pl/treksoft/kvision/i18n/I18n.kt | 123 +------ .../kotlin/pl/treksoft/kvision/i18n/I18nManager.kt | 96 ++++++ .../pl/treksoft/kvision/i18n/SimpleI18nManager.kt | 34 ++ src/main/kotlin/pl/treksoft/kvision/types/KDate.kt | 6 +- src/main/resources/css/paper.css | 12 - src/main/resources/js/bootstrap-select-i18n.min.js | 1 - src/main/resources/js/bootstrap.config.js | 64 ---- src/main/resources/js/bootstrap.config.less | 0 .../test/pl/treksoft/kvision/form/FormPanelSpec.kt | 6 +- .../test/pl/treksoft/kvision/form/FormSpec.kt | 10 +- .../kvision/form/select/SelectInputSpec.kt | 53 --- .../kvision/form/select/SelectOptGroupSpec.kt | 54 --- .../kvision/form/select/SelectOptionSpec.kt | 59 ---- .../pl/treksoft/kvision/form/select/SelectSpec.kt | 58 ---- .../kvision/form/spinner/SpinnerInputSpec.kt | 75 ----- .../treksoft/kvision/form/spinner/SpinnerSpec.kt | 82 ----- .../kvision/form/text/RichTextInputSpec.kt | 51 --- .../pl/treksoft/kvision/form/text/RichTextSpec.kt | 58 ---- .../kvision/form/time/DateTimeInputSpec.kt | 53 --- .../pl/treksoft/kvision/form/time/DateTimeSpec.kt | 62 ---- .../kvision/form/upload/UploadInputSpec.kt | 57 ---- .../pl/treksoft/kvision/form/upload/UploadSpec.kt | 56 ---- 42 files changed, 161 insertions(+), 4128 deletions(-) delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/select/SelectOptGroup.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/select/SelectOption.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/spinner/Spinner.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/spinner/SpinnerInput.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/text/RichText.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/text/RichTextInput.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/time/DateTime.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/time/DateTimeInput.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/upload/Upload.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/upload/UploadInput.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/i18n/I18nManager.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/i18n/SimpleI18nManager.kt delete mode 100644 src/main/resources/css/paper.css delete mode 100644 src/main/resources/js/bootstrap-select-i18n.min.js delete mode 100644 src/main/resources/js/bootstrap.config.js delete mode 100644 src/main/resources/js/bootstrap.config.less delete mode 100644 src/test/kotlin/test/pl/treksoft/kvision/form/select/SelectInputSpec.kt delete mode 100644 src/test/kotlin/test/pl/treksoft/kvision/form/select/SelectOptGroupSpec.kt delete mode 100644 src/test/kotlin/test/pl/treksoft/kvision/form/select/SelectOptionSpec.kt delete mode 100644 src/test/kotlin/test/pl/treksoft/kvision/form/select/SelectSpec.kt delete mode 100644 src/test/kotlin/test/pl/treksoft/kvision/form/spinner/SpinnerInputSpec.kt delete mode 100644 src/test/kotlin/test/pl/treksoft/kvision/form/spinner/SpinnerSpec.kt delete mode 100644 src/test/kotlin/test/pl/treksoft/kvision/form/text/RichTextInputSpec.kt delete mode 100644 src/test/kotlin/test/pl/treksoft/kvision/form/text/RichTextSpec.kt delete mode 100644 src/test/kotlin/test/pl/treksoft/kvision/form/time/DateTimeInputSpec.kt delete mode 100644 src/test/kotlin/test/pl/treksoft/kvision/form/time/DateTimeSpec.kt delete mode 100644 src/test/kotlin/test/pl/treksoft/kvision/form/upload/UploadInputSpec.kt delete mode 100644 src/test/kotlin/test/pl/treksoft/kvision/form/upload/UploadSpec.kt (limited to 'src') diff --git a/src/main/kotlin/pl/treksoft/kvision/KVManager.kt b/src/main/kotlin/pl/treksoft/kvision/KVManager.kt index 80279020..de819c98 100644 --- a/src/main/kotlin/pl/treksoft/kvision/KVManager.kt +++ b/src/main/kotlin/pl/treksoft/kvision/KVManager.kt @@ -29,13 +29,9 @@ import com.github.snabbdom.datasetModule import com.github.snabbdom.eventListenersModule import com.github.snabbdom.propsModule import com.github.snabbdom.styleModule -import org.w3c.dom.asList import pl.treksoft.kvision.core.Component -import pl.treksoft.kvision.i18n.I18n import pl.treksoft.kvision.utils.isIE11 -import pl.treksoft.kvision.utils.obj import kotlin.browser.document -import kotlin.browser.window import kotlin.dom.clear /** @@ -49,208 +45,18 @@ external fun require(name: String): dynamic */ @Suppress("EmptyCatchBlock", "TooGenericExceptionCaught", "LargeClass") internal object KVManager { - internal const val AJAX_REQUEST_DELAY = 300 - internal const val KVNULL = "#kvnull" - - private val links = document.getElementsByTagName("link") - private val bootstrapWebpack = try { - val bootswatch = links.asList().find { it.getAttribute("href")?.contains("bootstrap.min.css") ?: false } - if (bootswatch != null) { - if (bootswatch.getAttribute("href")?.contains("/paper/") == true) { - require("./css/paper.css") - } - require("bootstrap-webpack!./js/bootstrap.config.js") - } else { - require("bootstrap-webpack") - } - } catch (e: Throwable) { - } - private val fontAwesomeWebpack = try { - require("font-awesome-webpack") - } catch (e: Throwable) { - } - private val awesomeBootstrapCheckbox = try { - require("awesome-bootstrap-checkbox") - } catch (e: Throwable) { - } - private val bootstrapSelectCss = try { - require("bootstrap-select/dist/css/bootstrap-select.min.css") - } catch (e: Throwable) { - } - private val bootstrapSelect = try { - require("bootstrap-select/dist/js/bootstrap-select.min.js") - require("./js/bootstrap-select-i18n.min.js") - } catch (e: Throwable) { - } - private val bootstrapSelectAjaxCss = try { - require("ajax-bootstrap-select/dist/css/ajax-bootstrap-select.min.css") - } catch (e: Throwable) { - } - private val bootstrapSelectAjax = try { - require("ajax-bootstrap-select/dist/js/ajax-bootstrap-select.min.js") - require("../../js/js/locales/ajax-bootstrap-select/ajax-bootstrap-select.de-DE.min.js") - require("../../js/js/locales/ajax-bootstrap-select/ajax-bootstrap-select.es-ES.min.js") - require("../../js/js/locales/ajax-bootstrap-select/ajax-bootstrap-select.fr-FR.min.js") - require("../../js/js/locales/ajax-bootstrap-select/ajax-bootstrap-select.it-IT.min.js") - require("../../js/js/locales/ajax-bootstrap-select/ajax-bootstrap-select.ja-JP.min.js") - require("../../js/js/locales/ajax-bootstrap-select/ajax-bootstrap-select.ko-KR.min.js") - require("../../js/js/locales/ajax-bootstrap-select/ajax-bootstrap-select.nl-NL.min.js") - require("../../js/js/locales/ajax-bootstrap-select/ajax-bootstrap-select.pl-PL.min.js") - require("../../js/js/locales/ajax-bootstrap-select/ajax-bootstrap-select.pt-BR.min.js") - require("../../js/js/locales/ajax-bootstrap-select/ajax-bootstrap-select.ru-RU.min.js") - require("../../js/js/locales/ajax-bootstrap-select/ajax-bootstrap-select.tr-TR.min.js") - } catch (e: Throwable) { - } - private val trixCss = try { - require("trix/dist/trix.css") - } catch (e: Throwable) { - } - private val trix = try { - val trix = require("trix") - window.asDynamic().Trix = trix - trix.config.languages = obj {} - trix.config.languages["en"] = obj {} - for (key in js("Object").keys(trix.config.lang)) { - trix.config.languages["en"][key] = trix.config.lang[key] - } - val orig = trix.config.toolbar.getDefaultHTML - trix.config.toolbar.getDefaultHTML = { - val config = if (trix.config.languages[I18n.language] != undefined) { - trix.config.languages[I18n.language] - } else { - trix.config.languages["en"] - } - for (key in js("Object").keys(trix.config.lang)) { - trix.config.lang[key] = config[key] - } - orig() - } - require("../../js/js/locales/trix/trix.pl.js") - } catch (e: Throwable) { - } - private val bootstrapDateTimePickerCss = try { - require("bootstrap-datetime-picker/css/bootstrap-datetimepicker.min.css") - } catch (e: Throwable) { - } - private val bootstrapDateTimePicker = try { - require("bootstrap-datetime-picker/js/bootstrap-datetimepicker.min.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.ar.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.az.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.bg.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.bn.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.ca.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.cs.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.da.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.de.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.ee.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.el.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.es.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.fi.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.fr.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.he.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.hr.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.hu.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.hy.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.id.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.is.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.it.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.ja.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.ko.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.lt.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.lv.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.nl.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.no.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.pl.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.pt.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.ro.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.rs.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.ru.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.sk.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.sl.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.sv.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.th.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.tr.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.ua.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.uk.js") - require("../../js/js/locales/bootstrap-datetime-picker/bootstrap-datetimepicker.zh.js") - } catch (e: Throwable) { - } - private val bootstrapTouchspinCss = try { - require("bootstrap-touchspin/dist/jquery.bootstrap-touchspin.min.css") - } catch (e: Throwable) { - } - private val bootstrapTouchspin = try { - require("bootstrap-touchspin/dist/jquery.bootstrap-touchspin.min.js") + private val kvisionBootstrap = try { + require("kvision-bootstrap").pl.treksoft.kvision.KVManagerBootstrap } catch (e: Throwable) { } private val elementResizeEvent = try { require("element-resize-event") } catch (e: Throwable) { } - private val bootstrapFileinputCss = try { - require("bootstrap-fileinput/css/fileinput.min.css") - } catch (e: Throwable) { - } - private val bootstrapFileinputCssFa = try { - require("bootstrap-fileinput/themes/explorer-fa/theme.min.css") - } catch (e: Throwable) { - } - private val bootstrapFileinput = try { - require("bootstrap-fileinput") - require("../../js/js/locales/bootstrap-fileinput/ar.js") - require("../../js/js/locales/bootstrap-fileinput/az.js") - require("../../js/js/locales/bootstrap-fileinput/bg.js") - require("../../js/js/locales/bootstrap-fileinput/ca.js") - require("../../js/js/locales/bootstrap-fileinput/cr.js") - require("../../js/js/locales/bootstrap-fileinput/cs.js") - require("../../js/js/locales/bootstrap-fileinput/da.js") - require("../../js/js/locales/bootstrap-fileinput/de.js") - require("../../js/js/locales/bootstrap-fileinput/el.js") - require("../../js/js/locales/bootstrap-fileinput/es.js") - require("../../js/js/locales/bootstrap-fileinput/et.js") - require("../../js/js/locales/bootstrap-fileinput/fa.js") - require("../../js/js/locales/bootstrap-fileinput/fi.js") - require("../../js/js/locales/bootstrap-fileinput/fr.js") - require("../../js/js/locales/bootstrap-fileinput/gl.js") - require("../../js/js/locales/bootstrap-fileinput/id.js") - require("../../js/js/locales/bootstrap-fileinput/it.js") - require("../../js/js/locales/bootstrap-fileinput/ja.js") - require("../../js/js/locales/bootstrap-fileinput/ka.js") - require("../../js/js/locales/bootstrap-fileinput/ko.js") - require("../../js/js/locales/bootstrap-fileinput/kz.js") - require("../../js/js/locales/bootstrap-fileinput/lt.js") - require("../../js/js/locales/bootstrap-fileinput/nl.js") - require("../../js/js/locales/bootstrap-fileinput/no.js") - require("../../js/js/locales/bootstrap-fileinput/pl.js") - require("../../js/js/locales/bootstrap-fileinput/pt.js") - require("../../js/js/locales/bootstrap-fileinput/ro.js") - require("../../js/js/locales/bootstrap-fileinput/ru.js") - require("../../js/js/locales/bootstrap-fileinput/sk.js") - require("../../js/js/locales/bootstrap-fileinput/sl.js") - require("../../js/js/locales/bootstrap-fileinput/sv.js") - require("../../js/js/locales/bootstrap-fileinput/th.js") - require("../../js/js/locales/bootstrap-fileinput/tr.js") - require("../../js/js/locales/bootstrap-fileinput/uk.js") - require("../../js/js/locales/bootstrap-fileinput/vi.js") - require("../../js/js/locales/bootstrap-fileinput/zh.js") - } catch (e: Throwable) { - } - private val bootstrapFileinputFa = try { - require("bootstrap-fileinput/themes/explorer-fa/theme.min.js") - } catch (e: Throwable) { - } private val resizable = try { require("jquery-resizable-dom") } catch (e: Throwable) { } - private val handlebars = try { - require("handlebars/dist/handlebars.runtime.min.js") - } catch (e: Throwable) { - } - private val jed = try { - require("jed") - } catch (e: Throwable) { - } internal val fecha = require("fecha") private val sdPatch = Snabbdom.init( arrayOf( @@ -259,7 +65,10 @@ internal object KVManager { ) ) private val sdVirtualize = require("snabbdom-virtualize/strings").default - private val styleCss = require("./css/style.css") + private val styleCss = try { + require("./css/style.css") + } catch (e: Throwable) { + } internal fun patch(id: String, vnode: VNode): VNode { val container = document.getElementById(id) diff --git a/src/main/kotlin/pl/treksoft/kvision/core/StyledComponent.kt b/src/main/kotlin/pl/treksoft/kvision/core/StyledComponent.kt index c4ac8295..d54af6f7 100644 --- a/src/main/kotlin/pl/treksoft/kvision/core/StyledComponent.kt +++ b/src/main/kotlin/pl/treksoft/kvision/core/StyledComponent.kt @@ -462,13 +462,13 @@ abstract class StyledComponent : Component { return snstyle } - internal fun refreshOnUpdate(refreshFunction: ((T) -> Unit) = { this.refresh() }) = + protected fun refreshOnUpdate(refreshFunction: ((T) -> Unit) = { this.refresh() }) = RefreshDelegateProvider(null, refreshFunction) - internal fun refreshOnUpdate(initialValue: T, refreshFunction: ((T) -> Unit) = { this.refresh() }) = + protected fun refreshOnUpdate(initialValue: T, refreshFunction: ((T) -> Unit) = { this.refresh() }) = RefreshDelegateProvider(initialValue, refreshFunction) - internal inner class RefreshDelegateProvider( + protected inner class RefreshDelegateProvider( private val initialValue: T?, private val refreshFunction: (T) -> Unit ) { operator fun provideDelegate(thisRef: Any?, prop: KProperty<*>): RefreshDelegate { @@ -477,7 +477,7 @@ abstract class StyledComponent : Component { } } - internal inner class RefreshDelegate(private val refreshFunction: ((T) -> Unit)) { + protected inner class RefreshDelegate(private val refreshFunction: ((T) -> Unit)) { @Suppress("UNCHECKED_CAST") operator fun getValue(thisRef: StyledComponent, property: KProperty<*>): T { val value = propertyValues[property.name] diff --git a/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt b/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt index e7b94cba..52f60813 100644 --- a/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt +++ b/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt @@ -84,9 +84,9 @@ open class Widget(classes: Set = setOf()) : StyledComponent() { */ var draggable: Boolean? by refreshOnUpdate() - internal var surroundingSpan by refreshOnUpdate(false) + protected var surroundingSpan by refreshOnUpdate(false) - internal var eventTarget: Widget? = null + var eventTarget: Widget? = null private var vnode: VNode? = null @@ -602,7 +602,7 @@ open class Widget(classes: Set = setOf()) : StyledComponent() { } } - internal open fun dispatchEvent(type: String, eventInitDict: CustomEventInit): Boolean? { + protected open fun dispatchEvent(type: String, eventInitDict: CustomEventInit): Boolean? { val event = org.w3c.dom.CustomEvent(type, eventInitDict) return this.getElement()?.dispatchEvent(event) } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt index 323d0e1e..b45eed60 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt @@ -26,11 +26,9 @@ import kotlinx.serialization.Mapper import kotlinx.serialization.decode import kotlinx.serialization.json.JSON import kotlinx.serialization.serializer -import pl.treksoft.kvision.form.upload.Upload import pl.treksoft.kvision.i18n.I18n.trans import pl.treksoft.kvision.types.KDate import pl.treksoft.kvision.types.KFile -import pl.treksoft.kvision.utils.getContent import kotlin.js.Json import kotlin.reflect.KProperty1 @@ -254,22 +252,6 @@ class Form(private val panel: FormPanel? = null, private val seriali return modelFactory(map.withDefault { null }) } - /** - * Returns file with the content read. - * @param key key identifier of the control - * @param kFile object identifying the file - * @return KFile object - */ - @Suppress("EXPERIMENTAL_FEATURE_WARNING") - suspend fun getContent( - key: KProperty1?>, - kFile: KFile - ): KFile { - val control = getControl(key) as Upload - val content = control.getNativeFile(kFile)?.getContent() - return kFile.copy(content = content) - } - /** * Returns current data model as JSON. * @return data model as JSON diff --git a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt index a013c076..3811ed82 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt @@ -374,20 +374,6 @@ open class FormPanel( return form.getData() } - /** - * Returns an object with the content of the file. - * @param key key identifier of the control - * @param kFile object identifying the file - * @return KFile object - */ - @Suppress("EXPERIMENTAL_FEATURE_WARNING") - suspend fun getContent( - key: KProperty1?>, - kFile: KFile - ): KFile { - return form.getContent(key, kFile) - } - /** * Returns current data model as JSON. * @return data model as JSON diff --git a/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt b/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt index b4ed127a..fdc1e3af 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt @@ -28,7 +28,6 @@ import pl.treksoft.kvision.form.FieldLabel import pl.treksoft.kvision.form.HelpBlock import pl.treksoft.kvision.form.InputSize import pl.treksoft.kvision.form.StringFormControl -import pl.treksoft.kvision.form.select.Select import pl.treksoft.kvision.panel.SimplePanel /** @@ -99,7 +98,7 @@ open class RadioGroup( setSizeToChildren(value) } - private val idc = "kv_form_radiogroup_" + Select.counter + private val idc = "kv_form_radiogroup_$counter" final override val input = CheckInput() final override val flabel: FieldLabel = FieldLabel(idc, label, rich) final override val validationInfo: HelpBlock = HelpBlock().apply { visible = false } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt deleted file mode 100644 index d802a111..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright (c) 2017-present Robert Jaros - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package pl.treksoft.kvision.form.select - -import pl.treksoft.jquery.JQueryXHR -import pl.treksoft.kvision.KVManager.AJAX_REQUEST_DELAY -import pl.treksoft.kvision.KVManager.KVNULL -import pl.treksoft.kvision.i18n.I18n -import pl.treksoft.kvision.utils.obj - -/** - * HTTP protocol type for the AJAX call. - */ -enum class HttpType(internal val type: String) { - GET("GET"), - POST("POST") -} - -/** - * Data type for the AJAX call. - */ -enum class DataType(internal val type: String) { - JSON("json"), - JSONP("jsonp"), - XML("xml"), - TEXT("text"), - SCRIPT("script") -} - -/** - * Data class for AJAX options. - * - * @constructor - * @param url the url address - * @param preprocessData - * [AjaxBootstrapSelect preprocessOption](https://github.com/truckingsim/Ajax-Bootstrap-Select#optionspreprocessdata) - * option - * @param beforeSend - * [JQuery ajax.beforeSend](http://api.jquery.com/jquery.ajax/#jQuery-ajax-settings) option - * @param data - * [JQuery ajax.data](http://api.jquery.com/jquery.ajax/#jQuery-ajax-settings) option - * @param httpType - * [JQuery ajax.type](http://api.jquery.com/jquery.ajax/#jQuery-ajax-settings) option - * @param minLength - * [AjaxBootstrapSelect minLength](https://github.com/truckingsim/Ajax-Bootstrap-Select#optionsminlength) option - * @param cache - * [AjaxBootstrapSelect cache](https://github.com/truckingsim/Ajax-Bootstrap-Select#optionscache) option - * @param clearOnEmpty - * [AjaxBootstrapSelect clearOnEmpty](https://github.com/truckingsim/Ajax-Bootstrap-Select#optionsclearonempty) option - * @param clearOnError - * [AjaxBootstrapSelect clearOnError](https://github.com/truckingsim/Ajax-Bootstrap-Select#optionsclearonerror) option - * @param emptyRequest - * [AjaxBootstrapSelect emptyRequest](https://github.com/truckingsim/Ajax-Bootstrap-Select#optionsemptyrequest) option - * @param requestDelay - * [AjaxBootstrapSelect requestDelay](https://github.com/truckingsim/Ajax-Bootstrap-Select#optionsrequestdelay) option - * @param restoreOnError - * [AjaxBootstrapSelect restoreOnError](https://github.com/truckingsim/Ajax-Bootstrap-Select#optionsrestoreonerror) - * option - */ -data class AjaxOptions( - val url: String, val preprocessData: (dynamic) -> dynamic, val beforeSend: ((JQueryXHR) -> dynamic)? = null, - val data: dynamic = null, val httpType: HttpType = HttpType.GET, - val dataType: DataType = DataType.JSON, val minLength: Int = 0, - val cache: Boolean = true, val clearOnEmpty: Boolean = true, val clearOnError: Boolean = true, - val emptyRequest: Boolean = false, - val requestDelay: Int = AJAX_REQUEST_DELAY, val restoreOnError: Boolean = false -) - -/** - * Convert AjaxOptions to JavaScript JSON object. - * @param emptyOption add an empty position as the first select option - * @return JSON object - */ -fun AjaxOptions.toJs(emptyOption: Boolean): dynamic { - val procData = { data: dynamic -> - val processedData = this.preprocessData(data) - if (emptyOption) { - val ret = mutableListOf(obj { - this.value = KVNULL - this.text = "" - }) - @Suppress("UnsafeCastFromDynamic") - ret.addAll((processedData as Array).asList()) - ret.toTypedArray() - } else { - processedData - } - } - val language = I18n.language - return obj { - this.ajax = obj { - this.url = url - this.type = httpType.type - this.dataType = dataType.type - this.data = data - this.beforeSend = beforeSend - } - this.preprocessData = procData - this.minLength = minLength - this.cache = cache - this.clearOnEmpty = clearOnEmpty - this.clearOnError = clearOnError - this.emptyRequest = emptyRequest - this.preserveSelected = false - this.requestDelay = requestDelay - this.restoreOnError = restoreOnError - this.langCode = language - } -} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt deleted file mode 100644 index f19081e1..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt +++ /dev/null @@ -1,285 +0,0 @@ -/* - * Copyright (c) 2017-present Robert Jaros - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package pl.treksoft.kvision.form.select - -import pl.treksoft.kvision.core.Component -import pl.treksoft.kvision.core.Container -import pl.treksoft.kvision.core.StringBoolPair -import pl.treksoft.kvision.core.StringPair -import pl.treksoft.kvision.core.Widget -import pl.treksoft.kvision.form.FieldLabel -import pl.treksoft.kvision.form.HelpBlock -import pl.treksoft.kvision.form.StringFormControl -import pl.treksoft.kvision.panel.SimplePanel -import pl.treksoft.kvision.utils.SnOn - -/** - * The form field component for Select control. - * - * The select control can be populated directly from *options* parameter or manually by adding - * [SelectOption] or [SelectOptGroup] components to the container. - * - * @constructor - * @param options an optional list of options (label to value pairs) for the select control - * @param value selected value - * @param name the name attribute of the generated HTML input element - * @param multiple allows multiple value selection (multiple values are comma delimited) - * @param ajaxOptions additional options for remote (AJAX) data source - * @param label label text bound to the input element - * @param rich determines if [label] can contain HTML code - */ -@Suppress("TooManyFunctions") -open class Select( - options: List? = null, value: String? = null, name: String? = null, - multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, label: String? = null, - rich: Boolean = false -) : SimplePanel(setOf("form-group")), StringFormControl { - - /** - * A list of options (label to value pairs) for the select control. - */ - var options - get() = input.options - set(value) { - input.options = value - } - /** - * A value of the selected option. - */ - override var value - get() = input.value - set(value) { - input.value = value - } - /** - * Determines if multiple value selection is allowed. - */ - var multiple - get() = input.multiple - set(value) { - input.multiple = value - } - /** - * Additional options for remote (AJAX) data source. - */ - var ajaxOptions - get() = input.ajaxOptions - set(value) { - input.ajaxOptions = value - } - /** - * Maximal number of selected options. - */ - var maxOptions - get() = input.maxOptions - set(value) { - input.maxOptions = value - } - /** - * Determines if live search is available. - */ - var liveSearch - get() = input.liveSearch - set(value) { - input.liveSearch = value - } - /** - * The placeholder for the select control. - */ - var placeholder - get() = input.placeholder - set(value) { - input.placeholder = value - } - /** - * The style of the select control. - */ - var style - get() = input.style - set(value) { - input.style = value - } - /** - * The width of the select control. - */ - var selectWidth - get() = input.selectWidth - set(value) { - input.selectWidth = value - } - /** - * The width type of the select control. - */ - var selectWidthType - get() = input.selectWidthType - set(value) { - input.selectWidthType = value - } - /** - * Determines if an empty option is automatically generated. - */ - var emptyOption - get() = input.emptyOption - set(value) { - input.emptyOption = value - } - /** - * Determines if the select is automatically focused. - */ - var autofocus - get() = input.autofocus - set(value) { - input.autofocus = value - } - /** - * The label text bound to the select element. - */ - var label - get() = flabel.content - set(value) { - flabel.content = value - } - /** - * Determines if [label] can contain HTML code. - */ - var rich - get() = flabel.rich - set(value) { - flabel.rich = value - } - - private val idc = "kv_form_select_$counter" - final override val input: SelectInput = SelectInput( - options, value, multiple, ajaxOptions, - setOf("form-control") - ).apply { - this.id = idc - this.name = name - } - final override val flabel: FieldLabel = FieldLabel(idc, label, rich) - final override val validationInfo: HelpBlock = HelpBlock().apply { visible = false } - - init { - @Suppress("LeakingThis") - input.eventTarget = this - this.addInternal(flabel) - this.addInternal(input) - this.addInternal(validationInfo) - counter++ - } - - override fun getSnClass(): List { - val cl = super.getSnClass().toMutableList() - if (validatorError != null) { - cl.add("has-error" to true) - } - return cl - } - - @Suppress("UNCHECKED_CAST") - override fun setEventListener(block: SnOn.() -> Unit): Widget { - input.setEventListener(block) - return this - } - - override fun setEventListener(block: SnOn.() -> Unit): Widget { - input.setEventListener(block) - return this - } - - override fun removeEventListeners(): Widget { - input.removeEventListeners() - return this - } - - override fun add(child: Component): SimplePanel { - input.add(child) - return this - } - - override fun addAll(children: List): SimplePanel { - input.addAll(children) - return this - } - - override fun remove(child: Component): SimplePanel { - input.remove(child) - return this - } - - override fun removeAll(): SimplePanel { - input.removeAll() - return this - } - - override fun getChildren(): List { - return input.getChildren() - } - - /** - * Opens dropdown with options. - */ - open fun showOptions() { - input.showOptions() - } - - /** - * Hides dropdown with options. - */ - open fun hideOptions() { - input.hideOptions() - } - - /** - * Toggles visibility of dropdown with options. - */ - open fun toggleOptions() { - input.toggleOptions() - } - - override fun focus() { - input.focus() - } - - override fun blur() { - input.blur() - } - - companion object { - internal var counter = 0 - - /** - * DSL builder extension function. - * - * It takes the same parameters as the constructor of the built component. - */ - fun Container.select( - options: List? = null, value: String? = null, name: String? = null, - multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, label: String? = null, - rich: Boolean = false, init: (Select.() -> Unit)? = null - ): Select { - val select = Select(options, value, name, multiple, ajaxOptions, label, rich).apply { init?.invoke(this) } - this.add(select) - return select - } - } -} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt deleted file mode 100644 index 6d6f0fc5..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt +++ /dev/null @@ -1,363 +0,0 @@ -/* - * Copyright (c) 2017-present Robert Jaros - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package pl.treksoft.kvision.form.select - -import com.github.snabbdom.VNode -import pl.treksoft.kvision.KVManager.KVNULL -import pl.treksoft.kvision.core.Component -import pl.treksoft.kvision.core.Container -import pl.treksoft.kvision.core.CssSize -import pl.treksoft.kvision.core.StringBoolPair -import pl.treksoft.kvision.core.StringPair -import pl.treksoft.kvision.form.FormInput -import pl.treksoft.kvision.form.InputSize -import pl.treksoft.kvision.html.ButtonStyle -import pl.treksoft.kvision.panel.SimplePanel -import pl.treksoft.kvision.utils.asString -import pl.treksoft.kvision.utils.obj - -/** - * Select width types. See [Bootstrap Select width](http://silviomoreto.github.io/bootstrap-select/examples/#width). - */ -enum class SelectWidthType(internal val value: String) { - AUTO("auto"), - FIT("fit") -} - -/** - * The basic component for Select control. - * - * The select control can be populated directly from *options* parameter or manually by adding - * [SelectOption] or [SelectOptGroup] components to the container. - * - * @constructor - * @param options an optional list of options (label to value pairs) for the select control - * @param value selected value - * @param multiple allows multiple value selection (multiple values are comma delimited) - * @param ajaxOptions additional options for remote (AJAX) data source - * @param classes a set of CSS class names - */ -@Suppress("TooManyFunctions") -open class SelectInput( - options: List? = null, value: String? = null, - multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, - classes: Set = setOf() -) : SimplePanel(classes), FormInput { - - /** - * A list of options (label to value pairs) for the select control. - */ - internal var options by refreshOnUpdate(options, { setChildrenFromOptions() }) - /** - * A value of the selected option. - */ - var value by refreshOnUpdate(value, { refreshState() }) - /** - * The name attribute of the generated HTML select element. - */ - override var name: String? by refreshOnUpdate() - /** - * Determines if multiple value selection is allowed. - */ - var multiple by refreshOnUpdate(multiple) - /** - * Additional options for remote (AJAX) data source. - */ - var ajaxOptions by refreshOnUpdate(ajaxOptions, { - if (it != null) { - liveSearch = true - } - refresh() - }) - /** - * Maximal number of selected options. - */ - var maxOptions: Int? by refreshOnUpdate() - /** - * Determines if live search is available. - */ - var liveSearch by refreshOnUpdate(false) - /** - * The placeholder for the select control. - */ - var placeholder: String? by refreshOnUpdate() - /** - * The style of the select control. - */ - var style: ButtonStyle? by refreshOnUpdate() - /** - * The width of the select control. - */ - var selectWidth: CssSize? by refreshOnUpdate() - /** - * The width type of the select control. - */ - var selectWidthType: SelectWidthType? by refreshOnUpdate() - /** - * Determines if an empty option is automatically generated. - */ - var emptyOption by refreshOnUpdate(false, { setChildrenFromOptions() }) - /** - * Determines if the field is disabled. - */ - override var disabled by refreshOnUpdate(false) - /** - * Determines if the select is automatically focused. - */ - var autofocus: Boolean? by refreshOnUpdate() - /** - * The size of the input. - */ - override var size: InputSize? by refreshOnUpdate() - - init { - setChildrenFromOptions() - this.setInternalEventListener { - change = { - val v = getElementJQuery()?.`val`() - self.value = v?.let { - calculateValue(it) - } - } - } - } - - private fun calculateValue(v: Any): String? { - return if (this.multiple) { - @Suppress("UNCHECKED_CAST") - val arr = v as? Array - if (arr != null && arr.isNotEmpty()) { - arr.joinToString() - } else { - null - } - } else { - val vs = v as String - if (KVNULL == vs || vs.isEmpty()) { - null - } else { - vs - } - } - } - - override fun render(): VNode { - return render("select", childrenVNodes()) - } - - override fun add(child: Component): SimplePanel { - super.add(child) - refreshSelectInput() - return this - } - - override fun addAll(children: List): SimplePanel { - super.addAll(children) - refreshSelectInput() - return this - } - - override fun remove(child: Component): SimplePanel { - super.remove(child) - refreshSelectInput() - return this - } - - override fun removeAll(): SimplePanel { - super.removeAll() - refreshSelectInput() - return this - } - - private fun setChildrenFromOptions() { - if (ajaxOptions == null) { - super.removeAll() - if (emptyOption) { - super.add(SelectOption(KVNULL, "")) - } - options?.let { - val c = it.map { - SelectOption(it.first, it.second) - } - super.addAll(c) - } - } - this.refreshSelectInput() - } - - /** - * Opens dropdown with options. - */ - open fun showOptions() { - getElementJQueryD()?.selectpicker("show") - } - - /** - * Hides dropdown with options. - */ - open fun hideOptions() { - getElementJQueryD()?.selectpicker("hide") - } - - /** - * Toggles visibility of dropdown with options. - */ - open fun toggleOptions() { - getElementJQueryD()?.selectpicker("toggle") - } - - override fun getSnClass(): List { - val cl = super.getSnClass().toMutableList() - cl.add("selectpicker" to true) - size?.let { - cl.add(it.className to true) - } - return cl - } - - private fun refreshSelectInput() { - getElementJQueryD()?.selectpicker("refresh") - refreshState() - } - - @Suppress("ComplexMethod") - override fun getSnAttrs(): List { - val sn = super.getSnAttrs().toMutableList() - name?.let { - sn.add("name" to it) - } - if (multiple) { - sn.add("multiple" to "multiple") - } - maxOptions?.let { - sn.add("data-max-options" to "" + it) - } - if (liveSearch) { - sn.add("data-live-search" to "true") - } - placeholder?.let { - sn.add("title" to translate(it)) - } - autofocus?.let { - if (it) { - sn.add("autofocus" to "autofocus") - } - } - if (disabled) { - sn.add("disabled" to "disabled") - } - val btnStyle = style?.className ?: "btn-default" - when (size) { - InputSize.LARGE -> { - sn.add("data-style" to "$btnStyle btn-lg") - } - InputSize.SMALL -> { - sn.add("data-style" to "$btnStyle btn-sm") - } - else -> { - sn.add("data-style" to btnStyle) - } - } - selectWidthType?.let { - sn.add("data-width" to it.value) - } ?: selectWidth?.let { - sn.add("data-width" to it.asString()) - } - return sn - } - - @Suppress("UnsafeCastFromDynamic") - override fun afterInsert(node: VNode) { - ajaxOptions?.let { - getElementJQueryD()?.selectpicker("render").ajaxSelectPicker(it.toJs(emptyOption)) - } ?: getElementJQueryD()?.selectpicker("render") - - this.getElementJQuery()?.on("show.bs.select", { e, _ -> - this.dispatchEvent("showBsSelect", obj { detail = e }) - }) - this.getElementJQuery()?.on("shown.bs.select", { e, _ -> - this.dispatchEvent("shownBsSelect", obj { detail = e }) - }) - this.getElementJQuery()?.on("hide.bs.select", { e, _ -> - this.dispatchEvent("hideBsSelect", obj { detail = e }) - }) - this.getElementJQuery()?.on("hidden.bs.select", { e, _ -> - this.dispatchEvent("hiddenBsSelect", obj { detail = e }) - }) - this.getElementJQuery()?.on("loaded.bs.select", { e, _ -> - this.dispatchEvent("loadedBsSelect", obj { detail = e }) - }) - this.getElementJQuery()?.on("rendered.bs.select", { e, _ -> - this.dispatchEvent("renderedBsSelect", obj { detail = e }) - }) - this.getElementJQuery()?.on("refreshed.bs.select", { e, _ -> - this.dispatchEvent("refreshedBsSelect", obj { detail = e }) - }) - this.getElementJQueryD()?.on("changed.bs.select", { e, cIndex: Int -> - e["clickedIndex"] = cIndex - this.dispatchEvent("changedBsSelect", obj { detail = e }) - }) - refreshState() - } - - @Suppress("UnsafeCastFromDynamic") - private fun refreshState() { - value?.let { - if (multiple) { - getElementJQueryD()?.selectpicker("val", it.split(",").toTypedArray()) - } else { - getElementJQueryD()?.selectpicker("val", it) - } - } ?: getElementJQueryD()?.selectpicker("val", null) - } - - /** - * Makes the input element focused. - */ - open fun focus() { - getElementJQuery()?.focus() - } - - /** - * Makes the input element blur. - */ - open fun blur() { - getElementJQuery()?.blur() - } - - companion object { - /** - * DSL builder extension function. - * - * It takes the same parameters as the constructor of the built component. - */ - fun Container.selectInput( - options: List? = null, value: String? = null, - multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, - classes: Set = setOf(), init: (SelectInput.() -> Unit)? = null - ): SelectInput { - val selectInput = SelectInput(options, value, multiple, ajaxOptions, classes).apply { init?.invoke(this) } - this.add(selectInput) - return selectInput - } - } -} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/select/SelectOptGroup.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/SelectOptGroup.kt deleted file mode 100644 index e33b3457..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/select/SelectOptGroup.kt +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (c) 2017-present Robert Jaros - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package pl.treksoft.kvision.form.select - -import com.github.snabbdom.VNode -import pl.treksoft.kvision.core.StringPair -import pl.treksoft.kvision.panel.SimplePanel - -/** - * The helper container for adding option groups to [Select]. - * - * The option group can be populated directly from *options* parameter or manually by adding - * [SelectOption] components to the container. - * - * @constructor - * @param label the label of the group - * @param options an optional list of options (label to value pairs) for the group - * @param maxOptions maximal number of selected options in the group - * @param disabled renders a disabled group - * @param classes a set of CSS class names - */ -open class SelectOptGroup( - label: String, options: List? = null, maxOptions: Int? = null, - disabled: Boolean = false, classes: Set = setOf() -) : SimplePanel(classes) { - /** - * A label for the group. - */ - var label by refreshOnUpdate(label) - /** - * A list of options (label to value pairs) for the group. - */ - var options by refreshOnUpdate(options, { setChildrenFromOptions() }) - /** - * Maximal number of selected options in the group. - */ - var maxOptions by refreshOnUpdate(maxOptions) - /** - * Determines if the group is disabled. - */ - var disabled by refreshOnUpdate(disabled) - - init { - setChildrenFromOptions() - } - - override fun render(): VNode { - return render("optgroup", childrenVNodes()) - } - - private fun setChildrenFromOptions() { - this.removeAll() - options?.let { - val c = it.map { - SelectOption(it.first, it.second) - } - this.addAll(c) - } - } - - override fun getSnAttrs(): List { - val sn = super.getSnAttrs().toMutableList() - sn.add("label" to translate(label)) - maxOptions?.let { - sn.add("data-max-options" to "" + it) - } - if (disabled) { - sn.add("disabled" to "disabled") - } - return sn - } -} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/select/SelectOption.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/SelectOption.kt deleted file mode 100644 index d1bb636e..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/select/SelectOption.kt +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (c) 2017-present Robert Jaros - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package pl.treksoft.kvision.form.select - -import com.github.snabbdom.VNode -import pl.treksoft.kvision.core.StringPair -import pl.treksoft.kvision.core.Widget - -/** - * The helper component for adding options to [Select] or [SelectOptGroup]. - * - * @constructor - * @param value the value of the option - * @param label the label of the option - * @param subtext the small subtext after the label of the option - * @param icon the icon before the label of the option - * @param divider renders this option as a divider - * @param disabled renders a disabled option - * @param classes a set of CSS class names - */ -open class SelectOption( - value: String? = null, label: String? = null, subtext: String? = null, icon: String? = null, - divider: Boolean = false, disabled: Boolean = false, - classes: Set = setOf() -) : Widget(classes) { - - /** - * The value of the option. - */ - var value by refreshOnUpdate(value) - /** - * The label of the option. - */ - var label by refreshOnUpdate(label) - /** - * The subtext after the label of the option. - */ - var subtext by refreshOnUpdate(subtext) - /** - * The icon before the label of the option. - */ - var icon by refreshOnUpdate(icon) - /** - * Determines if the option should be rendered as divider. - */ - var divider by refreshOnUpdate(divider) - /** - * Determines if the option should be disabled. - */ - var disabled by refreshOnUpdate(disabled) - - override fun render(): VNode { - return if (!divider) { - render("option", arrayOf(translate(label) ?: value)) - } else { - render("option") - } - } - - override fun getSnAttrs(): List { - val sn = super.getSnAttrs().toMutableList() - if (!divider) { - value?.let { - sn.add("value" to it) - } - subtext?.let { - sn.add("data-subtext" to translate(it)) - } - icon?.let { - if (it.startsWith("fa-")) { - sn.add("data-icon" to "fa $it") - } else { - sn.add("data-icon" to "glyphicon-$it") - } - } - if (disabled) { - sn.add("disabled" to "disabled") - } - } else { - sn.add("data-divider" to "true") - } - return sn - } -} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/spinner/Spinner.kt b/src/main/kotlin/pl/treksoft/kvision/form/spinner/Spinner.kt deleted file mode 100644 index 4fa68e47..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/spinner/Spinner.kt +++ /dev/null @@ -1,263 +0,0 @@ -/* - * Copyright (c) 2017-present Robert Jaros - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package pl.treksoft.kvision.form.spinner - -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.HelpBlock -import pl.treksoft.kvision.form.NumberFormControl -import pl.treksoft.kvision.panel.SimplePanel -import pl.treksoft.kvision.utils.SnOn - -/** - * The form field component for spinner control. - * - * @constructor - * @param value spinner value - * @param name the name attribute of the generated HTML input element - * @param min minimal value (default 0) - * @param max maximal value (default 100) - * @param step step value (default 1) - * @param decimals number of decimal digits (default 0) - * @param buttonsType spinner buttons type - * @param forceType spinner force rounding type - * @param label label text bound to the input element - * @param rich determines if [label] can contain HTML code - */ -open class Spinner( - value: Number? = null, name: String? = null, min: Int = 0, max: Int = DEFAULT_MAX, step: Double = DEFAULT_STEP, - decimals: Int = 0, buttonsType: ButtonsType = ButtonsType.VERTICAL, - forceType: ForceType = ForceType.NONE, label: String? = null, - rich: Boolean = false -) : SimplePanel(setOf("form-group")), NumberFormControl { - - /** - * Spinner value. - */ - override var value - get() = input.value - set(value) { - input.value = value - } - /** - * The value attribute of the generated HTML input element. - * - * This value is placed directly in generated HTML code, while the [value] property is dynamically - * bound to the spinner input value. - */ - var startValue - get() = input.startValue - set(value) { - input.startValue = value - } - /** - * Minimal value. - */ - var min - get() = input.min - set(value) { - input.min = value - } - /** - * Maximal value. - */ - var max - get() = input.max - set(value) { - input.max = value - } - /** - * Step value. - */ - var step - get() = input.step - set(value) { - input.step = value - } - /** - * Number of decimal digits value. - */ - var decimals - get() = input.decimals - set(value) { - input.decimals = value - } - /** - * Spinner buttons type. - */ - var buttonsType - get() = input.buttonsType - set(value) { - input.buttonsType = value - } - /** - * Spinner force rounding type. - */ - var forceType - get() = input.forceType - set(value) { - input.forceType = value - } - /** - * The placeholder for the spinner input. - */ - var placeholder - get() = input.placeholder - set(value) { - input.placeholder = value - } - /** - * Determines if the spinner is automatically focused. - */ - var autofocus - get() = input.autofocus - set(value) { - input.autofocus = value - } - /** - * Determines if the spinner is read-only. - */ - var readonly - get() = input.readonly - set(value) { - input.readonly = value - } - /** - * The label text bound to the spinner input element. - */ - var label - get() = flabel.content - set(value) { - flabel.content = value - } - /** - * Determines if [label] can contain HTML code. - */ - var rich - get() = flabel.rich - set(value) { - flabel.rich = value - } - - protected val idc = "kv_form_spinner_$counter" - final override val input: SpinnerInput = SpinnerInput(value, min, max, step, decimals, buttonsType, forceType) - .apply { - this.id = idc - this.name = name - } - final override val flabel: FieldLabel = FieldLabel(idc, label, rich) - final override val validationInfo: HelpBlock = HelpBlock().apply { visible = false } - - init { - @Suppress("LeakingThis") - input.eventTarget = this - this.addInternal(flabel) - this.addInternal(input) - this.addInternal(validationInfo) - counter++ - } - - override fun getSnClass(): List { - val cl = super.getSnClass().toMutableList() - if (validatorError != null) { - cl.add("has-error" to true) - } - return cl - } - - @Suppress("UNCHECKED_CAST") - override fun setEventListener(block: SnOn.() -> Unit): Widget { - input.setEventListener(block) - return this - } - - override fun setEventListener(block: SnOn.() -> Unit): Widget { - input.setEventListener(block) - return this - } - - override fun removeEventListeners(): Widget { - input.removeEventListeners() - return this - } - - override fun getValueAsString(): String? { - return input.getValueAsString() - } - - /** - * Change value in plus. - */ -