-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathui-light.js
181 lines (133 loc) · 6.03 KB
/
ui-light.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
'use strict';
module.exports = function (RED) {
var ui = undefined;
var utils = require('./ui');
function model(node, config) {
return function (msg, value) {
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);
}
return {
msg: {
payload: {
action: action,
value: val
}
}
};
};
}
function view(node, config) {
var id = "light_" + node.id.replace(/[^\w]/g, "");
var height = 1;
if (config.height > 0) height = config.height;
var size = height * 24;
return String.raw`
<style>
#${id} span {
display: block;
margin: 0 auto;
width: ${size}px;
height: ${size}px;
content:;
background-color: #666;
border-top: 1px solid rgba(255,255,255,0.8);
-moz-border-radius: 100px;
-webkit-border-radius: 100px;
border-radius: 100px;
-moz-box-shadow: 0 0 4px rgba(0,0,0,0.8);
-webkit-box-shadow: 0 0 4px rgba(0,0,0,0.8);
box-shadow: 0 0 4px rgba(0,0,0,0.8);
background-image: -webkit-gradient(radial, 50% 0, 100, 50% 0, 0, from(rgba(255,255,255,0)), to(rgba(255,255,255,0.38)));
background-image: -moz-radial-gradient(top, ellipse cover, rgba(255,255,255,0.38) 0%, rgba(255,255,255,0) 100%);
background-image: -webkit-radial-gradient(top, ellipse cover, rgba(255,255,255,0.38) 0%, rgba(255,255,255,0) 100%);
background-image: radial-gradient(top, ellipse cover, rgba(255,255,255,0.38) 0%, rgba(255,255,255,0) 100%);
}
</style>
<div id="${id}"><span></span></div>
`;
}
function controller(node, config) {
var id = node.id.replace(/[^\w]/g, "");
var fn = String.raw`
${utils.literals.observeDOM("const observeDOM")}
const updateWithScope = (msg) => {
if (document) {
const attemptUpdate = () => {
const elem = document.getElementById('light_${id}');
if (elem) {
var card = elem.closest("md-card");
card.classList.remove("nr-dashboard-template");
card.classList.add("nr-dashboard-textinput");
var actions = {
element: elem,
set: function(msg) {
if (msg == null) return;
var color = msg.payload.value;
elem.firstElementChild.style.backgroundColor = color;
},
disable: function() { elem.disabled = true; card.classList.add("nr-dashboard-disabled"); },
enable: function() { elem.disabled = false; card.classList.remove("nr-dashboard-disabled"); }
};
var action = 'set';
if (msg && typeof msg.payload === 'object' && msg.payload.hasOwnProperty("action") && typeof msg.payload.action === 'string') {
action = msg.payload.action.toLowerCase();
}
if (typeof actions[action] === 'function') {
if (actions[action].length === 0) actions[action]();
else actions[action](msg);
}
if (typeof window._nrui === 'undefined') window._nrui = {};
window._nrui['_${id}'] = actions;
} else {
// HACK: is there a proper way to wait for this node's element to be rendered?
observeDOM(document, (change) => attemptUpdate());
}
};
attemptUpdate();
}
};
$scope.$watch('msg', updateWithScope);
`;
return Function("$scope", "events", fn);
};
function UiLight(config) {
if (ui === undefined) {
ui = RED.require("node-red-dashboard")(RED);
}
RED.nodes.createNode(this, config);
var node = this;
var height = 1;
if (config.height > 0) height = config.height;
// https://github.com/node-red/node-red-dashboard/blob/master/ui.js
var done = ui.addWidget({
node: node, // controlling node
order: config.order, // placeholder for position in page
group: config.group, // belonging Dashboard group
width: config.width, // width of widget
height: height, // height of widget
format: view(node, config), // HTML/Angular code
templateScope: "local", // scope of HTML/Angular(local/global)*
emitOnlyNewValues: false,
forwardInputMessages: false,
storeFrontEndInputAsState: false,
beforeEmit: model(node, config),
initController: controller(node, config),
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("ui_ui-light", UiLight);
};