-
Notifications
You must be signed in to change notification settings - Fork 8
/
service.js
61 lines (50 loc) · 1.49 KB
/
service.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
const { ShellCommand } = require('./shell_command');
class Service {
constructor (name, state) {
this.name = name;
this.state = state;
}
draw (rootElement) {
this.node = document.createElement('LI');
this.node.setAttribute('data-state', this.state);
const statusnode = document.createElement('SPAN');
statusnode.setAttribute('class', 'status');
const toggler = document.createElement('SPAN');
toggler.setAttribute('class', 'toggler');
statusnode.appendChild(toggler);
this.node.appendChild(statusnode);
const textnode = document.createTextNode(this.name);
this.node.appendChild(textnode);
rootElement.appendChild(this.node);
this.addListener();
}
setState (state) {
this.node.setAttribute('data-state', state);
this.state = state;
}
addListener (callback) {
this.node.addEventListener('click', () => {
const oldState = this.state;
this.setState('waiting');
let command;
if (oldState === 'started') {
command = 'stop';
} else if (oldState === 'stopped') {
command = 'start';
} else {
command = null;
}
if (command) {
ShellCommand.run('brew', ['services', command, this.name], (output) => {
const match = output.match(/(started)|(stopped)/);
if (match) {
const newState = match[0];
this.state = newState;
this.setState(newState);
}
});
}
});
}
}
exports.Service = Service;