Learn: remember expanded sections, open only in the last chapter by default
This commit is contained in:
@@ -35,12 +35,30 @@
|
||||
function closeModal(id){ var d=document.getElementById(id); if(d) d.close(); }
|
||||
function openModalData(btn){ openModal(btn.dataset.modal); }
|
||||
function closeModalData(btn){ closeModal(btn.dataset.modal); }
|
||||
function sectionStateKey(id){ return 'lms-section-open:' + id; }
|
||||
function applySectionState(acc){
|
||||
var id = acc.getAttribute('data-section-id');
|
||||
var stored = null;
|
||||
try { stored = id ? localStorage.getItem(sectionStateKey(id)) : null; } catch (e) { /* приватный режим */ }
|
||||
var open = stored === '1' ? true
|
||||
: stored === '0' ? false
|
||||
: acc.getAttribute('data-default-open') === 'true';
|
||||
acc.classList.toggle('collapsed', !open);
|
||||
var btn = acc.querySelector('.sub-toggle');
|
||||
if (btn) btn.setAttribute('aria-expanded', open ? 'true' : 'false');
|
||||
}
|
||||
function initSections(){
|
||||
document.querySelectorAll('.sub-acc[data-section-id]').forEach(applySectionState);
|
||||
}
|
||||
function toggleSection(btn){
|
||||
var acc = btn.closest('.sub-acc');
|
||||
if (!acc) return;
|
||||
var collapsed = acc.classList.toggle('collapsed');
|
||||
btn.setAttribute('aria-expanded', collapsed ? 'false' : 'true');
|
||||
var id = acc.getAttribute('data-section-id');
|
||||
if (id) { try { localStorage.setItem(sectionStateKey(id), collapsed ? '0' : '1'); } catch (e) { /* приватный режим */ } }
|
||||
}
|
||||
document.addEventListener('DOMContentLoaded', initSections);
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
Reference in New Issue
Block a user