(id, opts = {})
| 153 | } |
| 154 | |
| 155 | async function applyActiveSourceId(id, opts = {}) { |
| 156 | const select = document.getElementById('sourceSelector'); |
| 157 | if (!select) return false; |
| 158 | |
| 159 | const nextId = String(id || '').trim(); |
| 160 | if (!nextId) return false; |
| 161 | |
| 162 | const prevId = select.getAttribute('data-prev') || select.value || ''; |
| 163 | if (nextId === prevId && !opts.force) { |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | const hasOption = Array.from(select.options).some(opt => opt.value === nextId); |
| 168 | if (!hasOption) return false; |
| 169 | |
| 170 | select.value = nextId; |
| 171 | |
| 172 | const apiEnabled = window.__FR_SOURCES_API_ENABLED__ === true; |
| 173 | if (apiEnabled && opts.updateSession !== false) { |
| 174 | try { |
| 175 | const res = await fetch(withBase('/api/pro/sources/select.php'), { |
| 176 | method: 'POST', |
| 177 | credentials: 'include', |
| 178 | headers: { |
| 179 | 'Content-Type': 'application/json', |
| 180 | 'X-CSRF-Token': getCsrfToken(), |
| 181 | 'Accept': 'application/json', |
| 182 | }, |
| 183 | body: JSON.stringify({ id: nextId }), |
| 184 | }); |
| 185 | if (!res.ok) throw new Error('Select failed'); |
| 186 | } catch (e) { |
| 187 | if (prevId) select.value = prevId; |
| 188 | return false; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | select.setAttribute('data-prev', nextId); |
| 193 | try { localStorage.setItem('fr_active_source', nextId); } catch (e) { /* ignore */ } |
| 194 | |
| 195 | if (!opts.skipEvent) { |
| 196 | try { |
| 197 | window.dispatchEvent(new CustomEvent('filerise:source-change', { |
| 198 | detail: { id: nextId, origin: opts.origin || 'selector' } |
| 199 | })); |
| 200 | } catch (e) { /* ignore */ } |
| 201 | } |
| 202 | |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | try { |
| 207 | if (typeof window.__FR_SOURCES_API_ENABLED__ === 'undefined') { |
no test coverage detected