condition.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package app
  2. import (
  3. "context"
  4. "io"
  5. "github.com/maxence-charriere/go-app/v9/pkg/errors"
  6. )
  7. // Condition represents a control structure that displays nodes depending on a
  8. // given expression.
  9. type Condition interface {
  10. UI
  11. // ElseIf sets the condition with the given nodes if previous expressions
  12. // were not met and given expression is true.
  13. ElseIf(expr bool, elems ...UI) Condition
  14. // Else sets the condition with the given UI elements if previous
  15. // expressions were not met.
  16. Else(elems ...UI) Condition
  17. }
  18. // If returns a condition that filters the given elements according to the given
  19. // expression.
  20. func If(expr bool, elems ...UI) Condition {
  21. if !expr {
  22. elems = nil
  23. }
  24. return condition{
  25. body: FilterUIElems(elems...),
  26. satisfied: expr,
  27. }
  28. }
  29. type condition struct {
  30. body []UI
  31. satisfied bool
  32. }
  33. func (c condition) ElseIf(expr bool, elems ...UI) Condition {
  34. if c.satisfied {
  35. return c
  36. }
  37. if expr {
  38. c.body = FilterUIElems(elems...)
  39. c.satisfied = expr
  40. }
  41. return c
  42. }
  43. func (c condition) Else(elems ...UI) Condition {
  44. return c.ElseIf(true, elems...)
  45. }
  46. func (c condition) Kind() Kind {
  47. return Selector
  48. }
  49. func (c condition) JSValue() Value {
  50. return nil
  51. }
  52. func (c condition) Mounted() bool {
  53. return false
  54. }
  55. func (c condition) name() string {
  56. return "if.else"
  57. }
  58. func (c condition) self() UI {
  59. return c
  60. }
  61. func (c condition) setSelf(UI) {
  62. }
  63. func (c condition) getContext() context.Context {
  64. return nil
  65. }
  66. func (c condition) getDispatcher() Dispatcher {
  67. return nil
  68. }
  69. func (c condition) getAttributes() attributes {
  70. return nil
  71. }
  72. func (c condition) getEventHandlers() eventHandlers {
  73. return nil
  74. }
  75. func (c condition) getParent() UI {
  76. return nil
  77. }
  78. func (c condition) setParent(UI) {
  79. }
  80. func (c condition) getChildren() []UI {
  81. return c.body
  82. }
  83. func (c condition) mount(Dispatcher) error {
  84. return errors.New("condition is not mountable").
  85. Tag("name", c.name()).
  86. Tag("kind", c.Kind())
  87. }
  88. func (c condition) dismount() {
  89. }
  90. func (c condition) canUpdateWith(UI) bool {
  91. return false
  92. }
  93. func (c condition) updateWith(UI) error {
  94. return errors.New("condition cannot be updated").
  95. Tag("name", c.name()).
  96. Tag("kind", c.Kind())
  97. }
  98. func (c condition) preRender(Page) {
  99. }
  100. func (c condition) onComponentEvent(any) {
  101. }
  102. func (c condition) html(w io.Writer) {
  103. panic("shoulnd not be called")
  104. }
  105. func (c condition) htmlWithIndent(w io.Writer, indent int) {
  106. panic("shoulnd not be called")
  107. }