Skip to content

Commit

Permalink
build: separate js and html parts
Browse files Browse the repository at this point in the history
- split javascript and html parts in ccu-*.html
- store javascript in src/nodes-html/*/editor.js
- store html in src/nodes-html/*/main.html
  • Loading branch information
ptweety committed Oct 15, 2022
1 parent 1b9efa6 commit 0d2667b
Show file tree
Hide file tree
Showing 30 changed files with 5,556 additions and 0 deletions.
135 changes: 135 additions & 0 deletions src/nodes-html/ccu-alexa/editor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/* eslint-disable wrap-iife */

(function () {
'use strict';

RED.nodes.registerType('ccu-alexa', {
category: 'ccu',
defaults: {
name: {value: ''},
iface: {value: ''},
channel: {value: ''},
ccuConfig: {value: 'localhost', type: 'ccu-connection', required: true},
},
inputs: 1,
outputs: 1,
icon: 'ccu.png',
color: '#c2d5e4',
paletteLabel: 'alexa',
align: 'right',
label() {
return this.name || this.channel || 'alexa';
},
labelStyle() {
return this.name ? 'node_label_italic' : '';
},
oneditprepare() {
console.log(this);

const $nodeInputIface = $('#node-input-iface');
const $nodeInputCcuConfig = $('#node-input-ccuConfig');
const $nodeInputChannel = $('#node-input-channel');

let data;
let ifacesLoaded = false;
let ifacesPending = false;

function loadIfaces(iface, cb) {
if (ifacesPending) {
return;
}

ifacesPending = true;
console.log('loadIfaces()');
$nodeInputIface.html('<option value=""></option>');
const nodeId = $nodeInputCcuConfig.val();
if (nodeId === '_ADD_') {
if (typeof cb === 'function') {
cb();
ifacesPending = false;
}
} else {
const url = 'ccu?config=' + nodeId + '&type=ifaces';
$.getJSON(url, d => {
Object.keys(d).forEach(i => {
if (i !== 'ReGaHSS') {
$nodeInputIface.append('<option' + (d[i].enabled ? '' : ' disabled') + (i === iface ? ' selected' : '') + '>' + i + '</option>');
}
});
if (typeof cb === 'function') {
cb();
ifacesPending = false;
}
});
}
}

function loadConfig() {
if (!ifacesLoaded) {
return;
}

console.log('loadConfig()');
const nodeId = $nodeInputCcuConfig.val();
const url = 'ccu?config=' + nodeId + '&type=channels&iface=' + $nodeInputIface.val();
$.getJSON(url, d => {
data = d;
console.log(d);
autocompleteChannel();
});
}

$nodeInputCcuConfig.change(() => {
console.log('$nodeInputCcuConfig change');
loadIfaces(this.iface, () => {
ifacesLoaded = true;
$nodeInputIface.removeAttr('disabled');
loadConfig();
});
});

$nodeInputChannel.autocomplete({
source: [],
close() {
// autocompleteDatapoint();
},
delay: 0,
minLength: 0,
});

$nodeInputChannel.on('focus', () => {
$nodeInputChannel.autocomplete('search');
});

function autocompleteChannel() {
if (!data) {
return;
}

const channels = [];
Object.keys(data).forEach(addr => {
if (/:\d+$/.test(addr)) {
if (data[addr].name) {
addr += ' ' + data[addr].name;
}

channels.push(addr);
}
});
channels.sort((a, b) => a.localeCompare(b));
$nodeInputChannel.autocomplete('option', 'source', channels);

if (!data[$nodeInputChannel.val().split(' ')[0]]) {
$nodeInputChannel.val('');
}
}

$nodeInputIface.change(() => {
console.log('$nodeInputIface change');
loadConfig();
});
},

oneditsave() {},
});
}());
46 changes: 46 additions & 0 deletions src/nodes-html/ccu-alexa/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<script type="text/html" data-template-name="ccu-alexa">
<style>
label.ccu-checkbox {
display: block !important;
width: auto !important;
}
label.ccu-checkbox input {
width: auto !important;
margin-top: -3px !important;
}
</style>

<div class="form-row">
<label for="node-input-ccuConfig"><i class="icon-globe"></i> CCU</label>
<input type="text" id="node-input-ccuConfig">
</div>

<div class="form-row">
<label for="node-input-iface"><i class="fa fa-empire"></i> Interface</label>
<div style="display: inline-block; position: relative; width: 70%;">
<select id="node-input-iface" style="width:calc(100% - 40px);" disabled>
<option></option>
</select>
</div>
</div>

<div class="form-row">
<label for="node-input-channel"><i class="icon-tags"></i> channel</label>
<input type="text" id="node-input-channel">
</div>

<div class="form-row">
<label for="node-input-name"><i class="icon-bookmark"></i> Name</label>
<input type="text" id="node-input-name">
</div>
</script>

<script type="text/html" data-template-name="ccu-alexa">
<p>Homematic Geräte mit node-red-contrib-alexa-smart-home verbinden. Am Eingang den Node "alexa smart home v3"
anschließen, am Ausgang "alexa smart home v3 state".</p>
</script>

<script type="text/html" data-help-name="ccu-alexa" lang="en-US">
<p>Connect Homematic Devices with node-red-contrib-alexa-smart-home. Connect a Node "alexa smart home v3" to the
input and "alexa smart home v3 state" to the output.</p>
</script>
133 changes: 133 additions & 0 deletions src/nodes-html/ccu-connection/editor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/* eslint-disable wrap-iife */

(function () {
'use strict';

RED.nodes.registerType('ccu-connection', {
category: 'config',
defaults: {
name: {value: ''},
host: {value: '', required: true},

regaEnabled: {value: true},
bcrfEnabled: {value: true},
iprfEnabled: {value: true},
virtEnabled: {value: true},
bcwiEnabled: {value: false},
jackEnabled: {value: false},
cuxdEnabled: {value: false},

regaPoll: {value: true},
regaInterval: {value: 30},

rpcPingTimeout: {value: 60},
rpcInitAddress: {value: ''},
rpcServerHost: {value: '', required: true},
rpcBinPort: {value: '', required: true},
rpcXmlPort: {value: '', required: true},

tls: {value: false},
inSecure: {value: false},
authentication: {value: false},
username: {value: ''},
password: {value: ''},

queueTimeout: {value: 5000, required: true},
queuePause: {value: 250, required: true},

contextStore: {value: ''},
},
label() {
return this.name || this.host;
},

oneditprepare() {
const $nodeConfigInputHost = $('#node-config-input-host');
const $nodeConfigInputName = $('#node-config-input-name');
const $nodeConfigInputRpcServerHost = $('#node-config-input-rpcServerHost');
const $nodeInputContextStore = $('#node-config-input-contextStore');

if (typeof this.queueTimeout === 'undefined') {
$('#node-config-input-queueTimeout').val(5000);
}

if (typeof this.queuePause === 'undefined') {
$('#node-config-input-queuePause').val(250);
}

RED.settings.context.stores.forEach(store => {
$nodeInputContextStore.append('<option value="' + store + '"' + (this.contextStore === store ? ' selected' : '') + '>' + store + '</option>');
});

$nodeConfigInputHost.on('focus', () => $nodeConfigInputHost.autocomplete('search', ''));

$.getJSON('ccu', data => {
const discovered = [];
data.discover.forEach(ccu => {
discovered.push(ccu.address + ' ' + ccu.serial);
});

$nodeConfigInputHost.autocomplete({
source: discovered,
select(_, ui) {
const name = ui.item.label;
const address = name.split(' ').shift();
const serial = name.split(' ').pop();

$nodeConfigInputHost.val(address);
$nodeConfigInputName.val(serial);

data.discover.forEach(ccu => {
if (ccu.address === address) {
$('#node-config-input-regaEnabled').prop('checked', ccu.interfaces.ReGaHSS);
$('#node-config-input-bcrfEnabled').prop('checked', ccu.interfaces['BidCos-RF']);
$('#node-config-input-iprfEnabled').prop('checked', ccu.interfaces['HmIP-RF']);
$('#node-config-input-virtEnabled').prop('checked', ccu.interfaces.VirtualDevices);
$('#node-config-input-bcwiEnabled').prop('checked', ccu.interfaces['BidCos-Wired']);
$('#node-config-input-jackEnabled').prop('checked', ccu.interfaces['CCU-Jack']);
$('#node-config-input-cuxdEnabled').prop('checked', ccu.interfaces.CuXD);
}
});
},
delay: 0,
minLength: 0,
});

$nodeConfigInputHost.on('change', () => {
if (!$nodeConfigInputHost.val().endsWith($nodeConfigInputName.val())) {
$nodeConfigInputName.val($nodeConfigInputHost.val());
}
});

data.listen.forEach(addr => {
$nodeConfigInputRpcServerHost.append('<option>' + addr + '</option>');
});

$nodeConfigInputRpcServerHost.val(this.rpcServerHost || data.listen[1]);

if (!this.rpcBinPort) {
$('#node-config-input-rpcBinPort').val(data.ports[0]);
}

if (!this.rpcXmlPort) {
$('#node-config-input-rpcXmlPort').val(data.ports[1]);
}
});

setTimeout(() => {
if (!this.host) {
$nodeConfigInputHost.focus();
}
}, 250);
},
});

/* eslint-disable no-unused-vars, no-undef, no-alert */
function TLSSSL() {
if (document.getElementById('node-config-input-authentication').checked && !document.getElementById('node-config-input-tls').checked) {
document.getElementById('node-config-input-tls').checked = true;
alert('Authentication activated. \nWithout TLS/SSL your credentials would be sent unencrypted!');
}
}
/* eslint-enable no-unused-vars, no-undef, no-alert */
}());
Loading

0 comments on commit 0d2667b

Please sign in to comment.