';
});
}
function card(item) {
var meta = [];
if (item.type === 'video') {
meta.push('Tutorial video');
if (item.module) { meta.push(item.module); }
} else {
if (item.chapter) { meta.push(item.chapter.replace(/^Chapter\s*\d+\s*-\s*/i, '')); }
if (item.category) { meta.push(item.category); }
}
var other = item.other_module ? '· other module' : '';
var acts = '';
if (item.type === 'video') {
acts = '';
} else {
var report = item.module === 'reports' && item.page && !pageMatches(item.page); // a report found from another page: open it straight away
acts = (report ? '📊 Open report ↗' : '')
+ ''
+ (item.video ? '' : '');
}
return '
' + esc(item.title) + '
'
+ (item.blurb ? '
' + esc(item.blurb) + '
' : '')
+ (meta.length ? '
' + esc(meta.join(' · ')) + ' ' + other + '
' : '')
+ '
' + acts + '
';
}
function search(force) {
var q = document.getElementById('sa-q').value.trim();
if (!q) { state.lastQuery = ''; showHome(); return; }
if (!force && q.length < 3) { return; }
if (q === state.lastQuery && !force) { return; }
state.lastQuery = q; state.view = 'search';
if (state.ctrl) { state.ctrl.abort(); }
state.ctrl = window.AbortController ? new AbortController() : null;
var b = body();
b.innerHTML = '
Searching…
';
api('search', { q: q, page: CFG.pageWithQuery }, { signal: state.ctrl && state.ctrl.signal }).then(function (r) {
if (q !== state.lastQuery) { return; }
if (!r.success) { b.innerHTML = '
' + esc(r.msg || 'Search failed.') + '
'; return; }
state.logId = r.data.log_id || 0;
var html = '';
if (r.data.results.length) {
var top = r.data.results[0];
if (r.data.intent === 'define' && top && top.summary) {
html += '
' + textToHtml(top.summary) + '
From the guide ' + esc(top.title) + ' - open Read steps below for the how-to.
I could not find a lesson for "' + esc(q) + '".';
if (r.data.suggestions.length) {
html += '
Nearest topics: ' + r.data.suggestions.map(function (s) { return '' + esc(s.title) + ''; }).join('');
}
html += '
';
html += '
Report a gap?
';
}
b.innerHTML = html;
}).catch(function (e) {
if (e && e.name === 'AbortError') { return; }
b.innerHTML = '
The assistant is unreachable right now. Please try again.
';
});
}
/* ---------------------------------------------------------- read steps */
function toggleSteps(btn, startStep, preloaded) {
var cardEl = btn.closest('.sa-card');
var detail = cardEl.querySelector('.sa-detail');
if (!detail) { detail = document.createElement('div'); detail.className = 'sa-detail'; cardEl.appendChild(detail); } // admin Lessons cards have no detail box until first use
if (detail.innerHTML) { detail.innerHTML = ''; detail.__lesson = null; btn.textContent = 'Read steps'; clearFollow(); return; }
btn.disabled = true;
var qid = btn.getAttribute('data-id'), slug = btn.getAttribute('data-slug') || '';
var req = preloaded ? Promise.resolve(preloaded) : api('lesson', slug ? { slug: slug, page: CFG.pageWithQuery } : { id: qid, page: CFG.pageWithQuery });
req.then(function (r) {
btn.disabled = false;
if (!r.success) { detail.innerHTML = '
' + esc(r.msg) + '
'; return; }
var d = r.data, script = d.script;
var steps = (script && script.steps && script.steps.length) ? script.steps : (d.steps || []).map(function (s) { return { caption: s.text, shot_url: null, page: '' }; });
detail.__script = script;
detail.__lesson = { qid: qid, slug: slug, title: d.title, answer: d.answer_text, steps: steps };
var cur = Math.max(0, Math.min(steps.length - 1, typeof startStep === 'number' ? startStep : 0));
renderSteps(detail, cur, cur > 0);
btn.textContent = 'Hide steps';
if (cur === 0) { scrollCardToTop(cardEl); } // the opened lesson rises to the top of the panel, out of the long list
if (steps.length) { saveFollow({ slug: slug, qid: qid, title: d.title, step: cur, page: CFG.pageWithQuery }); }
}).catch(function () { btn.disabled = false; detail.innerHTML = '
Could not load the steps.
'; });
}
/* scroll the panel body so the card starts at its top edge (smooth when the browser supports it) */
function scrollCardToTop(cardEl) {
var b = body(); if (!b || !cardEl) { return; }
setTimeout(function () {
var top = cardEl.getBoundingClientRect().top - b.getBoundingClientRect().top + b.scrollTop - 6;
try { b.scrollTo({ top: Math.max(0, top), behavior: 'smooth' }); } catch (e) { b.scrollTop = Math.max(0, top); }
}, 30);
}
/* The step list: the current step is marked "You are here"; click a step to move the marker.
Steps that happen on another page get an "open page" link; the guide re-opens there. */
function renderSteps(detail, cur, scrollTo) {
var L = detail.__lesson, steps = L.steps, qid = L.qid, slug = L.slug, html = '';
detail.__cur = cur;
var pics = steps.filter(function (s) { return s.shot_url; }).length;
if (steps.length) {
html += '
'; }
if (steps.length) {
html += '' + steps.map(function (s, i) {
var cls = i === cur ? 'sa-cur' : (i < cur ? 'sa-done' : '');
var goto = s.page && !pageMatches(s.page) ? ' open page ↗' : '';
return '
' + (i === cur ? 'You are here' : '') + textToHtml(s.caption) + goto
+ (s.shot_url ? '' : '') + '
';
}).join('') + '';
if (pics) { html += ''; }
else if (CFG.canEdit) { html += ''; }
} else if (!L.answer) {
html += '
This lesson has no written steps yet.
';
}
detail.innerHTML = html;
if (scrollTo) { var li = detail.querySelector('li.sa-cur'); if (li && li.scrollIntoView) { setTimeout(function () { li.scrollIntoView({ block: 'center' }); }, 30); } }
if (steps[cur]) { setTimeout(function () { showTarget(steps[cur]); }, 80); } // keep the panel off the control this step is about
}
/* The control a step talks about: outline it for a moment and, when the panel covers it,
slide the panel to a free side of the window (the dragged position is not saved over). */
function showTarget(step) {
var g = guide(); if (!g || !g.resolve || !step || !step.target) { return; }
var r; try { r = g.resolve({ within: step.within, target: step.target }); } catch (e) { return; }
if (!r || !r.el || (r.status !== 'ok' && r.status !== 'ambiguous')) { return; }
var el = r.el, panel = document.getElementById('sa-panel');
var t = g.rectOf ? g.rectOf(el) : el.getBoundingClientRect(); // framed controls: add the iframe offset
if (t.width === 0 && t.height === 0) { return; }
if (panel.classList.contains('open')) {
var p = panel.getBoundingClientRect(), w = p.width, h = p.height, vw = window.innerWidth, vh = window.innerHeight;
var overlaps = function (a, b) { return a.left < b.right && a.right > b.left && a.top < b.bottom && a.bottom > b.top; };
if (overlaps(t, p)) {
var spots = [{ left: 22, top: p.top }, { left: vw - w - 22, top: p.top }, { left: p.left, top: vh - h - 20 }, { left: p.left, top: 10 }, { left: 22, top: vh - h - 20 }, { left: 22, top: 10 }, { left: vw - w - 22, top: 10 }];
for (var i = 0; i < spots.length; i++) {
var c = spots[i];
c.left = Math.max(0, Math.min(vw - w, c.left)); c.top = Math.max(0, Math.min(vh - h, c.top));
if (!overlaps(t, { left: c.left, top: c.top, right: c.left + w, bottom: c.top + h })) {
panel.style.left = c.left + 'px'; panel.style.top = c.top + 'px'; panel.style.right = 'auto'; panel.style.bottom = 'auto';
break;
}
}
}
}
if (g.highlight && g.clearHighlight) {
try { g.highlight(el, true, true); setTimeout(function () { try { g.clearHighlight(); } catch (e) {} }, 2500); } catch (e) {}
}
}
function moveStep(detail, cur) {
var L = detail.__lesson;
if (!L || !L.steps.length) { return; }
cur = Math.max(0, Math.min(L.steps.length - 1, cur));
renderSteps(detail, cur, true);
saveFollow({ slug: L.slug, qid: L.qid, title: L.title, step: cur, page: CFG.pageWithQuery });
}
/* On page load: if a guide was being followed, re-open the panel on it. When the page changed,
and the current step is not on this page, the marker moves to the next step that belongs here. */
function resumeFollow() {
var f = loadFollow();
if (!f || !(f.slug || f.qid)) { return; }
if (Date.now() - (f.at || 0) > FOLLOW_TTL) { clearFollow(); return; }
api('lesson', f.slug ? { slug: f.slug, page: CFG.pageWithQuery } : { id: f.qid, page: CFG.pageWithQuery }).then(function (r) {
if (!r.success) { clearFollow(); return; }
var d = r.data, script = d.script;
var steps = (script && script.steps && script.steps.length) ? script.steps : (d.steps || []);
var cur = f.step || 0;
if (steps[cur] && steps[cur].page && !pageMatches(steps[cur].page)) { // the current step is on another page: move to the next step that belongs here
for (var i = cur + 1; i < steps.length; i++) { if (steps[i].page && pageMatches(steps[i].page)) { cur = i; break; } }
}
state.view = 'follow'; state.lastQuery = '';
if (!state.open) { state.open = true; document.getElementById('sa-panel').classList.add('open'); } // open directly: toggle() is the bubble path and shows this page's lessons
var b = body();
b.innerHTML = '
Continuing your guide
' + card({ question_id: f.qid || 0, slug: f.slug || '', title: d.title, chapter: d.chapter, category: d.category, video: null });
var btn = b.querySelector('[data-act="steps"]');
if (btn) { toggleSteps(btn, cur, r); }
}).catch(function () {});
}
/* walkthroughs were retired: Read steps, slideshow and print are the user-facing features */
/* ------------------------------------------------------ video / slides */
function openVideo(file, title) {
var wrap = document.getElementById('sa-video');
var v = wrap.querySelector('video');
document.getElementById('sa-slides').style.display = 'none'; v.style.display = 'block';
state.slides = null;
wrap.querySelector('.sa-vt').textContent = title || '';
v.src = CFG.base + 'videos/stream.php?file=' + encodeURIComponent(file);
wrap.classList.add('open');
if (v.play) { var p = v.play(); if (p && p.catch) { p.catch(function () {}); } }
}
function openSlides(title, steps, startAt, ref) {
var wrap = document.getElementById('sa-video');
var v = wrap.querySelector('video');
v.pause(); v.removeAttribute('src'); v.load(); v.style.display = 'none';
wrap.querySelector('.sa-vt').textContent = title || '';
var frames = steps.map(function (s, i) { return { src: CFG.base + s.shot_url, caption: (i + 1) + '. ' + (s.caption || ''), ok: !!s.shot_url }; }).filter(function (f) { return f.ok; });
if (!frames.length) { return; }
state.slides = { frames: frames, i: Math.max(0, Math.min(frames.length - 1, startAt || 0)), timer: null, playing: true, ref: ref || null, title: title || '' };
document.getElementById('sa-slides').style.display = 'flex';
document.getElementById('sa-slides').classList.add('sa-big'); // opens zoomed out (almost full screen) by default; Zoom in returns to the normal size
wrap.classList.add('open');
renderSlide();
slideTimer();
}
function renderSlide() {
var s = state.slides; if (!s) { return; }
var box = document.getElementById('sa-slides');
box.querySelector('img').src = s.frames[s.i].src;
box.querySelector('.sa-cap').textContent = s.frames[s.i].caption;
box.querySelector('.sa-pos').textContent = (s.i + 1) + ' / ' + s.frames.length;
box.querySelector('[data-sl="play"]').innerHTML = s.playing ? '▮▮ Pause' : '▶ Play';
var last = s.i === s.frames.length - 1, nl = adjacentLesson(s.ref, 1), pl = adjacentLesson(s.ref, -1);
box.querySelector('[data-sl="next"]').innerHTML = last ? 'Close' : 'Next ›'; // the last picture closes the show
var nb = box.querySelector('[data-sl="nextlesson"]'), pb = box.querySelector('[data-sl="prevlesson"]');
nb.style.visibility = (last && nl) ? 'visible' : 'hidden'; nb.title = nl ? 'Open the next lesson: ' + nl.title : ''; // only on the last picture
pb.style.visibility = (s.i === 0 && pl) ? 'visible' : 'hidden'; pb.title = pl ? 'Open the previous lesson: ' + pl.title : ''; // only on the first picture
box.querySelector('[data-sl="size"]').innerHTML = box.classList.contains('sa-big') ? '⤡ Zoom in' : '⤢ Zoom out'; // wording chosen by the client: Zoom out = enlarge, Zoom in = back to normal
}
function slideTimer() {
var s = state.slides; if (!s) { return; }
clearTimeout(s.timer);
if (s.playing) { s.timer = setTimeout(function () { if (!state.slides) { return; } if (s.i < s.frames.length - 1) { s.i++; renderSlide(); slideTimer(); } else { s.playing = false; renderSlide(); } }, 3500); }
}
function slideCtl(what) {
var s = state.slides; if (!s) { return; }
if (what === 'next') { if (s.i >= s.frames.length - 1) { closeVideo(); return; } s.i++; }
else if (what === 'nextlesson' || what === 'prevlesson') { var dir = what === 'nextlesson' ? 1 : -1, al = adjacentLesson(s.ref, dir); if (al) { openLesson(al, dir); } return; }
else if (what === 'prev') { s.i = Math.max(0, s.i - 1); }
else if (what === 'play') { s.playing = !s.playing; if (s.playing && s.i === s.frames.length - 1) { s.i = 0; } }
else if (what === 'size') { var boxS = document.getElementById('sa-slides'); boxS.classList.toggle('sa-big', !boxS.classList.contains('sa-big')); } // almost full screen for pictures of the whole page
renderSlide(); slideTimer();
}
/* the lesson before (dir -1) or after (dir 1) `ref` in this page's "On this page" order; null at either end or when the lesson is not on this page */
function adjacentLesson(ref, dir) {
var list = state.pageLessons || []; if (!ref || !list.length) { return null; }
for (var i = 0; i < list.length; i++) {
if ((ref.slug && list[i].slug === ref.slug) || (!ref.slug && ref.qid && +list[i].qid === +ref.qid)) { return list[i + dir] || null; }
}
return null;
}
/* open a lesson's pictures at picture 1, paused (Previous / Next Lesson); lessons without pictures are skipped in the same direction */
function openLesson(item, dir) {
api('lesson', item.slug ? { slug: item.slug, page: CFG.pageWithQuery } : { id: item.qid, page: CFG.pageWithQuery }).then(function (r) {
var script = r.success && r.data ? r.data.script : null;
var pics = script && script.steps && script.steps.some(function (st) { return st.shot_url; });
if (pics) {
openSlides(r.data.title || item.title, script.steps, 0, { slug: item.slug, qid: item.qid });
if (state.slides) { state.slides.playing = false; clearTimeout(state.slides.timer); renderSlide(); }
return;
}
var beyond = adjacentLesson({ slug: item.slug, qid: item.qid }, dir); if (beyond) { openLesson(beyond, dir); }
}).catch(function () {});
}
function closeVideo() {
var wrap = document.getElementById('sa-video');
var v = wrap.querySelector('video');
v.pause(); v.removeAttribute('src'); v.load();
if (state.slides) { clearTimeout(state.slides.timer); state.slides = null; }
wrap.classList.remove('open');
}
function feedback(v, holder) {
holder.innerHTML = 'Thanks for the feedback.';
if (!state.logId) { return; }
api('feedback', { log_id: state.logId, helpful: v, page: CFG.pageWithQuery }, { post: true }).catch(function () {});
}
/* ------------------------------------------------------- admin: lessons */
function pill(l) {
if (l.state === 'published') { return 'v' + l.version + (l.health === 'stale' ? ' · needs review' : (l.health === 'ok' ? ' · OK' : ' · unchecked')) + ''; }
if (l.state === 'legacy') { return 'old steps'; }
return 'no steps';
}
function showLessons() {
state.view = 'lessons'; state.lastQuery = '';
var b = body();
b.innerHTML = '
'; return; }
var x = r.data, open = x.open;
var hist = (x.history || []).filter(function (h) { return !open || h.request_key !== open.request_key; });
var html = '
⚙ Auto Record again
'
+ '
Write what should change in this guide, for example which record or month to show, a step to add or drop, or wording to use. Leave it empty to record the guide again as it is on the current screens. After Proceed, the guide is recorded again by Claude Code, with new pictures, slideshow and PDF, and published as the next version.
'
+ '
Guide script: ' + (x.lesson_file ? 'pms/support-agent/' + esc(x.lesson_file) + '' : 'no lesson file found (it can still be queued; Claude Code will create one)') + '
'
+ '
Instructions are kept in pms/support-agent/' + esc(x.prompt_file) + ' so the same guide can be regenerated on any computer.
';
var ta = d.querySelector('.sa-rr-text'); if (ta) { ta.focus(); }
});
}
function submitRerecord(btn) {
var box = btn.closest('.sa-rr'), slug = box.getAttribute('data-slug');
var text = (box.querySelector('.sa-rr-text') || {}).value || '';
btn.disabled = true;
api('rerecord_request', { slug: slug, prompts: text }, { post: true }).then(function (r) {
btn.disabled = false;
if (!r.success) { alert(r.msg || 'Could not queue the request.'); return; }
var req = r.data.request;
box.innerHTML = '
⚙ Auto Record again: request saved
'
+ '
Reference ' + esc(req.request_key) + '. The instructions were saved' + (r.data.prompt_file_written ? ' in pms/support-agent/' + esc(r.data.prompt_file) + ' and' : '') + ' in the recording queue.
'
+ '
To record it now: open Claude Code in the karibu-pms folder (this computer or any computer with this project) and say Process the Support Assistant re-record requests. The guide is recorded again with your instructions and published as the next version; its card here then shows recorded again.
'
+ '';
var t = box.closest('.sa-card').querySelector('.sa-t');
if (t && !t.querySelector('.sa-pill.rr')) { t.insertAdjacentHTML('beforeend', rrPill({ key: req.request_key, status: 'pending', requested_at: req.requested_at })); }
});
}
/* ------------------------------------------------- admin: review draft */
function reviewLesson(lessonId) {
if (!state.open) { toggle(); }
state.view = 'review'; state.lastQuery = '';
var b = body();
b.innerHTML = '
Loading draft…
';
Promise.all([api('lesson_draft', { lesson_id: lessonId, page: CFG.pageWithQuery }), state.admin ? Promise.resolve({ success: true, data: state.admin }) : api('lessons_admin', { page: CFG.pageWithQuery })]).then(function (rs) {
var r = rs[0], a = rs[1];
if (!r.success) { b.innerHTML = '
' + esc(r.msg) + '
'; return; }
state.admin = a.success ? a.data : state.admin;
var L = r.data;
var qOpts = '' + ((state.admin && state.admin.lessons) || []).filter(function (l) { return l.question_id > 0; }).map(function (l) { return ''; }).join('');
var cOpts = ((state.admin && state.admin.categories) || []).map(function (c) { return ''; }).join('');
var roleList = (state.admin && state.admin.roles) || [];
var current = (L.audience && L.audience.roles) || [];
var rOpts = roleList.map(function (r) { return ''; }).join('');
var legacyAttach = (((state.admin && state.admin.categories) || []).length && qOpts.indexOf('