-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathconfigure.js
158 lines (133 loc) · 3.78 KB
/
configure.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
var Ractive = require('ractive')
var page = require('page')
var fs = require('fs')
var Client = require('electron-rpc/client')
var client = new Client()
Ractive.DEBUG = false
// Throw unhandled javascript errors
window.onerror = function errorHandler (message, url, lineNumber) {
message = message + '\n' + url + ':' + lineNumber
throwError(message)
}
var templates = {
configure: fs.readFileSync(__dirname + '/configure.tmpl').toString(),
detail: fs.readFileSync(__dirname + '/detail.tmpl').toString(),
about: fs.readFileSync(__dirname + '/about.html').toString()
}
var state = {}
var events = {
processAction: function (e) {
var action = e.node.attributes['data-action'].value
var procNameAttr = e.node.attributes['data-name']
var data = {task: action}
if (procNameAttr) data.name = procNameAttr.value
execTask(data)
},
quit: function () {
client.request('terminate')
},
openDir: function () {
client.request('open-dir')
},
openKillMenu: function (e) {
var remote = require('remote')
var Menu = remote.require('menu')
var MenuItem = remote.require('menu-item')
function sendSignal (signal) {
return function () {
var procNameAttr = e.node.attributes['data-name']
execTask({task: 'stop', name: procNameAttr.value, signal: signal})
}
}
var menu = new Menu()
;['SIGTERM', 'SIGHUP', 'SIGINT', 'SIGKILL', 'SIGQUIT'].forEach(function (signal) {
menu.append(new MenuItem({ label: 'Send ' + signal, click: sendSignal(signal) }))
})
menu.popup(remote.getCurrentWindow())
return false
},
openLogsDir: function (e) {
client.request('open-logs-dir', {name: e.context.name})
}
}
var routes = {
configure: function configure (ctx, next) {
ctx.template = templates.configure
state.configure = render(ctx, {loading: true})
getAndRenderAll(next)
},
detail: function detail (ctx, next) {
ctx.template = templates.detail
state.detail = render(ctx, {loading: true})
getAndRender(ctx.params.name, next)
},
about: function about (ctx, next) {
ctx.template = templates.about
state.about = render(ctx, {})
}
}
// set up routes
page('/', routes.configure)
page('/detail/:name', routes.detail)
page('/about', routes.about)
// initialize router
page.start()
page('/')
// Load all statuses when the app gets focused
client.on('show', function () {
getAndRenderAll()
var currentProcess = state.detail && state.detail.get('name')
if (currentProcess) getAndRender(currentProcess)
})
function execTask (task) {
client.request('task', task, function (err, data) {
if (err) return throwError(err)
if (!data) return
if (Array.isArray(data)) {
renderAll(data)
} else if (data.name) {
state.detail.set(data)
}
})
}
function render (ctx) {
var ract = new Ractive({
el: '#container',
template: ctx.template,
data: ctx.data
})
ract.on(events)
return ract
}
function getAndRenderAll (callback) {
callback = catchErrors(callback || function () {})
client.request('get-all', function (err, data) {
if (err) return callback(err)
renderAll(data)
callback()
})
}
function renderAll (data) {
data = data || []
var obj = {items: data, hasProcesses: data.length > 0}
state.configure.set(obj)
}
function getAndRender (name, callback) {
callback = catchErrors(callback || function () {})
client.request('get-one', {name: name}, function (err, data) {
if (err) return callback(err)
state.detail.set(data)
callback()
})
}
function catchErrors (callback) {
return function throwErrorsOrContinue (err) {
if (err) return throwError(err)
callback()
}
}
function throwError (error) {
var message = error.stack || error.message || JSON.stringify(error)
console.error(message)
window.alert(message)
}