SHA256
73 lines
2.9 KiB
JavaScript
73 lines
2.9 KiB
JavaScript
import { t } from '../i18n/index.js';
|
|
// Общие состояния экрана: загрузка, «пусто» и ошибка – один вид на всё приложение.
|
|
// Индикатор загрузки появляется с задержкой (CSS): если данные пришли быстро, его не видно
|
|
// вовсе, и экран не мелькает.
|
|
import { iconHtml } from './ui-icon.js';
|
|
|
|
export function createSpinner({ size = 'md', label = 'Загрузка' } = {}) {
|
|
const el = document.createElement('span');
|
|
el.className = `spinner spinner--${size}`;
|
|
el.setAttribute('role', 'status');
|
|
el.setAttribute('aria-label', label);
|
|
return el;
|
|
}
|
|
|
|
export function createLoadingState({ label = 'Загрузка', delayMs = 350, className = '' } = {}) {
|
|
const el = document.createElement('div');
|
|
el.className = `loading-state${className ? ` ${className}` : ''}`;
|
|
el.style.setProperty('--loading-delay', `${Math.max(0, Number(delayMs) || 0)}ms`);
|
|
el.append(createSpinner({ label }));
|
|
return el;
|
|
}
|
|
|
|
/**
|
|
* Сообщение вместо содержимого: пусто или ошибка, с необязательным действием.
|
|
* @param {{ kind?: 'empty'|'error', icon?: string, title?: string, text?: string,
|
|
* actionLabel?: string, onAction?: Function, className?: string }} opts
|
|
*/
|
|
export function createStateMessage({
|
|
kind = 'empty', icon = '', title = '', text = '', actionLabel = '', onAction = null, className = '',
|
|
} = {}) {
|
|
const el = document.createElement('div');
|
|
el.className = `state-message state-message--${kind}${className ? ` ${className}` : ''}`;
|
|
if (kind === 'error') el.setAttribute('role', 'alert');
|
|
if (icon) {
|
|
const badge = document.createElement('span');
|
|
badge.className = 'state-message__icon';
|
|
badge.innerHTML = iconHtml(icon);
|
|
el.append(badge);
|
|
}
|
|
if (title) {
|
|
const heading = document.createElement('p');
|
|
heading.className = 'state-message__title';
|
|
heading.textContent = title;
|
|
el.append(heading);
|
|
}
|
|
if (text) {
|
|
const body = document.createElement('p');
|
|
body.className = 'state-message__text';
|
|
body.textContent = text;
|
|
el.append(body);
|
|
}
|
|
if (actionLabel && typeof onAction === 'function') {
|
|
const button = document.createElement('button');
|
|
button.type = 'button';
|
|
button.className = `${kind === 'error' ? 'secondary-btn' : 'primary-btn'} state-message__action`;
|
|
button.textContent = actionLabel;
|
|
button.addEventListener('click', onAction);
|
|
el.append(button);
|
|
}
|
|
return el;
|
|
}
|
|
|
|
export function createErrorState({
|
|
title = 'Не удалось загрузить',
|
|
text = 'Проверьте соединение и попробуйте ещё раз.',
|
|
onRetry = null,
|
|
className = '',
|
|
} = {}) {
|
|
return createStateMessage({
|
|
kind: 'error', icon: 'alert', title, text, actionLabel: onRetry ? t('screens.feedback.text_001') : '', onAction: onRetry, className,
|
|
});
|
|
}
|