|
| 1 | +// Groups the hardware SDRs chapters into a collapsible section in the left sidebar |
| 2 | +// Expanded by default; the reader can expand/collapse in place without navigating away |
| 3 | +// State is remembered across pages via localStorage. |
| 4 | +(function () { |
| 5 | + var GROUP_LABEL = 'Specific SDR Hardware'; |
| 6 | + var FILES = ['pluto.html', 'usrp.html', 'bladerf.html', 'rtlsdr.html', 'hackrf.html']; |
| 7 | + |
| 8 | + function fileName(url) { |
| 9 | + return (url || '').split('#')[0].split('?')[0].split('/').pop(); |
| 10 | + } |
| 11 | + |
| 12 | + function init() { |
| 13 | + var sidebar = document.querySelector('.sphinxsidebarwrapper') || document.body; |
| 14 | + |
| 15 | + // Collect the SDR chapter <li> items, keeping the order in FILES. |
| 16 | + var byFile = {}; |
| 17 | + sidebar.querySelectorAll('li.toctree-l1 > a[href]').forEach(function (a) { |
| 18 | + var f = fileName(a.getAttribute('href')); |
| 19 | + if (FILES.indexOf(f) !== -1) byFile[f] = a.parentElement; |
| 20 | + }); |
| 21 | + |
| 22 | + // The chapter you're currently on is rendered as the "current" item with an |
| 23 | + // href of "#" (plus a nested list of its sections), so it needs its own |
| 24 | + // lookup rather than matching by filename. |
| 25 | + var currentFile = fileName(window.location.pathname); |
| 26 | + if (FILES.indexOf(currentFile) !== -1) { |
| 27 | + var currentLi = sidebar.querySelector('li.toctree-l1.current'); |
| 28 | + if (currentLi) byFile[currentFile] = currentLi; |
| 29 | + } |
| 30 | + |
| 31 | + var lis = []; |
| 32 | + FILES.forEach(function (f) { if (byFile[f]) lis.push(byFile[f]); }); |
| 33 | + if (!lis.length) return; |
| 34 | + |
| 35 | + var firstLi = lis[0]; |
| 36 | + var parentUl = firstLi.parentElement; |
| 37 | + |
| 38 | + // Build the collapsible group header and its (initially empty) child list. |
| 39 | + var groupLi = document.createElement('li'); |
| 40 | + groupLi.className = 'toctree-l1 sdr-group'; |
| 41 | + |
| 42 | + var toggle = document.createElement('a'); |
| 43 | + toggle.href = '#'; |
| 44 | + toggle.className = 'sdr-group-toggle'; |
| 45 | + toggle.setAttribute('role', 'button'); |
| 46 | + toggle.innerHTML = '<span class="sdr-caret"></span>' + GROUP_LABEL; |
| 47 | + |
| 48 | + var childUl = document.createElement('ul'); |
| 49 | + childUl.className = 'sdr-group-children'; |
| 50 | + |
| 51 | + groupLi.appendChild(toggle); |
| 52 | + groupLi.appendChild(childUl); |
| 53 | + parentUl.insertBefore(groupLi, firstLi); |
| 54 | + |
| 55 | + // Keep them as toctree-l1 so they retain the full-size chapter text; |
| 56 | + // indentation under the group is handled by .sdr-group-children CSS. |
| 57 | + lis.forEach(function (li) { |
| 58 | + childUl.appendChild(li); |
| 59 | + }); |
| 60 | + |
| 61 | + function setOpen(open) { |
| 62 | + groupLi.classList.toggle('open', open); |
| 63 | + toggle.setAttribute('aria-expanded', open ? 'true' : 'false'); |
| 64 | + } |
| 65 | + |
| 66 | + // Expanded by default. Stay open unless the reader explicitly collapsed it |
| 67 | + // last time (stored === '0'); always open when on one of these pages. |
| 68 | + var onSdrPage = FILES.indexOf(currentFile) !== -1; |
| 69 | + var stored = null; |
| 70 | + try { stored = window.localStorage.getItem('sdrGroupOpen'); } catch (e) {} |
| 71 | + setOpen(onSdrPage || stored !== '0'); |
| 72 | + |
| 73 | + toggle.addEventListener('click', function (e) { |
| 74 | + e.preventDefault(); |
| 75 | + var open = !groupLi.classList.contains('open'); |
| 76 | + setOpen(open); |
| 77 | + try { window.localStorage.setItem('sdrGroupOpen', open ? '1' : '0'); } catch (e2) {} |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + if (document.readyState === 'loading') { |
| 82 | + document.addEventListener('DOMContentLoaded', init); |
| 83 | + } else { |
| 84 | + init(); |
| 85 | + } |
| 86 | +})(); |
0 commit comments