P.E. 101 — Week A1
Foundations & Fitness Baseline
Read 0%
`; printWindow.document.write(html); printWindow.document.close(); printWindow.focus(); setTimeout(()=> printWindow.print(), 350); }; /* open quiz (this will attempt to call window.postMessage for portal; if embedded, you can hook) */ document.getElementById('openQuiz').onclick = ()=>{ // post a message for host page (if used as embed) to open the quiz const msg = { action:'openQuiz', chapter: currentChapter }; try{ window.parent.postMessage(msg, '*'); }catch(e){} alert('If the portal is embedded this will signal to open the quiz for ' + currentChapter + '. Otherwise open the quiz in the portal manually.'); }; /* ========== progress tracking (simple read percent via scroll) ========== */ function updateProgressUI(){ const p = state.progress[currentChapter] || {}; const readPct = p.readPct || 0; progressFill.style.width = (readPct) + '%'; progressLabel.textContent = (readPct ? readPct + '%' : '0%') + (p.markRead ? ' — marked read' : ' — not read'); readPercent.textContent = 'Read ' + (readPct || 0) + '%'; document.getElementById('toggleMark').textContent = (p.markRead ? 'Unmark Read' : 'Mark Read'); } /* mark read toggle */ document.getElementById('toggleMark').onclick = ()=>{ state.progress[currentChapter] = state.progress[currentChapter] || {}; state.progress[currentChapter].markRead = !state.progress[currentChapter].markRead; // if marking read, set readPct to 100 if(state.progress[currentChapter].markRead) state.progress[currentChapter].readPct = 100; saveState(); updateProgressUI(); }; /* capture scroll-based progress inside chapterBody */ chapterBody.addEventListener('scroll', ()=>{ // not needed because chapterBody is not scrollable itself; we can compute progress based on viewport on main }); /* compute read percentage by percentage of chapter scrolled within readerMain */ document.getElementById('readerMain').addEventListener('scroll', ()=>{ const el = document.getElementById('readerMain'); const content = chapterBody; // compute relative progress of scroll const total = content.scrollHeight - content.clientHeight; let pct = 0; if(total > 0) pct = Math.round((content.scrollTop / total) * 100); // fallback: if user marks read, use 100 state.progress[currentChapter] = state.progress[currentChapter] || {}; if(state.progress[currentChapter].markRead) state.progress[currentChapter].readPct = 100; else state.progress[currentChapter].readPct = pct; saveState(); updateProgressUI(); }); /* also compute progress when opening chapter (estimate using document height) */ function computeReadPctOnOpen(){ // small heuristic: count paragraphs read? For now set to 0 until they scroll; if they marked read we set 100 const p = state.progress[currentChapter] || {}; if(p.markRead) p.readPct = 100; else p.readPct = p.readPct || 0; state.progress[currentChapter] = p; saveState(); updateProgressUI(); } /* ========== local search within chapter ========== */ localSearch.addEventListener('input', (e)=>{ highlightLocalSearch(e.target.value); }); function highlightLocalSearch(term){ const content = TEXT[currentChapter].body; if(!term) { // render original chapterBody.innerHTML = content; return; } const re = new RegExp('(' + escapeRegExp(term) + ')', 'ig'); const html = content.replace(re, '$1'); chapterBody.innerHTML = html; } function escapeRegExp(s){ return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } /* ========== search TOC filtering ========== */ searchInput.addEventListener('input', (e)=>{ renderTOC(e.target.value || ''); }); /* ========== read-aloud (TTS) ========== */ document.getElementById('playTTS').onclick = ()=>{ const text = chapterBody.innerText || ''; if(!text) return alert('No text to read.'); if(window.speechSynthesis.speaking){ window.speechSynthesis.cancel(); } const utter = new SpeechSynthesisUtterance(text); utter.rate = 1; utter.pitch = 1; utter.onend = ()=> { console.log('TTS ended'); }; window.speechSynthesis.speak(utter); ttsUtter = utter; }; document.getElementById('stopTTS').onclick = ()=>{ if(window.speechSynthesis.speaking) window.speechSynthesis.cancel(); }; /* ========== export / import notes & state ========== */ document.getElementById('exportNotes').onclick = ()=>{ const payload = { exportedAt: new Date().toISOString(), state: state }; const blob = new Blob([JSON.stringify(payload, null, 2)], {type:'application/json'}); const a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = 'plera_pe_textbook_export.json'; a.click(); }; document.getElementById('importNotes').onclick = ()=> document.getElementById('importFile').click(); document.getElementById('importFile').addEventListener('change', async (e)=>{ const f = e.target.files[0]; if(!f) return; try{ const txt = await f.text(); const j = JSON.parse(txt); if(j && j.state){ state = j.state; saveState(); openChapter(currentChapter); renderTOC(); alert('Imported state.'); } else alert('Invalid import file.'); }catch(err){ alert('Import failed: ' + err); } }); /* download page HTML snapshot */ document.getElementById('downloadHTML').onclick = ()=>{ const html = '\n' + document.documentElement.outerHTML; const blob = new Blob([html], {type:'text/html'}); const a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = 'plera_pe_textbook_snapshot.html'; a.click(); }; /* export entire state */ document.getElementById('exportState').onclick = ()=>{ const payload = { exportedAt: new Date().toISOString(), state: state }; const blob = new Blob([JSON.stringify(payload, null, 2)], {type:'application/json'}); const a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = 'plera_pe_textbook_state.json'; a.click(); }; /* ========== quick init ========== */ function init(){ loadState(); // ensure default chapter exists if(!TEXT[currentChapter]) currentChapter = Object.keys(TEXT)[0]; renderTOC(); openChapter(currentChapter); updateTextStyles(); computeReadPctOnOpen(); } init(); /* ======== small utility to allow host to set progress (optional) ======== */ window.addEventListener('message', (ev)=>{ const data = ev.data; if(data && data.action === 'setProgress'){ // {chapter:'A1', progress:{score:85, ...}} state.progress[data.chapter] = Object.assign(state.progress[data.chapter] || {}, data.progress || {}); saveState(); renderTOC(); updateProgressUI(); } });