| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package app
- import (
- "context"
- "io"
- "github.com/maxence-charriere/go-app/v9/pkg/errors"
- )
- // Condition represents a control structure that displays nodes depending on a
- // given expression.
- type Condition interface {
- UI
- // ElseIf sets the condition with the given nodes if previous expressions
- // were not met and given expression is true.
- ElseIf(expr bool, elems ...UI) Condition
- // Else sets the condition with the given UI elements if previous
- // expressions were not met.
- Else(elems ...UI) Condition
- }
- // If returns a condition that filters the given elements according to the given
- // expression.
- func If(expr bool, elems ...UI) Condition {
- if !expr {
- elems = nil
- }
- return condition{
- body: FilterUIElems(elems...),
- satisfied: expr,
- }
- }
- type condition struct {
- body []UI
- satisfied bool
- }
- func (c condition) ElseIf(expr bool, elems ...UI) Condition {
- if c.satisfied {
- return c
- }
- if expr {
- c.body = FilterUIElems(elems...)
- c.satisfied = expr
- }
- return c
- }
- func (c condition) Else(elems ...UI) Condition {
- return c.ElseIf(true, elems...)
- }
- func (c condition) Kind() Kind {
- return Selector
- }
- func (c condition) JSValue() Value {
- return nil
- }
- func (c condition) Mounted() bool {
- return false
- }
- func (c condition) name() string {
- return "if.else"
- }
- func (c condition) self() UI {
- return c
- }
- func (c condition) setSelf(UI) {
- }
- func (c condition) getContext() context.Context {
- return nil
- }
- func (c condition) getDispatcher() Dispatcher {
- return nil
- }
- func (c condition) getAttributes() attributes {
- return nil
- }
- func (c condition) getEventHandlers() eventHandlers {
- return nil
- }
- func (c condition) getParent() UI {
- return nil
- }
- func (c condition) setParent(UI) {
- }
- func (c condition) getChildren() []UI {
- return c.body
- }
- func (c condition) mount(Dispatcher) error {
- return errors.New("condition is not mountable").
- Tag("name", c.name()).
- Tag("kind", c.Kind())
- }
- func (c condition) dismount() {
- }
- func (c condition) canUpdateWith(UI) bool {
- return false
- }
- func (c condition) updateWith(UI) error {
- return errors.New("condition cannot be updated").
- Tag("name", c.name()).
- Tag("kind", c.Kind())
- }
- func (c condition) preRender(Page) {
- }
- func (c condition) onComponentEvent(any) {
- }
- func (c condition) html(w io.Writer) {
- panic("shoulnd not be called")
- }
- func (c condition) htmlWithIndent(w io.Writer, indent int) {
- panic("shoulnd not be called")
- }
|