|
| 1 | +const project = JSON.parse(document.querySelector('#projectData').textContent); |
| 2 | +const mode = document.body.dataset.mode; |
| 3 | +const cards = document.querySelector('#cards'); |
| 4 | +const dynamicSurface = document.querySelector('#dynamicSurface'); |
| 5 | +const progressList = document.querySelector('#progressList'); |
| 6 | + |
| 7 | +function renderCards() { |
| 8 | + cards.innerHTML = project.cards.map((card, index) => ` |
| 9 | + <article class="card"> |
| 10 | + <span class="status">${project.code}.${index + 1}</span> |
| 11 | + <h3>${card}</h3> |
| 12 | + <p>Practice item for ${project.discipline.toLowerCase()} with local-only content.</p> |
| 13 | + </article> |
| 14 | + `).join(''); |
| 15 | +} |
| 16 | + |
| 17 | +function renderProgress() { |
| 18 | + progressList.innerHTML = project.features.map((item) => ` |
| 19 | + <p><strong>${item.name}</strong></p> |
| 20 | + <div class="bar" aria-label="${item.name} progress"><span style="--value: ${item.score}%"></span></div> |
| 21 | + `).join(''); |
| 22 | +} |
| 23 | + |
| 24 | +function formSurface() { |
| 25 | + return ` |
| 26 | + <form id="demoForm" novalidate> |
| 27 | + <div class="form-grid"> |
| 28 | + <label>Full name <input name="fullName" autocomplete="name" required><span class="error"></span></label> |
| 29 | + <label>Student code <input name="studentCode" placeholder="LAB-2026" required><span class="error"></span></label> |
| 30 | + <label>Email <input name="email" type="email" required><span class="error"></span></label> |
| 31 | + <label>Course <select name="course"><option>Computing</option><option>Networks</option></select><span class="error"></span></label> |
| 32 | + <label>Password <input name="password" type="password" required><span class="error"></span></label> |
| 33 | + </div> |
| 34 | + <button type="submit">Validate locally</button> |
| 35 | + <p id="formResult"></p> |
| 36 | + </form> |
| 37 | + `; |
| 38 | +} |
| 39 | + |
| 40 | +function defaultSurface() { |
| 41 | + return ` |
| 42 | + <ul class="timeline"> |
| 43 | + ${project.cards.map((card, index) => `<li><strong>Step ${index + 1}:</strong> ${card}</li>`).join('')} |
| 44 | + </ul> |
| 45 | + `; |
| 46 | +} |
| 47 | + |
| 48 | +function careerSurface() { |
| 49 | + const jobs = [ |
| 50 | + { role: 'Frontend Intern', skills: ['HTML', 'CSS', 'JavaScript'] }, |
| 51 | + { role: 'Support Assistant', skills: ['Troubleshooting', 'Documentation', 'Empathy'] }, |
| 52 | + { role: 'Network Trainee', skills: ['Wi-Fi', 'Monitoring', 'Security'] }, |
| 53 | + { role: 'Data Assistant', skills: ['CSV', 'Python', 'Charts'] }, |
| 54 | + ]; |
| 55 | + const counts = new Map(); |
| 56 | + jobs.forEach((job) => job.skills.forEach((skill) => counts.set(skill, (counts.get(skill) || 0) + 1))); |
| 57 | + return ` |
| 58 | + ${jobs.map((job) => `<article class="card"><h3>${job.role}</h3><p>${job.skills.join(', ')}</p></article>`).join('')} |
| 59 | + <p><strong>Top skills:</strong> ${[...counts.entries()].map(([skill, total]) => `${skill} (${total})`).join(', ')}</p> |
| 60 | + `; |
| 61 | +} |
| 62 | + |
| 63 | +function nocSurface() { |
| 64 | + const services = [ |
| 65 | + ['Internet link', 'ok', 18], |
| 66 | + ['Learning platform', 'ok', 34], |
| 67 | + ['Lab switch', 'warn', 62], |
| 68 | + ['Wi-Fi classroom', 'ok', 41], |
| 69 | + ['File service', 'bad', 0], |
| 70 | + ]; |
| 71 | + return services.map(([name, state, latency]) => ` |
| 72 | + <article class="card"> |
| 73 | + <span class="status ${state}">${state.toUpperCase()}</span> |
| 74 | + <h3>${name}</h3> |
| 75 | + <p>Latency sample: ${latency} ms</p> |
| 76 | + </article> |
| 77 | + `).join(''); |
| 78 | +} |
| 79 | + |
| 80 | +function renderDynamicSurface() { |
| 81 | + if (mode === 'form') dynamicSurface.innerHTML = formSurface(); |
| 82 | + else if (mode === 'career') dynamicSurface.innerHTML = careerSurface(); |
| 83 | + else if (mode === 'noc') dynamicSurface.innerHTML = nocSurface(); |
| 84 | + else dynamicSurface.innerHTML = defaultSurface(); |
| 85 | +} |
| 86 | + |
| 87 | +function wireForm() { |
| 88 | + const form = document.querySelector('#demoForm'); |
| 89 | + if (!form) return; |
| 90 | + form.addEventListener('submit', (event) => { |
| 91 | + event.preventDefault(); |
| 92 | + const rules = { |
| 93 | + fullName: (value) => value.trim().split(/\s+/).length >= 2 || 'Use at least two words.', |
| 94 | + studentCode: (value) => /^LAB-[0-9]{4}$/.test(value) || 'Use the format LAB-2026.', |
| 95 | + email: (value) => /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(value) || 'Use a valid email format.', |
| 96 | + password: (value) => value.length >= 8 || 'Use at least 8 characters.', |
| 97 | + }; |
| 98 | + let valid = true; |
| 99 | + for (const [name, rule] of Object.entries(rules)) { |
| 100 | + const input = form.elements[name]; |
| 101 | + const result = rule(input.value); |
| 102 | + const error = input.parentElement.querySelector('.error'); |
| 103 | + error.textContent = result === true ? '' : result; |
| 104 | + valid = valid && result === true; |
| 105 | + } |
| 106 | + document.querySelector('#formResult').textContent = valid ? 'Local validation passed. Nothing was sent.' : 'Review the highlighted fields.'; |
| 107 | + }); |
| 108 | +} |
| 109 | + |
| 110 | +document.querySelector('#themeToggle').addEventListener('click', () => { |
| 111 | + document.body.classList.toggle('dark'); |
| 112 | + localStorage.setItem('theme', document.body.classList.contains('dark') ? 'dark' : 'light'); |
| 113 | +}); |
| 114 | + |
| 115 | +document.querySelector('#exportButton').addEventListener('click', () => { |
| 116 | + const report = `# ${project.title}\n\nCode: ${project.code}\nDiscipline: ${project.discipline}\nMode: ${mode}\n`; |
| 117 | + const blob = new Blob([report], { type: 'text/markdown' }); |
| 118 | + const link = document.createElement('a'); |
| 119 | + link.href = URL.createObjectURL(blob); |
| 120 | + link.download = `${project.code.toLowerCase()}-report.md`; |
| 121 | + link.click(); |
| 122 | + URL.revokeObjectURL(link.href); |
| 123 | +}); |
| 124 | + |
| 125 | +if (localStorage.getItem('theme') === 'dark') document.body.classList.add('dark'); |
| 126 | +renderCards(); |
| 127 | +renderProgress(); |
| 128 | +renderDynamicSurface(); |
| 129 | +wireForm(); |
0 commit comments