-
Notifications
You must be signed in to change notification settings - Fork 0
/
association_slice_to_form.go
204 lines (167 loc) · 6.22 KB
/
association_slice_to_form.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// generated code - do not edit
package probe
import (
"fmt"
"log"
"sort"
gongtable_fullstack "github.com/fullstack-lang/gongtable/go/fullstack"
form "github.com/fullstack-lang/gongtable/go/models"
gongtable_models "github.com/fullstack-lang/gongtable/go/models"
"github.com/fullstack-lang/gongsim/go/models"
)
// AssociationSliceToForm add a form div with 2 buttons
// - one for selection
// - one for sorting
func AssociationSliceToForm[InstanceType models.PointerToGongstruct, FieldType models.PointerToGongstruct](
fieldName string,
instance InstanceType,
field *[]FieldType,
formGroup *form.FormGroup,
probe *Probe,
) {
formDiv := (&form.FormDiv{
Name: fieldName,
}).Stage(probe.formStage)
formGroup.FormDivs = append(formGroup.FormDivs, formDiv)
formEditAssocButton := (&form.FormEditAssocButton{
Name: fieldName,
Label: fieldName,
}).Stage(probe.formStage)
formDiv.FormEditAssocButton = formEditAssocButton
onAssocEditon := NewOnAssocEditon(instance, field, fieldName, probe)
formEditAssocButton.OnAssocEditon = onAssocEditon
formSortAssocButton := (&form.FormSortAssocButton{
Name: fieldName,
Label: fieldName,
}).Stage(probe.formStage)
formDiv.FormSortAssocButton = formSortAssocButton
onSortingEditon := NewOnSortingEditon(instance, field, probe)
formSortAssocButton.OnSortEdition = onSortingEditon
}
type OnAssocEditon[InstanceType models.PointerToGongstruct, FieldType models.PointerToGongstruct] struct {
instance InstanceType
field *[]FieldType
fieldName string
probe *Probe
}
func NewOnAssocEditon[InstanceType models.PointerToGongstruct, FieldType models.PointerToGongstruct](
instance InstanceType,
field *[]FieldType,
fieldName string,
probe *Probe,
) (onAssocEdition *OnAssocEditon[InstanceType, FieldType]) {
onAssocEdition = new(OnAssocEditon[InstanceType, FieldType])
onAssocEdition.instance = instance
onAssocEdition.field = field
onAssocEdition.fieldName = fieldName
onAssocEdition.probe = probe
return
}
func (onAssocEditon *OnAssocEditon[InstanceType, FieldType]) OnButtonPressed() {
tableStackName := onAssocEditon.probe.formStage.GetPath() + string(form.StackNamePostFixForTableForAssociation)
// tableStackName supposed to be "test-form-table"
tableStageForSelection, _ := gongtable_fullstack.NewStackInstance(onAssocEditon.probe.r, tableStackName)
instanceSet := *models.GetGongstructInstancesSetFromPointerType[FieldType](onAssocEditon.probe.stageOfInterest)
instanceSlice := make([]FieldType, 0)
for instance := range instanceSet {
instanceSlice = append(instanceSlice, instance)
}
sort.Slice(instanceSlice, func(i, j int) bool {
return instanceSlice[i].GetName() < instanceSlice[j].GetName()
})
table := new(gongtable_models.Table).Stage(tableStageForSelection)
table.Name = string(form.TableSelectExtraName)
table.HasColumnSorting = true
table.HasFiltering = true
table.HasPaginator = false
table.HasCheckableRows = true
table.HasSaveButton = true
// filterdInstanceSet is the set of instance that are part of the field
filterdInstanceSet := make(map[FieldType]any, 0)
for _, instance := range *onAssocEditon.field {
filterdInstanceSet[instance] = nil
}
for _, fieldName := range models.GetFieldsFromPointer[FieldType]() {
column := new(gongtable_models.DisplayedColumn).Stage(tableStageForSelection)
column.Name = fieldName
table.DisplayedColumns = append(table.DisplayedColumns, column)
}
for _, instance := range instanceSlice {
row := new(gongtable_models.Row).Stage(tableStageForSelection)
row.Name = instance.GetName()
table.Rows = append(table.Rows, row)
if _, ok := filterdInstanceSet[instance]; ok {
row.IsChecked = true
}
for _, fieldName := range models.GetFieldsFromPointer[FieldType]() {
cell := new(gongtable_models.Cell).Stage(tableStageForSelection)
cell.Name = fmt.Sprintf("Row %s - Column %s", instance.GetName(), fieldName)
cellString := new(gongtable_models.CellString).Stage(tableStageForSelection)
cellString.Name = models.GetFieldStringValueFromPointer(instance, fieldName)
cellString.Value = cellString.Name
cell.CellString = cellString
row.Cells = append(row.Cells, cell)
}
}
// set up control inversion for the saving of the table
table.Impl = NewTablePickSaver[InstanceType, FieldType](
onAssocEditon.instance,
onAssocEditon.field,
onAssocEditon.fieldName,
onAssocEditon.probe)
tableStageForSelection.Commit()
}
func NewTablePickSaver[InstanceType models.PointerToGongstruct, FieldType models.PointerToGongstruct](
instance InstanceType,
field *[]FieldType,
fieldName string,
probe *Probe,
) (tablePickSaver *TablePickSaver[InstanceType, FieldType]) {
tablePickSaver = new(TablePickSaver[InstanceType, FieldType])
tablePickSaver.instance = instance
tablePickSaver.field = field
tablePickSaver.fieldName = fieldName
tablePickSaver.probe = probe
return
}
type TablePickSaver[InstanceType models.PointerToGongstruct, FieldType models.PointerToGongstruct] struct {
instance InstanceType
field *[]FieldType
fieldName string
probe *Probe
}
func (tablePickSaver *TablePickSaver[InstanceType, FieldType]) TableUpdated(
stage *form.StageStruct,
table, updatedTable *form.Table) {
log.Println("TablePickSaver: TableUpdated")
// checkout to the stage to get the rows that have been checked and not
stage.Checkout()
instanceSet := *models.GetGongstructInstancesSetFromPointerType[FieldType](tablePickSaver.probe.stageOfInterest)
instanceSlice := make([]FieldType, 0)
for instance := range instanceSet {
instanceSlice = append(instanceSlice, instance)
}
sort.Slice(instanceSlice, func(i, j int) bool {
return instanceSlice[i].GetName() < instanceSlice[j].GetName()
})
*tablePickSaver.field = make([]FieldType, 0)
for idx, row := range table.Rows {
if row.IsChecked {
instance := instanceSlice[idx]
*tablePickSaver.field = append(*tablePickSaver.field, instance)
}
}
// first, force commit of instance for taking into account the slice
models.EvictInOtherSlices(
tablePickSaver.probe.stageOfInterest,
tablePickSaver.instance,
*tablePickSaver.field,
tablePickSaver.fieldName)
// commit the whole
tablePickSaver.probe.stageOfInterest.Commit()
// see the result
fillUpTablePointerToGongstruct[InstanceType](
tablePickSaver.probe,
)
tablePickSaver.probe.tableStage.Commit()
}