/* * ITFlow - a contents list for a long document or Knowledge Base article (agent/document.php, * agent/kb_article.php, and outside the agent app documentReadHtml() - the client portal, a shared * link, the public help center): its headings, in the [data-document-contents] card beside it, each * one a link down to it. Only when it has three headings or more - a short one doesn't need it. * Headings without an id get one, so a link to a section can be shared. The section being read is * marked in the list as the page scrolls. * * window.itflowDocumentContents() makes it again, for what is filled in after the page has loaded - * a shared link's document (js/guest_share.js). A card with data-document-contents-keep-address * leaves the address alone: a shared link's secret is after its #. * * A link with data-document-tab (the document page's "2 earlier versions") opens that tab of the * card beside the document, and goes to it. */ (function () { 'use strict'; let headings = []; let links = {}; let listening = false; let queued = false; let picked = null; let picked_until = 0; // The section being read is marked in the list - the last heading above the top quarter of the window, // or the one just picked from the list while the page scrolls to it, as the next may be close under it const mark = function () { queued = false; if (!headings.length) { return; } let current = headings[0]; const line = window.innerHeight * 0.25; headings.forEach(function (h) { if (h.getBoundingClientRect().top <= line) { current = h; } }); if (picked && Date.now() < picked_until && headings.indexOf(picked) !== -1) { current = picked; } Object.keys(links).forEach(function (id) { links[id].classList.toggle('active', id === current.id); }); }; const queue = function () { if (!queued) { queued = true; window.requestAnimationFrame(mark); } }; const build = function () { const card = document.querySelector('[data-document-contents]'); const body = document.querySelector('[data-document-body], .itflow-document-body'); if (!card || !body) { return; } const nav = card.querySelector('nav'); const old = nav.querySelector('ol'); if (old) { old.remove(); } card.hidden = true; links = {}; headings = Array.prototype.filter.call(body.querySelectorAll('h1, h2, h3, h4'), function (h) { return h.textContent.trim() !== ''; }); if (headings.length < 3) { headings = []; return; } const keep_address = card.hasAttribute('data-document-contents-keep-address'); // Indent from the highest level the document actually uses const top = Math.min.apply(null, headings.map(function (h) { return Number(h.tagName[1]); })); const used = {}; const list = document.createElement('ol'); list.className = 'list-unstyled mb-0'; headings.forEach(function (h, i) { if (!h.id) { let slug = 'section-' + h.textContent.trim().toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 60); if (slug === 'section-' || used[slug] || document.getElementById(slug)) { slug = 'section-' + (i + 1); } h.id = slug; } used[h.id] = true; const li = document.createElement('li'); li.className = 'itflow-document-contents-level-' + (Number(h.tagName[1]) - top); const a = document.createElement('a'); a.href = '#' + h.id; a.textContent = h.textContent.trim(); a.addEventListener('click', function (e) { e.preventDefault(); picked = h; picked_until = Date.now() + 1500; queue(); h.scrollIntoView({ behavior: 'smooth', block: 'start' }); if (!keep_address) { history.replaceState(null, '', '#' + h.id); } }); li.appendChild(a); list.appendChild(li); links[h.id] = a; }); nav.appendChild(list); card.hidden = false; // Opened at a section's link if (location.hash && !keep_address) { let target = null; try { target = document.getElementById(decodeURIComponent(location.hash.slice(1))); } catch (error) { // Not an address of a section } if (target && body.contains(target)) { target.scrollIntoView({ block: 'start' }); } } if (!listening) { listening = true; window.addEventListener('scroll', queue, { passive: true }); window.addEventListener('resize', queue); } mark(); }; window.itflowDocumentContents = build; build(); })(); (function () { 'use strict'; document.addEventListener('click', function (e) { const link = e.target.closest ? e.target.closest('a[data-document-tab]') : null; if (!link) { return; } const tab = document.querySelector('button[data-document-tab="' + link.getAttribute('data-document-tab') + '"]'); if (!tab || !window.bootstrap) { return; } e.preventDefault(); bootstrap.Tab.getOrCreateInstance(tab).show(); tab.closest('.card').scrollIntoView({ behavior: 'smooth', block: 'start' }); }); })();