213 lines
7.6 KiB
JavaScript
213 lines
7.6 KiB
JavaScript
(async function() {
|
|
'use strict';
|
|
let digests = [];
|
|
let editingId = null;
|
|
let saving = false;
|
|
|
|
function esc(s) {
|
|
return String(s || '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
|
|
}
|
|
|
|
function fmtShortIso(iso) {
|
|
if (!iso) return '';
|
|
const d = String(iso).slice(0, 10);
|
|
return d;
|
|
}
|
|
|
|
function digestCardHtml(d) {
|
|
const admin = isAdmin();
|
|
const mine = currentUser && d.author_id === currentUser.id;
|
|
const canEdit = admin || mine;
|
|
const pinned = !!d.is_pinned;
|
|
|
|
const actions = [];
|
|
if (canEdit) {
|
|
actions.push('<button type="button" class="btn-link" data-action="edit" data-id="' + d.id + '">Изменить</button>');
|
|
actions.push('<button type="button" class="btn-link danger" data-action="delete" data-id="' + d.id + '">Удалить</button>');
|
|
}
|
|
if (admin) {
|
|
actions.push('<button type="button" class="btn-link" data-action="pin" data-id="' + d.id + '" data-pin="' + (pinned ? '0' : '1') + '">' + (pinned ? 'Открепить' : 'Закрепить') + '</button>');
|
|
}
|
|
|
|
return '<div class="digest-card' + (pinned ? ' pinned' : '') + '" data-id="' + d.id + '">' +
|
|
'<div class="digest-head">' +
|
|
'<h3 class="digest-title">' + esc(d.title) + '</h3>' +
|
|
(pinned ? '<span class="digest-badge">Закреплено</span>' : '') +
|
|
'</div>' +
|
|
'<div class="digest-meta">' +
|
|
'<span>' + esc(d.author_name || '—') + '</span>' +
|
|
'<span>' + fmtShortIso(d.created_at) + '</span>' +
|
|
'</div>' +
|
|
'<div class="digest-body">' + esc(d.body) + '</div>' +
|
|
'<div class="digest-foot">' +
|
|
'<div class="digest-actions">' + actions.join('') + '</div>' +
|
|
'</div>' +
|
|
'</div>';
|
|
}
|
|
|
|
function renderDigests() {
|
|
const el = document.getElementById('digestList');
|
|
if (!el) return;
|
|
if (!digests.length) {
|
|
el.innerHTML = '<div class="digest-empty">Сообщений пока нет</div>';
|
|
return;
|
|
}
|
|
el.innerHTML = digests.map(digestCardHtml).join('');
|
|
renderIcons();
|
|
}
|
|
|
|
async function loadDigests() {
|
|
let res;
|
|
try {
|
|
res = await fetch(API + '/api/digests', { headers: hdr() });
|
|
} catch (e) {
|
|
showToast('Ошибка сети при загрузке дайджеста', true);
|
|
return;
|
|
}
|
|
if (!res.ok) return;
|
|
const data = await res.json().catch(function() { return {}; });
|
|
digests = Array.isArray(data.items) ? data.items : (Array.isArray(data) ? data : []);
|
|
renderDigests();
|
|
}
|
|
|
|
function openDigestModal(d) {
|
|
editingId = d ? d.id : null;
|
|
document.getElementById('digestModalTitle').textContent = d ? 'Редактирование сообщения' : 'Новое сообщение';
|
|
document.getElementById('digestTitle').value = d ? escAttr(d.title) : '';
|
|
document.getElementById('digestBody').value = d ? escAttr(d.body) : '';
|
|
const pinRow = document.getElementById('digestPinned').closest('.settings-field');
|
|
const pinInput = document.getElementById('digestPinned');
|
|
pinInput.checked = d ? !!d.is_pinned : false;
|
|
pinRow.style.display = isAdmin() ? '' : 'none';
|
|
document.getElementById('digestModal').classList.add('open');
|
|
document.getElementById('digestTitle').focus();
|
|
}
|
|
|
|
function escAttr(s) {
|
|
const d = document.createElement('textarea');
|
|
d.textContent = s || '';
|
|
return d.value;
|
|
}
|
|
|
|
function closeDigestModal() {
|
|
document.getElementById('digestModal').classList.remove('open');
|
|
editingId = null;
|
|
}
|
|
|
|
async function saveDigest() {
|
|
const title = document.getElementById('digestTitle').value.trim();
|
|
const body = document.getElementById('digestBody').value.trim();
|
|
const isPinned = document.getElementById('digestPinned').checked;
|
|
if (!title) { showToast('Введите заголовок', true); return; }
|
|
if (!body) { showToast('Введите текст сообщения', true); return; }
|
|
if (saving) return;
|
|
const payload = { title: title, body: body, is_pinned: isPinned };
|
|
saving = true;
|
|
let res;
|
|
try {
|
|
if (editingId) {
|
|
res = await fetch(API + '/api/digests/' + editingId, {
|
|
method: 'PUT', headers: hdrJson(), body: JSON.stringify(payload)
|
|
});
|
|
} else {
|
|
res = await fetch(API + '/api/digests', { method: 'POST', headers: hdrJson(), body: JSON.stringify(payload) });
|
|
}
|
|
} catch (e) {
|
|
saving = false;
|
|
showToast('Ошибка сети', true);
|
|
return;
|
|
}
|
|
saving = false;
|
|
const data = await res.json().catch(function() { return {}; });
|
|
if (!res.ok) {
|
|
showToast(data.error || 'Ошибка при сохранении', true);
|
|
return;
|
|
}
|
|
showToast(editingId ? 'Сообщение обновлено' : 'Сообщение создано');
|
|
closeDigestModal();
|
|
await loadDigests();
|
|
}
|
|
|
|
async function togglePin(id, toPin) {
|
|
let res;
|
|
try {
|
|
res = await fetch(API + '/api/digests/' + id + '/pin', { method: 'PUT', headers: hdr() });
|
|
} catch (e) {
|
|
showToast('Ошибка сети', true);
|
|
return;
|
|
}
|
|
const data = await res.json().catch(function() { return {}; });
|
|
if (!res.ok) {
|
|
showToast(data.error || 'Ошибка', true);
|
|
return;
|
|
}
|
|
await loadDigests();
|
|
showToast(toPin ? 'Закреплено' : 'Откреплено');
|
|
}
|
|
|
|
async function removeDigest(id) {
|
|
if (!confirm('Удалить это сообщение из дайджеста?')) return;
|
|
let res;
|
|
try {
|
|
res = await fetch(API + '/api/digests/' + id, { method: 'DELETE', headers: hdr() });
|
|
} catch (e) {
|
|
showToast('Ошибка сети', true);
|
|
return;
|
|
}
|
|
if (!res.ok) {
|
|
const data = await res.json().catch(function() { return {}; });
|
|
showToast(data.error || 'Ошибка при удалении', true);
|
|
return;
|
|
}
|
|
digests = digests.filter(function(d) { return d.id !== id; });
|
|
renderDigests();
|
|
showToast('Сообщение удалено');
|
|
}
|
|
|
|
function bindModal() {
|
|
document.getElementById('digestModalClose').addEventListener('click', closeDigestModal);
|
|
document.getElementById('digestCancel').addEventListener('click', closeDigestModal);
|
|
document.getElementById('digestSave').addEventListener('click', saveDigest);
|
|
document.getElementById('digestModal').addEventListener('click', function(e) {
|
|
if (e.target === e.currentTarget) closeDigestModal();
|
|
});
|
|
document.addEventListener('keydown', function(e) {
|
|
const m = document.getElementById('digestModal');
|
|
const open = m && m.classList.contains('open');
|
|
if (e.key === 'Escape' && open) closeDigestModal();
|
|
});
|
|
}
|
|
|
|
function bindPage() {
|
|
document.getElementById('digestNewBtn').addEventListener('click', function() { openDigestModal(null); });
|
|
document.addEventListener('click', async function(e) {
|
|
const actEl = e.target.closest('[data-action]');
|
|
if (actEl) {
|
|
const action = actEl.dataset.action;
|
|
const id = parseInt(actEl.dataset.id, 10);
|
|
if (action === 'edit') {
|
|
const d = digests.find(function(x) { return x.id === id; });
|
|
if (d) openDigestModal(d);
|
|
return;
|
|
}
|
|
if (action === 'delete') { await removeDigest(id); return; }
|
|
if (action === 'pin') { await togglePin(id, actEl.dataset.pin === '1'); return; }
|
|
return;
|
|
}
|
|
});
|
|
}
|
|
|
|
async function init() {
|
|
if (!(await checkAuth())) return;
|
|
buildSidebar(document.body.dataset.page);
|
|
const nb = document.getElementById('digestNewBtn');
|
|
if (nb) nb.style.display = isAdmin() ? '' : 'none';
|
|
bindModal();
|
|
bindPage();
|
|
renderIcons();
|
|
await loadDigests();
|
|
}
|
|
|
|
init();
|
|
})();
|