1
1
module . exports = function ( RED ) {
2
+ const uuid = require ( 'uuid' ) ;
3
+
2
4
// ---- elements of the reference config ----
3
5
4
6
// Datatypes defined by TypedInput widget
@@ -43,7 +45,7 @@ module.exports = function(RED) {
43
45
}
44
46
45
47
function buildNodeStatus ( node , msgProperty , blockFlow ) {
46
- let storage = node . config . storage ;
48
+ let storage = node . valueConfig . storage ;
47
49
if ( storage === kStorageDefault ) {
48
50
if ( RED . settings . contextStorage !== undefined && RED . settings . contextStorage . default !== undefined ) {
49
51
storage += ` (${ RED . settings . contextStorage . default } )` ;
@@ -53,7 +55,7 @@ module.exports = function(RED) {
53
55
node . status ( {
54
56
fill : `${ blockFlow ? 'red' : 'green' } ` ,
55
57
shape : 'dot' ,
56
- text : `${ msgProperty } [${ kSupportedDatatypesByTypedInput [ node . config . datatype ] } ,${ node . config . scope } ,${ storage } ]` ,
58
+ text : `${ msgProperty } [${ kSupportedDatatypesByTypedInput [ node . valueConfig . datatype ] } ,${ node . valueConfig . scope } ,${ storage } ]` ,
57
59
} ) ;
58
60
}
59
61
@@ -81,10 +83,10 @@ module.exports = function(RED) {
81
83
82
84
function getUsedContext ( node ) {
83
85
let context = node . context ( ) ;
84
- if ( node . config . scope === 'flow' ) {
86
+ if ( node . valueConfig . scope === 'flow' ) {
85
87
context = context . flow ;
86
88
}
87
- if ( node . config . scope === 'global' ) {
89
+ if ( node . valueConfig . scope === 'global' ) {
88
90
context = context . global ;
89
91
}
90
92
return context ;
@@ -98,24 +100,24 @@ module.exports = function(RED) {
98
100
99
101
function getContext ( node , context , contextKey ) {
100
102
let currentValue = undefined ;
101
- if ( node . config . storage === kStorageDefault ) {
103
+ if ( node . valueConfig . storage === kStorageDefault ) {
102
104
currentValue = context . get ( contextKey ) ;
103
105
} else {
104
- currentValue = context . get ( contextKey , node . config . storage ) ;
106
+ currentValue = context . get ( contextKey , node . valueConfig . storage ) ;
105
107
}
106
108
107
109
// Apply default value if context contains no value
108
110
if ( currentValue === undefined ) {
109
- currentValue = node . config . default ;
111
+ currentValue = node . valueConfig . default ;
110
112
}
111
113
return currentValue ;
112
114
}
113
115
114
116
function setContext ( node , context , contextKey , newValue ) {
115
- if ( node . config . storage === kStorageDefault ) {
117
+ if ( node . valueConfig . storage === kStorageDefault ) {
116
118
context . set ( contextKey , newValue ) ;
117
119
} else {
118
- context . set ( contextKey , newValue , node . config . storage ) ;
120
+ context . set ( contextKey , newValue , node . valueConfig . storage ) ;
119
121
}
120
122
}
121
123
@@ -134,7 +136,7 @@ module.exports = function(RED) {
134
136
135
137
function convertToExpectedType ( node , value ) {
136
138
let convertedValue = undefined ;
137
- switch ( node . config . datatype ) {
139
+ switch ( node . valueConfig . datatype ) {
138
140
case kConfigDatatypeBool :
139
141
if ( value === 'true' ) {
140
142
convertedValue = true ;
@@ -156,11 +158,11 @@ module.exports = function(RED) {
156
158
}
157
159
break ;
158
160
default :
159
- node . error ( `Unsupported compare value type '${ node . config . datatype } ' configured!` ) ;
161
+ node . error ( `Unsupported compare value type '${ node . valueConfig . datatype } ' configured!` ) ;
160
162
}
161
163
162
164
if ( convertedValue === undefined ) {
163
- node . error ( `Failed to convert value '${ value } ' to expected datatype '${ node . config . datatype } '!` ) ;
165
+ node . error ( `Failed to convert value '${ value } ' to expected datatype '${ node . valueConfig . datatype } '!` ) ;
164
166
}
165
167
166
168
return convertedValue ;
@@ -169,7 +171,7 @@ module.exports = function(RED) {
169
171
function compareToConfiguredDatatype ( node , value ) {
170
172
const typeOfValue = typeof value ;
171
173
return kSupportedDatatypesLanguageType . hasOwnProperty ( typeOfValue ) &&
172
- ( kSupportedDatatypesLanguageType [ typeOfValue ] === node . config . datatype ) ;
174
+ ( kSupportedDatatypesLanguageType [ typeOfValue ] === node . valueConfig . datatype ) ;
173
175
}
174
176
175
177
function checkBlockIfCondition ( node , currentValue ) {
@@ -213,14 +215,28 @@ module.exports = function(RED) {
213
215
return null ;
214
216
}
215
217
216
- node . config = configNode . values . find ( ( value ) => value . name === nodeConfig . value ) ;
217
- node . configName = configNode . name ; // Name of the referenced configuration
218
- node . value = nodeConfig . value ; // selected value
219
- if ( node . value === '' || node . value === undefined || node . value === null ) {
218
+ // Selected value by ID or name
219
+ node . valueId = nodeConfig . valueId ;
220
+ node . value = nodeConfig . value ;
221
+ if ( node . valueId !== undefined && ! uuid . validate ( node . valueId ) ) {
222
+ reportIncorrectConfiguration ( node ) ;
223
+ return null ;
224
+ }
225
+ // Selectd value must be referenced via ID or name (deprecated)
226
+ if ( node . valueId === undefined && ( node . value === undefined ) ) {
220
227
reportIncorrectConfiguration ( node ) ;
221
228
return null ;
222
229
}
223
230
231
+ node . configName = configNode . name ; // Name of the referenced configuration
232
+ if ( node . valueId ) {
233
+ node . valueConfig = configNode . values . find ( ( value ) => value . id === node . valueId ) ;
234
+ node . value = node . valueConfig . name ; // Update name again in case of inconsistent value / ID config
235
+ } else {
236
+ // Until version 1.1.0 no ID was existing for every value. Therefore search via value name.
237
+ node . valueConfig = configNode . values . find ( ( value ) => value . name === node . value ) ;
238
+ }
239
+
224
240
node . command = nodeConfig . command || kCommandDefault ;
225
241
node . msgProperty = nodeConfig . msgProperty || kMsgPropertyDefault ;
226
242
@@ -241,7 +257,7 @@ module.exports = function(RED) {
241
257
let currentValue = getContext ( node , context , contextKey ) ;
242
258
if ( ! compareToConfiguredDatatype ( node , currentValue ) ) {
243
259
node . warn ( `Persisted value ${ node . configName } / ${ node . value } does not have the configured datatype ` +
244
- `'${ node . config . datatype } '!` ) ;
260
+ `'${ node . valueConfig . datatype } '!` ) ;
245
261
}
246
262
247
263
let onChangeMsg = null ;
@@ -265,7 +281,7 @@ module.exports = function(RED) {
265
281
266
282
if ( ! compareToConfiguredDatatype ( node , inputValue ) ) {
267
283
node . error ( `Passed value in msg.${ node . msgProperty } does not have the configured datatype ` +
268
- `'${ node . config . datatype } '! Persistent value config: ${ node . configName } / ${ node . value } ` ) ;
284
+ `'${ node . valueConfig . datatype } '! Persistent value config: ${ node . configName } / ${ node . value } ` ) ;
269
285
return ;
270
286
}
271
287
0 commit comments