component_web_button.go 1.4 KB

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