-
Notifications
You must be signed in to change notification settings - Fork 34
/
script_box.go
243 lines (223 loc) · 7.23 KB
/
script_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
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
package forms
import (
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/utils/string"
"net/http"
)
type ScriptBox struct {
Element
}
func NewScriptBox(title string, subTitle string) *ScriptBox {
return &ScriptBox{
Element: Element{
Title: title,
Subtitle: subTitle,
},
}
}
func (this *ScriptBox) Super() *Element {
return &this.Element
}
func (this *ScriptBox) Compose() string {
value := maps.NewMap(this.Value)
scriptType := value.GetString("scriptType")
if len(scriptType) == 0 {
scriptType = "path"
}
scriptLang := value.GetString("scriptLang")
if len(scriptLang) == 0 {
scriptLang = "shell"
}
scriptPath := value.GetString("scriptPath")
scriptCode := value.GetString("scriptCode")
this.Javascript = `/**
* 脚本
*/
var scriptEditor = null;
this.scriptTab = ` + stringutil.JSONEncode(scriptType) + `;
this.scriptLang = ` + stringutil.JSONEncode(scriptLang) + `;
this.scriptPath = ` + stringutil.JSONEncode(scriptPath) + `;
this.scriptCode = ` + stringutil.JSONEncode(scriptCode) + `;
if (this.scriptTab == "code") {
this.$delay(function () {
this.loadEditor();
});
}
this.scriptLangs = [
{
"name": "Shell",
"code": "shell",
"value": (this.scriptLang == "shell" && this.scriptCode.length > 0) ? this.scriptCode : "#!/usr/bin/env bash\n\n# your commands here\n"
},
{
"name": "批处理(bat)",
"code": "bat",
"value": (this.scriptLang == "bat") ? this.scriptCode : ""
},
{
"name": "PHP",
"code": "php",
"value": (this.scriptLang == "php") ? this.scriptCode : "#!/usr/bin/env php\n\n<?php\n// your PHP codes here"
},
{
"name": "Python",
"code": "python",
"value": (this.scriptLang == "python") ? this.scriptCode : "#!/usr/bin/env python\n\n''' your Python codes here '''"
},
{
"name": "Ruby",
"code": "ruby",
"value": (this.scriptLang == "ruby") ? this.scriptCode : "#!/usr/bin/env ruby\n\n# your Ruby codes here"
},
{
"name": "NodeJS",
"code": "nodejs",
"value": (this.scriptLang == "nodejs") ? this.scriptCode : "#!/usr/bin/env node\n\n// your javascript codes here"
}
];
this.selectScriptTab = function (tab) {
this.scriptTab = tab;
if (tab == "path") {
this.$delay(function () {
this.$find("form input[name='scriptPath']").focus();
});
} else if (tab == "code") {
this.$delay(function () {
this.loadEditor();
});
}
};
this.selectScriptLang = function (lang) {
this.scriptLang = lang;
var value = this.scriptLangs.$find(function (k, v) {
return v.code == lang;
}).value;
switch (lang) {
case "shell":
scriptEditor.setValue(value);
var info = CodeMirror.findModeByMIME("text/x-sh");
if (info != null) {
scriptEditor.setOption("mode", info.mode);
CodeMirror.modeURL = "/codemirror/mode/%N/%N.js";
CodeMirror.autoLoadMode(scriptEditor, info.mode);
}
break;
case "bat":
scriptEditor.setValue("");
break;
case "php":
scriptEditor.setValue(value);
var info = CodeMirror.findModeByMIME("text/x-php");
if (info != null) {
scriptEditor.setOption("mode", info.mode);
CodeMirror.modeURL = "/codemirror/mode/%N/%N.js";
CodeMirror.autoLoadMode(scriptEditor, info.mode);
}
break;
case "python":
scriptEditor.setValue(value);
var info = CodeMirror.findModeByMIME("text/x-python");
if (info != null) {
scriptEditor.setOption("mode", info.mode);
CodeMirror.modeURL = "/codemirror/mode/%N/%N.js";
CodeMirror.autoLoadMode(scriptEditor, info.mode);
}
break;
case "ruby":
scriptEditor.setValue(value);
var info = CodeMirror.findModeByMIME("text/x-ruby");
if (info != null) {
scriptEditor.setOption("mode", info.mode);
CodeMirror.modeURL = "/codemirror/mode/%N/%N.js";
CodeMirror.autoLoadMode(scriptEditor, info.mode);
}
break;
case "nodejs":
scriptEditor.setValue(value);
var info = CodeMirror.findModeByMIME("text/javascript");
if (info != null) {
scriptEditor.setOption("mode", info.mode);
CodeMirror.modeURL = "/codemirror/mode/%N/%N.js";
CodeMirror.autoLoadMode(scriptEditor, info.mode);
}
break;
}
scriptEditor.save();
scriptEditor.focus();
};
this.loadEditor = function () {
if (scriptEditor == null) {
scriptEditor = CodeMirror.fromTextArea(document.getElementById("script-code-editor"), {
theme: "idea",
lineNumbers: true,
value: "",
readOnly: false,
showCursorWhenSelecting: true,
height: "auto",
//scrollbarStyle: null,
viewportMargin: Infinity,
lineWrapping: true,
highlightFormatting: false,
indentUnit: 4,
indentWithTabs: true
});
}
var that = this;
scriptEditor.setValue(this.scriptLangs.$find(function (k, v) {
return v.code == that.scriptLang;
}).value);
scriptEditor.save();
scriptEditor.focus();
var info = CodeMirror.findModeByMIME("text/x-sh");
if (info != null) {
scriptEditor.setOption("mode", info.mode);
CodeMirror.modeURL = "/codemirror/mode/%N/%N.js";
CodeMirror.autoLoadMode(scriptEditor, info.mode);
}
scriptEditor.on("change", function () {
scriptEditor.save();
that.scriptLangs.$find(function (k, v) {
return v.code == that.scriptLang;
}).value = scriptEditor.getValue();
});
};`
this.CSS = `/** codemirror **/
/** codemirror **/
.CodeMirror {
border: 1px solid #eee;
height: auto!important;
}
.CodeMirror-vscrollbar {
width: 6px;
border-radius: 3px!important;
}
.CodeMirror-vscrollbar::-webkit-scrollbar-thumb {
border-radius: 2px;
}
`
return `<input type="hidden" name="` + this.Namespace + `_scriptType" :value="scriptTab"/>
<input type="hidden" name="` + this.Namespace + `_scriptLang" :value="scriptLang"/>
<div class="ui tabular menu attached small">
<a class="item" :class="{active:scriptTab == 'path'}" @click.prevent="selectScriptTab('path')">脚本文件</a>
<a class="item" :class="{active:scriptTab == 'code'}" @click.prevent="selectScriptTab('code')" v-if="!teaDemoEnabled">脚本代码</a>
</div>
<div class="ui bottom segment attached" v-show="scriptTab == 'path'">
<input type="text" name="` + this.Namespace + `_scriptPath" v-model="scriptPath" maxlength="100"/>
<p class="comment">如果是Shell脚本,请不要忘记在头部添加 <em>#!脚本解释工具</em>,比如 <em>#!/bin/bash</em></p>
</div>
<div class="ui bottom segment attached" v-show="scriptTab == 'code'" style="padding-top:0">
<div class="ui menu text small">
<a class="item" v-for="lang in scriptLangs" :class="{active:lang.code == scriptLang}" @click.prevent="selectScriptLang(lang.code)">{{lang.name}}</a>
</div>
<textarea name="` + this.Namespace + `_scriptCode" id="script-code-editor" rows="1"></textarea>
<p class="comment">如果是Shell脚本,请不要忘记在头部添加 <em>#!脚本解释工具</em>,比如 <em>#!/bin/bash</em></p>
</div>`
}
func (this *ScriptBox) ApplyRequest(req *http.Request) (value interface{}, skip bool, err error) {
return map[string]interface{}{
"scriptType": req.Form.Get(this.Namespace + "_scriptType"),
"scriptLang": req.Form.Get(this.Namespace + "_scriptLang"),
"scriptPath": req.Form.Get(this.Namespace + "_scriptPath"),
"scriptCode": req.Form.Get(this.Namespace + "_scriptCode"),
}, false, nil
}