UNPKG

node-red-contrib-chronos

Version:

Time-based Node-RED scheduling, repeating, queueing, routing, filtering and manipulating nodes

396 lines (354 loc) 15.9 kB
<!-- Copyright (c) 2020 - 2025 Jens-Uwe Rossbach This code is licensed under the MIT License. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> <script type="text/html" data-template-name="chronos-config"> <div class="form-row"> <label for="node-config-input-name"><i class="fa fa-tag"></i> <span data-i18n="node-red-contrib-chronos/chronos-config:common.label.name"></span></label> <input type="text" id="node-config-input-name" data-i18n="[placeholder]node-red-contrib-chronos/chronos-config:common.label.name"> </div> <div class="form-row"> <div style="display: inline-block; width: auto; vertical-align: middle;"> <div> <label for="node-config-input-latitude"><i class="fa fa-map-marker"></i> <span data-i18n="config.label.latitude"></span></label> <input type="text" id="node-config-input-latitude" style="width: 60%;"> <input id="node-config-input-latitudeType" type="hidden"> </div> <div style="margin-top: 12px;"> <label for="node-config-input-longitude"><i class="fa fa-map-marker"></i> <span data-i18n="config.label.longitude"></span></label> <input type="text" id="node-config-input-longitude" style="width: 60%;"> <input id="node-config-input-longitudeType" type="hidden"> </div> </div> <div style="display: inline-block; width: auto; margin-left: 10px;"> <button type="button" id="node-config-input-locationDetection" class="red-ui-button" style="border-radius: 50%;" data-i18n="[title]config.label.locationDetection"><i class='fa fa-location-arrow'></i></button> </div> </div> <div class="form-row"> <iframe id="node-config-input-map" width="100%" height="200" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" style="border: 1px solid #CCCCCC; border-radius: 4px;"></iframe> </div> <div class="form-row"> <label for="node-config-input-timezone"><i class="fa fa-globe"></i> <span data-i18n="config.label.timezone"></span></label> <input type="text" id="node-config-input-timezone" data-i18n="[placeholder]config.label.hostTZ"> </div> <div class="form-row node-input-sunPositionList-row" style="padding-top: 10px"> <label for="node-input-sunPositionList" style="width: auto;"><i class="fa fa-calendar-o"></i> <span data-i18n="config.label.sunPositions"></span></label> <div class="form-row"> <ol id="node-input-sunPositionList"></ol> </div> </div> </script> <script type="text/javascript"> RED.nodes.registerType("chronos-config", { category: "config", icon: "chronos_config.svg", label: function() { return (this.name || ((this.credentials && this.credentials.latitude && this.credentials.longitude) ? (this.credentials.latitude + ", " + this.credentials.longitude) : this._("config.label.node"))); }, defaults: { name: { value: "" }, latitudeType: { value: "num" }, longitudeType: { value: "num" }, timezone: { value: "" }, sunPositions: { value: [], validate: function(v) { if (v && (v.length > 0)) { const EXP = /^[0-9a-zA-Z_]+$/; let valid = true; let names = []; // check for invalid names for (let i=0; i<v.length; ++i) { if (!EXP.test(v[i].riseName) || !EXP.test(v[i].setName)) { valid = false; break; } names.push(v[i].riseName); names.push(v[i].setName); if (hasDuplicates(names)) { valid = false; break; } } return valid; } else { return true; } function hasDuplicates(arr) { return arr.some(item => { return (arr.indexOf(item) !== arr.lastIndexOf(item)); }); } } } }, credentials: { latitude: { type: "text", value: "", required: true }, longitude: { type: "text", value: "", required: true } }, oneditprepare: function() { let node = this; let latitude = undefined; let longitude = undefined; let noMapUpdate = false; let latitudeInput = $("#node-config-input-latitude") .typedInput({types: ["num", "env"], typeField: "#node-config-input-latitudeType"}); let longitudeInput = $("#node-config-input-longitude") .typedInput({types: ["num", "env"], typeField: "#node-config-input-longitudeType"}); const updateMap = function() { let prevLatitude = latitude; let prevLongitude = longitude; if (latitudeInput.typedInput("type") == "num") { latitude = parseFloat(latitudeInput.typedInput("value")); } else { $.getJSON( "chronos_config", { command: "getenv", varname: latitudeInput.typedInput("value")}) .done(result => { latitude = parseFloat(result); if ((latitude != prevLatitude) || (longitude != prevLongitude)) { relocateMap(); } }); } if (longitudeInput.typedInput("type") == "num") { longitude = parseFloat(longitudeInput.typedInput("value")); } else { $.getJSON( "chronos_config", { command: "getenv", varname: longitudeInput.typedInput("value")}) .done(result => { longitude = parseFloat(result); if ((latitude != prevLatitude) || (longitude != prevLongitude)) { relocateMap(); } }); } if ((latitude != prevLatitude) || (longitude != prevLongitude)) { relocateMap(); } }; const relocateMap = function() { let map = $("#node-config-input-map"); if (!Number.isNaN(latitude) && !Number.isNaN(longitude)) { map.attr("src", "https://www.openstreetmap.org/export/embed.html?bbox=" + (longitude - 0.001) + "," + (latitude - 0.001) + "," + (longitude + 0.001) + "," + (latitude + 0.001) + "&layer=mapnik&marker=" + latitude + "," + longitude); map.show(); } else { map.hide(); } }; const validateInput = function(event, ui) { if (/^(\+|-)?\d+(\.\d+)?$/.test($(this).spinner("value"))) { let value = parseFloat($(this).spinner("value")); let min = $(this).spinner("option", "min"); let max = $(this).spinner("option", "max"); if (value < min) { $(this).spinner("value", min); } else if (value > max) { $(this).spinner("value", max); } } else { $(this).spinner("value", 0); } }; const sunAngleInput = { min: -90, max: 90, step: 0.001, numberFormat: "n", change: validateInput }; if (navigator.geolocation) { $("#node-config-input-locationDetection").click(function() { navigator.geolocation.getCurrentPosition(pos => { noMapUpdate = true; latitudeInput.typedInput("value", pos.coords.latitude); longitudeInput.typedInput("value", pos.coords.longitude); latitudeInput.typedInput("type", "num"); longitudeInput.typedInput("type", "num"); noMapUpdate = false; updateMap(); }); }); } else { $("#node-config-input-locationDetection").hide(); } updateMap(); let sunPositionList = $("#node-input-sunPositionList").css("min-width", "500px").css("min-height", "150px").editableList( { removable: true, sortable: true, addItem: function(item, index, data) { let fragment = document.createDocumentFragment(); let angleLabel = $("<label/>", {style: "width: auto; margin-left: 5px; margin-bottom: 0px;"}) .text(node._("config.label.angle") + " ") .appendTo(fragment); let angle = $("<input/>", {class: "node-input-angle", style: "width: 60px !important; margin-left: 4px;"}) .appendTo(angleLabel) .spinner(sunAngleInput); let riseLabel = $("<label/>", {title: node._("config.label.riseName"), style: "width: auto; margin-left: 12px; margin-bottom: 0px;"}) .html("<i class='fa fa-arrow-up' aria-hidden='true'></i>") .appendTo(fragment); let riseName = $("<input/>", {type: "text", class: "node-input-riseName", placeholder: node._("config.label.riseName"), style: "width: 110px; margin-left: 4px;"}) .appendTo(riseLabel); let setLabel = $("<label/>", {title: node._("config.label.setName"), style: "width: auto; margin-left: 12px; margin-bottom: 0px;"}) .html("<i class='fa fa-arrow-down' aria-hidden='true'></i>") .appendTo(fragment); let setName = $("<input/>", {type: "text", class: "node-input-setName", placeholder: node._("config.label.setName"), style: "width: 110px; margin-left: 4px;"}) .appendTo(setLabel); if (!("angle" in data)) { data = {angle: 0, riseName: "", setName: ""}; } angle.spinner("value", data.angle); riseName.val(data.riseName); setName.val(data.setName); item[0].appendChild(fragment); } }); latitudeInput.on("change", function(event, type, value) { if (!noMapUpdate) { updateMap(); } }); longitudeInput.on("change", function(event, type, value) { if (!noMapUpdate) { updateMap(); } }); if (!node.sunPositions) // backward compatibility to v1.1.0 { node.sunPositions = []; } node.sunPositions.forEach(data => { sunPositionList.editableList("addItem", data); }); }, oneditsave: function() { let node = this; let sunPositionList = $("#node-input-sunPositionList").editableList("items"); node.sunPositions = []; sunPositionList.each(function(index) { let data = {}; let angle = $(this).find(".node-input-angle"); let riseName = $(this).find(".node-input-riseName"); let setName = $(this).find(".node-input-setName"); data.angle = angle.spinner("value"); data.riseName = riseName.val(); data.setName = setName.val(); node.sunPositions.push(data); }); }, oneditresize: function(size) { let height = size.height; let sunPositionListRow = $("#node-config-dialog-edit-form>div.node-input-sunPositionList-row"); let otherRows = $("#node-config-dialog-edit-form>div:not(.node-input-sunPositionList-row)"); for (let i=0; i<otherRows.length; ++i) { height -= $(otherRows[i]).outerHeight(true); } height -= (parseInt(sunPositionListRow.css("marginTop")) + parseInt(sunPositionListRow.css("marginBottom"))); height -= $("#node-config-dialog-edit-form>div.node-input-sunPositionList-row>label").outerHeight(true); $("#node-input-sunPositionList").editableList("height", height); } }); </script>