From af9120d791589309bb29bc7c8b01a530a2a6a717 Mon Sep 17 00:00:00 2001 From: hidetak Date: Thu, 21 Mar 2024 00:11:12 +0900 Subject: [PATCH] simplified the process of using TypedInput --- package.json | 3 +- template/src/template-config.html | 68 +++----------------- template/src/template-config.ts | 11 +--- template/src/template-node.html | 101 +++++------------------------- template/src/template-node.ts | 11 +--- 5 files changed, 30 insertions(+), 164 deletions(-) diff --git a/package.json b/package.json index 579d733..913d658 100755 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/template/src/template-config.html b/template/src/template-config.html index 0ce2456..f866ae6 100644 --- a/template/src/template-config.html +++ b/template/src/template-config.html @@ -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 () { @@ -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`] = '' - } - } })() @@ -85,10 +38,9 @@ <%_ for(const setting of config.settings) { _%>
- - + + -
<%_ } _%> diff --git a/template/src/template-config.ts b/template/src/template-config.ts index 5ff5874..699a9e7 100644 --- a/template/src/template-config.ts +++ b/template/src/template-config.ts @@ -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, @@ -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 %>) } <%_ } _%> diff --git a/template/src/template-node.html b/template/src/template-node.html index 0b095a0..734ee9c 100755 --- a/template/src/template-node.html +++ b/template/src/template-node.html @@ -4,7 +4,6 @@ <%_ if(config) { _%> <%_ varConfigNodeName = config.nodeName[0].toLowerCase() + config.nodeName.substring(1) _%> <%_ } _%> - RED.nodes.registerType('<%- nodeType %>', { category: "<%- category %>", color: "<%- color %>", @@ -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 %> }, <%_ } _%> <%_ } _%> @@ -32,7 +33,6 @@ <%_ } _%> <%_ } _%> <%_ } _%> - <%_ if(config) { _%> <%- varConfigNodeName %>: { type: "<%- config.nodeType %>", @@ -40,13 +40,6 @@ } <%_ } _%> }, - 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) { @@ -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", + }) <%_ } _%> <%_ } _%> @@ -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`] = '' - } - } })(); @@ -214,10 +145,9 @@ <%_ if(input) { _%> <%_ for(const param of input.params) { _%>
- - + + -
<%_ } _%> <%_ } _%> @@ -228,10 +158,9 @@
<%_ for(const param of outputs[i].params) { _%>
- - + + -
<%_ } _%>
diff --git a/template/src/template-node.ts b/template/src/template-node.ts index 221e31e..4b185c9 100755 --- a/template/src/template-node.ts +++ b/template/src/template-node.ts @@ -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, @@ -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') {