// Шторка «Заметка о человеке»: текст и цветная метка, видны только владельцу аккаунта. // Открывается с карты связей и из профиля человека; хранение – 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: `Заметка: ${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 = 'Видна только вам. Пока хранится на этом устройстве.'; const textarea = document.createElement('textarea'); textarea.className = 'input person-note__text'; textarea.rows = 5; textarea.maxLength = PERSON_NOTE_MAX_TEXT; textarea.placeholder = 'Кто это, откуда знакомы, о чём договорились…'; textarea.value = current?.text || ''; textarea.setAttribute('aria-label', 'Текст заметки'); 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 = 'Метка на карте связей'; 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: 'Без метки' }, ...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 = 'Удалить'; 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 = 'Сохранить'; 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 = 'Изменить заметку'; el.addEventListener('click', onClick); } el.className = `person-note-preview${className ? ` ${className}` : ''}`; if (note.mark) el.dataset.mark = note.mark; el.textContent = note.text.trim() || 'Метка без текста'; return el; }