|
137 | 137 | <script type="module"> |
138 | 138 | import { examples } from './src/examples/index.js'; |
139 | 139 |
|
140 | | - let currentExampleIndex = 0; |
| 140 | + let currentExampleId = null; |
141 | 141 | const form = document.querySelector('vuetify-json-forms'); |
142 | 142 | const exampleIcon = document.getElementById('exampleIcon'); |
143 | 143 | const exampleList = document.getElementById('exampleList'); |
144 | 144 |
|
145 | | - // Function to load an example |
146 | | - function loadExample(exampleIndex) { |
147 | | - // Check if examples array exists and index is valid |
| 145 | + // Function to find example by ID |
| 146 | + function findExampleById(exampleId) { |
148 | 147 | if (!examples || examples.length === 0) { |
149 | | - console.warn('No examples available'); |
150 | | - return; |
| 148 | + return null; |
151 | 149 | } |
| 150 | + return examples.find((example) => example.id === exampleId); |
| 151 | + } |
152 | 152 |
|
153 | | - if (exampleIndex < 0 || exampleIndex >= examples.length) { |
154 | | - console.warn( |
155 | | - `Invalid example index: ${exampleIndex}. Available: 0-${examples.length - 1}`, |
156 | | - ); |
| 153 | + // Function to load an example by ID |
| 154 | + function loadExample(exampleId) { |
| 155 | + // Check if examples array exists |
| 156 | + if (!examples || examples.length === 0) { |
| 157 | + console.warn('No examples available'); |
157 | 158 | return; |
158 | 159 | } |
159 | 160 |
|
160 | | - const example = examples[exampleIndex]; |
| 161 | + const example = findExampleById(exampleId); |
161 | 162 | if (!example) { |
162 | | - console.warn(`Example at index ${exampleIndex} is null or undefined`); |
| 163 | + console.warn(`Example with ID '${exampleId}' not found`); |
163 | 164 | return; |
164 | 165 | } |
165 | 166 |
|
|
192 | 193 | } |
193 | 194 | } |
194 | 195 |
|
195 | | - currentExampleIndex = exampleIndex; |
| 196 | + currentExampleId = exampleId; |
196 | 197 | console.log('Selected example:', example); |
197 | 198 | } |
198 | 199 |
|
|
207 | 208 |
|
208 | 209 | // Populate example list |
209 | 210 | function populateExampleList() { |
| 211 | + if (!examples || examples.length === 0) { |
| 212 | + console.warn('No examples available for list'); |
| 213 | + return; |
| 214 | + } |
| 215 | + |
210 | 216 | exampleList.innerHTML = ''; |
211 | 217 | examples.forEach((example, index) => { |
212 | 218 | const item = document.createElement('div'); |
213 | 219 | item.className = 'example-item'; |
214 | 220 | item.innerHTML = `<div class="example-item-name">${example.title || `Example ${index + 1}`}</div>`; |
215 | 221 |
|
| 222 | + // Store the example ID in a data attribute for easier access |
| 223 | + item.dataset.exampleId = example.id; |
| 224 | + |
216 | 225 | item.addEventListener('click', () => { |
217 | | - loadExample(index); |
| 226 | + loadExample(example.id); |
218 | 227 | exampleList.classList.remove('show'); |
219 | 228 | }); |
220 | 229 |
|
|
240 | 249 |
|
241 | 250 | // Initialize |
242 | 251 | populateExampleList(); |
243 | | - loadExample(1); // Load first example by default |
| 252 | + |
| 253 | + // Load first example by ID if available |
| 254 | + if (examples && examples.length > 0) { |
| 255 | + // get the demo example and if not registered get the first available |
| 256 | + const example = findExampleById('demo') ?? examples[0]; |
| 257 | + if (example?.id) { |
| 258 | + loadExample(example.id); |
| 259 | + } else { |
| 260 | + console.warn('First example does not have an ID'); |
| 261 | + } |
| 262 | + } else { |
| 263 | + console.warn('No examples available to load'); |
| 264 | + } |
244 | 265 | </script> |
245 | 266 | </body> |
246 | 267 | </html> |
0 commit comments