-
-
Notifications
You must be signed in to change notification settings - Fork 551
/
CreateRecord.jsx
224 lines (216 loc) · 10 KB
/
CreateRecord.jsx
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
import get from 'lodash/get';
import includes from 'lodash/includes';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import React, { PureComponent } from 'react';
import Modal from 'app/components/Modal';
import Space from 'app/components/Space';
import { ToastNotification } from 'app/components/Notifications';
import ToggleSwitch from 'app/components/ToggleSwitch';
import { Form, Select, Textarea } from 'app/components/Validation';
import i18n from 'app/lib/i18n';
import * as validations from 'app/lib/validations';
import styles from '../form.styl';
const SYSTEM_EVENTS = [
// The following events are only available with system trigger (i.e. scripts)
'startup',
'port:open',
'port:close'
];
class CreateRecord extends PureComponent {
static propTypes = {
state: PropTypes.object,
actions: PropTypes.object
};
fields = {
enabled: null,
event: null,
trigger: null,
commands: null
};
get value() {
const {
event,
trigger,
commands
} = this.form.getValues();
return {
enabled: !!get(this.fields.enabled, 'state.checked'),
event: event,
trigger: trigger,
commands: commands
};
}
render() {
const { state, actions } = this.props;
const { modal } = state;
const disableTriggerOptions = includes(SYSTEM_EVENTS, modal.params.event);
let sampleCommands = '';
if (modal.params.trigger === 'system') {
sampleCommands = [
'sleep 5'
].join('\n');
} else if (modal.params.trigger === 'gcode') {
sampleCommands = [
'G21 ; Set units to mm',
'G90 ; Absolute positioning',
'G1 Z1 F500 ; Move to clearance level'
].join('\n');
}
return (
<Modal size="sm" onClose={actions.closeModal}>
<Modal.Header>
<Modal.Title>
{i18n._('Events')}
<Space width="8" />
›
<Space width="8" />
{i18n._('New')}
</Modal.Title>
</Modal.Header>
<Modal.Body>
{modal.params.alertMessage &&
<ToastNotification
style={{ margin: '-16px -24px 10px -24px' }}
type="error"
onDismiss={() => {
actions.updateModalParams({ alertMessage: '' });
}}
>
{modal.params.alertMessage}
</ToastNotification>
}
<Form
ref={node => {
this.form = node;
}}
onSubmit={(event) => {
event.preventDefault();
}}
>
<div className={styles.formFields}>
<div className={styles.formGroup}>
<label>{i18n._('Enabled')}</label>
<div>
<ToggleSwitch
ref={node => {
this.fields.enabled = node;
}}
size="sm"
checked={true}
/>
</div>
</div>
<div className={styles.formGroup}>
<label>{i18n._('Event')}</label>
<Select
name="event"
value={modal.params.event}
className={classNames(
'form-control',
styles.formControl,
styles.short
)}
onChange={(event) => {
const value = event.target.value;
let trigger = modal.params.trigger;
if (includes(SYSTEM_EVENTS, value)) {
trigger = 'system'; // system-only events
}
actions.updateModalParams({
event: value,
trigger: trigger
});
}}
validations={[validations.required]}
>
<option value="">{i18n._('Choose an event')}</option>
<option value="startup">{i18n._('Startup (System only)')}</option>
<option value="port:open">{i18n._('Open a serial port (System only)')}</option>
<option value="port:close">{i18n._('Close a serial port (System only)')}</option>
<option value="controller:ready">{i18n._('Ready to start')}</option>
<option value="gcode:load">{i18n._('G-code: Load')}</option>
<option value="gcode:unload">{i18n._('G-code: Unload')}</option>
<option value="gcode:start">{i18n._('G-code: Start')}</option>
<option value="gcode:stop">{i18n._('G-code: Stop')}</option>
<option value="gcode:pause">{i18n._('G-code: Pause')}</option>
<option value="gcode:resume">{i18n._('G-code: Resume')}</option>
<option value="feedhold">{i18n._('Feed Hold')}</option>
<option value="cyclestart">{i18n._('Cycle Start')}</option>
<option value="homing">{i18n._('Homing')}</option>
<option value="sleep">{i18n._('Sleep')}</option>
<option value="macro:run">{i18n._('Run Macro')}</option>
<option value="macro:load">{i18n._('Load Macro')}</option>
</Select>
</div>
<div className={styles.formGroup}>
<label>{i18n._('Trigger')}</label>
<Select
name="trigger"
value={modal.params.trigger}
className={classNames(
'form-control',
styles.formControl,
styles.short
)}
disabled={disableTriggerOptions}
onChange={(event) => {
const value = event.target.value;
actions.updateModalParams({
trigger: value
});
}}
validations={[validations.required]}
>
<option value="">{i18n._('Choose an trigger')}</option>
<option value="system">{i18n._('System')}</option>
<option value="gcode">{i18n._('G-code')}</option>
</Select>
</div>
<div className={styles.formGroup}>
<label>{i18n._('Commands')}</label>
<Textarea
name="commands"
value=""
rows="5"
className={classNames(
'form-control',
styles.formControl,
styles.long
)}
placeholder={sampleCommands}
validations={[validations.required]}
/>
</div>
</div>
</Form>
</Modal.Body>
<Modal.Footer>
<button
type="button"
className="btn btn-default"
onClick={actions.closeModal}
>
{i18n._('Cancel')}
</button>
<button
type="button"
className="btn btn-primary"
onClick={() => {
this.form.validate(err => {
if (err) {
return;
}
const { enabled, event, trigger, commands } = this.value;
actions.createRecord({ enabled, event, trigger, commands });
});
}}
>
{i18n._('OK')}
</button>
</Modal.Footer>
</Modal>
);
}
}
export default CreateRecord;