forked from node-red/node-red-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
414 lines (359 loc) · 13.1 KB
/
ui.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
var inited = false;
module.exports = function(RED) {
if (!inited) {
inited = true;
init(RED.server, RED.httpNode || RED.httpAdmin, RED.log, RED.settings);
}
return {
add: add,
addLink: addLink,
addBaseConfig: addBaseConfig,
emit: emit,
emitSocket: emitSocket,
toNumber: toNumber.bind(null, false),
toFloat: toNumber.bind(null, true),
updateUi: updateUi,
ev: ev,
getTheme: getTheme,
getSizes: getSizes
};
};
var serveStatic = require('serve-static'),
socketio = require('socket.io'),
path = require('path'),
fs = require('fs'),
events = require('events'),
dashboardVersion = require('./package.json').version;
var baseConfiguration = {};
var tabs = [];
var links = [];
var updateValueEventName = 'update-value';
var io;
var currentValues = {};
var replayMessages = {};
var removeStateTimers = {};
var removeStateTimeout = 1000;
var ev = new events.EventEmitter();
ev.setMaxListeners(0);
var settings = {};
function toNumber(keepDecimals, config, input) {
if (typeof input !== "number") {
var inputString = input.toString();
input = keepDecimals ? parseFloat(inputString) : parseInt(inputString);
}
if (config.step) { input = Math.round(Math.round(input/config.step)*config.step*10000)/10000; }
return isNaN(input) ? config.min : input;
}
function emit(event, data) {
io.emit(event, data);
}
function emitSocket(event, data) {
if (data.hasOwnProperty("socketid") && (data.socketid !== undefined)) {
io.to(data.socketid).emit(event,data);
}
else {
io.emit(event, data);
}
}
function noConvert(value) {
return value;
}
function beforeEmit(msg, value) {
return { value:value };
}
function beforeSend(msg) {
//do nothing
}
/*
options:
node - the node that represents the control on a flow
control - the control to be added
tab - tab config node that this control belongs to
group - group name
[emitOnlyNewValues] - boolean (default true).
If true, it checks if the payload changed before sending it
to the front-end. If the payload is the same no message is sent.
[convert] - callback to convert the value before sending it to the front-end
[convertBack] - callback to convert the message from front-end before sending it to the next connected node
[beforeEmit] - callback to prepare the message that is emitted to the front-end
[beforeSend] - callback to prepare the message that is sent to the output
[forwardInputMessages] - default true. If true, forwards input messages to the output
[storeFrontEndInputAsState] - default true. If true, any message received from front-end is stored as state
*/
function add(opt) {
clearTimeout(removeStateTimers[opt.node.id]);
delete removeStateTimers[opt.node.id];
if (typeof opt.emitOnlyNewValues === 'undefined') {
opt.emitOnlyNewValues = true;
}
if (typeof opt.forwardInputMessages === 'undefined') {
opt.forwardInputMessages = true;
}
if (typeof opt.storeFrontEndInputAsState === 'undefined') {
opt.storeFrontEndInputAsState = true;
}
opt.beforeEmit = opt.beforeEmit || beforeEmit;
opt.beforeSend = opt.beforeSend || beforeSend;
opt.convert = opt.convert || noConvert;
opt.convertBack = opt.convertBack || noConvert;
opt.control.id = opt.node.id;
var remove = addControl(opt.tab, opt.group, opt.control);
opt.node.on("input", function(msg) {
if (typeof msg.enabled === 'boolean') {
var state = replayMessages[opt.node.id];
if (!state) { replayMessages[opt.node.id] = state = {id: opt.node.id}; }
state.disabled = !msg.enabled;
io.emit(updateValueEventName, state);
}
// remove res and req as they are often circular
if (msg.hasOwnProperty("res")) { delete msg.res; }
if (msg.hasOwnProperty("req")) { delete msg.req; }
// Retrieve the dataset for this node
var oldValue = currentValues[opt.node.id];
// Call the convert function in the node to get the new value
// as well as the full dataset.
var conversion = opt.convert(msg.payload, oldValue, msg);
// If the update flag is set, emit the newPoint, and store the full dataset
var fullDataset;
var newPoint;
if ((typeof(conversion) === 'object') && (conversion.update !== undefined)) {
newPoint = conversion.newPoint;
fullDataset = conversion.updatedValues;
}
else {
// If no update flag is set, this means the conversion contains
// the full dataset or the new value (e.g. gauges)
fullDataset = conversion;
}
// If we have something new to emit
if (newPoint !== undefined || !opt.emitOnlyNewValues || oldValue != fullDataset) {
currentValues[opt.node.id] = fullDataset;
// Determine what to emit over the websocket
// (the new point or the full dataset).
// Always store the full dataset.
var toStore = opt.beforeEmit(msg, fullDataset);
var toEmit;
if (newPoint !== undefined) { toEmit = opt.beforeEmit(msg, newPoint); }
else { toEmit = toStore; }
toEmit.id = toStore.id = opt.node.id;
// Emit and Store the data
io.emit(updateValueEventName, toEmit);
replayMessages[opt.node.id] = toStore;
// Handle the node output
if (opt.forwardInputMessages && opt.node._wireCount) {
msg.payload = opt.convertBack(fullDataset);
msg = opt.beforeSend(msg) || msg;
opt.node.send(msg);
}
}
});
var handler = function (msg) {
if (msg.id !== opt.node.id) { return; }
var converted = opt.convertBack(msg.value);
if (opt.storeFrontEndInputAsState) {
currentValues[msg.id] = converted;
replayMessages[msg.id] = msg;
}
var toSend = {payload: converted};
toSend = opt.beforeSend(toSend, msg) || toSend;
toSend.socketid = toSend.socketid || msg.socketid;
opt.node.send(toSend);
if (opt.storeFrontEndInputAsState) {
//fwd to all UI clients
io.emit(updateValueEventName, msg);
}
};
ev.on(updateValueEventName, handler);
return function() {
ev.removeListener(updateValueEventName, handler);
remove();
removeStateTimers[opt.node.id] = setTimeout(function() {
delete currentValues[opt.node.id];
delete replayMessages[opt.node.id];
}, removeStateTimeout);
};
}
//from: http://stackoverflow.com/a/28592528/3016654
function join() {
var trimRegex = new RegExp('^\\/|\\/$','g'),
paths = Array.prototype.slice.call(arguments);
return '/'+paths.map(function(e) {return e.replace(trimRegex,"");}).filter(function(e) {return e;}).join('/');
}
function init(server, app, log, redSettings) {
var uiSettings = redSettings.ui || {};
settings.path = uiSettings.path || 'ui';
settings.defaultGroupHeader = uiSettings.defaultGroup || 'Default';
var fullPath = join(redSettings.httpNodeRoot, settings.path);
var socketIoPath = join(fullPath, 'socket.io');
io = socketio(server, {path: socketIoPath});
fs.stat(path.join(__dirname, 'dist/index.html'), function(err, stat) {
if (!err) {
app.use( join(settings.path), serveStatic(path.join(__dirname, "dist")) );
}
else {
log.info("Dashboard using development folder");
app.use(join(settings.path), serveStatic(path.join(__dirname, "src")));
var vendor_packages = [
'angular', 'angular-sanitize', 'angular-animate', 'angular-aria', 'angular-material',
'angular-material-icons', 'svg-morpheus', 'font-awesome',
'sprintf-js',
'jquery', 'jquery-ui',
'd3', 'raphael', 'justgage',
'angular-chart.js', 'chart.js', 'moment',
'angularjs-color-picker', 'tinycolor2', 'less'
];
vendor_packages.forEach(function (packageName) {
app.use(join(settings.path, 'vendor', packageName), serveStatic(path.join(__dirname, 'node_modules', packageName)));
});
}
});
log.info("Dashboard version " + dashboardVersion + " started at " + fullPath);
io.on('connection', function(socket) {
ev.emit("newsocket", socket.client.id, socket.request.connection.remoteAddress);
updateUi(socket);
socket.on(updateValueEventName, ev.emit.bind(ev, updateValueEventName));
socket.on('ui-replay-state', function() {
var ids = Object.getOwnPropertyNames(replayMessages);
ids.forEach(function (id) {
socket.emit(updateValueEventName, replayMessages[id]);
});
socket.emit('ui-replay-done');
});
socket.on('ui-refresh', function() {
updateUi();
});
socket.on('disconnect', function() {
ev.emit("endsocket", socket.client.id, socket.request.connection.remoteAddress);
});
});
}
var updateUiPending = false;
function updateUi(to) {
if (!to) {
if (updateUiPending) { return; }
updateUiPending = true;
to = io;
}
process.nextTick(function() {
tabs.forEach(function(t) {
t.theme = baseConfiguration.theme;
});
links.forEach(function(l) {
l.theme = baseConfiguration.theme;
});
to.emit('ui-controls', {
site: baseConfiguration.site,
theme: baseConfiguration.theme,
tabs: tabs,
links: links
});
updateUiPending = false;
});
}
function find(array, predicate) {
for (var i=0; i<array.length; i++) {
if (predicate(array[i])) {
return array[i];
}
}
}
function itemSorter(item1, item2) {
if (item1.order === 0 && item2.order !== 0) {
return 1;
}
else if (item1.order !== 0 && item2.order === 0) {
return -1;
}
return item1.order - item2.order;
}
function addControl(tab, groupHeader, control) {
if (typeof control.type !== 'string') { return function() {}; }
groupHeader = groupHeader || settings.defaultGroupHeader;
control.order = parseFloat(control.order);
var foundTab = find(tabs, function (t) {return t.id === tab.id });
if (!foundTab) {
foundTab = {
id: tab.id,
header: tab.config.name,
order: parseFloat(tab.config.order),
icon: tab.config.icon,
items: []
};
tabs.push(foundTab);
tabs.sort(itemSorter);
}
var foundGroup = find(foundTab.items, function (g) {return g.header === groupHeader;});
if (!foundGroup) {
foundGroup = {
header: groupHeader,
items: []
};
foundTab.items.push(foundGroup);
}
foundGroup.items.push(control);
foundGroup.items.sort(itemSorter);
foundGroup.order = groupHeader.config.order;
foundTab.items.sort(itemSorter);
updateUi();
// Return the remove function for this control
return function() {
var index = foundGroup.items.indexOf(control);
if (index >= 0) {
// Remove the item from the group
foundGroup.items.splice(index, 1);
// If the group is now empty, remove it from the tab
if (foundGroup.items.length === 0) {
index = foundTab.items.indexOf(foundGroup);
if (index >= 0) {
foundTab.items.splice(index, 1);
// If the tab is now empty, remove it as well
if (foundTab.items.length === 0) {
index = tabs.indexOf(foundTab);
if (index >= 0) {
tabs.splice(index, 1);
}
}
}
}
updateUi();
}
}
}
function addLink(name, link, icon, order, target) {
var newLink = {
name: name,
link: link,
icon: icon,
order: order || 1,
target: target
};
links.push(newLink);
links.sort(itemSorter);
updateUi();
return function() {
var index = links.indexOf(newLink);
if (index < 0) { return; }
links.splice(index, 1);
updateUi();
}
}
function addBaseConfig(config) {
if (config) { baseConfiguration = config; }
updateUi();
}
function getTheme() {
if (baseConfiguration && baseConfiguration.hasOwnProperty("theme") && (typeof baseConfiguration.theme !== "undefined") ) {
return baseConfiguration.theme.themeState;
}
else {
return undefined;
}
}
function getSizes() {
if (baseConfiguration && baseConfiguration.hasOwnProperty("site") && (typeof baseConfiguration.site !== "undefined") && baseConfiguration.site.hasOwnProperty("sizes")) {
return baseConfiguration.site.sizes;
}
else {
return { sx:48, sy:48, gx:6, gy:6, cx:6, cy:6, px:0, py:0 };
}
}