/** * LEDMatrix Radio Group Widget * * Exclusive option selection with radio buttons. * * Schema example: * { * "displayMode": { * "type": "string", * "x-widget": "radio-group", * "enum": ["auto", "manual", "scheduled"], * "x-options": { * "layout": "vertical", // "vertical", "horizontal" * "labels": { * "auto": "Automatic", * "manual": "Manual Control", * "scheduled": "Scheduled" * }, * "descriptions": { * "auto": "System decides when to display", * "manual": "You control when content shows", * "scheduled": "Display at specific times" * } * } * } * } * * @module RadioGroupWidget */ (function() { 'use strict'; const base = window.BaseWidget ? new window.BaseWidget('RadioGroup', '1.0.0') : null; function escapeHtml(text) { if (base) return base.escapeHtml(text); const div = document.createElement('div'); div.textContent = String(text); return div.innerHTML.replace(/"/g, '"').replace(/'/g, '''); } function sanitizeId(id) { if (base) return base.sanitizeId(id); return String(id).replace(/[^a-zA-Z0-9_-]/g, '_'); } function triggerChange(fieldId, value) { if (base) { base.triggerChange(fieldId, value); } else { const event = new CustomEvent('widget-change', { detail: { fieldId, value }, bubbles: true, cancelable: true }); document.dispatchEvent(event); } } window.LEDMatrixWidgets.register('radio-group', { name: 'Radio Group Widget', version: '1.0.0', render: function(container, config, value, options) { const fieldId = sanitizeId(options.fieldId || container.id || 'radio_group'); const xOptions = config['x-options'] || config['x_options'] || {}; const enumValues = config.enum || xOptions.options || []; const layout = xOptions.layout || 'vertical'; const labels = xOptions.labels || {}; const descriptions = xOptions.descriptions || {}; const disabled = xOptions.disabled === true; const currentValue = value !== null && value !== undefined ? String(value) : ''; const containerClass = layout === 'horizontal' ? 'flex flex-wrap gap-4' : 'space-y-3'; let html = `
'; container.innerHTML = html; }, getValue: function(fieldId) { const safeId = sanitizeId(fieldId); const widget = document.getElementById(`${safeId}_widget`); if (!widget) return ''; const checked = widget.querySelector('input[type="radio"]:checked'); return checked ? checked.value : ''; }, setValue: function(fieldId, value) { const safeId = sanitizeId(fieldId); const widget = document.getElementById(`${safeId}_widget`); if (!widget) return; const radios = widget.querySelectorAll('input[type="radio"]'); radios.forEach(radio => { radio.checked = radio.value === String(value); }); }, handlers: { onChange: function(fieldId, value) { triggerChange(fieldId, value); } } }); console.log('[RadioGroupWidget] Radio group widget registered'); })();