node-red-contrib-sun-position
Version:
NodeRED nodes to get sun and moon position
562 lines (534 loc) • 27.1 kB
HTML
<!--
This code is licensed under the Apache License Version 2.0.
Copyright (c) 2022 Robert Gester
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
-->
<!-- Finally, the node type is registered along with all of its properties -->
<!-- The example below shows a small subset of the properties that can be set-->
<script type="text/javascript">
(function() {
RED.nodes.registerType('sun-position', {
category: 'time and astro',
color: '#F3B567',
icon: 'sun-white.svg',
inputs: 1, // set the number of inputs - only 0 or 1
outputs: 1,
defaults: {
name: {
value: '',
required: false
},
positionConfig: {
value: '',
type: 'position-config',
required: true
},
rules: {
value: []
},
onlyOnChange: {
value: 'true',
required: true
},
topic: {
value: ''
},
outputs: {
value: 1
},
start: {
value: '',
required: false,
validate: RED.validators.typedInput('startType')
},
startType: {
value: 'none'
},
startOffset: {
value: 0,
validate(v){
return (
(typeof v === 'undefined') ||
(typeof this.startOffsetType === 'undefined') ||
RED.validators.typedInput('startOffsetType')(v) ||
($('#node-input-start').length ?
$('#node-input-start').typedInput('type') === 'none' :
this.startType === 'none')
);
}
},
startOffsetType: {
value: 'none'
},
startOffsetMultiplier: {
value: 60000,
required: true,
validate(v){
return (
typeof v === 'undefined' ||
RED.validators.number()(v) ||
$('#node-input-startOffsetType').typedInput('type') === 'none' ||
this.startOffsetType === 'none' ||
($('#node-input-start').length ?
$('#node-input-start').typedInput('type') === 'none' :
this.startType === 'none')
);
}
},
end: {
value: '',
required: false,
validate: RED.validators.typedInput('startType')
},
endType: {
value: 'none'
},
endOffset: {
value: 0,
validate(v){
return (
(typeof v === 'undefined') ||
(typeof this.endOffsetType === 'undefined') ||
RED.validators.typedInput('endOffsetType')(v) ||
($('#node-input-end').length ?
$('#node-input-end').typedInput('type') === 'none' :
this.endType === 'none')
);
}
},
endOffsetType: {
value: 'none'
},
endOffsetMultiplier: {
value: 60000,
required: true,
validate(v){
return (
typeof v === 'undefined' ||
RED.validators.number()(v) ||
$('#node-input-endOffsetType').typedInput('type') === 'none' ||
this.endOffsetType === 'none' ||
($('#node-input-end').length ?
$('#node-input-end').typedInput('type') === 'none' :
this.endType === 'none')
);
}
}
},
outputLabels(index) {
if (index === 0) {
return ((this.topic) ? this.topic + ':' : '') + 'sun-position';
}
const rule = this.rules[index - 1];
if (rule) {
return RED.nodes.getType('position-config').getRDGNodeValLbl(this, rule.valueLowType, rule.valueLow) + ' & ' +
RED.nodes.getType('position-config').getRDGNodeValLbl(this, rule.valueHighType, rule.valueHigh);
}
return undefined;
},
label() {
if (this.name) {
return this.name;
}
const result = 'sun-position';
if (this.topic && (this.topic.length + result.length <= 32)) {
return this.topic + ':' + result;
}
return result;
},
labelStyle() {
return this.name ? 'node_label_italic' : '';
},
paletteLabel: 'sun-position',
oneditprepare() {
setTimeout(() => {
$('.is-to-show-initially').show();
$('.is-to-hide-initially').hide();
}, 300);
const node = this;
const $nodeConfig = $('#node-input-positionConfig');
const setup = function(node) {
/* global getTypes appendOptions setupTInput */
const types = getTypes(node, () => $nodeConfig.val());
// #region initialize
/**
* update multiplier settings from a previous version
* @param {number} mp - the multiplier value
* @param {string} name - the name of the element
* @returns {number} the updated multiplier value
*/
function multiplierUpdate(mp, name) {
const field = $('#node-input-' + name);
appendOptions(node, field, 'multiplier', data => (data.id > 0));
if (mp === null || typeof mp === 'undefined' || isNaN(mp) || mp === '' || mp === 0) {
mp = 60000;
}
field.val(mp);
return mp;
}
// #endregion initialize
// #region start
const startTypeField = setupTInput(node, {
typeProp: 'startType',
valueProp: 'start',
width: 'calc(100% - 110px)',
defaultType: types.Undefined.value,
defaultValue: '',
types: [
types.Undefined,
types.TimeEntered,
types.TimeSun,
types.TimeSunCustom,
types.TimeSunNow,
types.TimeMoon,
'flow',
'global',
'env',
types.SunTimeByAzimuth,
types.SunTimeByElevationNext,
types.SunTimeByElevationRise,
types.SunTimeByElevationSet
],
onChange(_type, _value) {
if ($('#node-input-start').typedInput('type') === types.Undefined.value) {
$('.sun-position-row-startOffset').hide();
} else {
$('.sun-position-row-startOffset').show();
}
$('#node-input-property').change();
}
});
node.startOffsetMultiplier = multiplierUpdate(node.startOffsetMultiplier, 'startOffsetMultiplier');
setupTInput(node, {
typeProp: 'startOffsetType',
valueProp: 'startOffset',
width: 'calc(100% - 255px)',
defaultType: (node.startOffset === 0 || node.startOffset === '') ? types.Undefined.value : 'num',
defaultValue: 0,
types: [types.Undefined, 'num', 'msg', 'flow', 'global', 'env', types.randomNumber, types.randmNumCachedDay, types.randmNumCachedWeek],
onChange(_type, _value) {
const type = $('#node-input-startOffset').typedInput('type');
if (type === types.Undefined.value) {
$('#node-input-startOffsetMultiplier').prop('disabled', true);
} else {
$('#node-input-startOffsetMultiplier').prop('disabled', false);
}
}
}).change();
startTypeField.change();
// #endregion start
// #region end
const endTypeField = setupTInput(node, {
typeProp: 'endType',
valueProp: 'end',
width: 'calc(100% - 110px)',
defaultType: types.Undefined.value,
defaultValue: '',
types: [
types.Undefined,
types.TimeEntered,
types.TimeSun,
types.TimeSunCustom,
types.TimeSunNow,
types.TimeMoon,
'flow',
'global',
'env',
types.SunTimeByAzimuth,
types.SunTimeByElevationNext,
types.SunTimeByElevationRise,
types.SunTimeByElevationSet
],
onChange(_type, _value) {
if ($('#node-input-end').typedInput('type') === types.Undefined.value) {
$('.sun-position-row-endOffset').hide();
} else {
$('.sun-position-row-endOffset').show();
}
$('#node-input-property').change();
}
});
node.endOffsetMultiplier = multiplierUpdate(node.endOffsetMultiplier, 'endOffsetMultiplier');
setupTInput(node, {
typeProp: 'endOffsetType',
valueProp: 'endOffset',
width: 'calc(100% - 255px)',
defaultType: (node.endOffset === 0 || node.endOffset === '') ? types.Undefined.value : 'num',
defaultValue: 0,
types: [types.Undefined, 'num', 'msg', 'flow', 'global', 'env', types.randomNumber, types.randmNumCachedDay, types.randmNumCachedWeek],
onChange(_type, _value) {
const type = $('#node-input-endOffset').typedInput('type');
if (type === types.Undefined.value) {
$('#node-input-endOffsetMultiplier').prop('disabled', true);
} else {
$('#node-input-endOffsetMultiplier').prop('disabled', false);
}
}
}).change();
endTypeField.change();
// #endregion end
const outputCount = $('#node-input-outputs').val('{}');
const btwnLabel = node._('node-red-contrib-sun-position/position-config:common.label.between');
const andLabel = node._('node-red-contrib-sun-position/position-config:common.label.and');
/**
* resizes a rule
* @param {jQuery} rule - the jQuery selector of the rule
*/
function resizeRule(rule) {
const newWidth = rule.width();
const btwnField1 = rule.find('.node-input-rule-btwn-value');
const btwnField2 = rule.find('.node-input-rule-btwn-value2');
const editwidth = (newWidth / 2);
btwnField1.typedInput('width', (editwidth - 70));
btwnField2.typedInput('width', (editwidth - 70));
}
$('#node-input-rule-container').css('min-height', '100px').css('min-width', '200px').editableList({
addItem(container, i, opt) {
container.css({
overflow: 'hidden',
whiteSpace: 'nowrap'
});
if (!Object.prototype.hasOwnProperty.call(opt, 'r')) {
opt.r = {};
}
if (!Object.prototype.hasOwnProperty.call(opt, 'i')) {
opt._i = Math.floor((0x99999 - 0x10000) * Math.random()).toString();
}
const rule = opt.r;
const row = $('<div/>').appendTo(container);
$('<span/>', {
class: 'node-input-rule-btwn-label'
}).text(' ' + btwnLabel + ' ').appendTo(row);
$('<input/>', {
class: 'node-input-rule-btwn-value',
type: 'text',
style: 'margin-left: 5px;',
value: rule.valueLow
}).appendTo(row).typedInput({
default: rule.valueLowType || 'num',
types: [types.Undefined, 'msg', 'flow', 'global', 'num', 'env']
});
$('<span/>', {
class: 'node-input-rule-btwn-label'
}).text(' ' + andLabel + ' ').appendTo(row);
$('<input/>', {
class: 'node-input-rule-btwn-value2',
type: 'text',
style: 'margin-left:2px;',
value: rule.valueHigh
}).appendTo(row).typedInput({
default: rule.valueHighType || 'num',
types: [types.Undefined, 'msg', 'flow', 'global', 'num', 'env']
});
const finalspan = $('<span/>', {
style: 'float: right;margin-top: 6px;'
}).appendTo(row);
finalspan.append(' → <span class="node-input-rule-index">' + (i + 2) + '</span> ');
resizeRule(container);
// {"0":0,"1":-1,"2":-1,"3":-1}
// {"0":0}
const currentOutputs = JSON.parse(outputCount.val() || '{}');
currentOutputs[Object.prototype.hasOwnProperty.call(opt, 'i') ? opt.i : opt._i] = i;
currentOutputs.m = i + 1;
outputCount.val(JSON.stringify(currentOutputs));
},
removeItem(opt) {
const currentOutputs = JSON.parse(outputCount.val() || '{"-1":-1}');
if (Object.prototype.hasOwnProperty.call(opt, 'i')) {
currentOutputs[opt.i] = -1;
} else {
delete currentOutputs[opt._i];
}
const rules = $('#node-input-rule-container').editableList('items');
rules.each(function (i) {
$(this).find('.node-input-rule-index').html(i + 2);
const data = $(this).data('data');
currentOutputs[Object.prototype.hasOwnProperty.call(data, 'i') ? data.i : data._i] = i;
});
currentOutputs.m = rules.length;
outputCount.val(JSON.stringify(currentOutputs));
},
resizeItem: resizeRule,
sortItems(rules) {
const currentOutputs = JSON.parse(outputCount.val() || '{}');
rules.each(function (i) {
$(this).find('.node-input-rule-index').html(i + 2);
const data = $(this).data('data');
currentOutputs[Object.prototype.hasOwnProperty.call(data, 'i') ? data.i : data._i] = i;
});
currentOutputs.m = rules.length;
outputCount.val(JSON.stringify(currentOutputs));
},
sortable: true,
removable: true
});
for (let i = 0; i < node.rules.length; i++) {
const rule = node.rules[i];
$('#node-input-rule-container').editableList('addItem', {
r: rule,
i,
l: node.rules.length
});
}
}; // setup
$.getScript('sun-position/js/htmlglobal.js')
.done((_data, _textStatus, _jqxhr) => {
try {
setup(node);
} catch (err) {
console.log("failed to setup editor"); // eslint-disable-line
console.log(err); // eslint-disable-line
console.log(err.stack); // eslint-disable-line
}
})
.fail((jqxhr, settings, exception) => {
console.log("failed to load htmlglobal.js"); // eslint-disable-line
console.log(exception); // eslint-disable-line
console.log(exception.stack); // eslint-disable-line
});
},
oneditsave() {
const rules = $('#node-input-rule-container').editableList('items');
const node = this;
node.rules = [];
rules.each(function ( _i ) {
const rule = $(this);
const r = {};
r.valueLow = rule.find('.node-input-rule-btwn-value').typedInput('value');
r.valueLowType = rule.find('.node-input-rule-btwn-value').typedInput('type');
r.valueHigh = rule.find('.node-input-rule-btwn-value2').typedInput('value');
r.valueHighType = rule.find('.node-input-rule-btwn-value2').typedInput('type');
node.rules.push(r);
});
node.propertyType = $('#node-input-property').typedInput('type');
const currentOutputs = JSON.parse($('#node-input-outputs').val() || '{"m":0}');
currentOutputs.m = rules.length;
$('#node-input-outputs').val(JSON.stringify(currentOutputs));
},
oneditresize(size) {
try {
const rows = $('#dialog-form>div:not(.node-input-rule-container-row)');
let height = size.height;
for (let i = 0; i < rows.size(); i++) {
height -= $(rows[i]).outerHeight(true);
}
const editorRow = $('#dialog-form>div.node-input-rule-container-row');
height -= (parseInt(editorRow.css('marginTop')) + parseInt(editorRow.css('marginBottom')));
height -= 50;
if (typeof $().editableList !== 'undefined') {
$('#node-input-rule-container').editableList('height', height);
}
} catch (err) {
console.debug(err); // eslint-disable-line no-console
}
}
});
})();</script>
<script type="text/html" data-template-name="sun-position">
<div class="form-row block-noindent is-to-show-initially">
<span><i class="fa fa-info-circle"></i> <span data-i18n="[html]node-red-contrib-sun-position/position-config:common.label.infoText" class="row-full-width"></span></span></span>
</div>
<div class="form-row" data-i18n="[title]node-red-contrib-sun-position/position-config:common.placeholder.positionConfig">
<label for="node-input-positionConfig"><i class="fa fa-globe"></i> <span data-i18n="node-red-contrib-sun-position/position-config:common.label.positionConfig"></span></label>
<input type="text" id="node-input-positionConfig">
<input type="hidden" id="node-input-outputs"/>
</div>
<div class="form-row time-topic" data-i18n="[title]node-red:common.label.topic">
<label for="node-input-topic"><i class="fa fa-tasks"></i> <span data-i18n="node-red:common.label.topic"></span></label>
<input type="text" id="node-input-topic" data-i18n="[placeholder]node-red:common.label.topic">
</div>
<hr>
<div class="form-row node-input-rule-container-row" data-i18n="[title]sun-position.placeholder.azimuthpos">
<label for="node-input-rule-container"><i class="fa fa-tasks"></i> <span data-i18n="sun-position.label.azimuthpos"></span></label>
<ol id="node-input-rule-container"></ol>
</div>
<hr>
<div class="form-row block-noindent" data-i18n="[title]sun-position.placeholder.start">
<label for="node-input-start"><i class="fa fa-clock-o"></i> <span data-i18n="sun-position.label.start"></span></label>
<input type="text" id="node-input-start" data-i18n="[title]sun-position.placeholder.start"/>
<input type="hidden" id="node-input-startType">
</div>
<div class="form-row block-indent1 sun-position-row-startOffset is-initial-hidden" data-i18n="[title]sun-position.placeholder.startOffset">
<label for="node-input-startOffset"><i class="fa fa-calculator"></i> <span data-i18n="sun-position.label.startOffset"></span></label>
<input id="node-input-startOffset" data-i18n="[placeholder]sun-position.placeholder.startOffset"/>
<input type="hidden" id="node-input-startOffsetType">
<select id="node-input-startOffsetMultiplier" class="node-input-multiplier"></select>
</div>
<div class="form-row block-noindent" data-i18n="[title]sun-position.placeholder.end">
<label for="node-input-end"><i class="fa fa-clock-o"></i> <span data-i18n="sun-position.label.end"></span></label>
<input type="text" id="node-input-end" data-i18n="[placeholder]sun-position.placeholder.end"/>
<input type="hidden" id="node-input-endType">
</div>
<div class="form-row block-indent1 sun-position-row-endOffset is-initial-hidden" data-i18n="[title]sun-position.placeholder.endOffset">
<label for="node-input-endOffset"><i class="fa fa-calculator"></i> <span data-i18n="sun-position.label.endOffset"></span></label>
<input id="node-input-endOffset" data-i18n="[placeholder]sun-position.placeholder.endOffset"/>
<input type="hidden" id="node-input-endOffsetType">
<select id="node-input-endOffsetMultiplier" class="node-input-multiplier"></select>
</div>
<div class="form-row form-tips" data-i18n="[html]sun-position.tips.config"></div>
<hr>
<div class="form-row" data-i18n="[title]node-red:common.label.name">
<label for="node-input-name"><i class="icon-tag"></i> <span data-i18n="node-red:common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name">
</div>
<div class="form-tips">
<span data-i18n="[html]sun-position.tips.documentation" style="word-wrap:break-word;"></span>
</div>
</script>
<style>
hr {
width: 100%
}
.is-to-show-initially {
display:none
}
.is-initial-hidden {
display:none
}
.block-indent1 {
float: left;
min-height: 1px;
margin-left: 10px;
width: 100%
}
.block-indent2 {
float: left;
min-height: 1px;
margin-left: 20px;
width: 100%
}
.block-noindent .row-full-width {
width : calc(100% - 110px);
}
.block-indent1 .row-full-width {
width : calc(100% - 120px);
}
.block-indent2 .row-full-width {
width : calc(100% - 130px);
}
.block-noindent .ui-spinner {
width : calc(100% - 245px);
}
.block-indent1 .ui-spinner {
width : calc(100% - 255px);
}
.block-indent2 .ui-spinner {
width : calc(100% - 265px);
}
.node-input-multiplier {
width: 125px;
max-width: 125px;
margin-left: 5px;
}
</style>