UNPKG

node-red-contrib-bravia

Version:
108 lines (95 loc) 3.96 kB
<script type="text/javascript"> RED.nodes.registerType('bravia-ircc', { category: 'devices', color: '#b8535a', icon: 'tv.png', defaults: { tv: { type: 'bravia-tv', required: true }, ircc: { value: '', required: false }, name: { value: '', required: false } }, inputs: 1, outputs: 0, align: 'right', label: function() { return (this.name || 'bravia ircc'); }, oneditprepare: function() { function toggleSelect() { var value = $('#node-input-ircc').val(); $('#node-input-scan').html('<i class="fa fa-search"></i>'); $('#node-input-ircc').replaceWith('<input type="text" id="node-input-ircc" style="width: 100%;">'); $('#node-input-ircc').val(value); } function toggleInput(invoked = false) { var value = $('#node-input-ircc').val(); var config = RED.nodes.node($('#node-input-tv').val()); if (config && config.host && config.port && config.psk) { if (invoked) { RED.notify('Retrieving Sony BRAVIA IRCC codes...'); } $.get('/bravia/ircc', { host: config.host, port: config.port, psk: config.psk }) .done(function(data) { var codes = JSON.parse(data); if (codes.length === 0) { RED.notify('No IRCC codes retrieved, check your TV connection settings.', 'error'); return; } var found = codes.find(function(code) { return code.name == value || code.value == value; }); if (!invoked && value && !found) { return; } $('#node-input-scan').html('<i class="fa fa-i-cursor"></i>'); $('#node-input-ircc').replaceWith('<select id="node-input-ircc" style="width: 100%;"></select>'); codes.forEach(function(code) { $('#node-input-ircc').append('<option value="' + code.name + '">' + code.name + '</option>'); }); $('#node-input-ircc').val((found) ? found.name : ''); }) .fail(function() { RED.notify('Failed to retrieve IRCC codes, check your TV connection settings.', 'error'); }); } } $('#node-input-scan').click(function() { if ($('#node-input-ircc').prop('tagName') === 'INPUT') { toggleInput(true); } else { toggleSelect(); } }); $('#node-input-tv').on('change', function() { if ($('#node-input-ircc').prop('tagName') === 'SELECT') { toggleSelect(); } toggleInput(); }); } }); </script> <script type="text/x-red" data-template-name="bravia-ircc"> <div class="form-row"> <label for="node-input-tv"><i class="fa fa-television"></i> TV</label> <input type="text" id="node-input-tv"> </div> <div class="form-row"> <label for="node-input-ircc"><i class="fa fa-tasks"></i> IRCC</label> <div style="display: inline-block; position: relative; width: 70%; height: 20px;"> <div style="position: absolute; left: 0; right: 40px;"> <input type="text" id="node-input-ircc" style="width: 100%;"> </div> <a id="node-input-scan" class="editor-button" style="position: absolute; right: 0; top: 0; width: 35px;"><i class="fa fa-search"></i></a> </div> </div> <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> </script> <script type="text/x-red" data-help-name="bravia-ircc"> <p>Sends IRCC codes to a Sony BRAVIA Android TV.</p> <p>The IRCC codes can be sent by name or value.</p> <p>To send multiple IRCC codes at once specify in the config or via <b>msg.payload</b> as a comma-delimited string, e.g. <i>Hdmi1, AAAAAgAAABoAAABaAw==, Hdmi2, AAAAAgAAABoAAABbAw==</i>.</p> </script>