-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathui-action.js
102 lines (76 loc) · 3.25 KB
/
ui-action.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
'use strict';
module.exports = function (RED) {
var ui = undefined;
function UiAction(config) {
RED.nodes.createNode(this, config);
var node = this;
if (ui === undefined) {
ui = RED.require("node-red-dashboard")(RED);
}
var node = this;
var done = ui.addWidget({
node: node,
templateScope: "local",
group: config.group,
order: config.order,
width: -1, height: -1, //https://discourse.nodered.org/t/custom-dashboard-node-without-md-card-possible/14919/20
emitOnlyNewValues: false,
forwardInputMessages: false,
storeFrontEndInputAsState: false,
format: "<div></div>",
beforeEmit: function(msg) {
var action = config.action;
var val = config.write;
if (config.actionType === 'msg' || config.actionType === 'flow' || config.actionType === 'global') {
action = RED.util.evaluateNodeProperty(config.action, config.actionType, node, msg);
}
if (config.writeType === 'msg' || config.writeType === 'flow' || config.writeType === 'global') {
val = RED.util.evaluateNodeProperty(config.write, config.writeType, node, msg);
}
var result = {
msg: {
action: action,
value: val,
target: config.target,
targetKey: '_' + config.target.replace(/[^\w]/g, ""),
passthru: config.passthru
}
};
Object.assign(result.msg, msg);
return result;
},
initController: function($scope) {
const updateWithScope = (msg) => {
if (!msg) return;
if (!msg.action) return;
if (!msg.target) return;
if (typeof window._nrui === 'undefined') return;
if (typeof window._nrui[msg.targetKey] === 'undefined') return;
var actionName = msg.action.toString().toLowerCase();
if (typeof window._nrui[msg.targetKey][actionName] !== 'function') return;
var actionFn = window._nrui[msg.targetKey][actionName];
if (actionFn.length === 0) {
var result = actionFn();
if (result) {
$scope.send({payload: result});
}
}
else {
actionFn(msg);
}
};
$scope.$watch('msg', updateWithScope);
},
beforeSend: function (msg, orig) { // callback to prepare the message that is sent to the output
if (orig) {
return orig.msg;
}
}
});
node.on("close",function() {
node.status({});
done();
});
};
RED.nodes.registerType("hidden-ui-action", UiAction);
};