(function () {
const treeEl = document.getElementById('dpTree');
const articleEl = document.getElementById('dpArticle');
const headSub = document.getElementById('dpHeadSub');
const lightbox = document.getElementById('dpLightbox');
const lightboxImg = document.getElementById('dpLightboxImg');
const COLLAPSE_KEY = 'whatido.docsPublic.collapsed';
const ICON_CHEVRON = '';
const ICON_FOLDER = '';
let tree = [];
let currentSlug = '';
let collapsed = new Set();
try {
const saved = JSON.parse(localStorage.getItem(COLLAPSE_KEY) || '[]');
if (Array.isArray(saved)) collapsed = new Set(saved.map(Number));
} catch (e) {}
function esc(value) {
return String(value == null ? '' : value)
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"');
}
function saveCollapsed() {
localStorage.setItem(COLLAPSE_KEY, JSON.stringify([...collapsed]));
}
async function getJson(url) {
const res = await fetch(url);
const data = await res.json().catch(() => ({}));
if (!res.ok) throw new Error(data.error || 'Ошибка загрузки');
return data;
}
function articleRow(article, depth) {
return `
`;
}
function sectionNode(node, depth) {
const closed = collapsed.has(Number(node.id));
const hasItems = (node.children && node.children.length) || (node.articles && node.articles.length);
const chev = hasItems
? ``
: '';
const children = closed ? '' : [
...(node.children || []).map(child => sectionNode(child, depth + 1)),
...(node.articles || []).map(article => articleRow(article, depth + 1)),
].join('');
return `
${chev}${ICON_FOLDER}${esc(node.title)}
${children}
`;
}
function renderTree() {
if (!tree.length) {
treeEl.innerHTML = 'Документация пока пуста
';
return;
}
treeEl.innerHTML = tree.map(node => sectionNode(node, 0)).join('');
}
async function open(slug, updateUrl) {
const data = await getJson('/api/docs/public/articles/' + encodeURIComponent(slug));
const article = data.item;
currentSlug = article.slug;
articleEl.innerHTML = `${esc(article.section_title || '')}
${esc(article.title)}
${WhatIDoMD.render(article.body)}
`;
document.title = article.title + ' — Документация';
if (headSub) headSub.textContent = article.section_title || '';
if (updateUrl !== false) history.replaceState(null, '', '?slug=' + encodeURIComponent(article.slug));
renderTree();
}
function bind() {
treeEl.addEventListener('click', e => {
const toggle = e.target.closest('[data-toggle]');
if (toggle) {
e.preventDefault();
const id = Number(toggle.dataset.toggle);
if (collapsed.has(id)) collapsed.delete(id); else collapsed.add(id);
saveCollapsed();
renderTree();
return;
}
const link = e.target.closest('[data-slug]');
if (link) {
e.preventDefault();
open(link.dataset.slug).catch(err => { articleEl.textContent = err.message; });
return;
}
const row = e.target.closest('.dp-section-row');
if (row) {
const id = Number(row.dataset.section);
if (collapsed.has(id)) collapsed.delete(id); else collapsed.add(id);
saveCollapsed();
renderTree();
}
});
articleEl.addEventListener('click', e => {
const img = e.target.closest('[data-img]');
if (!img) return;
e.preventDefault();
lightboxImg.src = img.dataset.img;
lightbox.classList.add('open');
});
lightbox.addEventListener('click', () => {
lightbox.classList.remove('open');
lightboxImg.removeAttribute('src');
});
document.addEventListener('keydown', e => {
if (e.key === 'Escape' && lightbox.classList.contains('open')) {
lightbox.classList.remove('open');
lightboxImg.removeAttribute('src');
}
});
}
function firstSlug(nodes) {
for (const node of nodes) {
if (node.articles && node.articles.length) return node.articles[0].slug;
const nested = firstSlug(node.children || []);
if (nested) return nested;
}
return '';
}
async function init() {
bind();
try {
const data = await getJson('/api/docs/public/tree');
tree = data.sections || [];
renderTree();
const params = new URLSearchParams(location.search);
const slug = params.get('slug') || firstSlug(tree);
if (slug) await open(slug, !params.get('slug'));
else articleEl.innerHTML = 'Статьи не опубликованы
';
} catch (e) {
articleEl.textContent = e.message;
treeEl.innerHTML = 'Не удалось загрузить документацию
';
}
}
init();
})();