-
Notifications
You must be signed in to change notification settings - Fork 34
/
form.go
229 lines (201 loc) · 4.33 KB
/
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
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
package agents
import (
"encoding/json"
"github.com/TeaWeb/code/teaconfigs/forms"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/logs"
)
type FormAction actions.Action
// 表单测试
func (this *FormAction) Run(params struct{}) {
form := forms.NewForm("user")
group := form.NewGroup()
{
field := &forms.TextField{}
field.Title = "主机"
field.Subtitle = "Host"
field.Placeholder = "请输入主机名"
field.Comment = "比如127.0.0.1"
field.Code = "host"
field.Attr("id", "host-field")
//field.Attr("v-model", "host")
field.Attr("@input", "changeHost()")
field.CSS = `
#host-field {
background: red!important;
}
`
field.Javascript = `
this.host = "";
this.$delay(function () {
this.$find("#host-field").focus();
});
this.changeHost = function () {
console.log("change:", this.host);
};
`
group.Add(field)
}
{
field := &forms.TextField{}
field.Title = "端口"
field.Subtitle = "Port"
field.Placeholder = "请输入端口"
field.Comment = "比如8080"
field.Code = "port"
field.Value = "8080"
group.Add(field)
}
{
field := forms.NewOptions("请求方法", "")
field.Code = "method"
field.AddOption("GET", "GET")
field.AddOption("POST", "POST")
field.Attr("style", "width:10em")
group.Add(field)
}
{
field := forms.NewTextField("刷新间隔", "")
field.Code = "interval"
field.RightLabel = "分钟"
field.Attr("style", "width:5em")
field.Value = "30"
group.Add(field)
}
{
field := forms.NewScriptBox("脚本", "")
field.Code = "script"
field.IsRequired = true
group.Add(field)
}
{
field := forms.NewCheckBox("是否启用", "")
field.IsChecked = true
field.Label = "这是标签"
field.Code = "on"
field.Value = 1
group.Add(field)
}
{
field := forms.NewTextBox("描述", "")
field.Code = "description"
field.Value = "描述文字..."
field.Placeholder = "请输入..."
field.Rows = 5
field.Cols = 60
group.Add(field)
}
{
field := forms.NewEnvBox("环境变量", "")
field.Code = "env"
group.Add(field)
}
data := []byte(`{
"description": "描述文字...",
"env": [
{
"name": "a",
"value": "b"
},
{
"name": "name",
"value": "YinLu"
}
],
"host": "127.0.0.1",
"interval": "55",
"method": "POST",
"on": "0",
"port": "8881",
"script": {
"scriptCode": "hello world",
"scriptLang": "php",
"scriptPath": "/home/www.sh",
"scriptType": "code"
}
}`)
m := map[string]interface{}{}
err := json.Unmarshal(data, &m)
if err != nil {
this.Fail(err.Error())
}
form.Init(m)
form.Compose()
this.Data["form"] = form
this.Show()
}
func (this *FormAction) RunPost(params struct{}) {
form := forms.NewForm("user")
group := form.NewGroup()
{
field := &forms.TextField{}
field.Title = "主机"
field.Subtitle = "Host"
field.Placeholder = "请输入主机名"
field.Comment = "比如127.0.0.1"
field.Code = "host"
group.Add(field)
}
{
field := &forms.TextField{}
field.Title = "端口"
field.Subtitle = "Port"
field.Placeholder = "请输入端口"
field.Comment = "比如8080"
field.Code = "port"
field.ValidateCode = `if (value.length < 4) {
throw new Error("端口长度不能小于4");
}
return value;
`
group.Add(field)
}
{
field := forms.NewOptions("请求方法", "")
field.Code = "method"
field.AddOption("GET", "GET")
field.AddOption("POST", "POST")
field.Attr("style", "width:10em")
group.Add(field)
}
{
field := forms.NewTextField("刷新间隔", "")
field.Code = "interval"
field.RightLabel = "分钟"
field.Attr("style", "width:5em")
field.Value = "30"
group.Add(field)
}
{
field := forms.NewScriptBox("脚本", "")
field.IsRequired = true
field.Code = "script"
group.Add(field)
}
{
field := forms.NewCheckBox("是否启用", "")
field.IsChecked = true
field.Code = "on"
field.Label = "这是标签"
group.Add(field)
}
{
field := forms.NewTextBox("描述", "")
field.Code = "description"
field.Value = "描述文字..."
field.Placeholder = "请输入..."
field.Rows = 5
field.Cols = 60
group.Add(field)
}
{
field := forms.NewEnvBox("环境变量", "")
field.Code = "env"
group.Add(field)
}
values, field, err := form.ApplyRequest(this.Request)
if err != nil {
this.FailField(form.Namespace+"_"+field, err.Error())
}
logs.PrintAsJSON(values)
}