|
| 1 | +// Copyright (c) 2025 worldiety GmbH |
| 2 | +// |
| 3 | +// This file is part of the NAGO Low-Code Platform. |
| 4 | +// Licensed under the terms specified in the LICENSE file. |
| 5 | +// |
| 6 | +// SPDX-License-Identifier: Custom-License |
| 7 | + |
| 8 | +package uiflow |
| 9 | + |
| 10 | +import ( |
| 11 | + "fmt" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "go.wdy.de/nago/application/flow" |
| 15 | + "go.wdy.de/nago/presentation/core" |
| 16 | + "go.wdy.de/nago/presentation/ui" |
| 17 | +) |
| 18 | + |
| 19 | +// magic field values |
| 20 | +const ( |
| 21 | + mfVEmpty = "[empty]" |
| 22 | + mfVNotEmpty = "[not empty]" |
| 23 | + mfVTrue = "true" |
| 24 | + mfVFalse = "false" |
| 25 | + mfVNull = "[null]" |
| 26 | + mfVHas = "[has]" |
| 27 | + mfVDelete = "[delete]" |
| 28 | +) |
| 29 | + |
| 30 | +func (c TFormEditor) viewHelpFieldsExpr(structType *flow.StructType, state *core.State[string]) core.View { |
| 31 | + var views []core.View |
| 32 | + views = append(views, ui.Text("Field helper templates: click a value to append it as expression")) |
| 33 | + for field := range structType.Fields.All() { |
| 34 | + headingName := field.JSONName() |
| 35 | + if headingName != string(field.Name()) { |
| 36 | + headingName += " (" + string(field.Name()) + ")" |
| 37 | + } |
| 38 | + |
| 39 | + views = append(views, ui.VStack( |
| 40 | + ui.Heading(5, headingName), |
| 41 | + c.possibleFieldValuesExpr(field, state), |
| 42 | + ).Alignment(ui.Leading)) |
| 43 | + } |
| 44 | + |
| 45 | + return ui.VStack(views...).Alignment(ui.Leading).FullWidth().Gap(ui.L8) |
| 46 | +} |
| 47 | + |
| 48 | +func (c TFormEditor) possibleFieldValuesExpr(field flow.Field, state *core.State[string]) core.View { |
| 49 | + var views []core.View |
| 50 | + switch field := field.(type) { |
| 51 | + case *flow.StringField: |
| 52 | + views = append(views, pillTextExpr(state, field, mfVEmpty), pillTextExpr(state, field, mfVNotEmpty), pillTextExpr(state, field, mfVHas), pillTextExpr(state, field, `any text`)) |
| 53 | + case *flow.BoolField: |
| 54 | + views = append(views, pillTextExpr(state, field, mfVTrue), pillTextExpr(state, field, mfVFalse), pillTextExpr(state, field, mfVHas)) |
| 55 | + case *flow.TypeField: |
| 56 | + baseType, ok := c.ws.Packages.TypeByID(field.Type) |
| 57 | + if !ok { |
| 58 | + views = append(views, pillTextExpr(state, field, "unknown type")) |
| 59 | + } else { |
| 60 | + if str, ok := baseType.(*flow.StringType); ok { |
| 61 | + views = append(views, pillTextExpr(state, field, mfVEmpty), pillTextExpr(state, field, mfVNotEmpty), pillTextExpr(state, field, mfVHas)) |
| 62 | + if str.Enumeration.Len() == 0 { |
| 63 | + views = append(views, pillTextExpr(state, field, `any text`)) |
| 64 | + } |
| 65 | + |
| 66 | + for literal := range str.Enumeration.All() { |
| 67 | + views = append(views, pillTextExpr(state, field, literal.Value)) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + return ui.HStack(views...).Gap(ui.L8).Wrap(true) |
| 73 | +} |
| 74 | + |
| 75 | +func pillTextExpr(state *core.State[string], field flow.Field, s string) core.View { |
| 76 | + return ui.HStack(ui.Text(s)). |
| 77 | + BackgroundColor(ui.ColorCardFooter). |
| 78 | + Action(func() { |
| 79 | + fname := field.JSONName() |
| 80 | + var expr string |
| 81 | + switch s { |
| 82 | + case mfVEmpty: |
| 83 | + expr = fmt.Sprintf(`field("%s").String() == ""`, fname) |
| 84 | + case mfVNotEmpty: |
| 85 | + expr = fmt.Sprintf(`field("%s").String() != ""`, fname) |
| 86 | + case mfVTrue: |
| 87 | + expr = fmt.Sprintf(`field("%s").Bool() == true`, fname) |
| 88 | + case mfVFalse: |
| 89 | + expr = fmt.Sprintf(`field("%s").Bool() == false`, fname) |
| 90 | + case mfVHas: |
| 91 | + expr = fmt.Sprintf(`has("%s")`, fname) |
| 92 | + default: |
| 93 | + expr = fmt.Sprintf(`field("%s").String() == "%s"`, fname, s) |
| 94 | + } |
| 95 | + |
| 96 | + state.Set(strings.TrimSpace(state.Get())) |
| 97 | + if state.Get() != "" { |
| 98 | + expr = " && " + expr |
| 99 | + } |
| 100 | + |
| 101 | + state.Set(state.Get() + expr) |
| 102 | + }). |
| 103 | + Padding(ui.Padding{}.All(ui.L4).Horizontal(ui.L8)). |
| 104 | + Border(ui.Border{}.Radius(ui.L8)) |
| 105 | +} |
0 commit comments