mirror of
https://github.com/itflow-org/itflow
synced 2026-12-02 09:09:01 +00:00
Documents and articles outside the agent app read like the document page: the portal document and Help article pages, shared links and the public help center lay out what they say as on the agent document page, with a long one's Contents beside it in view as you scroll and marking the section being read, or above it on a phone; pictures open full size when clicked; a section picked from Contents stays marked, on the agent page too; 26.12.3 changelog entries
This commit is contained in:
@@ -1,76 +1,35 @@
|
||||
/*
|
||||
* ITFlow - a contents list for a long document or Knowledge Base article (agent/document.php,
|
||||
* agent/kb_article.php): 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.
|
||||
* 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';
|
||||
|
||||
const card = document.querySelector('[data-document-contents]');
|
||||
const body = document.querySelector('.itflow-document-body');
|
||||
if (!card || !body) {
|
||||
return;
|
||||
}
|
||||
const headings = Array.prototype.filter.call(body.querySelectorAll('h1, h2, h3, h4'), function (h) {
|
||||
return h.textContent.trim() !== '';
|
||||
});
|
||||
if (headings.length < 3) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 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();
|
||||
h.scrollIntoView({ behavior: 'smooth', block: 'start' });
|
||||
history.replaceState(null, '', '#' + h.id);
|
||||
});
|
||||
li.appendChild(a);
|
||||
list.appendChild(li);
|
||||
});
|
||||
|
||||
card.querySelector('nav').appendChild(list);
|
||||
card.hidden = false;
|
||||
|
||||
// Opened at a section's link
|
||||
if (location.hash) {
|
||||
const target = document.getElementById(decodeURIComponent(location.hash.slice(1)));
|
||||
if (target && body.contains(target)) {
|
||||
target.scrollIntoView({ block: 'start' });
|
||||
}
|
||||
}
|
||||
|
||||
// The section being read is marked in the list - the last heading above the top quarter of the window
|
||||
const links = {};
|
||||
list.querySelectorAll('a').forEach(function (a) {
|
||||
links[a.getAttribute('href').slice(1)] = a;
|
||||
});
|
||||
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) {
|
||||
@@ -78,6 +37,9 @@
|
||||
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);
|
||||
});
|
||||
@@ -88,9 +50,90 @@
|
||||
window.requestAnimationFrame(mark);
|
||||
}
|
||||
};
|
||||
window.addEventListener('scroll', queue, { passive: true });
|
||||
window.addEventListener('resize', queue);
|
||||
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 () {
|
||||
|
||||
@@ -277,6 +277,10 @@
|
||||
if (typeof prettyContent === 'function') {
|
||||
prettyContent();
|
||||
}
|
||||
// A long one's Contents beside it (js/document_contents.js)
|
||||
if (typeof window.itflowDocumentContents === 'function') {
|
||||
window.itflowDocumentContents();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user