Files
SHiNE-server/shine-UI/js/components/topbar.js
T

184 lines
5.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { t } from '../i18n/index.js';
import { iconHtml } from './ui-icon.js';
import { createDropdownMenu } from './dropdown-menu.js';
function appendNode(target, node) {
if (!target || !(node instanceof Node)) return;
target.append(node);
}
function createActionButton(action = {}, cleanupFns) {
const button = document.createElement('button');
button.type = 'button';
button.className = `icon-btn topbar__action${action.className ? ` ${action.className}` : ''}`;
if (action.title) button.title = action.title;
if (action.ariaLabel) button.setAttribute('aria-label', action.ariaLabel);
if (action.id) button.dataset.action = action.id;
if (action.iconNode instanceof Node) {
button.append(action.iconNode);
} else if (action.icon) {
button.innerHTML = iconHtml(action.icon);
} else if (action.label === '⋮' || action.label === '⋯') {
// Меню экрана везде рисуется одной иконкой «⋯»
button.innerHTML = iconHtml('more');
} else {
button.textContent = String(action.label ?? '');
}
if (action.menu) {
const menu = createDropdownMenu({
anchorEl: button,
transparent: true,
dimBackground: true,
keepAnchorPressed: true,
...(typeof action.menu === 'object' ? action.menu : {}),
});
cleanupFns.add(() => menu.destroy());
} else if (typeof action.onClick === 'function') {
button.addEventListener('click', action.onClick);
}
return button;
}
export function createTopBar({
title = '',
className = '',
left = null,
center = null,
back = null,
leftLabel = '',
actions = [],
} = {}) {
const cleanupFns = new Set();
const topbar = document.createElement('header');
topbar.className = `topbar${className ? ` ${className}` : ''}`;
const leftSlot = document.createElement('div');
leftSlot.className = 'topbar__left';
const backAction = back;
if (backAction?.visible !== false && typeof backAction?.onClick === 'function') {
const button = document.createElement('button');
button.type = 'button';
button.className = `icon-btn topbar__back${backAction.className ? ` ${backAction.className}` : ''}`;
button.innerHTML = iconHtml('back');
button.setAttribute('aria-label', backAction.ariaLabel || t('screens.topbar.text_004'));
button.title = backAction.title || t('screens.topbar.text_005');
button.addEventListener('click', backAction.onClick);
leftSlot.append(button);
}
appendNode(leftSlot, left);
if (leftLabel) {
const label = document.createElement('span');
label.className = 'topbar__left-label';
label.textContent = leftLabel;
leftSlot.append(label);
}
const centerSlot = document.createElement('div');
centerSlot.className = 'topbar__center';
const resolvedCenter = center;
if (resolvedCenter instanceof Node) {
centerSlot.append(resolvedCenter);
} else {
const heading = document.createElement('h1');
heading.className = 'topbar__title';
heading.textContent = String(title || '');
centerSlot.append(heading);
}
const rightSlot = document.createElement('div');
rightSlot.className = 'topbar__right';
const normalizedActions = actions;
normalizedActions.forEach((action) => {
rightSlot.append(createActionButton(action, cleanupFns));
});
topbar.append(leftSlot, centerSlot, rightSlot);
topbar.addCleanup = (cleanup) => {
if (typeof cleanup === 'function') cleanupFns.add(cleanup);
return cleanup;
};
topbar.cleanup = () => {
for (const cleanup of cleanupFns) {
try {
cleanup();
} catch (error) {
console.warn('[TopBar] cleanup failed', error);
}
}
cleanupFns.clear();
};
return topbar;
}
/**
* Шапка-строка поиска (поиск по переписке, по связям и т. п.): «назад» закрывает поиск,
* крестик очищает поле, Enter – onSubmit, Escape – onClose.
* trailing – узел справа внутри поля (например, счётчик «3 из 12»).
*/
export function createSearchTopBar({
placeholder = 'Поиск',
value = '',
onInput = null,
onSubmit = null,
onClose = null,
trailing = null,
actions = [],
className = '',
} = {}) {
const field = document.createElement('label');
field.className = 'topbar-search';
field.innerHTML = `
<span class="topbar-search__icon" aria-hidden="true">${iconHtml('search')}</span>
<input class="topbar-search__input" type="search" autocomplete="off" autocapitalize="off" spellcheck="false" enterkeyhint="search" />
`;
const input = field.querySelector('input');
input.placeholder = placeholder;
input.setAttribute('aria-label', placeholder);
input.value = value;
if (trailing instanceof Node) field.append(trailing);
const clear = document.createElement('button');
clear.type = 'button';
clear.className = 'topbar-search__clear';
clear.innerHTML = iconHtml('close');
clear.setAttribute('aria-label', t('screens.topbar.text_006'));
clear.title = t('screens.topbar.text_001');
clear.hidden = !value;
clear.addEventListener('click', (event) => {
event.preventDefault();
input.value = '';
clear.hidden = true;
input.focus();
onInput?.('');
});
field.append(clear);
input.addEventListener('input', () => {
clear.hidden = !input.value;
onInput?.(input.value);
});
input.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
event.preventDefault();
onSubmit?.(input.value, event);
} else if (event.key === 'Escape') {
event.preventDefault();
onClose?.();
}
});
const topbar = createTopBar({
center: field,
className: `topbar--search${className ? ` ${className}` : ''}`,
back: { onClick: () => onClose?.(), ariaLabel: t('screens.topbar.text_002'), title: t('screens.topbar.text_003') },
actions,
});
topbar.searchInput = input;
return topbar;
}