forked from atom-community/atom-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript-options-view.js
195 lines (171 loc) · 5.88 KB
/
script-options-view.js
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
"use babel"
import { CompositeDisposable, Emitter } from "atom"
import { View } from "atom-space-pen-views-plus"
import _ from "underscore"
import ScriptInputView from "./script-input-view"
export default class ScriptOptionsView extends View {
static content() {
this.div({ class: "options-view" }, () => {
this.h4({ class: "modal-header" }, "Configure Run Options")
this.div({ class: "modal-body" }, () => {
this.table(() => {
this.tr(() => {
this.td({ class: "first" }, () => this.label("Current Working Directory:"))
this.td({ class: "second" }, () =>
this.tag("atom-text-editor", { mini: "", class: "editor mini", outlet: "inputCwd" })
)
})
this.tr(() => {
this.td(() => this.label("Command"))
this.td(() => this.tag("atom-text-editor", { mini: "", class: "editor mini", outlet: "inputCommand" }))
})
this.tr(() => {
this.td(() => this.label("Command Arguments:"))
this.td(() => this.tag("atom-text-editor", { mini: "", class: "editor mini", outlet: "inputCommandArgs" }))
})
this.tr(() => {
this.td(() => this.label("Program Arguments:"))
this.td(() => this.tag("atom-text-editor", { mini: "", class: "editor mini", outlet: "inputScriptArgs" }))
})
this.tr(() => {
this.td(() => this.label("Environment Variables:"))
this.td(() => this.tag("atom-text-editor", { mini: "", class: "editor mini", outlet: "inputEnv" }))
})
})
})
this.div({ class: "modal-footer" }, () => {
const css = "btn inline-block-tight"
this.button({ class: `btn ${css} cancel`, outlet: "buttonCancel", click: "close" }, () =>
this.span({ class: "icon icon-x" }, "Cancel")
)
this.span({ class: "pull-right" }, () => {
this.button({ class: `btn ${css} save-profile`, outlet: "buttonSaveProfile", click: "saveProfile" }, () =>
this.span({ class: "icon icon-file-text" }, "Save as profile")
)
this.button({ class: `btn ${css} run`, outlet: "buttonRun", click: "run" }, () =>
this.span({ class: "icon icon-playback-play" }, "Run")
)
})
})
})
}
initialize(runOptions) {
this.runOptions = runOptions
this.emitter = new Emitter()
this.subscriptions = new CompositeDisposable()
this.subscriptions.add(
atom.commands.add("atom-workspace", {
"core:cancel": () => this.hide(),
"core:close": () => this.hide(),
"script:close-options": () => this.hide(),
"script:run-options": () => (this.panel.isVisible() ? this.hide() : this.show()),
"script:save-options": () => this.saveOptions(),
})
)
// handling focus traversal and run on enter
this.find("atom-text-editor").on("keydown", (e) => {
if (e.keyCode !== 9 && e.keyCode !== 13) {
return true
}
switch (e.keyCode) {
case 9: {
e.preventDefault()
e.stopPropagation()
const row = this.find(e.target).parents("tr:first").nextAll("tr:first")
if (row.length) {
return row.find("atom-text-editor").focus()
}
return this.buttonCancel.focus()
}
case 13:
return this.run()
}
return null
})
this.panel = atom.workspace.addModalPanel({ item: this })
this.panel.hide()
}
static splitArgs(argText) {
const text = argText.trim()
const argSubstringRegex = /([^\s"']+)|((["'])(.*?)\3)/g
const args = []
let lastMatchEndPosition = -1
let match = argSubstringRegex.exec(text)
while (match !== null) {
const matchWithoutQuotes = match[1] || match[4]
// Combine current result with last match, if last match ended where this
// one begins.
if (lastMatchEndPosition === match.index) {
args[args.length - 1] += matchWithoutQuotes
} else {
args.push(matchWithoutQuotes)
}
lastMatchEndPosition = argSubstringRegex.lastIndex
match = argSubstringRegex.exec(text)
}
return args
}
getOptions() {
return {
workingDirectory: this.inputCwd.get(0).getModel().getText(),
cmd: this.inputCommand.get(0).getModel().getText(),
cmdArgs: this.constructor.splitArgs(this.inputCommandArgs.get(0).getModel().getText()),
env: this.inputEnv.get(0).getModel().getText(),
scriptArgs: this.constructor.splitArgs(this.inputScriptArgs.get(0).getModel().getText()),
}
}
saveOptions() {
const options = this.getOptions()
for (const option in options) {
const value = options[option]
this.runOptions[option] = value
}
}
onProfileSave(callback) {
return this.emitter.on("on-profile-save", callback)
}
// Saves specified options as new profile
saveProfile() {
this.hide()
const options = this.getOptions()
const inputView = new ScriptInputView({ caption: "Enter profile name:" })
inputView.onCancel(() => this.show())
inputView.onConfirm((profileName) => {
if (!profileName) {
return
}
_.forEach(this.find("atom-text-editor"), (editor) => {
editor.getModel().setText("")
})
// clean up the options
this.saveOptions()
// add to global profiles list
this.emitter.emit("on-profile-save", { name: profileName, options })
})
inputView.show()
}
close() {
this.hide()
}
destroy() {
if (this.subscriptions) {
this.subscriptions.dispose()
}
}
show() {
this.panel.show()
this.inputCwd.focus()
}
hide() {
this.panel.hide()
atom.workspace.getActivePane().activate()
}
run() {
this.saveOptions()
this.hide()
atom.commands.dispatch(this.getWorkspaceView(), "script:run")
}
getWorkspaceView() {
return atom.views.getView(atom.workspace)
}
}