@openhab-ui/setup-and-maintenance
Version:
Configuration and maintenance interface for openHAB
62 lines (52 loc) • 1.91 kB
JavaScript
/**
* This module converts an array of parameter objects into an HTML table.
* @module parameterTableBuilder
*/
;
function extraColumnInfo(params) {
const result = {
showAttributes: false,
showDefaultValue: false,
};
params.forEach((param) => {
result.showAttributes = result.showAttributes || param.optional;
result.showDefaultValue = result.showDefaultValue || param.defaultvalue !== undefined;
});
return result;
}
function buildTableEntry(param, columnInfo) {
const attributeCell = columnInfo.showAttributes ? `<td class="attributes">${param.optional}</td>` : '';
const defaultCell = columnInfo.showDefaultValue ? `<td class="default">${param.defaultvalue === undefined ? '' : param.defaultvalue}</td>` : '';
return `<tr>
<td class="name"><code>${param.name}</code></td>
<td class="type">${param.type}</td>
${attributeCell}
${defaultCell}
<td class="description last">${param.description}</td>
</tr>`;
}
function buildTableHeader(columnInfo) {
const attributeHeader = columnInfo.showAttributes ? '<th>Attributes</th>' : '';
const defaultHeader = columnInfo.showDefaultValue ? '<th>Default</th>' : '';
return `<thead>
<tr>
<th>Name</th>
<th>Type</th>
${attributeHeader}
${defaultHeader}
<th class="last">Description</th>
</tr>
</thead>`;
}
exports.build = function (title, params) {
const columnInfo = extraColumnInfo(params);
const paramTableEntries = [];
params.forEach((param) => {
paramTableEntries.push(buildTableEntry(param, columnInfo));
});
return `<h5>${title}</h5>
<table class="params" style="margin-bottom:10px">
${buildTableHeader(columnInfo)}
${paramTableEntries.join('')}
</table>`;
}