-
Notifications
You must be signed in to change notification settings - Fork 34
/
env_box.go
125 lines (113 loc) · 3.15 KB
/
env_box.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
package forms
import (
"github.com/TeaWeb/code/teaconfigs/shared"
"github.com/iwind/TeaGo/types"
"github.com/iwind/TeaGo/utils/string"
"net/http"
)
// 环境变量
type EnvBox struct {
Element
}
func NewEnvBox(title string, subtitle string) *EnvBox {
return &EnvBox{
Element{
Title: title,
Subtitle: subtitle,
},
}
}
func (this *EnvBox) Super() *Element {
return &this.Element
}
func (this *EnvBox) Compose() string {
this.Javascript = `
/**
* 环境变量
*/
this.env = `
if types.IsSlice(this.Value) {
this.Javascript += stringutil.JSONEncode(this.Value)
} else {
this.Javascript += "[]"
}
this.Javascript += `;
this.envAdding = false;
this.envAddingName = "";
this.envAddingValue = "";
this.addEnv = function () {
this.envAdding = !this.envAdding;
this.$delay(function () {
this.$find("form input[name='envAddingName']").focus();
});
};
this.confirmAddEnv = function () {
if (this.envAddingName.length == 0) {
alert("请输入变量名");
this.$find("form input[name='envAddingName']").focus();
return;
}
this.env.push({
"name": this.envAddingName,
"value": this.envAddingValue
});
this.envAdding = false;
this.envAddingName = "";
this.envAddingValue = "";
};
this.removeEnv = function (index) {
this.env.$remove(index);
};
this.cancelEnv = function () {
this.envAdding = false;
};`
return `
<div class="ui field">
<span class="ui label small" v-for="(var1, index) in env">
<input type="hidden" name="` + this.Namespace + "_" + this.Code + `_envNames" :value="var1.name"/>
<input type="hidden" name="` + this.Namespace + "_" + this.Code + `_envValues" :value="var1.value"/>
<em>{{var1.name}}</em>: {{var1.value}}
<a href="" @click.prevent="removeEnv(index)"><i class="icon remove"></i></a>
</span>
</div>
<div v-if="envAdding" class="ui fields inline">
<div class="ui field">
<input type="text" name="envAddingName" v-model="envAddingName" placeholder="变量名" style="width:9em" @keyup.enter="confirmAddEnv" @keypress.enter.prevent="1"/>
</div>
<div class="ui field">
<input type="text" name="envAddingValue" v-model="envAddingValue" placeholder="变量值" style="width:15em" @keyup.enter="confirmAddEnv" @keypress.enter.prevent="1"/>
</div>
<div class="ui field">
<button class="ui button" type="button" @click="confirmAddEnv()">添加</button>
</div>
<div class="ui field" style="padding-left:0;padding-right:0">
<a href="" @click.prevent="cancelEnv()" title="删除"><i class="icon remove"></i></a>
</div>
</div>
<div class="ui field">
<button class="ui button small" type="button" @click="addEnv()">+</button>
</div>`
}
func (this *EnvBox) ApplyRequest(req *http.Request) (value interface{}, skip bool, err error) {
names, found := req.Form[this.Namespace+"_"+this.Code+"_envNames"]
envs := []*shared.Variable{}
if found {
values, found := req.Form[this.Namespace+"_"+this.Code+"_envValues"]
if found {
for index, name := range names {
if index < len(values) {
envs = append(envs, &shared.Variable{
Name: name,
Value: values[index],
})
} else {
envs = append(envs, &shared.Variable{
Name: name,
Value: "",
})
}
}
}
}
return envs, false, nil
}