/
render.go
187 lines (172 loc) · 4.71 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
// 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 list
import (
"context"
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda/apistructs"
protocol "github.com/erda-project/erda/modules/openapi/component-protocol"
)
// SetCtxBundle 设置bundle
func (i *ComponentList) SetCtxBundle(b protocol.ContextBundle) error {
if b.Bdl == nil || b.I18nPrinter == nil {
err := fmt.Errorf("invalie context bundle")
return err
}
logrus.Infof("inParams:%+v, identity:%+v", b.InParams, b.Identity)
i.ctxBdl = b
return nil
}
// GenComponentState 获取state
func (i *ComponentList) GenComponentState(c *apistructs.Component) error {
if c == nil || c.State == nil {
return nil
}
var state State
cont, err := json.Marshal(c.State)
if err != nil {
logrus.Errorf("marshal component state failed, content:%v, err:%v", c.State, err)
return err
}
err = json.Unmarshal(cont, &state)
if err != nil {
logrus.Errorf("unmarshal component state failed, content:%v, err:%v", cont, err)
return err
}
i.State = state
return nil
}
func (i *ComponentList) SetComponentValue() error {
i.Props = Props{
PageSizeOptions: []string{"10", "20", "50", "100"},
}
visible, err := i.CheckVisible() // true 展示list,隐藏emptyText / false 隐藏list,展示emptyText
if err != nil {
return err
}
if visible {
i.Props.Visible = true
i.State.IsEmpty = false
} else {
i.Props.Visible = false
i.State.IsEmpty = true
}
opPageNo := Operation{
Key: apistructs.OnChangePageNoOperation.String(),
Reload: true,
FillMeta: "pageNo",
}
opPageSize := Operation{
Key: apistructs.OnChangePageSizeOperation.String(),
Reload: true,
FillMeta: "pageSize",
}
i.Operations = make(map[string]interface{})
i.Operations[apistructs.OnChangePageNoOperation.String()] = opPageNo
i.Operations[apistructs.OnChangePageSizeOperation.String()] = opPageSize
return nil
}
// RenderProtocol 渲染组件
func (i *ComponentList) RenderProtocol(c *apistructs.Component, g *apistructs.GlobalStateData) error {
if c.Data == nil {
d := make(apistructs.ComponentData)
c.Data = d
}
(*c).Data["list"] = i.Data.List
stateValue, err := json.Marshal(i.State)
if err != nil {
return err
}
var state map[string]interface{}
err = json.Unmarshal(stateValue, &state)
if err != nil {
return err
}
c.State = state
c.Props = i.Props
c.Operations = i.Operations
return nil
}
func (i *ComponentList) Render(ctx context.Context, c *apistructs.Component, _ apistructs.ComponentProtocolScenario, event apistructs.ComponentEvent, gs *apistructs.GlobalStateData) (err error) {
bdl := ctx.Value(protocol.GlobalInnerKeyCtxBundle.String()).(protocol.ContextBundle)
if err = i.SetCtxBundle(bdl); err != nil {
return
}
if err = i.GenComponentState(c); err != nil {
return
}
switch event.Operation {
case apistructs.InitializeOperation:
i.State.PageNo = 1
i.State.PageSize = 20
if err := i.RenderList(); err != nil {
return err
}
if err := i.SetComponentValue(); err != nil {
return err
}
case apistructs.RenderingOperation:
// 如果触发搜索,跳转到第一页
if i.State.IsFirstFilter {
i.State.PageNo = 1
i.State.IsFirstFilter = false
}
if err := i.RenderList(); err != nil {
return err
}
if err := i.SetComponentValue(); err != nil {
return err
}
case apistructs.OnChangePageSizeOperation:
if err := i.RenderChangePageSize(event.OperationData); err != nil {
return err
}
if err = i.RenderList(); err != nil {
return err
}
if err := i.SetComponentValue(); err != nil {
return err
}
case apistructs.OnChangePageNoOperation:
if err := i.RenderChangePageNo(event.OperationData); err != nil {
return err
}
if err = i.RenderList(); err != nil {
return err
}
if err := i.SetComponentValue(); err != nil {
return err
}
case apistructs.ListProjectExistOperationKey:
err := i.RenderExist(event.OperationData)
if err != nil {
return err
}
err = i.RenderList()
if err != nil {
return err
}
if err := i.SetComponentValue(); err != nil {
return err
}
}
if err := i.RenderProtocol(c, gs); err != nil {
return err
}
return
}
func RenderCreator() protocol.CompRender {
return &ComponentList{}
}