-
Notifications
You must be signed in to change notification settings - Fork 373
/
render.go
251 lines (227 loc) · 6.59 KB
/
render.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package spaceList
import (
"context"
"encoding/json"
"fmt"
"github.com/erda-project/erda/apistructs"
protocol "github.com/erda-project/erda/modules/openapi/component-protocol"
spec "github.com/erda-project/erda/modules/openapi/component-protocol/component_spec/table"
)
type ComponentSpaceList struct {
CtxBdl protocol.ContextBundle
State state `json:"state"`
Props spec.Props `json:"props"`
Data map[string]interface{} `json:"data"`
}
type state struct {
Total int64 `json:"total"`
PageSize int64 `json:"pageSize"`
PageNo int64 `json:"pageNo"`
}
type props struct {
RowKey string `json:"rowKey,omitempty"`
Columns []columns `json:"columns,omitempty"`
}
type spaceList struct {
ID uint64 `json:"id"`
Name string `json:"name"`
Desc string `json:"desc"`
Operate dataTask `json:"operate"`
Status map[string]interface{} `json:"status"`
}
// apistructs.AutoTestSpaceStatus
type columns struct {
Title string `json:"title"`
DataIndex string `json:"dataIndex"`
Width int `json:"width,omitempty"`
}
type inParams struct {
ProjectID int64 `json:"projectId"`
}
type RenderType string
var (
RenderTable RenderType = "tableOperation"
)
type dataTask struct {
RenderType RenderType `json:"renderType"`
Value string `json:"value"`
Operations map[string]interface{} `json:"operations"`
}
func (a *ComponentSpaceList) SetBundle(b protocol.ContextBundle) error {
if b.Bdl == nil {
err := fmt.Errorf("invalid bundle")
return err
}
a.CtxBdl = b
return nil
}
func (a *ComponentSpaceList) Render(ctx context.Context, c *apistructs.Component, scenario apistructs.ComponentProtocolScenario, event apistructs.ComponentEvent, gs *apistructs.GlobalStateData) error {
bdl := ctx.Value(protocol.GlobalInnerKeyCtxBundle.String()).(protocol.ContextBundle)
err := a.SetBundle(bdl)
if err != nil {
return err
}
if a.CtxBdl.InParams == nil {
return fmt.Errorf("params is empty")
}
inParamsBytes, err := json.Marshal(a.CtxBdl.InParams)
if err != nil {
return fmt.Errorf("failed to marshal inParams, inParams:%+v, err:%v", a.CtxBdl.InParams, err)
}
var inParams inParams
if err := json.Unmarshal(inParamsBytes, &inParams); err != nil {
return err
}
switch event.Operation {
case apistructs.AutoTestSpaceChangePageNoOperationKey, apistructs.AutoTestSpaceChangePageSizeOperationKey, apistructs.InitializeOperation, apistructs.RenderingOperation:
if err := a.handlerListOperation(bdl, c, inParams, event); err != nil {
return err
}
case apistructs.AutoTestSpaceDeleteOperationKey:
if err := a.handlerDeleteOperation(bdl, c, inParams, event); err != nil {
return err
}
case apistructs.AutoTestSpaceCopyOperationKey:
if err := a.handlerCopyOperation(bdl, c, inParams, event); err != nil {
return err
}
case apistructs.AutoTestSpaceRetryOperationKey:
if err := a.handlerRetryOperation(bdl, c, inParams, event); err != nil {
return err
}
case apistructs.AutoTestSpaceExportOperationKey:
if err := a.handlerExportOperation(bdl, c, inParams, event); err != nil {
return err
}
}
c.Operations = getOperations()
a.Props = getProps()
return nil
}
func getStatus(req apistructs.AutoTestSpaceStatus) map[string]interface{} {
res := map[string]interface{}{"renderType": "textWithBadge"}
if req == apistructs.TestSpaceFailed {
res["status"] = "error"
res["value"] = "失败"
}
if req == apistructs.TestSpaceCopying {
res["status"] = "processing"
res["value"] = "复制中"
}
return res
}
func (a *ComponentSpaceList) setData(spaces apistructs.AutoTestSpaceList) error {
lists := []spaceList{}
for _, each := range spaces.List {
list := spaceList{
ID: each.ID,
Name: each.Name,
Desc: each.Description,
Operate: dataTask{
RenderType: RenderTable,
Value: "",
Operations: map[string]interface{}{},
},
Status: getStatus(each.Status),
}
if each.Status == apistructs.TestSpaceFailed {
edit.Command = setCommand(list)
edit.Disabled = true
list.Operate.Operations["a-edit"] = edit
delete.Meta = setMeta(list)
delete.Disabled = false
list.Operate.Operations["delete"] = delete
retry.Meta = setMeta(list)
export.Meta = setMeta(list)
export.Disabled = true
list.Operate.Operations["export"] = export
list.Operate.Operations["retry"] = retry
} else {
edit.Command = setCommand(list)
copy.Meta = setMeta(list)
delete.Meta = setMeta(list)
delete.Disabled = true
copy.Disabled = true
edit.Disabled = true
if each.Status == apistructs.TestSpaceOpen {
edit.Disabled = false
copy.Disabled = false
}
list.Operate.Operations["a-edit"] = edit
list.Operate.Operations["copy"] = copy
export.Meta = setMeta(list)
list.Operate.Operations["export"] = export
list.Operate.Operations["delete"] = delete
}
lists = append(lists, list)
}
a.Data["list"] = lists
return nil
}
func getOperations() map[string]interface{} {
return map[string]interface{}{
"changePageNo": ListSpaceOperation{
Key: "changePageNo",
Reload: true,
},
"changePageSize": ListSpaceOperation{
Key: "changePageSize",
Reload: true,
},
"clickRow": ClickRowOperation{
Key: "clickRow",
Reload: false,
Command: ClickRowOperationCommand{
Key: "goto",
State: map[string]interface{}{},
Target: "project_test_spaceDetail_scenes",
JumpOut: false,
},
},
}
}
func getProps() spec.Props {
return spec.Props{
PageSizeOptions: []string{"10", "20", "50", "100"},
RowKey: "id",
Columns: []spec.Column{
{
Title: "空间名",
DataIndex: "name",
},
{
Title: "描述",
DataIndex: "desc",
},
{
Title: "状态",
DataIndex: "status",
},
{
Title: "操作",
DataIndex: "operate",
Width: 180,
},
},
Visible: true,
}
}
func RenderCreator() protocol.CompRender {
return &ComponentSpaceList{
CtxBdl: protocol.ContextBundle{},
State: state{},
Data: map[string]interface{}{},
}
}