-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtext-input.js
192 lines (141 loc) · 6.93 KB
/
text-input.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
'use strict';
// References:
//https://github.com/node-red/node-red-dashboard/wiki/Creating-New-Dashboard-Widgets
//https://github.com/Adorkable/node-red-contrib-ui-led/blob/master/ledUtility.js
module.exports = function (RED) {
var ui = undefined;
var utils = require('./ui');
function model(config, node) {
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(config) {
var id = "txt_" + config.id.replace(/[^\w]/g, "");
var delay = config.delay || 0;
return String.raw`
<md-input-container class="md-block" flex md-is-error="false" ng-class="{'has-label': config.label}">
<label ng-bind-html="config.label"></label>
<md-tooltip ng-if="config.tooltip" md-delay="700" md-direction="bottom" ng-bind-html="config.tooltip"></md-tooltip>
<input type="${config.mode}" id="${id}" ng-model="value" ng-change="valueChanged(value)" ng-model-options="{ 'timezone': 'UTC', debounce: ${delay} }" aria-label="{{config.label}}" style="z-index:1" />
</md-input-container>
`;
}
function controller(node, config) {
var id = node.id.replace(/[^\w]/g, "");
var passthru = config.passthru ? 'true' : 'false';
var changed = config.change ? '$scope.send({payload: value});' : '';
var fn = String.raw`
${utils.literals.observeDOM("const observeDOM")}
$scope.value = "";
$scope.valueChanged = function (value) { ${changed} };
const updateWithScope = (msg) => {
var config = $scope.$eval("config");
if (!config) {
config = ${JSON.stringify(config)};
$scope.config = config;
}
if (document) {
const attemptUpdate = () => {
var id = "txt_" + config.id.replace(/[^\w]/g, "");
const elem = document.getElementById(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;
if (typeof msg.payload === 'string' || typeof msg.payload === 'number') {
$scope.value = msg.payload.toString();
if (${passthru} && msg.passthru !== false) {
$scope.send({payload: $scope.value});
}
}
else if (typeof msg.payload === 'object') {
$scope.value = msg.payload.value;
if (${passthru} && msg.payload.passthru !== false) {
$scope.send({payload: $scope.value});
}
}
},
get: function() { $scope.send({ payload: elem.value }); },
retrieve: function() { return elem.value; },
hide: function() { card.style.display = 'none'; },
show: function() { card.style.display = ''; },
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 TextInput(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(config), // HTML/Angular code
templateScope: "local", // scope of HTML/Angular(local/global)*
emitOnlyNewValues: false,
forwardInputMessages: false,
storeFrontEndInputAsState: false,
beforeEmit: model(config, node),
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_text-input", TextInput);
};