<!--
  Copyright 2014 Nicholas Humfrey
  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="collector">
    <div class="form-row">
        <label for="node-input-name"><i class="icon-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
    <div class="form-row" id="node-onlyIfChanged">
        <label>&nbsp;</label>
        <input type="checkbox" id="node-input-onlyIfChanged" style="display: inline-block; width: auto; vertical-align: top;">
        <label for="node-input-onlyIfChanged" style="width: 70%;">Only return message if value changed ?</label>
    </div>
    <div class="form-row" id="node-mode">
        <label for="node-input-mode">Mode</label>
        <select id="node-input-mode">
            <option selected value="replace">Replace Values</option>
            <option value="merge">Merge Values</option>
        </select>
    </div>
</script>

<script type="text/x-red" data-help-name="collector">
    <p>Collects message payloads and stores them against topic names. Upon recieving a message it will output all previously received <b>msg.topic</b>/<b>msg.payload</b> pairs as an object.</p>

    <p>If "<b>Only return message if value changed?</b>" is set, then a message will only be output, if the payload has changed since the last message with the same topic was received.</p>

    <p>If if a message is received with a <b>msg.topic</b> but no <b>msg.payload</b>, then that topic will be deleted from the collector.</p>

    <p>If "<b>Mode</b>" is set to "<b>Replace Values</b>", then values contained in new messages replace previously stored values.  If however "<b>Mode</b>" is set to "<b>Merge Values</b>", then composite values (such as objects and arrays) contained in new messages are merged with existing composite values.  Refer to the <a href="https://www.npmjs.com/package/deep-extend">deep-extend</a> module for examples of how values are merged.</p>
</script>

<script type="text/javascript">
    RED.nodes.registerType('collector',{
        category: 'function',
        defaults: {
            name: {value:""},
            onlyIfChanged: {value:true},
            mode: {value:"replace"}
        },
        color:"rgb(243, 181, 103)",
        inputs:1,
        outputs:1,
        icon: "collector.png",
        label: function() {
            return this.name||"collector";
        },
        labelStyle: function() {
            return this.name?"node_label_italic":"";
        }
    });
</script>
