Debian Codegraph @

inst/resources/snap.js

raw symbols SCIP
1 (function(d) {
2 let p = d.body; // container of slides; assume <body> for now
3 const s1 = ':scope > hr:not([class])', s2 = ':scope > h2';
4 // find a container that has at least n "slides"
5 function findContainer(s, n = 1) {
6 if (p.querySelectorAll(s).length >= n) return true;
7 // if body doesn't contain headings or <hr>s, look into children
8 for (let i = 0; i < p.children.length; i++) {
9 if (p.children[i].querySelectorAll(s).length >= n) {
10 p = p.children[i]; break;
11 }
12 }
13 return false;
14 }
15 function newEl(tag, cls) {
16 const el = d.createElement(tag);
17 if (cls) el.className = cls;
18 return el;
19 }
20 if (!findContainer(s1, 3)) {
21 // if not enough <hr>s found in children; look for <h2> instead
22 if (p.tagName === 'BODY') {
23 // not enough h2 found, this page is not appropriate for slides
24 if (!findContainer(s2) && p.tagName === 'BODY') return;
25 p.querySelectorAll(s2).forEach(h2 => h2.before(newEl('hr')));
26 }
27 }
28 p.classList.add('slide-container');
29 // add 'slide' class to the frontmatter/abstract divs and toc
30 ['.frontmatter', '.abstract', '#TOC'].forEach(sel => {
31 const el = d.body.querySelector(sel);
32 if (!el) return;
33 if (sel === '.frontmatter') {
34 el.classList.add('slide');
35 } else {
36 const s = newSlide(); el.before(s); s.append(el);
37 }
38 });
39
40 function newSlide(s) {
41 return (s?.innerHTML === '') ? s : newEl('div', 'slide');
42 }
43 function isSep(el) {
44 return el.tagName === 'HR' && el.attributes.length === 0;
45 }
46 let el = p.firstElementChild; isSep(el) && el.remove();
47 el = p.firstElementChild; if (!el) return;
48 let s = newSlide(); el.before(s);
49 while (true) {
50 let el = s.nextSibling;
51 if (!el) break;
52 // remove slide separators (<hr>) and create new slide
53 if (isSep(el)) {
54 s = newSlide(s);
55 el.before(s); el.remove();
56 } else if (el.classList?.contains('slide')) {
57 s = newSlide(s);
58 el.after(s);
59 } else {
60 s.append(el);
61 }
62 }
63 function setAttr(el, attr) {
64 const m = newEl('div');
65 m.innerHTML = `<div ${attr}></div>`;
66 const attrs = m.firstElementChild.attributes;
67 for (const a of attrs) {
68 el.setAttribute(a.name, a.value);
69 }
70 m.remove();
71 }
72 function reveal(el) {
73 setTimeout(() => el?.scrollIntoView(), 100);
74 }
75 const dE = d.documentElement, dC = d.body.classList;
76 const slides = d.querySelectorAll('div.slide'), N = slides.length,
77 tm = d.querySelector('span.timer'), fn = d.querySelector('.footnotes');
78 slides.forEach((s, i) => {
79 // slide header, main body, and footer
80 const header = newEl('div', 'header'), main = newEl('div', 'main'), footer = newEl('div', 'footer');
81 main.append(...s.childNodes);
82 s.append(main);
83 s.insertAdjacentElement('afterbegin', header);
84 s.insertAdjacentElement('beforeend', footer);
85 // append footnotes
86 if (fn) s.querySelectorAll('.footnote-ref > a[href^="#fn"]').forEach(a => {
87 const li = fn.querySelector('li' + a.getAttribute('href'));
88 if (!li) return;
89 let f = s.querySelector('section.footnotes');
90 if (!f || f.contains(li)) {
91 f = newEl('section', 'footnotes'); footer.before(f);
92 }
93 f.append(li);
94 li.firstElementChild?.insertAdjacentHTML('afterbegin', `[${a.innerHTML}] `);
95 li.outerHTML = li.innerHTML;
96 });
97 // add a timer
98 footer.append(tm ? tm.cloneNode() : newEl('span', 'timer'));
99 // add page numbers
100 const n = newEl('span', 'page-number');
101 n.innerText = i + 1 + '/' + N;
102 n.onclick = e => location.hash = i + 1;
103 footer.append(n);
104 // apply slide attributes in <!--# -->
105 for (const node of main.childNodes) {
106 if (node.nodeType !== Node.COMMENT_NODE) continue;
107 let t = node.textContent;
108 if (!/^#/.test(t)) continue;
109 t = t.replace(/^#/, '');
110 const r = /[\s\n]class="([^"]+)"/, m = t.match(r);
111 if (m) {
112 t = t.replace(r, '').trim();
113 s.className += ' ' + m[1];
114 }
115 if (t) setAttr(s, t);
116 break;
117 }
118 s.addEventListener('click', e => {
119 (e.altKey || e.ctrlKey) && (toggleView(e), reveal(e.target));
120 });
121 });
122 [...d.querySelectorAll('a.footnote-backref'), fn, tm].forEach(el => el?.remove());
123 const tms = d.querySelectorAll('span.timer'), t1 = 1000 * tms[0].dataset.total;
124 let t0;
125 function startTimers() {
126 t0 = new Date();
127 setInterval(setTimers, 1000);
128 }
129 function setTimers() {
130 if (!dC.contains('slide-mode')) return; // set timer only in slide mode
131 let t = (new Date() - t0);
132 if (t1) t = t1 - t;
133 const t2 = new Date(Math.abs(t)).toISOString().substr(11, 8).replace(/^00:/, '');
134 tms.forEach(el => {
135 el.innerText = t2;
136 if (t < 0) el.style.opacity = Math.ceiling(t/1000) % 2;
137 });
138 }
139 function toggleView(e) {
140 (dC.toggle('overview') ? dC.remove('slide-mode') : setScale(e));
141 }
142 // press f for fullscreen mode
143 d.addEventListener('keyup', e => {
144 if (e.target !== d.body) return;
145 e.key === 'f' && dE.requestFullscreen();
146 e.key === 'o' && toggleView(e);
147 e.key === 'm' && dC.toggle('mirrored');
148 sessionStorage.setItem('body-class', d.body.className);
149 });
150 // start timer and set scale on fullscreen
151 d.onfullscreenchange = e => {
152 d.fullscreenElement && (!t0 && startTimers(), setScale(e));
153 }
154 tms.forEach(el => el.addEventListener('click', e => startTimers()));
155 // measure the height of an empty slide
156 let H = -1;
157 function slideHeight() {
158 if (H >= 0) return H;
159 const s = newEl('div', 'slide');
160 p.querySelector('.slide:last-of-type').after(s);
161 H = s.offsetHeight;
162 s.remove()
163 return H;
164 }
165 // scale slides according to window height (baseline: 900px)
166 const sty = newEl('style'); sty.setAttribute('type', 'text/css');
167 d.head.append(sty);
168 // default height/width ratio from screen size
169 sty.innerHTML = `:root{--slide-ratio:${screen.height / screen.width}}`;
170 // read --slide-ratio in case users have set it in their CSS
171 const ratio = +getComputedStyle(dE).getPropertyValue('--slide-ratio');
172 function setScale(e) {
173 // navigate to a slide indicated by the hash if provided
174 e.type === 'load' && reveal(slides[location.hash.replace(/^#/, '') - 1]);
175 if (dC.contains('overview')) return;
176 let h = window.innerHeight, p = h / 900, r2 = h / window.innerWidth, full = d.fullscreenElement;
177 // add slide mode if there's enough window width, and remove it in case of scrollbar
178 dC.toggle('slide-mode', full || r2 <= ratio) &&
179 (!full && (dE.scrollWidth > dE.offsetWidth)) && dC.remove('slide-mode');
180 sty.innerHTML = `:root{--slide-ratio:${ratio};--slide-scale:${p};--slide-top:${(p - 1)/2 * d.body.scrollHeight + 'px'};}`;
181 // add spacers with enough height on load
182 !d.querySelector('.spacer.fade') && dC.contains('slide-mode') && slides.forEach(s => {
183 const sp = newEl('div', 'spacer fade'), h = s.offsetHeight, h2 = slideHeight();
184 s.append(sp);
185 if (h <= h2) return;
186 sp.style.height = (h2 - h % h2) * p + 'px';
187 });
188 }
189 ['load', 'resize'].forEach(evt => window.addEventListener(evt, setScale));
190 // restore previously saved body class
191 const bc = sessionStorage.getItem('body-class');
192 if (bc) d.body.className += ' ' + bc;
193 })(document);