UNPKG

@iobroker/create-adapter

Version:

Command line utility to create customized ioBroker adapters

134 lines (119 loc) 4.33 kB
"use strict"; const questions_1 = require("../../src/lib/core/questions"); function generateSettingsDiv(settings) { if (settings.inputType === "select" && settings.options) { const options = settings.options .map(opt => ` <option value="${opt.value}">${opt.text}</option>`) .join(""); return ` <div class="col s6 input-field"> <select class="value" id="${settings.key}">${options} </select> <label for="${settings.key}" class="translate">${settings.label || settings.key}</label> </div>`; } return ` <div class="col s6 input-field"> <input type="${settings.inputType}" class="value" id="${settings.key}" /> <label for="${settings.key}" class="translate">${settings.label || settings.key}</label> </div>`; } module.exports = (answers => { const isAdapter = answers.features.indexOf("adapter") > -1; const useJsonConfig = answers.adminUi === "json"; const noConfig = answers.adminUi === "none"; if (!isAdapter || useJsonConfig || noConfig) { return; } const useReact = answers.adminUi === "react"; const adapterSettings = answers.adapterSettings ?? (0, questions_1.getDefaultAnswer)("adapterSettings"); const template = ` <html> <head> <!-- Load ioBroker scripts and styles--> <link rel="stylesheet" type="text/css" href="../../css/adapter.css" /> ${useReact ? ` <script type="text/javascript" src="../../socket.io/socket.io.js"></script>` : ` <link rel="stylesheet" type="text/css" href="../../lib/css/materialize.css"> <script type="text/javascript" src="../../lib/js/jquery-3.2.1.min.js"></script> <script type="text/javascript" src="../../socket.io/socket.io.js"></script> <script type="text/javascript" src="../../js/translate.js"></script> <script type="text/javascript" src="../../lib/js/materialize.js"></script> <script type="text/javascript" src="../../js/adapter-settings.js"></script>`} <!-- Load our own files --> <link rel="stylesheet" type="text/css" href="style.css" /> ${useReact ? "" : ` <script type="text/javascript" src="words.js"></script> <script type="text/javascript"> // This will be called by the admin adapter when the settings page loads function load(settings, onChange) { // example: select elements with id=key and class=value and insert value if (!settings) return; $('.value').each(function () { var $key = $(this); var id = $key.attr('id'); if ($key.attr('type') === 'checkbox') { // do not call onChange direct, because onChange could expect some arguments $key.prop('checked', settings[id]) .on('change', () => onChange()) ; } else { // do not call onChange direct, because onChange could expect some arguments $key.val(settings[id]) .on('change', () => onChange()) .on('keyup', () => onChange()) ; } }); onChange(false); // reinitialize all the Materialize labels on the page if you are dynamically adding inputs: if (M) M.updateTextFields(); } // This will be called by the admin adapter when the user presses the save button function save(callback) { // example: select elements with class=value and build settings object var obj = {}; $('.value').each(function () { var $this = $(this); if ($this.attr('type') === 'checkbox') { obj[$this.attr('id')] = $this.prop('checked'); } else if ($this.attr('type') === 'number') { obj[$this.attr('id')] = parseFloat($this.val()); } else { obj[$this.attr('id')] = $this.val(); } }); callback(obj); } </script> `} </head> <body> ${useReact ? ` <!-- this is where the React components are loaded into --> <div class="m adapter-container" id="root"></div> <!-- load compiled React scripts --> <script type="text/javascript" src="build/index.js"></script> ` : ` <div class="m adapter-container"> <div class="row"> <div class="col s12 m4 l2"> <img src="${(0, questions_1.getIconName)(answers)}" class="logo"> </div> </div> <!-- Put your content here --> <!-- For example columns with settings: --> <div class="row">${adapterSettings.map(generateSettingsDiv).join("\n")} </div> </div> `} </body> </html> `; return template.trim(); }); //# sourceMappingURL=index_m.html.js.map