|
1 | | -// js/main.js |
2 | 1 | import { fetchCombinedData } from './api.js'; |
3 | 2 | import { populateBonesetDropdown, setupDropdownListeners } from './dropdowns.js'; |
4 | 3 | import { initializeSidebar } from './sidebar.js'; |
| 4 | +import { setupNavigation, setBoneAndSubbones, disableButtons } from './navigation.js'; |
| 5 | +import { loadDescription } from './description.js'; // ✅ CORRECT function name |
5 | 6 |
|
6 | 7 | let combinedData = { bonesets: [], bones: [], subbones: [] }; |
7 | 8 |
|
8 | 9 | document.addEventListener('DOMContentLoaded', async () => { |
9 | | - // Initialize sidebar toggle behavior |
| 10 | + // 1. Sidebar behavior |
10 | 11 | initializeSidebar(); |
11 | 12 |
|
12 | | - // Fetch and render bone data |
| 13 | + // 2. Fetch data and populate dropdowns |
13 | 14 | combinedData = await fetchCombinedData(); |
14 | 15 | populateBonesetDropdown(combinedData.bonesets); |
15 | 16 | setupDropdownListeners(combinedData); |
16 | 17 |
|
| 18 | + // 3. Hook up navigation buttons |
| 19 | + const prevButton = document.getElementById('prev-button'); |
| 20 | + const nextButton = document.getElementById('next-button'); |
| 21 | + const subboneDropdown = document.getElementById('subbone-select'); |
| 22 | + const boneDropdown = document.getElementById('bone-select'); |
| 23 | + |
| 24 | + setupNavigation(prevButton, nextButton, subboneDropdown, loadDescription); |
| 25 | + |
| 26 | + // 4. Update navigation when bone changes |
| 27 | + boneDropdown.addEventListener('change', (event) => { |
| 28 | + const selectedBone = event.target.value; |
| 29 | + |
| 30 | + const relatedSubbones = combinedData.subbones |
| 31 | + .filter(sb => sb.bone === selectedBone) |
| 32 | + .map(sb => sb.id); |
| 33 | + |
| 34 | + setBoneAndSubbones(selectedBone, relatedSubbones); |
| 35 | + populateSubboneDropdown(subboneDropdown, relatedSubbones); |
| 36 | + disableButtons(prevButton, nextButton); |
| 37 | + }); |
| 38 | + |
| 39 | + // 5. Auto-select the first boneset |
17 | 40 | const boneset = combinedData.bonesets[0]; |
18 | 41 | if (boneset) { |
19 | 42 | document.getElementById('boneset-select').value = boneset.id; |
20 | 43 | const event = new Event('change'); |
21 | 44 | document.getElementById('boneset-select').dispatchEvent(event); |
22 | 45 | } |
23 | 46 | }); |
| 47 | + |
| 48 | +function populateSubboneDropdown(dropdown, subbones) { |
| 49 | + dropdown.innerHTML = ''; |
| 50 | + subbones.forEach((subboneId) => { |
| 51 | + const option = document.createElement('option'); |
| 52 | + option.value = subboneId; |
| 53 | + option.textContent = subboneId.replace(/_/g, ' '); |
| 54 | + dropdown.appendChild(option); |
| 55 | + }); |
| 56 | +} |
0 commit comments