forked from Gippsman2017/node-red-node-ui-etable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetable.js
160 lines (150 loc) · 7.02 KB
/
etable.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
/**
* 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');
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);
var mid = (dark) ? "_midnight" : "";
var html = String.raw`
<link href='ui-etable/css/tabulator`+mid+`.min.css' rel='stylesheet' type='text/css'>
<script type='text/javascript' src='ui-etable/js/tabulator.js'></script>
<div id='ui_etable-{{$id}}'></div>
<input type='hidden' ng-init='init(` + configAsJson + `)'>
`;
return html;
};
function eTableNode(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 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
}
if (config.height == 0) { config.height = 20; } // min height to 2 so auto will show something
config.columns = JSON.parse(config.payload);
delete config.payload;
delete config.payloadType;
config.options = JSON.parse(config.options);
var html = HTML(config,(luma < 128));
done = ui.addWidget({
node: node,
width: config.width,
height: config.height,
format: html,
templateScope: 'local',
order: config.order,
group: config.group,
forwardInputMessages: false,
beforeEmit: function (msg, value) {
return {
msg: {
payload: value,
config: msg.config
}
};
},
beforeSend: function (msg, orig) {
if (orig) { return orig.msg; }
},
initController: function ($scope, events) {
$scope.inited = false;
$scope.tabledata = [];
var tablediv;
var createTable = function(basediv, tabledata, columndata, options, outputs) {
var y = (columndata.length === 0) ? 25 : 32;
var opts1 = {
data: tabledata,
columns: columndata,
autoColumns: columndata.length == 0,
height: tabledata.length * y + 26
}
var opts = Object.assign(opts1,options);
if (outputs > 0) {
opts.cellClick = function(e,cell) {
$scope.send({topic:cell.getField(),callback:"cellClick",payload:cell.getData(),options:opts});
};
opts.cellEdited = function(cell) {
$scope.send({topic:cell.getField(),callback:"cellEdited",payload:cell.getData(),options:opts});
};
}
var table = new Tabulator(basediv, opts);
};
$scope.init = function (config) {
$scope.config = config;
tablediv = '#ui_etable-' + $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.options,$scope.config.outputs);
$scope.tabledata = [];
}
}, 40);
};
$scope.$watch('msg', function (msg) {
var columns = $scope.config.columns;
var options = $scope.config.options;
if(msg && msg.hasOwnProperty("config")){
if(msg.config.options){
options = msg.config.options;
}
if(msg.config.columns){
columns = msg.config.columns;
}
}
if (msg && msg.hasOwnProperty("payload") && Array.isArray(msg.payload)) {
if ($scope.inited == false) {
$scope.tabledata = msg.payload;
return;
}
createTable(tablediv,msg.payload,columns,options,$scope.config.outputs);
}
});
}
});
}
}
catch (e) { console.log(e); }
node.on('close', function () {
if (done) { done(); }
});
}
RED.nodes.registerType('ui_etable', eTableNode);
var uipath = 'ui';
if (RED.settings.ui) { uipath = RED.settings.ui.path; }
var fullPath = path.join(RED.settings.httpNodeRoot, uipath, '/ui-etable/*').replace(/\\/g, '/');;
RED.httpNode.get(fullPath, function (req, res) {
var options = {
root: __dirname + '/lib/',
dotfiles: 'deny'
};
res.sendFile(req.params[0], options)
});
};