component_web_text.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //go:build js && wasm
  2. package ultraviolet
  3. import (
  4. "fmt"
  5. "syscall/js"
  6. )
  7. type TextComponentWeb struct {
  8. node js.Value
  9. view View
  10. format string
  11. values []Value
  12. }
  13. func NewTextComponentWeb(view View) *TextComponentWeb {
  14. return &TextComponentWeb{
  15. view: view,
  16. values: []Value{},
  17. }
  18. }
  19. // Component
  20. func (this *TextComponentWeb) Build(parent Component) {
  21. values := []any{}
  22. for _, text := range this.values {
  23. if text.Value() == nil {
  24. values = append(values, "")
  25. } else {
  26. values = append(values, text.Value().(string))
  27. }
  28. }
  29. this.node = WebBuildDiv(
  30. this.node,
  31. parent,
  32. this.view,
  33. "",
  34. fmt.Sprintf(
  35. this.format,
  36. values...,
  37. ),
  38. AbstractHandlers{},
  39. )
  40. }
  41. func (this *TextComponentWeb) Invalidate() bool {
  42. for _, value := range this.values {
  43. if !value.Invalidate() {
  44. continue
  45. }
  46. return true
  47. }
  48. return false
  49. }
  50. func (this *TextComponentWeb) Clean() {
  51. WebCleanChildren(this.node)
  52. }
  53. // JS
  54. func (this *TextComponentWeb) Node() js.Value {
  55. return this.node
  56. }
  57. // ComponentText
  58. func (this *TextComponentWeb) SetValue(format string, values []Value) {
  59. this.format = format
  60. this.values = values
  61. }