/** * ViewToggle - Unified component for view mode selection * * Usage: * const toggle = new ViewToggle('#container', { * options: [ * { value: 'list', label: 'List' }, * { value: 'charts', label: 'Charts' } * ], * default: 'charts', * storageKey: 'gapup_view_mode', * onChange: (mode) => { } * }) * * Or use the helper for common configs: * ViewToggle.createListChart('#container', 'gapup_view_mode') * ViewToggle.createGridView('#container', 'scanner_view') */ class ViewToggle { constructor(containerOrSelector, options = {}) { this.container = typeof containerOrSelector === "string" ? document.querySelector(containerOrSelector) : containerOrSelector; this.options = { options: options.options || [ { value: "list", label: "List" }, { value: "charts", label: "Charts" }, ], default: options.default || options.options?.[0]?.value || "list", storageKey: options.storageKey || null, onChange: options.onChange || (() => {}), align: options.align || "start", // 'start', 'center', 'end' }; this.currentMode = this._loadState(); this._render(); } _loadState() { if (this.options.storageKey) { return ( localStorage.getItem(this.options.storageKey) || this.options.default ); } return this.options.default; } _saveState(mode) { if (this.options.storageKey) { localStorage.setItem(this.options.storageKey, mode); } } _render() { const buttons = this.options.options .map((opt) => { const isActive = opt.value === this.currentMode; return ``; }) .join(""); this.container.innerHTML = `