@@ -325,7 +325,8 @@ fun ElementCreator<Element>.meta(
325
325
}
326
326
}
327
327
328
- open class InputElement (override val element : Element ) : ValueElement(element) {
328
+ open class InputElement (override val element : Element , initialValue : String? = null ) :
329
+ ValueElement (element, initialValue = initialValue) {
329
330
fun select () = element.callJsFunction(" document.getElementById({}).select();" , id.json)
330
331
331
332
fun setSelectionRange (start : Int , end : Int ) = element.callJsFunction(
@@ -336,7 +337,8 @@ open class InputElement(override val element: Element) : ValueElement(element) {
336
337
id.json, ro.json)
337
338
338
339
fun checked (initialValue : Boolean = false) : KVar <Boolean > {
339
- val kv = bind(accessor = { " document.getElementById(\" $it \" ).checked" }, updateOnEvent = " change" , initialValue = JsonPrimitive (initialValue))
340
+ val kv = bind(accessor = { " document.getElementById(\" $it \" ).checked" }, updateOnEvent = " change" ,
341
+ initialValue = JsonPrimitive (initialValue))
340
342
return kv.map(object : ReversibleFunction <JsonElement , Boolean >(" " ) {
341
343
override fun invoke (from : JsonElement ) = from.jsonPrimitive.boolean
342
344
@@ -365,7 +367,7 @@ fun ElementCreator<Element>.input(
365
367
.set(" value" , JsonPrimitive (initialValue))
366
368
.set(" placeholder" , JsonPrimitive (placeholder))
367
369
.set(" size" , JsonPrimitive (size))
368
- )
370
+ ), initialValue = initialValue
369
371
).also {
370
372
if (new != null ) new(ElementCreator (parent = it, insertBefore = null ))
371
373
}
@@ -390,7 +392,8 @@ fun ElementCreator<Element>.textArea(
390
392
/* *
391
393
* [<SELECT>](https://www.w3schools.com/tags/tag_select.asp)
392
394
*/
393
- class SelectElement (parent : Element ) : ValueElement(parent, kvarUpdateEvent = " change" )
395
+ class SelectElement (parent : Element , initialValue : String? = null ) :
396
+ ValueElement (parent, kvarUpdateEvent = " change" , initialValue = initialValue)
394
397
395
398
/* *
396
399
* [<SELECT>](https://www.w3schools.com/tags/tag_select.asp)
@@ -430,7 +433,7 @@ private fun select_sample() {
430
433
/* *
431
434
* https://www.w3schools.com/tags/tag_textarea.asp
432
435
*/
433
- open class TextAreaElement (parent : Element ) : ValueElement(parent) {
436
+ open class TextAreaElement (parent : Element , initialValue : String? = null ) : ValueElement(parent, initialValue = initialValue ) {
434
437
// TODO ValueElement already provides a way to get the value of an element. I'm not sure why this function is here.
435
438
// But, something needs to be done with it.
436
439
override val read get() = TextAreaElementReader (this )
@@ -476,7 +479,8 @@ fun ElementCreator<Element>.label(
476
479
*
477
480
* @param kvarUpdateEvent The [value] of this element will update on this event, defaults to [input](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event)
478
481
*/
479
- abstract class ValueElement (open val element : Element , val kvarUpdateEvent : String = " input" ) : Element(element) {
482
+ abstract class ValueElement (open val element : Element , val kvarUpdateEvent : String = " input" ,
483
+ val initialValue : String? = null ) : Element(element) {
480
484
val valueJsExpression : String by lazy { " document.getElementById(\" $id \" ).value" }
481
485
482
486
suspend fun getValue ():String = element.
@@ -514,14 +518,14 @@ abstract class ValueElement(open val element: Element, val kvarUpdateEvent: Stri
514
518
get() {
515
519
synchronized(this ) {
516
520
if (_valueKvar == null ) {
517
- value = KVar (" " )
521
+ value = KVar (initialValue ? : " " )
518
522
}
519
523
}
520
524
return _valueKvar !!
521
525
}
522
526
set(v) {
523
527
if (_valueKvar != null ) error(" `value` may only be set once, and cannot be set after it has been retrieved" )
524
- setValue (v, updateOn = kvarUpdateEvent)
528
+ updateKVar (v, updateOn = kvarUpdateEvent)
525
529
_valueKvar = v
526
530
}
527
531
@@ -534,7 +538,8 @@ abstract class ValueElement(open val element: Element, val kvarUpdateEvent: Stri
534
538
data class DiffData (val prefixEndIndex : Int , val postfixOffset : Int , val diffString : String )
535
539
536
540
private fun applyDiff (oldString : String , diffData : DiffData ) : String {
537
- return when {
541
+
542
+ val newString = when {
538
543
diffData.postfixOffset == - 1 -> {// these 2 edge cases prevent the prefix or the postfix from being
539
544
// repeated when you append text to the beginning of the text or the end of the text
540
545
oldString.substring(0 , diffData.prefixEndIndex) + diffData.diffString
@@ -547,11 +552,10 @@ abstract class ValueElement(open val element: Element, val kvarUpdateEvent: Stri
547
552
oldString.substring(oldString.length - diffData.postfixOffset)
548
553
}
549
554
}
555
+ return newString
550
556
}
551
557
552
- fun setValue (toBind : KVar <String >, updateOn : String = "input") {
553
- setValue(toBind as KVal <String >)
554
-
558
+ private fun updateKVar (toBind : KVar <String >, updateOn : String = "input") {
555
559
on(
556
560
// language=JavaScript
557
561
retrieveJs = " get_diff_changes(document.getElementById(\" ${element.id} \" ))" )
0 commit comments