/* * SoulSync — Video settings additions (isolated). * * The video side shows the real music settings page; this module only drives the * VIDEO-specific bits added to it — for now, the Movies/TV library mapping * (which server library the scan reads). It populates the dropdowns from * /api/video/libraries when the Settings page is shown on the video side and * saves the choice back. Self-contained IIFE, no globals, no inline handlers. */ (function () { 'use strict'; var PAGE_ID = 'video-settings'; var URL = '/api/video/libraries'; function status(text) { var n = document.querySelector('[data-video-lib-status]'); if (n) n.textContent = text || ''; } function fill(select, items, selected) { if (!select) return; select.textContent = ''; var none = document.createElement('option'); none.value = ''; none.textContent = '— None —'; select.appendChild(none); for (var i = 0; i < items.length; i++) { var opt = document.createElement('option'); opt.value = items[i].title; opt.textContent = items[i].title; if (items[i].title === selected) opt.selected = true; select.appendChild(opt); } } function load() { status('Loading…'); fetch(URL, { headers: { 'Accept': 'application/json' } }) .then(function (r) { return r.ok ? r.json() : null; }) .then(function (d) { if (!d || d.error) { status('Could not load libraries'); return; } var sel = d.selected || {}; fill(document.querySelector('[data-video-lib-select="movies"]'), d.movies || [], sel.movies); fill(document.querySelector('[data-video-lib-select="tv"]'), d.tv || [], sel.tv); status(''); }) .catch(function () { status('Could not load libraries'); }); } function save() { var m = document.querySelector('[data-video-lib-select="movies"]'); var t = document.querySelector('[data-video-lib-select="tv"]'); status('Saving…'); fetch(URL, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' }, body: JSON.stringify({ movies: m ? m.value : '', tv: t ? t.value : '' }) }) .then(function (r) { return r.json(); }) .then(function () { status('Saved'); }) .catch(function () { status('Save failed'); }); } function onPageShown(e) { if (e && e.detail === PAGE_ID) load(); } function init() { // Save the moment a library is picked — same behaviour as the music // 'Music Library' selector above (which saves on change, no button). var selects = document.querySelectorAll('[data-video-lib-select]'); for (var i = 0; i < selects.length; i++) { selects[i].addEventListener('change', save); } document.addEventListener('soulsync:video-page-shown', onPageShown); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })();