Files
breadcrumb-the-shire/web/js/components/app-tenant-switcher.js
fs c7b8fd516a feat: extend module platform with UI slots, runtime components, CLI tooling and {{userId}} search support
Completes the generic module platform that enables modules to contribute
UI elements, runtime JS components, and search resources without any
core hardcoding.

New generic UI slot types:
- topbar.right_item: module-contributed topbar buttons
- layout.body_end_template: module-contributed dialog/overlay templates
- layout.head_style: module-contributed global CSS
- runtime.component: declarative JS component registration with phase ordering

New infrastructure:
- ModuleAutoloader: PSR-4 autoloading for module-local PHP classes
- ModuleRuntimePageBuilder: symlinks module pages into runtime directory
- ModuleRuntimeAssetPublisher: publishes module CSS/JS to web/modules/
- ModulePermissionSynchronizer: syncs module permissions to DB
- CLI scripts: module-runtime-sync, module-build, module-migrate,
  module-permissions-sync, module-assets-sync
- {{userId}} placeholder in SearchDataService for user-scoped search queries
- Component runtime with phased initialization (early/default/late)
- AppContainer.protectExistingBindings() to prevent module→core overwrites
- Architecture tests: ModuleStructureContractTest, CoreTemplateIsolationTest,
  FrontendComponentRuntimeContractTest, AppContainerIsolationContractTest

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 22:19:56 +01:00

106 lines
3.4 KiB
JavaScript

/**
* Tenant context switcher dropdown with logo display.
*/
import { showAsyncFlash } from './app-async-flash.js';
import { warnOnce, resolveHost } from '../core/app-dom.js';
const SWITCHER_ROOT_SELECTOR = '[data-tenant-switcher]';
const TENANT_LINK_SELECTOR = '[data-switch-tenant]';
const setPending = (link, pending) => {
link.style.pointerEvents = pending ? 'none' : '';
link.style.opacity = pending ? '0.5' : '';
if (pending) {
link.setAttribute('aria-busy', 'true');
} else {
link.removeAttribute('aria-busy');
}
};
const switchTenant = async (link) => {
const tenantId = link.dataset.switchTenant;
const csrfKey = link.dataset.csrfKey;
const csrfToken = link.dataset.csrfToken;
const errorMessage = link.dataset.errorMessage || 'Failed to switch tenant';
if (!tenantId || !csrfKey || !csrfToken) {
warnOnce('UI_DATA_MISSING', 'Missing tenant ID or CSRF token', { module: 'tenant-switcher' });
return;
}
// Disable link during request
setPending(link, true);
try {
const body = new URLSearchParams({
tenant_id: tenantId,
[csrfKey]: csrfToken
});
const response = await fetch('admin/users/switch-tenant', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'X-Requested-With': 'fetch'
},
body
});
if (response.ok) {
const data = await response.json();
if (data.ok) {
if (data.theme) {
document.documentElement.dataset.theme = data.theme;
}
window.location.reload();
} else {
warnOnce('FETCH_FAILED', 'Tenant switch failed', { module: 'tenant-switcher', error: data.error });
showAsyncFlash('error', errorMessage);
setPending(link, false);
}
} else {
warnOnce('FETCH_FAILED', `Tenant switch server error: ${response.status}`, { module: 'tenant-switcher' });
showAsyncFlash('error', errorMessage);
setPending(link, false);
}
} catch (error) {
warnOnce('FETCH_ERROR', 'Tenant switch network error', { module: 'tenant-switcher', error });
showAsyncFlash('error', errorMessage);
setPending(link, false);
}
};
export const initTenantSwitcher = (root = document, options = {}) => {
const host = resolveHost(root);
const switcherSelector = String(options.rootSelector || SWITCHER_ROOT_SELECTOR).trim() || SWITCHER_ROOT_SELECTOR;
const tenantLinkSelector = String(options.linkSelector || TENANT_LINK_SELECTOR).trim() || TENANT_LINK_SELECTOR;
const switcherRoot = host.matches?.(switcherSelector) ? host : host.querySelector(switcherSelector);
if (!switcherRoot) {
// Single-tenant users do not render a switcher; this is expected.
return { destroy: () => {} };
}
const tenantLinks = Array.from(switcherRoot.querySelectorAll(tenantLinkSelector));
if (!tenantLinks.length) {
warnOnce('UI_EL_MISSING', 'Tenant switcher root exists but contains no switch links', { module: 'tenant-switcher' });
return { destroy: () => {} };
}
const cleanupFns = [];
tenantLinks.forEach(link => {
const onClick = (e) => {
e.preventDefault();
switchTenant(link);
};
link.addEventListener('click', onClick);
cleanupFns.push(() => link.removeEventListener('click', onClick));
});
return {
destroy: () => {
cleanupFns.forEach((cleanup) => cleanup());
},
};
};