// ==========================================
// Chat Switchboard - Admin Surface Scaffold
// ==========================================
// Injects section-specific DOM into adminDynamic, wires listeners,
// and calls the appropriate ADMIN_LOADERS[section]() from ui-admin.js.
// Loaded only on the admin surface (/admin/:section).
(function() {
'use strict';
// Each admin section loader (ui-admin.js) expects specific container
// elements. SCAFFOLDING maps section name -> HTML to inject before
// the loader runs.
var SCAFFOLDING = {};
// -- People -----------------------------------------------------------
SCAFFOLDING.users =
'
';
SCAFFOLDING.teams =
'
' +
'' +
'
' +
'← Back ' +
'
' +
'' +
'
' +
'Require private providers (BYOK) ' +
'Allow team providers ' +
'
' +
'
' +
'
' +
'
+ Add Member ' +
'
';
SCAFFOLDING.groups =
'
' +
'' +
'
' +
'← Back ' +
'
' +
' ' +
'' +
'
' +
'
' +
'
+ Add Member ' +
'
';
// -- AI ---------------------------------------------------------------
SCAFFOLDING.providers =
'
';
SCAFFOLDING.models =
'' +
'Fetch Models ' +
' ' +
'
' +
'
';
SCAFFOLDING.personas =
'
';
SCAFFOLDING.roles = '
';
SCAFFOLDING.knowledgeBases = '
';
SCAFFOLDING.memory = '
';
// -- System -----------------------------------------------------------
SCAFFOLDING.settings =
'';
SCAFFOLDING.storage = '
';
SCAFFOLDING.extensions =
'
' +
'';
// -- Routing ----------------------------------------------------------
SCAFFOLDING.health =
'
' +
'
';
SCAFFOLDING.routing =
'+ New Policy
' +
'
' +
'
' +
'Test Routing ' +
'
' +
'
' +
'
';
SCAFFOLDING.capabilities =
'';
// -- Monitoring -------------------------------------------------------
SCAFFOLDING.usage =
'
' +
'
' +
'
' +
'
' +
'
';
SCAFFOLDING.audit =
'' +
'' +
'';
SCAFFOLDING.stats =
'';
// === Add button actions ==============================================
var ADD_ACTIONS = {
users: function() {
openModal('createUserModal');
var n = document.getElementById('adminNewUsername');
if (n) n.focus();
},
teams: function() {
openModal('createTeamModal');
var n = document.getElementById('adminTeamName');
if (n) n.focus();
},
providers: function() {
if (typeof UI !== 'undefined' && UI._adminProvForm) UI._adminProvForm.setCreateMode();
document.getElementById('providerFormTitle').textContent = 'Add Provider';
openModal('providerFormModal');
},
personas: function() {
if (typeof ensureAdminPersonaForm === 'function') {
var form = ensureAdminPersonaForm();
if (form) { form.setSubmitLabel('Create'); form.clearForm(); }
}
document.getElementById('personaFormTitle').textContent = 'Create Persona';
openModal('personaFormModal');
},
groups: function() {
openModal('createGroupModal');
var n = document.getElementById('adminGroupName');
if (n) n.focus();
},
extensions: function() {
var f = document.getElementById('adminInstallExtForm');
if (f) f.style.display = f.style.display === 'none' ? '' : 'none';
},
};
// === Init on DOMContentLoaded ========================================
document.addEventListener('DOMContentLoaded', function() {
var el = document.getElementById('adminContentBody');
if (!el) return;
var section = el.dataset.section;
if (!section) return;
// -- Category / nav resolution -----------
var catMap = {
users:'people', teams:'people', groups:'people',
providers:'ai', models:'ai', personas:'ai', roles:'ai', knowledgeBases:'ai', memory:'ai',
health:'routing', routing:'routing', capabilities:'routing',
settings:'system', storage:'system', extensions:'system',
usage:'monitoring', audit:'monitoring', stats:'monitoring',
};
var cat = catMap[section] || 'people';
var secMap = {
people: [{id:'users',l:'Users'},{id:'teams',l:'Teams'},{id:'groups',l:'Groups'}],
ai: [{id:'providers',l:'Providers'},{id:'models',l:'Models'},{id:'personas',l:'Personas'},{id:'roles',l:'Roles'},{id:'knowledgeBases',l:'Knowledge'},{id:'memory',l:'Memory'}],
routing: [{id:'health',l:'Health'},{id:'routing',l:'Routing'},{id:'capabilities',l:'Capabilities'}],
system: [{id:'settings',l:'Settings'},{id:'storage',l:'Storage'},{id:'extensions',l:'Extensions'}],
monitoring: [{id:'usage',l:'Usage'},{id:'audit',l:'Audit'},{id:'stats',l:'Stats'}],
};
// Rebuild left nav for current category
var navEl = document.getElementById('adminNav');
var base = window.__BASE__ || '';
if (navEl && secMap[cat]) {
navEl.innerHTML = secMap[cat].map(function(s) {
return '' + s.l + ' ';
}).join('');
}
// Update section title
var titleEl = document.getElementById('adminSectionTitle');
var curSec = secMap[cat] && secMap[cat].find(function(s) { return s.id === section; });
if (titleEl && curSec) titleEl.textContent = curSec.l;
// -- Inject scaffolding ------------------
var target = document.getElementById('adminDynamic');
if (target && SCAFFOLDING[section]) {
target.innerHTML = SCAFFOLDING[section];
} else if (target) {
target.innerHTML = 'Section: ' + section + '
';
}
// -- Show/wire Add button for sections that have one --
var addBtn = document.getElementById('adminAddBtn');
if (addBtn && ADD_ACTIONS[section]) {
addBtn.style.display = '';
addBtn.addEventListener('click', ADD_ACTIONS[section]);
}
// -- Wire install extension cancel -------
var extCancel = document.getElementById('adminInstallExtCancelBtn');
if (extCancel) {
extCancel.addEventListener('click', function() {
document.getElementById('adminInstallExtForm').style.display = 'none';
});
}
// -- Wire admin-handlers.js listeners (uses ?. so missing elements are safe) --
if (typeof _initAdminListeners === 'function') _initAdminListeners();
// -- Wire settings toggle show/hide listeners (banner, compaction, etc.) --
if (typeof _initAdminSettingsToggles === 'function') _initAdminSettingsToggles();
// -- Call the section data loader --------
if (typeof ADMIN_LOADERS !== 'undefined' && ADMIN_LOADERS[section]) {
ADMIN_LOADERS[section]();
}
});
})();