/** * LEDMatrix Select Dropdown Widget * * Enhanced dropdown select with custom labels. * * Schema example: * { * "theme": { * "type": "string", * "x-widget": "select-dropdown", * "enum": ["light", "dark", "auto"], * "x-options": { * "placeholder": "Select a theme...", * "labels": { * "light": "Light Mode", * "dark": "Dark Mode", * "auto": "System Default" * } * } * } * } * * @module SelectDropdownWidget */ (function() { 'use strict'; const base = window.BaseWidget ? new window.BaseWidget('SelectDropdown', '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('select-dropdown', { name: 'Select Dropdown Widget', version: '1.0.0', render: function(container, config, value, options) { const fieldId = sanitizeId(options.fieldId || container.id || 'select'); const xOptions = config['x-options'] || config['x_options'] || {}; const enumValues = config.enum || xOptions.options || []; const placeholder = xOptions.placeholder || 'Select...'; const labels = xOptions.labels || {}; const icons = xOptions.icons || {}; const disabled = xOptions.disabled === true; const required = xOptions.required === true; const currentValue = value !== null && value !== undefined ? String(value) : ''; let html = `
'; container.innerHTML = html; }, getValue: function(fieldId) { const safeId = sanitizeId(fieldId); const input = document.getElementById(`${safeId}_input`); return input ? input.value : ''; }, setValue: function(fieldId, value) { const safeId = sanitizeId(fieldId); const input = document.getElementById(`${safeId}_input`); if (input) { input.value = value !== null && value !== undefined ? String(value) : ''; } }, handlers: { onChange: function(fieldId) { const widget = window.LEDMatrixWidgets.get('select-dropdown'); triggerChange(fieldId, widget.getValue(fieldId)); } } }); console.log('[SelectDropdownWidget] Select dropdown widget registered'); })();