<script type="text/x-red" data-template-name="throttle">
    <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>
    <div class="form-row">
        <label for=""><i class="fa fa-filter"></i> Throttle</label>
        <select id="node-input-throttleType" style="width: 70%">
            <option value="time">by time</option>
            <option value="count">by count</option>
            <option value="block">by block size</option>
            <option value="reset">by reset</option>
        </select>
    </div>
    <div class="form-row throttle-limit-row" id="throttle-limit-row-time">
        <label for="node-input-timeLimit"><i class="fa fa-clock-o"></i> Time Limit</label>
        <input type="text" id="node-input-timeLimit" placeholder="0" style="padding-left: 5px; width: 70%">
        <input type="hidden" id="node-input-timeLimitType">
    </div>
    <div class="form-row throttle-limit-row hidden" id="throttle-limit-row-count">
        <label for="node-input-countLimit"><i class="fa fa-hashtag"></i> Count Limit</label>
        <input type="text" id="node-input-countLimit" placeholder="0">
    </div>
    <div class="form-row throttle-limit-row hidden" id="throttle-limit-row-block">
        <label for="node-input-blockSize"><i class="fa fa-hashtag"></i> Block Size</label>
        <input type="text" id="node-input-blockSize" placeholder="0">
    </div>
    <div class="form-row">
        <label>&nbsp;</label>
        <input type="checkbox" id="node-input-locked" style="display: inline-block; width: auto; vertical-align: top;">
        <label for="node-input-locked" style="width: 70%;">Locked by default?</label>
    </div>
</script>

<script type="text/x-red" data-help-name="throttle">
    <p>A simple node to throttle down passed through message amount.</p>
    <p>Just insert the throttle node in between two others, and the passed through message amount can be limited by different parameters.</p>
    <p><strong>By Time:</strong><br/>
    Limits the passed through messages by a given amount of time.
    For example: setting the node to <code>10 seconds</code> means, that only one message in ten seconds will be forwarded.</p>
    <p><strong>By Count:</strong><br/>
    Limits the passed through messages by a given count.
    For example: setting the node to a count of <code>5</code> means, that only every fifth message will be forwarded.</p>
    <p><strong>By Block Size:</strong><br/>
    Limits the passed through messages by a given block size.
    The counter will be reset when a message with <code>msg.reset</code> was received.
    For example: setting the node to a block size of <code>5</code> means, that only the first five messages will be forwarded.</p>
    <p><strong>By Reset:</strong><br/>
    Will only pass through a single message, when a message with <code>msg.reset</code> was received before.</p>
    <p>Every other messages will be dropped!</p>
</script>

<script type="text/javascript">
    RED.nodes.registerType("throttle", {
        category: "function",
        defaults: {
            name: {value:""},
            throttleType: {value:"time"},
            timeLimit: {value:0,validate:RED.validators.number()},
            timeLimitType: {value:"seconds"},
            countLimit: {value:0,validate:RED.validators.number()},
            blockSize: {value:0,validate:RED.validators.number()},
            locked: {value:false}
        },
        color:"#e6e0f8",
        inputs: 1,
        outputs: 1,
        icon: "throttle.png",
        label: function() {
            return this.name || "throttle";
        },
        labelStyle: function() {
            return this.name ? "node_label_italic" : "";
        },
        oneditprepare: function() {
            // defaults
            this.throttleType = !this.throttleType ? "time" : this.throttleType;
            this.countLimit = !this.countLimit ? 0 : this.countLimit;
            this.blockSize = !this.blockSize ? 0 : this.blockSize;
            this.timeLimit = !this.timeLimit ? 0 : this.timeLimit;
            this.timeLimitType = !this.timeLimitType ? "seconds" : this.timeLimitType;

            // change listener for type select
            $("#node-input-throttleType").change(function() {
                $(".throttle-limit-row").hide();
                $("#throttle-limit-row-" + $(this).val()).show();
            });

            // typed input for time throttle
            $("#node-input-timeLimit").typedInput({
                default: "seconds",
                typeField: $("#node-input-timeLimitType"),
                types: [
                    {value: "milliseconds", label: "milliseconds", hasValue: true},
                    {value: "seconds", label: "seconds", hasValue: true},
                    {value: "minutes", label: "minutes", hasValue: true},
                    {value: "hours", label: "hours", hasValue: true}
                ]
            });
        }
    });
</script>