442 lines
16 KiB
JavaScript
442 lines
16 KiB
JavaScript
/**
|
|||
|
|
* Shared Results container helpers.
|
||
|
|
*
|
||
|
|
* Exposes:
|
||
|
|
* - window.NCResultsContainer.apply(config)
|
||
|
|
* - window.NCResultsContainer.ensurePaginationBars(paginationBars)
|
||
|
|
*/
|
||
|
|
(() => {
|
||
|
|
const VIEW_MODES = [
|
||
|
|
{
|
||
|
|
value: 'list',
|
||
|
|
label: 'List view',
|
||
|
|
icon: '<svg aria-hidden="true" viewBox="0 0 24 24" fill="none" class="h-4 w-4"><path d="M4 6h16M4 12h16M4 18h16" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
||
|
|
},
|
||
|
|
{
|
||
|
|
value: 'grid',
|
||
|
|
label: 'Grid view',
|
||
|
|
icon: '<svg aria-hidden="true" viewBox="0 0 24 24" fill="none" class="h-4 w-4"><rect x="4" y="4" width="6" height="6" rx="1" stroke="currentColor" stroke-width="2"/><rect x="14" y="4" width="6" height="6" rx="1" stroke="currentColor" stroke-width="2"/><rect x="4" y="14" width="6" height="6" rx="1" stroke="currentColor" stroke-width="2"/><rect x="14" y="14" width="6" height="6" rx="1" stroke="currentColor" stroke-width="2"/></svg>',
|
||
|
|
},
|
||
|
|
];
|
||
|
|
|
||
|
|
const VIEW_MODE_SYNC_KEY = '__ncViewModeSelectorSync';
|
||
|
|
const VIEW_MODE_CHANGE_KEY = '__ncViewModeSelectorChange';
|
||
|
|
const DEFAULT_RESULTS_CONTAINER_CLASSES = [
|
||
|
|
'rounded-lg',
|
||
|
|
'border',
|
||
|
|
'border-[var(--color-border)]',
|
||
|
|
'bg-[var(--color-surface)]',
|
||
|
|
];
|
||
|
|
|
||
|
|
function toElement(target) {
|
||
|
|
if (!target) return null;
|
||
|
|
if (target instanceof Element) return target;
|
||
|
|
if (typeof target === 'string') {
|
||
|
|
return document.getElementById(target) || document.querySelector(target);
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
function toElementList(targets) {
|
||
|
|
if (!targets) return [];
|
||
|
|
const list = Array.isArray(targets) ? targets : [targets];
|
||
|
|
return list.map(toElement).filter(el => el instanceof Element);
|
||
|
|
}
|
||
|
|
|
||
|
|
function addClasses(el, classNames) {
|
||
|
|
if (!el || !classNames) return;
|
||
|
|
const list = Array.isArray(classNames) ? classNames : String(classNames).split(/\s+/);
|
||
|
|
for (const name of list) {
|
||
|
|
const token = String(name || '').trim();
|
||
|
|
if (token) el.classList.add(token);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function ensurePaginationBars(bars) {
|
||
|
|
for (const bar of toElementList(bars)) {
|
||
|
|
if (!bar.style.minHeight) bar.style.minHeight = '48px';
|
||
|
|
bar.classList.add('w-full');
|
||
|
|
bar.dataset.ncPaginationSlot = '1';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function normalizeViewModeConfig(config) {
|
||
|
|
if (!config) return null;
|
||
|
|
if (config instanceof Element || typeof config === 'string') {
|
||
|
|
return { select: config };
|
||
|
|
}
|
||
|
|
return config;
|
||
|
|
}
|
||
|
|
|
||
|
|
function normalizeModeValue(mode, fallback = 'grid') {
|
||
|
|
const value = String(mode || '').toLowerCase();
|
||
|
|
if (value === 'compact') return 'list';
|
||
|
|
if (value === 'list' || value === 'grid') return value;
|
||
|
|
return fallback;
|
||
|
|
}
|
||
|
|
|
||
|
|
function readStoredMode(storageKey) {
|
||
|
|
if (!storageKey) return null;
|
||
|
|
try {
|
||
|
|
const raw = window.localStorage.getItem(storageKey);
|
||
|
|
return raw ? normalizeModeValue(raw, null) : null;
|
||
|
|
} catch {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function writeStoredMode(storageKey, mode) {
|
||
|
|
if (!storageKey) return;
|
||
|
|
try {
|
||
|
|
window.localStorage.setItem(storageKey, mode);
|
||
|
|
} catch {
|
||
|
|
// ignore storage errors
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function applyViewModeSelector(resultsContainer, config) {
|
||
|
|
const viewMode = normalizeViewModeConfig(config);
|
||
|
|
if (!resultsContainer || !viewMode) return;
|
||
|
|
|
||
|
|
const resultsContent = resultsContainer.querySelector('[data-nc-results-content="1"]');
|
||
|
|
|
||
|
|
function applyModeToResultsContent(mode) {
|
||
|
|
if (!resultsContent) return;
|
||
|
|
resultsContent.dataset.ncDisplayMode = normalizeModeValue(mode, 'grid');
|
||
|
|
}
|
||
|
|
|
||
|
|
const select = toElement(
|
||
|
|
viewMode.select ||
|
||
|
|
viewMode.selectInput ||
|
||
|
|
viewMode.selectElement ||
|
||
|
|
viewMode.target ||
|
||
|
|
viewMode.id
|
||
|
|
);
|
||
|
|
const selectEl = select && select.tagName === 'SELECT' ? select : null;
|
||
|
|
|
||
|
|
const selectId = (selectEl && selectEl.id) || String(viewMode.key || 'view-mode');
|
||
|
|
const selectorId = `nc-view-mode-selector-${selectId}`;
|
||
|
|
|
||
|
|
let modes = VIEW_MODES;
|
||
|
|
if (Array.isArray(viewMode.modes) && viewMode.modes.length > 0) {
|
||
|
|
const allowed = new Set(
|
||
|
|
viewMode.modes.map(mode => normalizeModeValue(mode, null)).filter(Boolean)
|
||
|
|
);
|
||
|
|
modes = VIEW_MODES.filter(mode => allowed.has(mode.value));
|
||
|
|
}
|
||
|
|
if (selectEl) {
|
||
|
|
const modeValues = new Set(
|
||
|
|
Array.from(selectEl.options || [])
|
||
|
|
.map(opt => normalizeModeValue(opt.value, null))
|
||
|
|
.filter(Boolean)
|
||
|
|
);
|
||
|
|
modes = modes.filter(mode => modeValues.has(mode.value));
|
||
|
|
}
|
||
|
|
if (!modes.length) return;
|
||
|
|
|
||
|
|
const defaultMode = normalizeModeValue(viewMode.defaultMode, 'grid');
|
||
|
|
const storageKey =
|
||
|
|
typeof viewMode.storageKey === 'string' && viewMode.storageKey.trim().length > 0
|
||
|
|
? viewMode.storageKey.trim()
|
||
|
|
: null;
|
||
|
|
|
||
|
|
let inlineMode = defaultMode;
|
||
|
|
if (!selectEl) {
|
||
|
|
const fromGetter =
|
||
|
|
typeof viewMode.getMode === 'function'
|
||
|
|
? normalizeModeValue(viewMode.getMode(), defaultMode)
|
||
|
|
: null;
|
||
|
|
const fromStorage = readStoredMode(storageKey);
|
||
|
|
inlineMode = fromGetter || fromStorage || defaultMode;
|
||
|
|
}
|
||
|
|
|
||
|
|
function getActiveMode() {
|
||
|
|
if (selectEl) return normalizeModeValue(selectEl.value, defaultMode);
|
||
|
|
return normalizeModeValue(inlineMode, defaultMode);
|
||
|
|
}
|
||
|
|
|
||
|
|
function setActiveMode(mode, source = 'ui') {
|
||
|
|
const nextMode = normalizeModeValue(mode, defaultMode);
|
||
|
|
const available = modes.some(m => m.value === nextMode);
|
||
|
|
if (!available) return;
|
||
|
|
|
||
|
|
applyModeToResultsContent(nextMode);
|
||
|
|
|
||
|
|
if (selectEl) {
|
||
|
|
if (normalizeModeValue(selectEl.value, defaultMode) !== nextMode) {
|
||
|
|
selectEl.value = nextMode;
|
||
|
|
if (source !== 'select') {
|
||
|
|
selectEl.dispatchEvent(new Event('change', { bubbles: true }));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
inlineMode = nextMode;
|
||
|
|
if (typeof viewMode.setMode === 'function') {
|
||
|
|
try {
|
||
|
|
viewMode.setMode(nextMode);
|
||
|
|
} catch {
|
||
|
|
// ignore consumer callback errors
|
||
|
|
}
|
||
|
|
}
|
||
|
|
writeStoredMode(storageKey, nextMode);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!selectEl && typeof viewMode.onModeChange === 'function') {
|
||
|
|
try {
|
||
|
|
viewMode.onModeChange(nextMode);
|
||
|
|
} catch {
|
||
|
|
// ignore consumer callback errors
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const ownedHostSelector = '[data-nc-display-mode-host="1"]';
|
||
|
|
|
||
|
|
function getOwnedHosts() {
|
||
|
|
return Array.from(resultsContainer.querySelectorAll(ownedHostSelector)).filter(
|
||
|
|
host => host.dataset.ncViewModeOwner === selectorId
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
function hasResultCards() {
|
||
|
|
if (!(resultsContent instanceof Element)) return false;
|
||
|
|
return Boolean(resultsContent.querySelector('[data-nc-result-card="1"]'));
|
||
|
|
}
|
||
|
|
|
||
|
|
function syncSelectorVisibility() {
|
||
|
|
const showSelector = hasResultCards();
|
||
|
|
for (const host of getOwnedHosts()) {
|
||
|
|
host.classList.toggle('hidden', !showSelector);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (selectEl && viewMode.hideSelect === false) {
|
||
|
|
selectEl.classList.toggle('hidden', !showSelector);
|
||
|
|
selectEl.setAttribute('aria-hidden', showSelector ? 'false' : 'true');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function resolveRenderHosts() {
|
||
|
|
const centerSlots = Array.from(
|
||
|
|
resultsContainer.querySelectorAll('[data-nc-pagination-center-slot="1"]')
|
||
|
|
);
|
||
|
|
const paginationBars = Array.from(
|
||
|
|
resultsContainer.querySelectorAll('[data-nc-pagination-slot="1"]')
|
||
|
|
);
|
||
|
|
const hosts = [];
|
||
|
|
|
||
|
|
if (centerSlots.length) {
|
||
|
|
for (const slot of centerSlots) {
|
||
|
|
let host = Array.from(slot.querySelectorAll(ownedHostSelector)).find(
|
||
|
|
candidate => candidate.dataset.ncViewModeOwner === selectorId
|
||
|
|
);
|
||
|
|
if (!host) {
|
||
|
|
host = document.createElement('div');
|
||
|
|
host.className = 'flex items-center justify-center';
|
||
|
|
host.dataset.ncDisplayModeHost = '1';
|
||
|
|
host.dataset.ncViewModeOwner = selectorId;
|
||
|
|
slot.appendChild(host);
|
||
|
|
}
|
||
|
|
hosts.push(host);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
let host = getOwnedHosts().find(candidate => candidate.dataset.ncViewModeFallback === '1');
|
||
|
|
if (!host) {
|
||
|
|
host = document.createElement('div');
|
||
|
|
host.className = 'mb-3 flex items-center justify-center';
|
||
|
|
host.dataset.ncDisplayModeHost = '1';
|
||
|
|
host.dataset.ncViewModeOwner = selectorId;
|
||
|
|
host.dataset.ncViewModeFallback = '1';
|
||
|
|
}
|
||
|
|
|
||
|
|
const anchor = paginationBars.find(bar => bar && bar.parentElement === resultsContainer);
|
||
|
|
if (anchor) {
|
||
|
|
resultsContainer.insertBefore(host, anchor);
|
||
|
|
} else {
|
||
|
|
resultsContainer.insertBefore(host, resultsContainer.firstChild);
|
||
|
|
}
|
||
|
|
hosts.push(host);
|
||
|
|
}
|
||
|
|
|
||
|
|
const keep = new Set(hosts);
|
||
|
|
for (const existingHost of getOwnedHosts()) {
|
||
|
|
if (!keep.has(existingHost)) existingHost.remove();
|
||
|
|
}
|
||
|
|
|
||
|
|
return hosts;
|
||
|
|
}
|
||
|
|
|
||
|
|
let syncActiveButtons = () => {};
|
||
|
|
|
||
|
|
function renderSelectorHosts() {
|
||
|
|
const hosts = resolveRenderHosts();
|
||
|
|
const buttonGroups = [];
|
||
|
|
|
||
|
|
for (const host of hosts) {
|
||
|
|
host.innerHTML = '';
|
||
|
|
|
||
|
|
const shell = document.createElement('div');
|
||
|
|
shell.className = 'inline-flex items-center justify-center';
|
||
|
|
|
||
|
|
const group = document.createElement('div');
|
||
|
|
group.className = 'inline-flex items-center gap-1.5';
|
||
|
|
group.setAttribute('role', 'group');
|
||
|
|
group.setAttribute('aria-label', viewMode.ariaLabel || 'Display mode');
|
||
|
|
|
||
|
|
const buttonByMode = new Map();
|
||
|
|
for (const mode of modes) {
|
||
|
|
const button = document.createElement('button');
|
||
|
|
button.type = 'button';
|
||
|
|
button.className =
|
||
|
|
'inline-flex h-8 w-8 items-center justify-center rounded border border-transparent text-[var(--color-text-dim)] transition-colors hover:text-[var(--color-text)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-300/20';
|
||
|
|
button.setAttribute('aria-label', mode.label);
|
||
|
|
button.setAttribute('title', mode.label);
|
||
|
|
button.dataset.ncDisplayMode = mode.value;
|
||
|
|
button.innerHTML = mode.icon;
|
||
|
|
button.addEventListener('click', () => {
|
||
|
|
if (getActiveMode() === mode.value) return;
|
||
|
|
setActiveMode(mode.value, 'ui');
|
||
|
|
syncActiveButtons();
|
||
|
|
});
|
||
|
|
buttonByMode.set(mode.value, button);
|
||
|
|
group.appendChild(button);
|
||
|
|
}
|
||
|
|
|
||
|
|
buttonGroups.push(buttonByMode);
|
||
|
|
shell.appendChild(group);
|
||
|
|
host.appendChild(shell);
|
||
|
|
}
|
||
|
|
|
||
|
|
syncActiveButtons = () => {
|
||
|
|
const activeMode = getActiveMode();
|
||
|
|
for (const group of buttonGroups) {
|
||
|
|
for (const mode of modes) {
|
||
|
|
const button = group.get(mode.value);
|
||
|
|
if (!button) continue;
|
||
|
|
const active = mode.value === activeMode;
|
||
|
|
button.setAttribute('aria-pressed', active ? 'true' : 'false');
|
||
|
|
button.classList.toggle('text-[var(--color-text)]', active);
|
||
|
|
button.classList.toggle('text-[var(--color-text-dim)]', !active);
|
||
|
|
button.classList.toggle('bg-[var(--color-surface)]', active);
|
||
|
|
button.classList.toggle('border-[var(--color-border-strong)]', active);
|
||
|
|
button.classList.toggle('shadow-[var(--shadow-sm)]', active);
|
||
|
|
button.classList.toggle('opacity-100', active);
|
||
|
|
button.classList.toggle('border-transparent', !active);
|
||
|
|
button.classList.toggle('opacity-80', !active);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
syncActiveButtons();
|
||
|
|
syncSelectorVisibility();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (selectEl && viewMode.hideSelect !== false) {
|
||
|
|
selectEl.style.display = 'none';
|
||
|
|
selectEl.setAttribute('aria-hidden', 'true');
|
||
|
|
selectEl.tabIndex = -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (selectEl && typeof selectEl[VIEW_MODE_SYNC_KEY] === 'function') {
|
||
|
|
selectEl.removeEventListener('change', selectEl[VIEW_MODE_SYNC_KEY]);
|
||
|
|
}
|
||
|
|
if (selectEl && typeof selectEl[VIEW_MODE_CHANGE_KEY] === 'function') {
|
||
|
|
selectEl.removeEventListener('change', selectEl[VIEW_MODE_CHANGE_KEY]);
|
||
|
|
}
|
||
|
|
if (selectEl) {
|
||
|
|
selectEl[VIEW_MODE_SYNC_KEY] = () => syncActiveButtons();
|
||
|
|
selectEl[VIEW_MODE_CHANGE_KEY] = () => {
|
||
|
|
const currentMode = getActiveMode();
|
||
|
|
applyModeToResultsContent(currentMode);
|
||
|
|
if (storageKey) writeStoredMode(storageKey, currentMode);
|
||
|
|
if (typeof viewMode.onModeChange === 'function') {
|
||
|
|
try {
|
||
|
|
viewMode.onModeChange(currentMode);
|
||
|
|
} catch {
|
||
|
|
// ignore consumer callback errors
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
selectEl.addEventListener('change', selectEl[VIEW_MODE_SYNC_KEY]);
|
||
|
|
selectEl.addEventListener('change', selectEl[VIEW_MODE_CHANGE_KEY]);
|
||
|
|
|
||
|
|
const stored = readStoredMode(storageKey);
|
||
|
|
if (stored && modes.some(mode => mode.value === stored) && selectEl.value !== stored) {
|
||
|
|
selectEl.value = stored;
|
||
|
|
selectEl.dispatchEvent(new Event('change', { bubbles: true }));
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
setActiveMode(inlineMode, 'init');
|
||
|
|
}
|
||
|
|
|
||
|
|
const paginationListenerKey = `__ncViewModePaginationListener_${selectorId}`;
|
||
|
|
if (typeof resultsContainer[paginationListenerKey] === 'function') {
|
||
|
|
resultsContainer.removeEventListener(
|
||
|
|
'nc:pagination-rendered',
|
||
|
|
resultsContainer[paginationListenerKey]
|
||
|
|
);
|
||
|
|
}
|
||
|
|
const onPaginationRendered = event => {
|
||
|
|
const target = event?.target;
|
||
|
|
if (!(target instanceof Element)) return;
|
||
|
|
if (!resultsContainer.contains(target)) return;
|
||
|
|
if (target.dataset.ncPaginationSlot !== '1') return;
|
||
|
|
renderSelectorHosts();
|
||
|
|
};
|
||
|
|
resultsContainer[paginationListenerKey] = onPaginationRendered;
|
||
|
|
resultsContainer.addEventListener('nc:pagination-rendered', onPaginationRendered);
|
||
|
|
|
||
|
|
const resultsObserverKey = `__ncViewModeResultsObserver_${selectorId}`;
|
||
|
|
const existingObserver = resultsContainer[resultsObserverKey];
|
||
|
|
if (existingObserver && typeof existingObserver.disconnect === 'function') {
|
||
|
|
existingObserver.disconnect();
|
||
|
|
}
|
||
|
|
if (resultsContent instanceof Element && typeof MutationObserver !== 'undefined') {
|
||
|
|
const observer = new MutationObserver(() => {
|
||
|
|
syncSelectorVisibility();
|
||
|
|
});
|
||
|
|
observer.observe(resultsContent, { childList: true, subtree: true });
|
||
|
|
resultsContainer[resultsObserverKey] = observer;
|
||
|
|
}
|
||
|
|
|
||
|
|
renderSelectorHosts();
|
||
|
|
applyModeToResultsContent(getActiveMode());
|
||
|
|
syncSelectorVisibility();
|
||
|
|
}
|
||
|
|
|
||
|
|
function applyResultsContainer(config = {}) {
|
||
|
|
const container = toElement(config.container || config.resultsContainer || config.id);
|
||
|
|
if (!container) return null;
|
||
|
|
|
||
|
|
if (config.useDefaultShellClasses === true) {
|
||
|
|
addClasses(container, DEFAULT_RESULTS_CONTAINER_CLASSES);
|
||
|
|
}
|
||
|
|
addClasses(container, config.classes);
|
||
|
|
|
||
|
|
const role = Object.prototype.hasOwnProperty.call(config, 'role') ? config.role : 'region';
|
||
|
|
if (role) container.setAttribute('role', role);
|
||
|
|
if (config.label) container.setAttribute('aria-label', config.label);
|
||
|
|
|
||
|
|
container.dataset.ncResultsContainer = '1';
|
||
|
|
|
||
|
|
const content = toElement(config.resultsContent || config.grid || config.list);
|
||
|
|
if (content) {
|
||
|
|
content.dataset.ncResultsContent = '1';
|
||
|
|
if (!content.hasAttribute('aria-live')) content.setAttribute('aria-live', 'polite');
|
||
|
|
}
|
||
|
|
|
||
|
|
const emptyState = toElement(config.emptyState);
|
||
|
|
if (emptyState) emptyState.dataset.ncResultsEmpty = '1';
|
||
|
|
|
||
|
|
ensurePaginationBars(config.topPagination);
|
||
|
|
ensurePaginationBars(config.bottomPagination);
|
||
|
|
ensurePaginationBars(config.paginationBars);
|
||
|
|
applyViewModeSelector(container, config.viewModeSelector || config.viewMode);
|
||
|
|
|
||
|
|
return container;
|
||
|
|
}
|
||
|
|
|
||
|
|
window.NCResultsContainer = {
|
||
|
|
apply: applyResultsContainer,
|
||
|
|
ensurePaginationBars,
|
||
|
|
};
|
||
|
|
})();
|