I copied the original node and I need to modify it, but it contains internationalized content. How should I deal with it?
I also copied the folder for international content
This is my current directory structure
I copied the original node and I need to modify it, but it contains internationalized content. How should I deal with it?
I also copied the folder for international content
This is my current directory structure
The docs:
https://nodered.org/docs/creating-nodes/i18n
But I have trouble understanding your question.
I checked this document and put the locales directory at the same level as the js file as described in it, but it still doesn't seem to work. Now the internationalization mark in the node content is "node type/node name: corresponding internationalization content", and it seems that I haven't found the correct path
**
**
nodes
┣ native-node
┃ ┣ locales
┃ ┃ ┣ function-custom
┃ ┃ ┃ ┗ zh-CN
┃ ┃ ┃ ┃ ┗ messages.json
┃ ┃ ┗ switch-custom
┃ ┃ ┃ ┗ zh-CN
┃ ┃ ┃ ┃ ┗ messages.json
┃ ┣ function-custom.html
┃ ┣ function-custom.js
┃ ┣ package.json
┃ ┣ switch-custom.html
┃ ┗ switch-custom.js
**
**
module.exports = function(RED) {
"use strict";
var operators = {
'eq': function(a, b) { return a == b; },
'neq': function(a, b) { return a != b; },
'lt': function(a, b) { return a < b; },
'lte': function(a, b) { return a <= b; },
'gt': function(a, b) { return a > b; },
'gte': function(a, b) { return a >= b; },
'btwn': function(a, b, c) { return (a >= b && a <= c) || (a <= b && a >= c); },
'cont': function(a, b) { return (a + "").indexOf(b) != -1; },
'regex': function(a, b, c, d) { return (a + "").match(new RegExp(b,d?'i':'')); },
'true': function(a) { return a === true; },
'false': function(a) { return a === false; },
'null': function(a) { return (typeof a == "undefined" || a === null); },
'nnull': function(a) { return (typeof a != "undefined" && a !== null); },
'empty': function(a) {
if (typeof a === 'string' || Array.isArray(a) || Buffer.isBuffer(a)) {
return a.length === 0;
} else if (typeof a === 'object' && a !== null) {
return Object.keys(a).length === 0;
}
return false;
},
'nempty': function(a) {
if (typeof a === 'string' || Array.isArray(a) || Buffer.isBuffer(a)) {
return a.length !== 0;
} else if (typeof a === 'object' && a !== null) {
return Object.keys(a).length !== 0;
}
return false;
},
'istype': function(a, b) {
if (b === "array") { return Array.isArray(a); }
else if (b === "buffer") { return Buffer.isBuffer(a); }
else if (b === "json") {
try { JSON.parse(a); return true; } // or maybe ??? a !== null; }
catch(e) { return false;}
}
else if (b === "null") { return a === null; }
else if (b === "number") { return typeof a === b && !isNaN(a) }
else { return typeof a === b && !Array.isArray(a) && !Buffer.isBuffer(a) && a !== null; }
},
'head': function(a, b, c, d, parts) {
var count = Number(b);
return (parts.index < count);
},
'tail': function(a, b, c, d, parts) {
var count = Number(b);
return (parts.count -count <= parts.index);
},
'index': function(a, b, c, d, parts) {
var min = Number(b);
var max = Number(c);
var index = parts.index;
return ((min <= index) && (index <= max));
},
'hask': function(a, b) {
return a !== undefined && a !== null && (typeof b !== "object" ) && a.hasOwnProperty(b+"");
},
'jsonata_exp': function(a, b) { return (b === true); },
'else': function(a) { return a === true; }
};
var _maxKeptCount;
function getMaxKeptCount() {
if (_maxKeptCount === undefined) {
var name = "nodeMessageBufferMaxLength";
if (RED.settings.hasOwnProperty(name)) {
_maxKeptCount = RED.settings[name];
}
else {
_maxKeptCount = 0;
}
}
return _maxKeptCount;
}
function getProperty(node,msg,done) {
if (node.propertyType === 'jsonata') {
RED.util.evaluateJSONataExpression(node.property,msg,(err,value) => {
if (err) {
done(RED._("switch.errors.invalid-expr",{error:err.message}));
} else {
done(undefined,value);
}
});
} else {
RED.util.evaluateNodeProperty(node.property,node.propertyType,node,msg,(err,value) => {
if (err) {
done(undefined,undefined);
} else {
done(undefined,value);
}
});
}
}
function getV1(node,msg,rule,hasParts,done) {
if (rule.vt === 'prev') {
return done(undefined,node.previousValue);
} else if (rule.vt === 'jsonata') {
var exp = rule.v;
if (rule.t === 'jsonata_exp') {
if (hasParts) {
exp.assign("I", msg.parts.index);
exp.assign("N", msg.parts.count);
}
}
RED.util.evaluateJSONataExpression(exp,msg,(err,value) => {
if (err) {
done(RED._("switch.errors.invalid-expr",{error:err.message}));
} else {
done(undefined, value);
}
});
} else if (rule.vt === 'json') {
done(undefined,"json"); // TODO: ?! invalid case
} else if (rule.vt === 'null') {
done(undefined,"null");
} else {
RED.util.evaluateNodeProperty(rule.v,rule.vt,node,msg, function(err,value) {
if (err) {
done(undefined, undefined);
} else {
done(undefined, value);
}
});
}
}
function getV2(node,msg,rule,done) {
var v2 = rule.v2;
if (rule.v2t === 'prev') {
return done(undefined,node.previousValue);
} else if (rule.v2t === 'jsonata') {
RED.util.evaluateJSONataExpression(rule.v2,msg,(err,value) => {
if (err) {
done(RED._("switch.errors.invalid-expr",{error:err.message}));
} else {
done(undefined,value);
}
});
} else if (typeof v2 !== 'undefined') {
RED.util.evaluateNodeProperty(rule.v2,rule.v2t,node,msg, function(err,value) {
if (err) {
done(undefined,undefined);
} else {
done(undefined,value);
}
});
} else {
done(undefined,v2);
}
}
function applyRule(node, msg, property, state, done) {
var rule = node.rules[state.currentRule];
var v1,v2;
getV1(node,msg,rule,state.hasParts, (err,value) => {
if (err) {
// This only happens if v1 is an invalid JSONata expr
// But that will have already been logged and the node marked
// invalid as part of the constructor
return done(err);
}
v1 = value;
getV2(node,msg,rule, (err,value) => {
if (err) {
// This only happens if v1 is an invalid JSONata expr
// But that will have already been logged and the node marked
// invalid as part of the constructor
return done(err);
}
v2 = value;
if (rule.t == "else") {
property = state.elseflag;
state.elseflag = true;
}
try {
if (operators[rule.t](property,v1,v2,rule.case,msg.parts)) {
state.onward.push(msg);
state.elseflag = false;
if (node.checkall == "false") {
return done(undefined,false);
}
} else {
state.onward.push(null);
}
done(undefined, state.currentRule < node.rules.length - 1);
} catch(err) {
// An error occurred evaluating the rule - for example, an
// invalid RegExp value.
done(err);
}
});
});
}
function applyRules(node, msg, property,state,done) {
if (!state) {
if (node.rules.length === 0) {
done(undefined, []);
return;
}
state = {
currentRule: 0,
elseflag: true,
onward: [],
hasParts: msg.hasOwnProperty("parts") &&
msg.parts.hasOwnProperty("id") &&
msg.parts.hasOwnProperty("index")
}
}
applyRule(node,msg,property,state,(err,hasMore) => {
if (err) {
return done(err);
}
if (hasMore) {
state.currentRule++;
applyRules(node,msg,property,state,done);
} else {
node.previousValue = property;
done(undefined,state.onward);
}
});
}
function SwitchNode(n) {
RED.nodes.createNode(this, n);
this.rules = n.rules || [];
this.property = n.property;
this.propertyType = n.propertyType || "msg";
if (this.propertyType === 'jsonata') {
try {
this.property = RED.util.prepareJSONataExpression(this.property,this);
} catch(err) {
this.error(RED._("switch.errors.invalid-expr",{error:err.message}));
return;
}
}
this.checkall = n.checkall || "true";
this.previousValue = null;
var node = this;
var valid = true;
var repair = n.repair;
var needsCount = repair;
for (var i=0; i<this.rules.length; i+=1) {
var rule = this.rules[i];
needsCount = needsCount || ((rule.t === "tail"));
if (!rule.vt) {
if (!isNaN(Number(rule.v))) {
rule.vt = 'num';
} else {
rule.vt = 'str';
}
}
if (rule.vt === 'num') {
if (!isNaN(Number(rule.v))) {
rule.v = Number(rule.v);
}
} else if (rule.vt === "jsonata") {
try {
rule.v = RED.util.prepareJSONataExpression(rule.v,node);
} catch(err) {
this.error(RED._("switch.errors.invalid-expr",{error:err.message}));
valid = false;
}
}
if (typeof rule.v2 !== 'undefined') {
if (!rule.v2t) {
if (!isNaN(Number(rule.v2))) {
rule.v2t = 'num';
} else {
rule.v2t = 'str';
}
}
if (rule.v2t === 'num') {
rule.v2 = Number(rule.v2);
} else if (rule.v2t === 'jsonata') {
try {
rule.v2 = RED.util.prepareJSONataExpression(rule.v2,node);
} catch(err) {
this.error(RED._("switch.errors.invalid-expr",{error:err.message}));
valid = false;
}
}
}
}
if (!valid) {
return;
}
var pendingCount = 0;
var pendingId = 0;
var pendingIn = {};
var pendingOut = {};
var received = {};
function addMessageToGroup(id, msg, parts) {
if (!(id in pendingIn)) {
pendingIn[id] = {
count: undefined,
msgs: [],
seq_no: pendingId++
};
}
var group = pendingIn[id];
group.msgs.push(msg);
pendingCount++;
var max_msgs = getMaxKeptCount();
if ((max_msgs > 0) && (pendingCount > max_msgs)) {
clearPending();
node.error(RED._("switch.errors.too-many"), msg);
}
if (parts.hasOwnProperty("count")) {
group.count = parts.count;
}
return group;
}
function drainMessageGroup(msgs,count,done) {
var msg = msgs.shift();
msg.parts.count = count;
processMessage(msg,false, err => {
if (err) {
done(err);
} else {
if (msgs.length === 0) {
done()
} else {
drainMessageGroup(msgs,count,done);
}
}
})
}
function addMessageToPending(msg,done) {
var parts = msg.parts;
// We've already checked the msg.parts has the require bits
var group = addMessageToGroup(parts.id, msg, parts);
var msgs = group.msgs;
var count = group.count;
var msgsCount = msgs.length;
if (count === msgsCount) {
// We have a complete group - send the individual parts
drainMessageGroup(msgs,count,err => {
pendingCount -= msgsCount;
delete pendingIn[parts.id];
done();
})
return;
}
done();
}
function sendGroup(onwards, port_count) {
var counts = new Array(port_count).fill(0);
for (var i = 0; i < onwards.length; i++) {
var onward = onwards[i];
for (var j = 0; j < port_count; j++) {
counts[j] += (onward[j] !== null) ? 1 : 0
}
}
var ids = new Array(port_count);
for (var j = 0; j < port_count; j++) {
ids[j] = RED.util.generateId();
}
var ports = new Array(port_count);
var indexes = new Array(port_count).fill(0);
for (var i = 0; i < onwards.length; i++) {
var onward = onwards[i];
for (var j = 0; j < port_count; j++) {
var msg = onward[j];
if (msg) {
var new_msg = RED.util.cloneMessage(msg);
var parts = new_msg.parts;
parts.id = ids[j];
parts.index = indexes[j];
parts.count = counts[j];
ports[j] = new_msg;
indexes[j]++;
}
else {
ports[j] = null;
}
}
node.send(ports);
}
}
function sendGroupMessages(onward, msg) {
var parts = msg.parts;
var gid = parts.id;
received[gid] = ((gid in received) ? received[gid] : 0) +1;
var send_ok = (received[gid] === parts.count);
if (!(gid in pendingOut)) {
pendingOut[gid] = {
onwards: []
};
}
var group = pendingOut[gid];
var onwards = group.onwards;
onwards.push(onward);
pendingCount++;
if (send_ok) {
sendGroup(onwards, onward.length, msg);
pendingCount -= onward.length;
delete pendingOut[gid];
delete received[gid];
}
var max_msgs = getMaxKeptCount();
if ((max_msgs > 0) && (pendingCount > max_msgs)) {
clearPending();
node.error(RED._("switch.errors.too-many"), msg);
}
}
function processMessage(msg, checkParts, done) {
var hasParts = msg.hasOwnProperty("parts") &&
msg.parts.hasOwnProperty("id") &&
msg.parts.hasOwnProperty("index");
if (needsCount && checkParts && hasParts) {
addMessageToPending(msg,done);
} else {
getProperty(node,msg,(err,property) => {
if (err) {
node.warn(err);
done();
} else {
applyRules(node,msg,property,undefined,(err,onward) => {
if (err) {
node.error(err, msg);
} else {
if (!repair || !hasParts) {
node.send(onward);
} else {
sendGroupMessages(onward, msg);
}
}
done();
});
}
});
}
}
function clearPending() {
pendingCount = 0;
pendingId = 0;
pendingIn = {};
pendingOut = {};
received = {};
}
var pendingMessages = [];
var handlingMessage = false;
var processMessageQueue = function(msg) {
if (msg) {
// A new message has arrived - add it to the message queue
pendingMessages.push(msg);
if (handlingMessage) {
// The node is currently processing a message, so do nothing
// more with this message
return;
}
}
if (pendingMessages.length === 0) {
// There are no more messages to process, clear the active flag
// and return
handlingMessage = false;
return;
}
// There are more messages to process. Get the next message and
// start processing it. Recurse back in to check for any more
var nextMsg = pendingMessages.shift();
handlingMessage = true;
processMessage(nextMsg,true,err => {
if (err) {
node.error(err,nextMsg);
}
processMessageQueue()
});
}
this.on('input', function(msg) {
processMessageQueue(msg);
});
this.on('close', function() {
clearPending();
});
}
RED.nodes.registerType("switch-custom", SwitchNode);
}
**
**
<script type="text/html" data-template-name="switch-custom">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
<input type="text" id="node-input-name" style="width: calc(100% - 105px)" data-i18n="[placeholder]common.label.name">
</div>
<div class="form-row">
<label for="node-input-property"><i class="fa fa-ellipsis-h"></i> <span data-i18n="switch.label.property"></span></label>
<input type="text" id="node-input-property" style="width: calc(100% - 105px)"/>
<input type="hidden" id="node-input-outputs"/>
</div>
<div class="form-row node-input-rule-container-row">
<ol id="node-input-rule-container"></ol>
</div>
<div class="form-row">
<select id="node-input-checkall" style="width:100%; margin-right:5px;">
<option value="true" data-i18n="switch.checkall"></option>
<option value="false" data-i18n="switch.stopfirst"></option>
</select>
</div>
<div class="form-row">
<input type="checkbox" id="node-input-repair" style="display: inline-block; width: auto; vertical-align: top;">
<label style="width: auto;" for="node-input-repair"><span data-i18n="switch.label.repair"></span></label></input>
</div>
</script>
<script type="text/javascript">
(function() {
var operators = [
{v:"eq",t:"==",kind:'V'},
{v:"neq",t:"!=",kind:'V'},
{v:"lt",t:"<",kind:'V'},
{v:"lte",t:"<=",kind:'V'},
{v:"gt",t:">",kind:'V'},
{v:"gte",t:">=",kind:'V'},
{v:"hask",t:"switch.rules.hask",kind:'V'},
{v:"btwn",t:"switch.rules.btwn",kind:'V'},
{v:"cont",t:"switch.rules.cont",kind:'V'},
{v:"regex",t:"switch.rules.regex",kind:'V'},
{v:"true",t:"switch.rules.true",kind:'V'},
{v:"false",t:"switch.rules.false",kind:'V'},
{v:"null",t:"switch.rules.null",kind:'V'},
{v:"nnull",t:"switch.rules.nnull",kind:'V'},
{v:"istype",t:"switch.rules.istype",kind:'V'},
{v:"empty",t:"switch.rules.empty",kind:'V'},
{v:"nempty",t:"switch.rules.nempty",kind:'V'},
{v:"head",t:"switch.rules.head",kind:'S'},
{v:"index",t:"switch.rules.index",kind:'S'},
{v:"tail",t:"switch.rules.tail",kind:'S'},
{v:"jsonata_exp",t:"switch.rules.exp",kind:'O'},
{v:"else",t:"switch.rules.else",kind:'O'}
];
var previousValueType = {value:"prev",label:RED._("node-red:switch.previous"),hasValue:false};
function clipValueLength(v) {
if (v.length > 15) {
return v.substring(0,15)+"...";
}
return v;
}
function prop2name(key) {
var result = RED.utils.parseContextKey(key);
return result.key;
}
function getValueLabel(t,v) {
if (t === 'str') {
return '"'+clipValueLength(v)+'"';
} else if (t === 'msg') {
return t+"."+clipValueLength(v);
} else if (t === 'flow' || t === 'global') {
return t+"."+clipValueLength(prop2name(v));
}
return clipValueLength(v);
}
function exportRule(rule) {
var type = rule.find("select").val();
var r = {t:type};
if (!(type === "true" || type === "false" || type === "null" || type === "nnull" || type === "empty" || type === "nempty" || type === "else")) {
if ((type === "btwn") || (type === "index")) {
r.v = rule.find(".node-input-rule-btwn-value").typedInput('value');
r.vt = rule.find(".node-input-rule-btwn-value").typedInput('type');
r.v2 = rule.find(".node-input-rule-btwn-value2").typedInput('value');
r.v2t = rule.find(".node-input-rule-btwn-value2").typedInput('type');
} else if ((type === "head") || (type === "tail")) {
r.v = rule.find(".node-input-rule-num-value").typedInput('value');
r.vt = rule.find(".node-input-rule-num-value").typedInput('type');
} else if (type === "istype") {
r.v = rule.find(".node-input-rule-type-value").typedInput('type');
r.vt = rule.find(".node-input-rule-type-value").typedInput('type');
} else if (type === "jsonata_exp") {
r.v = rule.find(".node-input-rule-exp-value").typedInput('value');
r.vt = rule.find(".node-input-rule-exp-value").typedInput('type');
} else {
r.v = rule.find(".node-input-rule-value").typedInput('value');
r.vt = rule.find(".node-input-rule-value").typedInput('type');
}
if (type === "regex") {
r.case = rule.find(".node-input-rule-case").prop("checked");
}
}
return r;
}
function createValueField(row, defaultType){
return $('<input/>',{class:"node-input-rule-value",type:"text",style:"width: 100%;"}).appendTo(row)
.typedInput({default:defaultType||'str',types:['msg','flow','global','str','num','jsonata','env',previousValueType]});
}
function createNumValueField(row, defaultType){
return $('<input/>',{class:"node-input-rule-num-value",type:"text",style:"width: 100%;"}).appendTo(row)
.typedInput({default:defaultType||'num',types:['flow','global','num','jsonata','env']});
}
function createExpValueField(row){
return $('<input/>',{class:"node-input-rule-exp-value",type:"text",style:"width: 100%;"}).appendTo(row)
.typedInput({default:'jsonata',types:['jsonata']});
}
function createBtwnValueField(row, defaultType){
return $('<input/>',{class:"node-input-rule-btwn-value",type:"text",style:"width: 100%;"}).appendTo(row)
.typedInput({default:defaultType||'num',types:['msg','flow','global','str','num','jsonata','env',previousValueType]});
}
function createBtwnValue2Field(row3, andLabel, defaultType){
$('<div/>',{class:"node-input-rule-btwn-label", style:"width: 120px; text-align: right;"}).text(" "+andLabel+" ").appendTo(row3);
var row3InputCell = $('<div/>',{style:"flex-grow:1; margin-left: 5px;"}).appendTo(row3);
return $('<input/>',{class:"node-input-rule-btwn-value2",type:"text",style:"width: 100%"}).appendTo(row3InputCell)
.typedInput({default:defaultType||'num',types:['msg','flow','global','str','num','jsonata','env',previousValueType]});
}
function createTypeValueField(row, defaultType){
return $('<input/>',{class:"node-input-rule-type-value",type:"text",style:"width: 100%;"}).appendTo(row).typedInput({default:defaultType || 'string',types:[
{value:"string",label:RED._("common.type.string"),hasValue:false,icon:"red/images/typedInput/az.svg"},
{value:"number",label:RED._("common.type.number"),hasValue:false,icon:"red/images/typedInput/09.svg"},
{value:"boolean",label:RED._("common.type.boolean"),hasValue:false,icon:"red/images/typedInput/bool.svg"},
{value:"array",label:RED._("common.type.array"),hasValue:false,icon:"red/images/typedInput/json.svg"},
{value:"buffer",label:RED._("common.type.buffer"),hasValue:false,icon:"red/images/typedInput/bin.svg"},
{value:"object",label:RED._("common.type.object"),hasValue:false,icon:"red/images/typedInput/json.svg"},
{value:"json",label:RED._("common.type.jsonString"),hasValue:false,icon:"red/images/typedInput/json.svg"},
{value:"undefined",label:RED._("common.type.undefined"),hasValue:false},
{value:"null",label:RED._("common.type.null"),hasValue:false}
]});
}
RED.nodes.registerType('switch-custom', {
color: "#E2D96E",
category: 'native-node',
defaults: {
name: {value:""},
property: {value:"payload", required:true,
label:RED._("node-red:common.label.payload"),
validate: RED.validators.typedInput("propertyType", false)},
propertyType: { value:"msg" },
rules: {
value:[{t:"eq", v:"", vt:"str"}],
validate: function (rules, opt) {
let msg;
const errors = []
if (!rules || rules.length === 0) { return true }
for (var i=0;i<rules.length;i++) {
const opt = { label: RED._('node-red:switch.label.rule')+' '+(i+1) }
const r = rules[i];
if (r.t !== 'istype') {
if (r.hasOwnProperty('v')) {
if ((msg = RED.utils.validateTypedProperty(r.v,r.vt,opt)) !== true) {
errors.push(msg)
}
}
if (r.hasOwnProperty('v2')) {
if ((msg = RED.utils.validateTypedProperty(r.v2,r.v2t,opt)) !== true) {
errors.push(msg)
}
}
}
}
if (errors.length) {
console.log(errors)
return errors
}
return true;
}
},
checkall: {value:"true", required:true},
repair: {value:false},
outputs: {value:1}
},
inputs: 1,
outputs: 1,
outputLabels: function(index) {
var rule = this.rules[index];
var label = "";
if (rule) {
for (var i=0;i<operators.length;i++) {
if (operators[i].v === rule.t) {
label = /^switch/.test(operators[i].t)?this._(operators[i].t):operators[i].t;
break;
}
}
if ((rule.t === 'btwn') || (rule.t === 'index')) {
label += " "+getValueLabel(rule.vt,rule.v)+" & "+getValueLabel(rule.v2t,rule.v2);
} else if (rule.t !== 'true' && rule.t !== 'false' && rule.t !== 'null' && rule.t !== 'nnull' && rule.t !== 'empty' && rule.t !== 'nempty' && rule.t !== 'else' ) {
label += " "+getValueLabel(rule.vt,rule.v);
}
return label;
}
},
icon: "switch.svg",
label: function() {
return this.name||this._("switch.switch");
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: function() {
var node = this;
$("#node-input-property").typedInput({default:this.propertyType||'msg',types:['msg','flow','global','jsonata','env']});
var outputCount = $("#node-input-outputs").val("{}");
var andLabel = this._("switch.and");
var caseLabel = this._("switch.ignorecase");
$("#node-input-rule-container").css('min-height','150px').css('min-width','450px').editableList({
addItem: function(container,i,opt) {
var focusValueField = false;
if (!opt.hasOwnProperty('r')) {
opt.r = {};
if (i > 0) {
var lastRule = $("#node-input-rule-container").editableList('getItemAt',i-1);
var exportedRule = exportRule(lastRule.element);
if (exportedRule.t === "istype") {
opt.r.vt = (exportedRule.vt === "number") ? "num" : "str";
} else {
opt.r.vt = exportedRule.vt;
}
opt.r.v = "";
// We could copy the value over as well and preselect it (see the 'activeElement' code below)
// But not sure that feels right. Is copying over the last value 'expected' behaviour?
// It would make sense for an explicit 'copy' action, but not sure where the copy button would
// go for each rule without being wasted space for most users.
// opt.r.v = exportedRule.v;
focusValueField = true;
}
}
opt.element = container;
var rule = opt.r;
if (!rule.hasOwnProperty('t')) {
rule.t = 'eq';
}
if (!opt.hasOwnProperty('i')) {
opt._i = Math.floor((0x99999-0x10000)*Math.random()).toString();
}
container.css({
overflow: 'hidden',
whiteSpace: 'nowrap',
display: "flex",
"align-items":"center"
});
var inputRows = $('<div></div>',{style:"flex-grow:1"}).appendTo(container);
var row = $('<div></div>',{style:"display: flex;"}).appendTo(inputRows);
var row2 = $('<div/>',{style:"display: flex; padding-top: 5px; padding-left: 175px;"}).appendTo(inputRows);
var row3 = $('<div/>',{style:"display: flex; padding-top: 5px; align-items: center"}).appendTo(inputRows);
var row4 = $('<div/>',{style:"visibility: hidden; height: 0px;"}).appendTo(inputRows);
var textSpan = $("<span/>").appendTo(row4);
var selectField = $('<select/>',{style:"width:120px; text-align: center;"}).appendTo(row);
var group0 = $('<optgroup/>', { label: RED._("node-red:switch.label.value-rules") }).appendTo(selectField);
for (var d in operators) {
if(operators[d].kind === 'V') {
group0.append($("<option></option>").val(operators[d].v).text(/^switch/.test(operators[d].t)?node._(operators[d].t):operators[d].t));
}
}
var group1 = $('<optgroup/>', { label: RED._("node-red:switch.label.sequence-rules") }).appendTo(selectField);
for (var d in operators) {
if(operators[d].kind === 'S') {
group1.append($("<option></option>").val(operators[d].v).text(/^switch/.test(operators[d].t)?node._(operators[d].t):operators[d].t));
}
}
for (var d in operators) {
if(operators[d].kind === 'O') {
selectField.append($("<option></option>").val(operators[d].v).text(/^switch/.test(operators[d].t)?node._(operators[d].t):operators[d].t));
}
}
var rowInputCell = $('<div>',{style:"flex-grow:1; margin-left: 5px;"}).appendTo(row);
var valueField = null;
var numValueField = null;
var expValueField = null;
var btwnAndLabel = null;
var btwnValueField = null;
var btwnValue2Field = null;
var typeValueField = null;
var finalspan = $('<span/>',{style:"margin-left: 5px;"}).appendTo(container);
finalspan.append(' → <span class="node-input-rule-index">'+(i+1)+'</span> ');
var caseSensitive = $('<input/>',{id:"node-input-rule-case-"+i,class:"node-input-rule-case",type:"checkbox",style:"width:auto;vertical-align:top"}).appendTo(row2);
$('<label/>',{for:"node-input-rule-case-"+i,style:"margin-left: 3px;"}).text(caseLabel).appendTo(row2);
selectField.on("change", function() {
var fieldToFocus;
var type = selectField.val();
if (valueField) { valueField.typedInput('hide'); }
if (expValueField) { expValueField.typedInput('hide'); }
if (numValueField) { numValueField.typedInput('hide'); }
if (typeValueField) { typeValueField.typedInput('hide'); }
if (btwnValueField) { btwnValueField.typedInput('hide'); }
if (btwnValue2Field) { btwnValue2Field.typedInput('hide'); }
if ((type === "btwn") || (type === "index")) {
if (!btwnValueField){
btwnValueField = createBtwnValueField(rowInputCell);
}
btwnValueField.typedInput('show');
fieldToFocus = btwnValueField;
} else if ((type === "head") || (type === "tail")) {
if (!numValueField){
numValueField = createNumValueField(rowInputCell);
}
numValueField.typedInput('show');
fieldToFocus = numValueField;
} else if (type === "jsonata_exp") {
if (!expValueField){
expValueField = createExpValueField(rowInputCell);
}
expValueField.typedInput('show');
fieldToFocus = expValueField;
} else if (type === "istype") {
if (!typeValueField){
typeValueField = createTypeValueField(rowInputCell);
}
typeValueField.typedInput('show');
fieldToFocus = typeValueField;
} else if (! (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "empty" || type === "nempty" || type === "else" )) {
if (!valueField){
valueField = createValueField(rowInputCell);
}
valueField.typedInput('show');
fieldToFocus = valueField;
}
if (type === "regex") {
row2.show();
row3.hide();
} else if ((type === "btwn") || (type === "index")) {
row2.hide();
row3.show();
if (!btwnValue2Field){
btwnValue2Field = createBtwnValue2Field(row3, andLabel);
}
btwnValue2Field.typedInput('show');
} else {
row2.hide();
row3.hide();
}
var selectedLabel = selectField.find("option:selected").text();
textSpan.text(selectedLabel);
var width = textSpan.width();
if (width <= 30) {
selectField.outerWidth(60);
} else if (width <= 85) {
selectField.outerWidth(120);
} else {
selectField.width("auto")
}
if (fieldToFocus) {
fieldToFocus.typedInput("focus");
}
// Preselect the contents of the element
// if (focusValueField && document.activeElement) {
// document.activeElement.selectionStart = 0;
// document.activeElement.selectionEnd = document.activeElement.value.length;
// }
});
selectField.val(rule.t);
if ((rule.t == "btwn") || (rule.t == "index")) {
btwnValueField = createBtwnValueField(rowInputCell,rule.vt||'num');
btwnValueField.typedInput('value',rule.v);
btwnValue2Field = createBtwnValue2Field(row3, andLabel,rule.v2t||'num');
btwnValue2Field.typedInput('value',rule.v2);
} else if ((rule.t === "head") || (rule.t === "tail")) {
numValueField = createNumValueField(rowInputCell,rule.vt||'num');
numValueField.typedInput('value',rule.v);
} else if (rule.t === "istype") {
typeValueField = createTypeValueField(rowInputCell,rule.vt);
typeValueField.typedInput('value',rule.vt);
} else if (rule.t === "jsonata_exp") {
expValueField = createExpValueField(rowInputCell,rule.vt||'jsonata');
expValueField.typedInput('value',rule.v);
} else if (typeof rule.v != "undefined") {
valueField = createValueField(rowInputCell,rule.vt||'str');
valueField.typedInput('value',rule.v);
}
caseSensitive.prop('checked',!!rule.case);
selectField.change();
var currentOutputs = JSON.parse(outputCount.val()||"{}");
currentOutputs[opt.hasOwnProperty('i')?opt.i:opt._i] = i;
outputCount.val(JSON.stringify(currentOutputs));
},
removeItem: function(opt) {
var currentOutputs = JSON.parse(outputCount.val()||"{}");
if (opt.hasOwnProperty('i')) {
currentOutputs[opt.i] = -1;
} else {
delete currentOutputs[opt._i];
}
var rules = $("#node-input-rule-container").editableList('items');
rules.each(function(i) {
$(this).find(".node-input-rule-index").html(i+1);
var data = $(this).data('data');
currentOutputs[data.hasOwnProperty('i')?data.i:data._i] = i;
});
outputCount.val(JSON.stringify(currentOutputs));
},
sortItems: function(rules) {
var currentOutputs = JSON.parse(outputCount.val()||"{}");
var rules = $("#node-input-rule-container").editableList('items');
rules.each(function(i) {
$(this).find(".node-input-rule-index").html(i+1);
var data = $(this).data('data');
currentOutputs[data.hasOwnProperty('i')?data.i:data._i] = i;
});
outputCount.val(JSON.stringify(currentOutputs));
},
sortable: true,
removable: true
});
for (var i=0;i<this.rules.length;i++) {
var rule = this.rules[i];
$("#node-input-rule-container").editableList('addItem',{r:rule,i:i});
}
},
oneditsave: function() {
var rules = $("#node-input-rule-container").editableList('items');
var node = this;
node.rules = [];
rules.each(function(i) {
node.rules.push(exportRule($(this)));
});
this.propertyType = $("#node-input-property").typedInput('type');
},
oneditresize: function(size) {
var rows = $("#dialog-form>div:not(.node-input-rule-container-row)");
var height = size.height;
for (var i=0;i<rows.length;i++) {
height -= $(rows[i]).outerHeight(true);
}
var editorRow = $("#dialog-form>div.node-input-rule-container-row");
height -= (parseInt(editorRow.css("marginTop"))+parseInt(editorRow.css("marginBottom")));
height += 16;
$("#node-input-rule-container").editableList('height',height);
}
});
})();
</script>
**
**
{
"name": "native-node",
"version": "1.0.0",
"description": "A custom node set",
"keywords": [],
"node-red" : {
"nodes": {
"switch-custom": "switch-custom.js"
}
}
}
**
**
A lot of problems:
package.json
:
"node-red" : {
"nodes": {
"switch-custom": "nodes/native-node/switch-custom.js"
}
}
switch-custom.html
:
<div class="form-row">
<label for="node-input-property"><i class="fa fa-ellipsis-h"></i> <span data-i18n="switch-custom.label.property"></span></label>
<input type="text" id="node-input-property" style="width: calc(100% - 105px)"/>
<input type="hidden" id="node-input-outputs"/>
</div>
I cannot remember if the node name is mandatory (switch-custom.label.property
instead of label.property
)
catalog filename:
It's the name of your node switch-custom.json
instead of messages.json
See the example below: