Skip to content

Commit

Permalink
simplified the process of using TypedInput
Browse files Browse the repository at this point in the history
  • Loading branch information
hidetak committed Mar 20, 2024
1 parent 79e9de4 commit af9120d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 164 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hidetak/create-node-red-node",
"version": "0.0.1-beta.9",
"version": "0.1.0-beta.1",
"description": "",
"repository": {
"type": "git",
Expand All @@ -14,6 +14,7 @@
"build": "tsc && npm run copy:dist2bin",
"copy:dist2bin": "node -e \"require('fs-extra').copySync('./dist', './bin')\"",
"build:node": "tsc && node dist/index out-node/ node-gen-config.json template/",
"publish:beta": "npm publish --tag beta --tag latest",
"deprecated_build:node": "ejs-cli --base-dir template/ '**/*.*' --out out-node/ -O node-gen-config.json"
},
"author": "hidetak",
Expand Down
68 changes: 10 additions & 58 deletions template/src/template-config.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@
defaults: {
name: { value: '<%- config.defaultName %>' },
<%_ for(const setting of config.settings) { _%>
<%- setting.name %>Type: {value:'<%- setting.defaultType %>'},
<%- setting.name %>ConstValue: {value:'<%- setting.defaultValue %>'},
<%_ } _%>
},
credentials: {
<%_ for(const setting of config.settings) { _%>
<%- setting.name %>: { type: 'text' },
<%- setting.name %>Type: {value:'<%- setting.defaultType %>', required:<%- setting.required %>},
<%- setting.name %>ConstValue: {value:'<%- setting.defaultValue %>', required:<%- setting.required %>},
<%_ } _%>
},
label: function () {
Expand All @@ -23,57 +18,15 @@
},
oneditprepare: function() {
<%_ for(const setting of config.settings) { _%>
createInOutParamSetting({
name: "<%- setting.name %>",
defaultType: "<%- setting.defaultType %>",
types: <%- JSON.stringify(setting.types) %>,
defaultValue: "<%- setting.defaultValue %>",
required: <%- setting.required %>
}, this)
<%_ } _%>
},
oneditsave: function() {
<%_ for(const setting of config.settings) { _%>
saveInOutParamSetting({
name: "<%- setting.name %>"
}, this)
$("#node-config-input-<%- setting.name %>ConstValue").typedInput({
defaultType: "<%- setting.defaultType %>",
defaultValue: "<%- setting.defaultValue %>",
types: <%- JSON.stringify(setting.types) %>,
typeField: "#node-config-input-<%- setting.name %>Type",
})
<%_ } _%>
}
})

const createInOutParamSetting = (params, _this) => {
const {name, types, defaultType, defaultValue, required} = params
if (!_this[name+"Type"]) {
_this[name+"Type"] = defaultType
}
if(_this[name+"Type"] == 'str') {
$(`#node-config-input-${name}ConstValue`).val("")
} else {
if(_this[name+"ConstValue"] == "") {
$(`#node-config-input-${name}ConstValue`).val(_this.credentials[name]);
} else {
_this.credentials[name] = _this[name+"ConstValue"];
$(`#node-config-input-${name}`).val(_this.credentials[name]);
}
}
$(`#node-config-input-${name}Type`).val(_this[name+"Type"])
$(`#node-config-input-${name}`).typedInput({
default: defaultType,
typeField: $(`#node-config-input-${name}Type`),
types: types
})
$(`#node-config-input-${name}`).typedInput('type', _this[name+"Type"])
}
const saveInOutParamSetting = (params, _this) => {
const {name} = params
if($(`#node-config-input-${name}Type`).val() != 'str') {
_this[`${name}ConstValue`] = $(`#node-config-input-${name}`).val()
$(`#node-config-input-${name}ConstValue`).val(_this[`${name}ConstValue`])
} else {
$(`#node-config-input-${name}ConstValue`).val("")
_this[`${name}ConstValue`] = ''
}
}
})()
</script>

Expand All @@ -85,10 +38,9 @@

<%_ for(const setting of config.settings) { _%>
<div class='form-row'>
<label for='node-config-input-<%- setting.name %>'><span data-i18n="editor.<%- setting.name %>.label"></span></label>
<input required type='text' id='node-config-input-<%- setting.name %>' data-i18n="[placeholder]editor.<%- setting.name %>.placeholder">
<label for='node-config-input-<%- setting.name %>ConstValue'><span data-i18n="editor.<%- setting.name %>.label"></span></label>
<input type='text' id='node-config-input-<%- setting.name %>ConstValue' data-i18n="[placeholder]editor.<%- setting.name %>.placeholder">
<input type='hidden' id='node-config-input-<%- setting.name %>Type'>
<input type='hidden' id='node-config-input-<%- setting.name %>ConstValue'>
</div>
<%_ } _%>
</script>
Expand Down
11 changes: 2 additions & 9 deletions template/src/template-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ module.exports = function (RED) {
const node = this

<%_ for(const setting of config.settings) { _%>
node.<%- setting.name %> = node.credentials.<%- setting.name %>
if (node.<%- setting.name %>ConstValue && node.<%- setting.name %>Type) {
if (config.<%- setting.name %>ConstValue && config.<%- setting.name %>Type) {
node.<%- setting.name %> = RED.util.evaluateNodeProperty(
config.<%- setting.name %>ConstValue,
config.<%- setting.name %>Type,
Expand All @@ -16,12 +15,6 @@ module.exports = function (RED) {
<%_ } _%>
}

RED.nodes.registerType("<%- config.nodeType %>", <%- config.nodeName %>, {
credentials: {
<%_ for(let i = 0; i < config.settings.length; i++) { _%>
<%- config.settings[i].name %>: { type: 'text' }<%- i < config.settings.length - 1 ? "," : "" %>
<%_ } _%>
}
})
RED.nodes.registerType("<%- config.nodeType %>", <%- config.nodeName %>)
}
<%_ } _%>
101 changes: 15 additions & 86 deletions template/src/template-node.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<%_ if(config) { _%>
<%_ varConfigNodeName = config.nodeName[0].toLowerCase() + config.nodeName.substring(1) _%>
<%_ } _%>

RED.nodes.registerType('<%- nodeType %>', {
category: "<%- category %>",
color: "<%- color %>",
Expand All @@ -13,10 +12,12 @@
<%_ if(input && input.params) { _%>
<%_ for(const param of input.params) { _%>
inParams_<%- param.name %>Type: {
value: "<%- param.defaultType %>"
value: "<%- param.defaultType %>",
required: <%- param.required %>
},
inParams_<%- param.name %>ConstValue: {
value: "<%- param.defaultValue %>"
value: "<%- param.defaultValue %>",
required: <%- param.required %>
},
<%_ } _%>
<%_ } _%>
Expand All @@ -32,21 +33,13 @@
<%_ } _%>
<%_ } _%>
<%_ } _%>

<%_ if(config) { _%>
<%- varConfigNodeName %>: {
type: "<%- config.nodeType %>",
required: true
}
<%_ } _%>
},
credentials: {
<%_ if(input && input.params) { _%>
<%_ for(const param of input.params) { _%>
inParams_<%- param.name %>: {type: "text"},
<%_ } _%>
<%_ } _%>
},
inputs:<%- input ? 1 : 0 %>,
outputs:<%- outputs ? outputs.length : 0 %>,
outputLabels: function(index) {
Expand Down Expand Up @@ -91,13 +84,12 @@

// 入力パラメータの指定項目作成
<%_ for(const param of input.params) { _%>
prepareInOutParamSetting("inParams", {
name:"<%- param.name %>",
types: <%- JSON.stringify(param.types) %>,
$("#node-input-inParams_<%- param.name %>ConstValue").typedInput({
defaultType: "<%- param.defaultType %>",
defaultValue: "<%- param.defaultValue %>",
required: <%- param.required %>
}, this)
types: <%- JSON.stringify(param.types) %>,
typeField: "#node-input-inParams_<%- param.name %>Type",
})
<%_ } _%>
<%_ } _%>

Expand All @@ -109,78 +101,17 @@
});
// 出力パラメータの指定項目作成
<%_ for(const param of outputs[i].params) { _%>
prepareInOutParamSetting("outParams<%- i+1 %>", {
name: "<%- param.name %>",
types: <%- JSON.stringify(param.types) %>,
defaultType: "<%- param.defaultType %>",
defaultValue: "<%- param.defaultValue %>",
required: <%- param.required %>
}, this)
<%_ } _%>
<%_ } _%>
<%_ } _%>
},
oneditsave: function() {
<%_ if(input) { _%>
<%_ for(const param of input.params) { _%>
saveInOutParamSetting("inParams", {
name:"<%- param.name %>",
types: <%- JSON.stringify(param.types) %>,
$("#node-input-outParams<%- i+1 %>_<%- param.name %>ConstValue").typedInput({
defaultType: "<%- param.defaultType %>",
defaultValue: "<%- param.defaultValue %>",
required: <%- param.required %>
}, this)
<%_ } _%>
<%_ } _%>
<%_ if(outputs) { _%>
<%_ for(let i = 0; i < outputs.length; i++) { _%>
<%_ for(const param of outputs[i].params) { _%>
saveInOutParamSetting("outParams<%- i+1 %>", {
name: "<%- param.name %>",
types: <%- JSON.stringify(param.types) %>,
defaultType: "<%- param.defaultType %>",
defaultValue: "<%- param.defaultValue %>",
required: <%- param.required %>
}, this)
typeField: "#node-input-outParams<%- i+1 %>_<%- param.name %>Type",
})
<%_ } _%>
<%_ } _%>
<%_ } _%>
}
});
const prepareInOutParamSetting = (inOrOutParams, params, _this) => {
const {name, types, defaultType, defaultValue, required} = params
const varName = `${inOrOutParams}_${name}`
if (!_this[`${varName}Type`]) {
_this[`${varName}Type`] = defaultType
}
if(this[`${varName}Type`] == 'str') {
$(`#node-input-${varName}ConstValue`).val("")
} else {
if(_this[`${varName}ConstValue`] == "") {
$(`#node-input-${varName}ConstValue`).val(_this.credentials[varName]);
} else {
_this.credentials[varName] = _this[`${varName}ConstValue`];
$(`#node-input-${varName}`).val(_this.credentials[`${varName}`]);
}
}
$(`#node-input-${varName}Type`).val(_this[`${varName}Type`])
$(`#node-input-${varName}`).typedInput({
default: defaultType,
typeField: $(`#node-input-${varName}Type`),
types: types
})
$(`#node-input-${varName}`).typedInput('type', _this[`${varName}Type`])
}
const saveInOutParamSetting = (inOrOutParams, params, _this) => {
const varName = `${inOrOutParams}_${params.name}`
if($(`#node-input-${varName}Type`).val() != 'str') {
_this[`${varName}ConstValue`] = $(`#node-input-${varName}`).val()
$(`#node-input-${varName}ConstValue`).val(_this[`${varName}ConstValue`])
} else {
$(`#node-input-${varName}ConstValue`).val("")
_this[`${varName}ConstValue`] = ''
}
}
})();

</script>
Expand Down Expand Up @@ -214,10 +145,9 @@
<%_ if(input) { _%>
<%_ for(const param of input.params) { _%>
<div class='form-row'>
<label for='node-input-inParams_<%- param.name %>'><span data-i18n="editor.inParams.<%- param.name %>.label"></span></label>
<input required type='text' id='node-input-inParams_<%- param.name %>' data-i18n="[placeholder]editor.inParams.<%- param.name %>.placeholder">
<label for='node-input-inParams_<%- param.name %>ConstValue'><span data-i18n="editor.inParams.<%- param.name %>.label"></span></label>
<input type='text' id='node-input-inParams_<%- param.name %>ConstValue' data-i18n="[placeholder]editor.inParams.<%- param.name %>.placeholder">
<input type='hidden' id='node-input-inParams_<%- param.name %>Type'>
<input type='hidden' id='node-input-inParams_<%- param.name %>ConstValue'>
</div>
<%_ } _%>
<%_ } _%>
Expand All @@ -228,10 +158,9 @@
<div id="tab-outParams<%- i+1 %>-settings">
<%_ for(const param of outputs[i].params) { _%>
<div class='form-row'>
<label for='node-input-outParams<%- i+1 %>_<%- param.name %>'><span data-i18n="editor.outParams<%- i+1 %>.<%- param.name %>.label"></span></label>
<input required type='text' id='node-input-outParams<%- i+1 %>_<%- param.name %>' data-i18n="[placeholder]editor.outParams<%- i+1 %>.<%- param.name %>.placeholder">
<label for='node-input-outParams<%- i+1 %>_<%- param.name %>ConstValue'><span data-i18n="editor.outParams<%- i+1 %>.<%- param.name %>.label"></span></label>
<input type='text' id='node-input-outParams<%- i+1 %>_<%- param.name %>ConstValue' data-i18n="[placeholder]editor.outParams<%- i+1 %>.<%- param.name %>.placeholder">
<input type='hidden' id='node-input-outParams<%- i+1 %>_<%- param.name %>Type'>
<input type='hidden' id='node-input-outParams<%- i+1 %>_<%- param.name %>ConstValue'>
</div>
<%_ } _%>
</div>
Expand Down
11 changes: 1 addition & 10 deletions template/src/template-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module.exports = function (RED) {
// 入力パラメータを取得
<%_ if(input && input.params) { _%>
<%_ for(const param of input.params) { _%>
node.inParams_<%- param.name %> = node.credentials.inParams_<%- param.name %>
if (config.inParams_<%- param.name %>ConstValue && config.inParams_<%- param.name %>Type) {
node.inParams_<%- param.name %> = RED.util.evaluateNodeProperty(
config.inParams_<%- param.name %>ConstValue,
Expand Down Expand Up @@ -70,15 +69,7 @@ module.exports = function (RED) {
done()
})
}
RED.nodes.registerType('<%- nodeType %>', <%- nodeName %>, {
credentials: {
<%_ if(input && input.params) { _%>
<%_ for(let i = 0; i < input.params.length; i++) { _%>
inParams_<%- input.params[i].name %>: { type: 'text' }<%- i < input.params.length - 1 ? "," : "" %>
<%_ } _%>
<%_ } _%>
}
})
RED.nodes.registerType('<%- nodeType %>', <%- nodeName %>)

const setOutput = (type, valueName, msg, context, value) => {
if (type === 'msg') {
Expand Down

0 comments on commit af9120d

Please sign in to comment.