-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebthingsio-execute-action.html
176 lines (171 loc) · 8.35 KB
/
webthingsio-execute-action.html
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
<script type="text/javascript">
window.RED.nodes.registerType('webthingsio-execute-action', {
category: 'Web Things IO',
color: '#5d9bc7',
defaults: {
/* eslint-disable no-undefined */
name: {value: ''},
gateway: {value: '', type: 'webthingsio-gateway'},
thing: {value: undefined, validate: (v) => v ? true : false},
action: {value: undefined, validate: (v) => v ? true : false},
inputMode: {value: undefined},
input: {value: undefined},
inputt: {value: undefined},
},
inputs: 1,
outputs: 0,
icon: 'webthingsio.svg',
label: function() {
function limitlen(s) {
if (s.length > RED.settings.webthingsioGatewayLimitInputLen) {
s = `${
s.substring(0, RED.settings.webthingsioGatewayLimitInputLen)
}...`;
}
return s;
}
if (this.name) {
return this.name;
} else if (this.gateway && this.thing && this.action) {
let base, baseWith, baseWithout;
const decodedThing = decodeURIComponent(this.thing);
if (RED.settings.webthingsioGatewayShorterLabels) {
base = this._('webthingsio-execute-action.shortLabel')
.replace('%action', this.action)
.replace('%thing', decodedThing);
baseWith = this._('webthingsio-execute-action.shortLabelWith')
.replace('%action', this.action)
.replace('%thing', decodedThing);
// eslint-disable-next-line max-len
baseWithout = this._('webthingsio-execute-action.shortLabelWithout')
.replace('%action', this.action)
.replace('%thing', decodedThing);
} else {
base = this._('webthingsio-execute-action.longLabel')
.replace('%action', this.action)
.replace('%thing', decodedThing);
baseWith = this._('webthingsio-execute-action.longLabelWith')
.replace('%action', this.action)
.replace('%thing', decodedThing);
// eslint-disable-next-line max-len
baseWithout = this._('webthingsio-execute-action.longLabelWithout')
.replace('%action', this.action)
.replace('%thing', decodedThing);
}
if (!this.input && this.inputMode !== 'injected') {
return baseWithout;
}
if (RED.settings.webthingsioGatewayLimitInputLen <= 0) {
return base;
}
switch (this.inputMode) {
case 'injected':
return base;
case 'fixed':
return baseWith
.replace('%input', limitlen(this.input));
case 'advanced':
if (
['msg', 'flow', 'global', 'env'].includes(this.inputt)
) {
return baseWith
.replace(
'%input',
`${this.inputt}.${limitlen(this.input)}`,
);
} else if (['json', 'jsonata'].includes(this.inputt)) {
return baseWith
.replace('%input', limitlen(this.input));
}
}
}
return this._('webthingsio-execute-action.defaultLabel');
},
paletteLabel: 'Execute action',
oneditprepare: async function() {
async function initializeInputs(node) {
if (
$('#node-input-gateway')[0].options.length > 1 &&
!node.gateway
) {
$('#node-input-gateway')[0].selectedIndex = 0;
$('#node-input-gateway').removeClass('input-error');
}
await webthingsio.updateThing(false);
$('#node-input--thing').prop('value', node.thing);
await webthingsio.updateAction(false);
$('#node-input--action').prop('value', node.action);
await webthingsio.updateActionInput(false);
webthingsio.initializeInput(node.input, node.inputt);
$('#node-input--inputMode').prop('value', node.inputMode);
await webthingsio.inputModeChanged('action');
}
function addListeners() {
$('#node-input-gateway')[0].addEventListener(
'change',
() => webthingsio.updateThing(),
);
$('#node-input--thing')[0].addEventListener(
'change',
() => webthingsio.updateAction(),
);
$('#node-input--action')[0].addEventListener(
'change',
() => webthingsio.updateActionInput(),
);
$('#node-input--inputMode')[0].addEventListener(
'change',
() => webthingsio.inputModeChanged('action'),
);
}
$('#node-input--input-advanced').typedInput({
type: 'msg',
types: ['msg', 'flow', 'global', 'json', 'jsonata', 'env'],
typeField: '#node-input--input-advanced-type',
});
await initializeInputs(this);
addListeners();
},
oneditsave: function() {
webthingsio.saveThing(this);
webthingsio.saveAction(this);
webthingsio.saveInputMode(this);
webthingsio.saveInput(this);
},
});
</script>
<script type="text/html" data-template-name="webthingsio-execute-action">
<div class="form-row" id="node-errorGatewayConnect" data-i18n="webthingsio-execute-action.errorGatewayConnect" style="display: none; color: red;">
</div>
<div class="form-row">
<label for="node-input-name" data-i18n="webthingsio-execute-action.labelName"><i class="fa fa-tag"></i></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]webthingsio-execute-action.placeholderName">
</div>
<div class="form-row">
<label for="node-input-gateway" data-i18n="webthingsio-execute-action.labelGateway"><i class="fa fa-tag"></i></label>
<input type="text" id="node-input-gateway">
</div>
<div class="form-row" id="thing-row" style="display: none;">
<label for="node-input--thing" data-i18n="webthingsio-execute-action.labelThing"><i class="fa fa-tag"></i></label>
<select id="node-input--thing" style="width: 70%;"></select>
</div>
<div class="form-row" id="action-row" style="display: none;">
<label for="node-input--action" data-i18n="webthingsio-execute-action.labelAction"><i class="fa fa-tag"></i></label>
<select id="node-input--action" style="width: 70%;"></select>
</div>
<div class="form-row" id="inputMode-row" style="display: none;">
<select id="node-input--inputMode" style="width: calc(100px + 70%);">
<option value="injected" data-i18n="webthingsio-execute-action.optionInjectedInput"></option>
<option value="fixed" data-i18n="webthingsio-execute-action.optionFixedInput"></option>
<option value="advanced" data-i18n="webthingsio-execute-action.optionAdvancedInput"></option>
</select>
</div>
<div class="form-row" id="input-row" style="display: none;">
<label for="node-input--input" data-i18n="webthingsio-execute-action.labelInput"><i class="fa fa-tag"></i></label>
</div>
<div class="form-row" id="input-advanced-row" style="display: none;">
<label for="node-input--input-advanced" data-i18n="webthingsio-execute-action.labelInput"><i class="fa fa-tag"></i></label>
<input type="" id="node-input--input-advanced" style="position: absolute; bottom: 9999px; right: 9999px">
<input type="hidden" id="node-input--input-advanced-type">
</div>
</script>