mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			142 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			142 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<!--
 | 
						|
  Copyright 2013 IBM Corp.
 | 
						|
 | 
						|
  Licensed under the Apache License, Version 2.0 (the "License");
 | 
						|
  you may not use this file except in compliance with the License.
 | 
						|
  You may obtain a copy of the License at
 | 
						|
 | 
						|
  http://www.apache.org/licenses/LICENSE-2.0
 | 
						|
 | 
						|
  Unless required by applicable law or agreed to in writing, software
 | 
						|
  distributed under the License is distributed on an "AS IS" BASIS,
 | 
						|
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
						|
  See the License for the specific language governing permissions and
 | 
						|
  limitations under the License.
 | 
						|
-->
 | 
						|
 | 
						|
<script type="text/x-red" data-template-name="change">
 | 
						|
    <div>
 | 
						|
        <select id="node-input-action" style="width:95%; margin-right:5px;">
 | 
						|
            <option value="replace">Set the value of the message property</option>
 | 
						|
            <option value="change">Search/replace the value of the message property</option>
 | 
						|
            <option value="delete">Delete the message property</option>
 | 
						|
        </select>
 | 
						|
    </div>
 | 
						|
    <div class="form-row" style="padding-top:10px;" id="node-prop1-row">
 | 
						|
        <label for="node-input-property">called</label> msg.<input type="text" id="node-input-property" style="width: 63%;"/>
 | 
						|
    </div>
 | 
						|
    <div class="form-row" id="node-from-row">
 | 
						|
        <label for="node-input-from" id="node-input-f"></label>
 | 
						|
        <input type="text" id="node-input-from" placeholder="this"/>
 | 
						|
    </div>
 | 
						|
    <div class="form-row" id="node-to-row">
 | 
						|
        <label for="node-input-to" id="node-input-t"></label>
 | 
						|
        <input type="text" id="node-input-to" placeholder="that"/>
 | 
						|
    </div>
 | 
						|
    <div class="form-row" id="node-reg-row">
 | 
						|
        <label> </label>
 | 
						|
        <input type="checkbox" id="node-input-reg" style="display: inline-block; width: auto; vertical-align: top;">
 | 
						|
        <label for="node-input-reg" style="width: 70%;">Use regular expressions</label>
 | 
						|
    </div>
 | 
						|
    <div class="form-tips" id="node-tip"></div>
 | 
						|
    <br/>
 | 
						|
    <div class="form-row">
 | 
						|
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
 | 
						|
        <input type="text" id="node-input-name" placeholder="Name">
 | 
						|
    </div>
 | 
						|
</script>
 | 
						|
 | 
						|
<script type="text/x-red" data-help-name="change">
 | 
						|
    <p>A simple function node to set, replace or delete properties of a message.</p>
 | 
						|
    <p>When a message arrives, the selected property is modified by the defined rules.
 | 
						|
    The message is then sent to the output.</p>
 | 
						|
    <p><b>Note:</b> Set and replace only operate using <b>strings</b>. Anything else will be passed straight through.</p>
 | 
						|
</script>
 | 
						|
 | 
						|
<script type="text/javascript">
 | 
						|
    RED.nodes.registerType('change', {
 | 
						|
        color: "#E2D96E",
 | 
						|
        category: 'function',
 | 
						|
        defaults: {
 | 
						|
            action: {value:"replace",required:true},
 | 
						|
            property: {value:"payload",required:true},
 | 
						|
            from: {value:"",validate: function(v) {
 | 
						|
                if (this.action == "change" && this.reg) {
 | 
						|
                    try {
 | 
						|
                        var re = new RegExp(this.from, "g");
 | 
						|
                        return true;
 | 
						|
                    } catch(err) {
 | 
						|
                        return false;
 | 
						|
                    }
 | 
						|
                }
 | 
						|
                return true;
 | 
						|
            }},
 | 
						|
            to: {value:""},
 | 
						|
            reg: {value:false},
 | 
						|
            name: {value:""}
 | 
						|
        },
 | 
						|
        inputs: 1,
 | 
						|
        outputs: 1,
 | 
						|
        icon: "swap.png",
 | 
						|
        label: function() {
 | 
						|
            if (this.name) {
 | 
						|
                return this.name;
 | 
						|
            }
 | 
						|
            if (this.action === "replace") {
 | 
						|
                return "set msg."+this.property;
 | 
						|
            } else if (this.action === "change") {
 | 
						|
                return "replace msg."+this.property;
 | 
						|
            } else {
 | 
						|
                return this.action+" msg."+this.property
 | 
						|
            }
 | 
						|
        },
 | 
						|
        labelStyle: function() {
 | 
						|
            return this.name ? "node_label_italic" : "";
 | 
						|
        },
 | 
						|
        oneditprepare: function() {
 | 
						|
            if (this.reg === null) { $("#node-input-reg").prop('checked', true); }
 | 
						|
            $("#node-input-action").change( function() {
 | 
						|
                var a = $("#node-input-action").val();
 | 
						|
                if (a === "replace") {
 | 
						|
                    $("#node-input-todo").html("called");
 | 
						|
                    //$("#node-input-f").html("name");
 | 
						|
                    $("#node-input-t").html("to");
 | 
						|
                    $("#node-from-row").hide();
 | 
						|
                    $("#node-to-row").show();
 | 
						|
                    $("#node-reg-row").hide();
 | 
						|
                    $("#node-tip").show();
 | 
						|
                    $("#node-tip").html("Tip: expects a new property name and either a fixed value OR the full name of another message property eg: msg.sentiment.score");
 | 
						|
                }
 | 
						|
                if (a === "delete") {
 | 
						|
                    $("#node-input-todo").html("called");
 | 
						|
                    //$("#node-input-f").html("called");
 | 
						|
                    //$("#node-input-t").html("to");
 | 
						|
                    $("#node-from-row").hide();
 | 
						|
                    $("#node-to-row").hide();
 | 
						|
                    $("#node-reg-row").hide();
 | 
						|
                    $("#node-tip").hide();
 | 
						|
                }
 | 
						|
                if (a === "change") {
 | 
						|
                    $("#node-input-todo").html("called");
 | 
						|
                    $("#node-input-f").html("Search for");
 | 
						|
                    $("#node-input-t").html("replace with");
 | 
						|
                    $("#node-from-row").show();
 | 
						|
                    $("#node-to-row").show();
 | 
						|
                    $("#node-reg-row").show();
 | 
						|
                    $("#node-tip").show();
 | 
						|
                    $("#node-tip").html("Tip: only works on string properties. If regular expressions are used, the <i>replace with</i> field can contain capture results, eg $1.");
 | 
						|
                }
 | 
						|
                //if (a === "replace") {
 | 
						|
                 //   $("#node-input-todo").html("called");
 | 
						|
                 //   //$("#node-input-f").html("with");
 | 
						|
                 //   $("#node-input-t").html("with");
 | 
						|
                 //   $("#node-from-row").hide();
 | 
						|
                 //   $("#node-to-row").show();
 | 
						|
                 //   $("#node-tip").html("Tip: accepts either a fixed value OR the full name of another msg.property eg: msg.sentiment.score");
 | 
						|
                //}
 | 
						|
            });
 | 
						|
            $("#node-input-action").change();
 | 
						|
        }
 | 
						|
    });
 | 
						|
</script>
 |