UNPKG

node-red-contrib-irtemplate

Version:

IRTemplate node for node-red, allows to store a set of IR commands to be sent in some IR blaster (Glogal Cache`, for exapmle)

202 lines (179 loc) 9.81 kB
<!-- Copyright 2013, 2015 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="irtemplate"> <div class="form-row"> <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="Name"></span></label> <input type="text" id="node-input-name" data-i18n="[placeholder]Name"> </div> <div class="form-row node-input-ircommand-container-row" style="margin-bottom: 0px;"> <div id="node-input-ircommand-container-div" style="box-sizing: border-box; border-radius: 5px; height: 310px; padding: 5px; border: 1px solid #ccc; overflow-y:scroll;"> <ol id="node-input-ircommand-container" style=" list-style-type:none; margin: 0;"></ol> </div> </div> <div class="form-row"> <a href="#" class="editor-button editor-button-small" id="node-input-add-ircommand" style="margin-top: 4px;"><i class="fa fa-plus"></i> <span data-i18n="irtemplate.label.rule"></span></a> </div> </script> <script type="text/x-red" data-help-name="irtemplate"> <p>A simple function node to route messages based on its properties.</p> <p>When a message arrives, the selected property is evaluated against each of the defined ircommands. The message is then sent to the output of <i>all</i> ircommands that pass.</p> <p>Note: the <i>otherwise</i> rule applies as a "not any of" the ircommands preceding it.</p> <p> One able to fill field "Cmd name" with few names (aliases) as folows: <ul> <li><code>start, stop, power</code></li> <li>or <code>start;stop;power</code></li> </ul> </p> </script> <script type="text/javascript"> RED.nodes.registerType('irtemplate', { color: "#E2D96E", category: 'multimedia', defaults: { name: {value: ""}, ircommands: {value: [{name: "", value: ""}]}, checkall: {value: "true", required: true}, outputs: {value: 1} }, inputs: 1, outputs: 1, icon: "irtemplate.png", label: function () { return this.name || "irtemplate"; }, oneditprepare: function () { var node = this; function generateIRCmd(i, ircommand) { var container = $('<li/>', {style: "background: #fff; margin:0; padding:8px 0px; border-bottom: 1px solid #ccc;"}); var row = $('<div/>').appendTo(container); $('<i style="color: #eee; cursor: move;" class="node-input-ircommand-handle fa fa-bars"></i>').appendTo(row); var btnField = $('<a href="#" class="editor-button editor-button-small" style="margin-left: 5px; width: 34px; height: 34px; position: relative; top: 6px; font-size: 30px;"><i class="fa fa-bullseye"></i></a>').appendTo(row); btnField.click(function () { $.ajax({ url: "irtemplate/" + node.id + "/" + valueField.val() + "/" + i, type: "POST", success: function (resp) { RED.notify(node._("irtemplate.success", {label: valueField.val()}), "success"); }, error: function (jqXHR, textStatus, errorThrown) { if (jqXHR.status == 404) { RED.notify(node._("common.notification.error", {message: node._("common.notification.errors.not-deployed")}), "error"); } else if (jqXHR.status == 500) { RED.notify(node._("common.notification.error", {message: node._("irtemplate.errors.failed")}), "error"); } else if (jqXHR.status == 0) { RED.notify(node._("common.notification.error", {message: node._("common.notification.errors.no-response")}), "error"); } else { RED.notify(node._("common.notification.error", { message: node._("common.notification.errors.unexpected", { status: jqXHR.status, message: textStatus }) }), "error"); } } }); }); var labelField = $('<input/>', { class: "node-input-ircommand-label", type: "text", style: "margin-left: 5px; width: 100px;", placeholder:"Cmd name" }).appendTo(row); var valueField = $('<input/>', { class: "node-input-ircommand-value", type: "text", style: "margin-left: 5px; width: 145px;", placeholder:"Cmd value [IR code]" }).appendTo(row); var finalspan = $('<span/>', {style: "float: right;margin-right: 10px;"}).appendTo(row); finalspan.append(' &#8594; <span class="node-input-ircommand-index">' + i + '</span> '); var deleteButton = $('<a/>', { href: "#", class: "editor-button editor-button-small", style: "margin-top: 7px; margin-left: 5px;" }).appendTo(finalspan); $('<i/>', {class: "fa fa-remove"}).appendTo(deleteButton); deleteButton.click(function () { container.css({"background": "#fee"}); container.fadeOut(300, function () { $(this).remove(); $("#node-input-ircommand-container").children().each(function (i) { $(this).find(".node-input-ircommand-index").html(i + 1); }); }); }); $("#node-input-ircommand-container").append(container); labelField.val(ircommand.name); valueField.val(ircommand.value); } $("#node-input-add-ircommand").click(function () { generateIRCmd($("#node-input-ircommand-container").children().length + 1, {name: "", value: ""}); $("#node-input-ircommand-container-div").scrollTop($("#node-input-ircommand-container-div").get(0).scrollHeight); }); for (var i = 0; i < this.ircommands.length; i++) { var ircommand = this.ircommands[i]; generateIRCmd(i + 1, ircommand); } function irtemplateDialogResize() { var rows = $("#dialog-form>div:not(.node-input-ircommand-container-row)"); var height = $("#dialog-form").height(); for (var i = 0; i < rows.size(); i++) { height -= $(rows[i]).outerHeight(true); } var editorRow = $("#dialog-form>div.node-input-ircommand-container-row"); height -= (parseInt(editorRow.css("marginTop")) + parseInt(editorRow.css("marginBottom"))); $("#node-input-ircommand-container-div").css("height", height + "px"); }; $("#node-input-ircommand-container").sortable({ axis: "y", update: function (event, ui) { var ircommands = $("#node-input-ircommand-container").children(); ircommands.each(function (i) { $(this).find(".node-input-ircommand-index").html(i + 1); }); }, handle: ".node-input-ircommand-handle", cursor: "move" }); $("#node-input-ircommand-container .node-input-ircommand-handle").disableSelection(); $("#dialog").on("dialogresize", irtemplateDialogResize); $("#dialog").one("dialogopen", function (ev) { var size = $("#dialog").dialog('option', 'sizeCache-irtemplate'); if (size) { $("#dialog").dialog('option', 'width', size.width); $("#dialog").dialog('option', 'height', size.height); irtemplateDialogResize(); } }); $("#dialog").one("dialogclose", function (ev, ui) { $("#dialog").off("dialogresize", irtemplateDialogResize); }); }, oneditsave: function () { var ircommands = $("#node-input-ircommand-container").children(); var node = this; node.ircommands = []; ircommands.each(function (i) { var ircommand = $(this); node.ircommands.push({ name: ircommand.find(".node-input-ircommand-label").val(), value: ircommand.find(".node-input-ircommand-value").val() }); }); node.outputs = node.ircommands.length; } }); </script>