SHA256
93 lines
4.4 KiB
JavaScript
93 lines
4.4 KiB
JavaScript
import { t } from '../i18n/index.js';
|
||
// Шторка «Заметка о человеке»: текст и цветная метка, видны только владельцу аккаунта.
|
||
// Открывается с карты связей и из профиля человека; хранение – services/people-notes.js.
|
||
import { openSheet } from './sheet.js';
|
||
import { NOTE_MARKS, PERSON_NOTE_MAX_TEXT, getPersonNote, setPersonNote } from '../services/people-notes.js';
|
||
|
||
export function openPersonNoteSheet({ login, name = '', onClose = null } = {}) {
|
||
const current = getPersonNote(login);
|
||
let mark = current?.mark || '';
|
||
return openSheet({
|
||
title: t('dialogs.remaining.components.person_note_sheet.openpersonnotesheet.zametka_value1', { value1: name || login }),
|
||
className: 'person-note-sheet',
|
||
onClose,
|
||
content: (sheet) => {
|
||
const wrap = document.createElement('div');
|
||
wrap.className = 'stack person-note';
|
||
const hint = document.createElement('p');
|
||
hint.className = 'meta-muted person-note__hint';
|
||
hint.textContent = t('dialogs.modals.person_note_sheet.label_001');
|
||
const textarea = document.createElement('textarea');
|
||
textarea.className = 'input person-note__text';
|
||
textarea.rows = 5;
|
||
textarea.maxLength = PERSON_NOTE_MAX_TEXT;
|
||
textarea.placeholder = t('dialogs.modals.person_note_sheet.label_002');
|
||
textarea.value = current?.text || '';
|
||
textarea.setAttribute('aria-label', t('dialogs.modals.person_note_sheet.label_003'));
|
||
textarea.autofocus = true;
|
||
|
||
const marksLabel = document.createElement('div');
|
||
marksLabel.className = 'person-note__label';
|
||
marksLabel.id = `person-note-marks-${Math.random().toString(36).slice(2, 8)}`;
|
||
marksLabel.textContent = t('dialogs.modals.person_note_sheet.label_004');
|
||
const marks = document.createElement('div');
|
||
marks.className = 'person-note__marks';
|
||
marks.setAttribute('role', 'radiogroup');
|
||
marks.setAttribute('aria-labelledby', marksLabel.id);
|
||
const markButtons = [];
|
||
const syncMarks = () => markButtons.forEach((button) => button.setAttribute('aria-checked', String(button.dataset.mark === mark)));
|
||
for (const option of [{ id: '', label: t('dialogs.remaining.components.person_note_sheet.openpersonnotesheet.bez_metki') }, ...NOTE_MARKS]) {
|
||
const button = document.createElement('button');
|
||
button.type = 'button';
|
||
button.className = 'person-note__mark';
|
||
button.dataset.mark = option.id;
|
||
button.setAttribute('role', 'radio');
|
||
button.setAttribute('aria-label', option.label);
|
||
button.title = option.label;
|
||
button.addEventListener('click', () => { mark = option.id; syncMarks(); });
|
||
markButtons.push(button);
|
||
marks.append(button);
|
||
}
|
||
syncMarks();
|
||
wrap.append(hint, textarea, marksLabel, marks);
|
||
|
||
if (current) {
|
||
const remove = document.createElement('button');
|
||
remove.type = 'button';
|
||
remove.className = 'ghost-btn person-note__remove';
|
||
remove.textContent = t('dialogs.modals.person_note_sheet.label_005');
|
||
remove.addEventListener('click', () => {
|
||
setPersonNote(login, { text: '', mark: '' });
|
||
sheet.close();
|
||
});
|
||
sheet.footer.append(remove);
|
||
}
|
||
const save = document.createElement('button');
|
||
save.type = 'button';
|
||
save.className = 'primary-btn person-note__save';
|
||
save.textContent = t('dialogs.modals.person_note_sheet.label_006');
|
||
save.addEventListener('click', () => {
|
||
setPersonNote(login, { text: textarea.value, mark });
|
||
sheet.close();
|
||
});
|
||
sheet.footer.append(save);
|
||
return wrap;
|
||
},
|
||
});
|
||
}
|
||
|
||
// Готовый блок заметки (для карточки на карте и профиля); null, если заметки нет.
|
||
export function createPersonNotePreview(note, { className = '', onClick = null } = {}) {
|
||
if (!note) return null;
|
||
const el = document.createElement(onClick ? 'button' : 'div');
|
||
if (onClick) {
|
||
el.type = 'button';
|
||
el.title = t('screens.person_note_sheet.text_001');
|
||
el.addEventListener('click', onClick);
|
||
}
|
||
el.className = `person-note-preview${className ? ` ${className}` : ''}`;
|
||
if (note.mark) el.dataset.mark = note.mark;
|
||
el.textContent = note.text.trim() || t('dialogs.modals.person_note_sheet.label_007');
|
||
return el;
|
||
}
|