|
| 1 | +const supportedTools = ['cli', 'desktop', 'webui'] |
| 2 | +const detectedTools = new Set() |
| 3 | + |
| 4 | +export default function displayToolSpecificContent () { |
| 5 | + let tool = getDefaultTool() |
| 6 | + |
| 7 | + if (!tool) tool = 'webui' |
| 8 | + |
| 9 | + const toolsInContent = findToolSpecificContent(tool) |
| 10 | + |
| 11 | + hideSwitcherLinks(toolsInContent) |
| 12 | + |
| 13 | + showContentForTool(tool) |
| 14 | + |
| 15 | + // configure links for switching tool content |
| 16 | + switcherLinks().forEach(link => { |
| 17 | + link.addEventListener('click', (event) => { |
| 18 | + event.preventDefault() |
| 19 | + showContentForTool(event.target.dataset.tool) |
| 20 | + findToolSpecificContent(event.target.dataset.tool) |
| 21 | + }) |
| 22 | + }) |
| 23 | +} |
| 24 | + |
| 25 | +function showContentForTool (tool) { |
| 26 | + // (de)activate switcher link appearances |
| 27 | + switcherLinks().forEach(link => { |
| 28 | + (link.dataset.tool === tool) |
| 29 | + ? link.classList.add('selected') |
| 30 | + : link.classList.remove('selected') |
| 31 | + }) |
| 32 | +} |
| 33 | + |
| 34 | +function findToolSpecificContent (tool) { |
| 35 | + // find all tool-specific *block* elements and hide or show as appropriate |
| 36 | + // example: {{ #cli }} block content {{/cli}} |
| 37 | + Array.from(document.querySelectorAll('.extended-markdown')) |
| 38 | + .filter(el => supportedTools.some(tool => el.classList.contains(tool))) |
| 39 | + .forEach(el => { |
| 40 | + detectTools(el) |
| 41 | + el.style.display = el.classList.contains(tool) |
| 42 | + ? '' |
| 43 | + : 'none' |
| 44 | + }) |
| 45 | + |
| 46 | + // find all tool-specific *inline* elements and hide or show as appropriate |
| 47 | + // example: <span class="tool-cli">inline content</span> |
| 48 | + Array.from(document.querySelectorAll('.tool-cli, .tool-desktop, .tool-webui')) |
| 49 | + .forEach(el => { |
| 50 | + detectTools(el) |
| 51 | + el.style.display = el.classList.contains('tool-' + tool) |
| 52 | + ? '' |
| 53 | + : 'none' |
| 54 | + }) |
| 55 | + |
| 56 | + return Array.from(detectedTools) |
| 57 | +} |
| 58 | + |
| 59 | +// hide links for any tool-specific sections that are not present |
| 60 | +function hideSwitcherLinks (toolsInContent) { |
| 61 | + Array.from(document.querySelectorAll('a.tool-switcher')) |
| 62 | + .forEach(link => { |
| 63 | + if (toolsInContent.includes(link.dataset.tool)) return |
| 64 | + link.style.display = 'none' |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +function detectTools (el) { |
| 69 | + el.classList.forEach(elClass => { |
| 70 | + const value = elClass.replace(/tool-/, '') |
| 71 | + if (supportedTools.includes(value)) detectedTools.add(value) |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +function getDefaultTool () { |
| 76 | + const el = document.querySelector('[data-default-tool]') |
| 77 | + if (el) return el.dataset.defaultTool |
| 78 | +} |
| 79 | + |
| 80 | +function switcherLinks () { |
| 81 | + return Array.from(document.querySelectorAll('a.tool-switcher')) |
| 82 | +} |
0 commit comments