-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathnode.js
343 lines (316 loc) · 18.4 KB
/
node.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
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var path = require('path');
var mergeTabulator = function(target,source) {
if (typeof source === 'object') {
Object.keys(source).forEach(element => {
if (typeof source[element] !== "object") {
target[element] = source[element];
} else {
if (!target.hasOwnProperty(element)) {
target[element] = (Array.isArray(source[element])) ? [] : {};
}
// handle the columns array to merge columns if the field property matches. Otherwise push a new column
if (element==='columns' && Array.isArray(source[element])){
source[element].forEach(sourceElement => {
let index = target[element].findIndex(targetElement => (
(targetElement.field && sourceElement.field && targetElement.field===sourceElement.field) || // normal column object
(targetElement.title && sourceElement.title && targetElement.title===sourceElement.title) // parent object with nested columns
));
if (index<0) { // add new column
index=target[element].push({})-1;
}
mergeTabulator(target[element][index],sourceElement);
})
} else {
mergeTabulator(target[element],source[element])
}
}
});
} else {
target=source;
}
}
module.exports = function (RED) {
function checkConfig(node, conf) {
if (!conf || !conf.hasOwnProperty('group')) {
node.error(RED._('table.error.no-group'));
return false;
}
else {
return true;
}
}
function HTML(config,dark) {
var configAsJson = JSON.stringify(config, (key, value) => {
// exclude the node description
if (key === "info") {
return undefined;
}
// replace single quotation mark (apostrophe) by html code in strings
if (typeof (value) === "string") {
return value.replace(/'/g, "'");
}
// all others leave unchanged
return value;
});
var mid = (dark) ? "_midnight" : "";
var html = String.raw`
<style>.nr-dashboard-ui_table { padding:0; }</style>
<link href='ui-table/css/tabulator`+mid+`.min.css' rel='stylesheet' type='text/css'>
<script type='text/javascript' src='ui-table/js/tabulator.js'></script>
<div id='ui_table-{{$id}}' style="background-color:unset; border:unset;"></div>
<input type='hidden' ng-init='init(` + configAsJson + `)'>
`;
return html;
}
function TableNode(config) {
var done = null;
var node = this;
try {
RED.nodes.createNode(this, config);
if (checkConfig(node, config)) {
var ui = RED.require('node-red-dashboard')(RED);
var sizes = ui.getSizes();
// var luma = 255;
// if (ui.hasOwnProperty("getTheme") && (ui.getTheme() !== undefined)) {
// var rgb = parseInt(ui.getTheme()["page-sidebar-backgroundColor"].value.substring(1), 16); // convert rrggbb to decimal
// luma = 0.2126 * ((rgb >> 16) & 0xff) + 0.7152 * ((rgb >> 8) & 0xff) + 0.0722 * ((rgb >> 0) & 0xff); // per ITU-R BT.709
// }
var html = HTML(config,ui.isDark());
done = ui.addWidget({
node: node,
width: config.width,
height: (config.height > 2) ? config.height : 2, // min height to 2 so auto will show something
format: html,
templateScope: 'local',
order: config.order || 0,
group: config.group,
forwardInputMessages: false,
storeFrontEndInputAsState: false,
// to make msg.ui_control work without msg.payload we have to send msg.payload=null.
// we correct this here into undefined to get the last known payload form currentValues[opt.node.id].
convert: function (value) {
if (value===null) value=undefined;
return value;
},
// merge new ui_control messages into config.ui_control
// Help needed: use the already build in ui_control mechanism from ui.js
beforeEmit: function (msg, value) {
// cache ui_control messages for new clients
if (msg.hasOwnProperty('ui_control')) {
if (!config.hasOwnProperty('ui_control')){
config.ui_control={
"tabulator":{
"columns":config.columns
}};
}
// use mergeTabulator to correctly merge columns arrays if field property matches
mergeTabulator(config.ui_control,msg.ui_control);
// delete column definitions by sending a empty columns array (requires page reload)
if (msg.ui_control.tabulator && msg.ui_control.tabulator.columns && Array.isArray(msg.ui_control.tabulator.columns) &&
msg.ui_control.tabulator.columns.length==0) {
config.ui_control.tabulator.columns=[];
config.ui_control.tabulator.autoColumns=true;
}
}
return { msg: {
payload: value,
ui_control: config.ui_control,
socketid: msg.socketid
}};
},
beforeSend: function (msg, orig) {
if (orig) { return orig.msg; }
},
initController: function ($scope, events) {
$scope.inited = false;
$scope.tabledata = [];
var tablediv;
var mergeObject = function(target,source) {
if (typeof source === 'object') {
Object.keys(source).forEach(element => {
if (typeof source[element] !== "object") {
target[element] = source[element];
} else {
if (!target.hasOwnProperty(element)) {
target[element] = (Array.isArray(source[element])) ? [] : {};
}
mergeObject(target[element],source[element])
}
});
} else {
target = source;
}
};
var createTable = function(basediv, tabledata, columndata, outputs, ui_control) {
// add id field if not already exists
if (columndata.length>0 && tabledata.length>0 && tabledata[0] && typeof tabledata[0] === 'object' && !tabledata[0].hasOwnProperty('id')) {
tabledata.map((row,index) => row.id = index);
}
var opts = {
data: tabledata,
layout: 'fitColumns',
columns: columndata,
autoColumns: columndata.length == 0,
movableColumns: true,
}
if (!ui_control || !ui_control.tabulator) {
var y = (columndata.length === 0) ? 25 : 32;
if ($scope.height==2) { // auto height
opts.height = (tabledata.length > 0 )? tabledata.length * y + 26 : $scope.height*(sizes.sy+sizes.cy);
} else {
opts.height = $scope.height*(sizes.sy+sizes.cy);
}
}
else { // configuration via ui_control
//as Object.assign is not supported by Internet Explorer
//opts = Object.assign(opts, ui_control.tabulator);
mergeObject(opts,ui_control.tabulator);
var y = (opts.columns && (opts.columns.length > 0)) ? 32 : 25;
if (ui_control.customHeight) {
opts.height= ui_control.customHeight * y + 26;
} else {
if ($scope.height==2) { // auto height
opts.height= (tabledata.length > 0 )? tabledata.length * y + 26 : $scope.height*(sizes.sy+sizes.cy);
} else {
opts.height = $scope.height*(sizes.sy+sizes.cy);
}
}
} // end of configuration via ui_control
if ((outputs > 0) && !opts.hasOwnProperty('cellClick')) { // default cellClick if not already defined by ui_control
opts.cellClick = function(e, cell) {
$scope.send({topic:cell.getField(), payload:cell.getData(), row:(cell.getRow()).getPosition()});
};
}
//turn autoColumns off if opts.columns is array with length > 0
if (opts.columns && Array.isArray(opts.columns) && opts.columns.length>0) {
opts.autoColumns = false;
}
// console.log("createTabulator",opts);
if ($scope.table !== undefined) {
$scope.table.destroy();
}
$scope.table = new Tabulator(basediv, opts);
};
$scope.init = function (config) {
$scope.config = config;
tablediv = '#ui_table-' + $scope.$eval('$id')
var stateCheck = setInterval(function() {
if (document.querySelector(tablediv) && $scope.tabledata) {
clearInterval(stateCheck);
$scope.inited = true;
createTable(tablediv,$scope.tabledata,$scope.config.columns,$scope.config.outputs,$scope.config.ui_control);
$scope.tabledata = [];
}
}, 200); // lowest setting on my side ... still fails sometimes ;)
};
$scope.$watch('msg', function (msg) {
//console.log("ui-table message arrived:",msg);
if (msg && msg.hasOwnProperty("ui_control") && msg.ui_control.hasOwnProperty("callback")) return msg; // to avoid loopback from callbacks. No better solution jet. Help needed.
//console.log("ui-table msg: ", msg);
// configuration via ui_control
if (msg && msg.hasOwnProperty("ui_control")) {
var addValueOrFunction = function (config,param,value) {
if (typeof String.prototype.parseFunction != 'function') {
String.prototype.parseFunction = function () {
var funcReg = /function *\(([^()]*)\)[ \n\t]*{(.*)}/gmi;
var match = funcReg.exec(this.replace(/\n/g, ' '));
if (match) {
return new Function(match[1].split(','), match[2]);
}
return null;
};
}
var valueFunction;
if (typeof value === "string" && (valueFunction = value.parseFunction())) {
config[param]=valueFunction.bind($scope); // to enable this.send() for callback functions.
}
else config[param]= value;
}
var addObject = function (destinationObject,sourceObject) {
for (var element in sourceObject) {
if (!destinationObject[element]) destinationObject[element]=(Array.isArray(sourceObject[element]))? [] : {};
if (typeof sourceObject[element] === "object") {
addObject(destinationObject[element],sourceObject[element])
} else {
addValueOrFunction(destinationObject,element,sourceObject[element]);
}
}
}
if (!$scope.config.ui_control) { $scope.config.ui_control={}; }
addObject($scope.config.ui_control,msg.ui_control);
} // end of configuration via ui_control
if (msg && msg.hasOwnProperty("payload")) {
if (Array.isArray(msg.payload)) {
$scope.tabledata = msg.payload;
}
// commands to tabulator via msg.payload object
if (typeof msg.payload === "object" && msg.payload!==null && !Array.isArray(msg.payload)) {
if (msg.payload.hasOwnProperty("command") && $scope.table!==undefined) {
if (!msg.payload.hasOwnProperty("arguments") || !Array.isArray(msg.payload.arguments)) {
msg.payload.arguments=[];
}
if (msg.payload.returnPromise) {
let commandReturn = $scope.table[msg.payload.command].apply($scope.table,msg.payload.arguments);
if (commandReturn) {
if (typeof commandReturn.then === "function") {
commandReturn.then(function(...args){
$scope.send({topic:"success", ui_control: {callback:$scope.msg.payload.command}, return:$scope.msg.payload});
}).catch(function(error){
if (Object.keys(error).length>0) {
$scope.send({topic:"error", ui_control: {callback:$scope.msg.payload.command}, return:$scope.msg.payload, error: error});
}
});
} else {
$scope.send({topic:"success", ui_control: {callback:$scope.msg.payload.command}, return:$scope.msg.payload, payload:commandReturn});
}
}
} else {
$scope.table[msg.payload.command].apply($scope.table,msg.payload.arguments);
}
return;
}
return;
} // end of commands to tabulator via msg.payload object
}
if ($scope.inited == false) {
return;
} else {
createTable(tablediv, $scope.tabledata, $scope.config.columns, $scope.config.outputs, $scope.config.ui_control);
}
});
}
});
}
}
catch (e) { console.log(e); }
node.on('close', function () {
if (done) { done(); }
});
}
RED.nodes.registerType('ui_table', TableNode);
var uipath = 'ui';
if (RED.settings.ui) { uipath = RED.settings.ui.path; }
var fullPath = path.join('/', uipath, '/ui-table/*').replace(/\\/g, '/');
RED.httpNode.get(fullPath, function (req, res) {
var options = {
root: __dirname + '/lib/',
dotfiles: 'deny'
};
res.sendFile(req.params[0], options)
});
};