|
| 1 | +// Acquire the VS Code API for communication with the extension backend |
1 | 2 | const vscode = acquireVsCodeApi(); |
| 3 | + |
| 4 | +// Initialize project mapping and source files from the window's global data |
2 | 5 | const projectMap = new Map(window.projectData || []); |
3 | 6 | const initialSourceFiles = window.initialSourceFiles || []; |
4 | 7 |
|
5 | | -// receive envFileSelected? (not used here but pattern in other scripts) |
| 8 | +// --------------------------------------- |
| 9 | +// UI Helper Functions |
| 10 | +// --------------------------------------- |
6 | 11 |
|
| 12 | +/** |
| 13 | + * Adds a new source file row to the "Source Files" container. |
| 14 | + * @param {string} filePath - Optional initial file path to populate. |
| 15 | + */ |
7 | 16 | function addSourceRow(filePath = '') { |
8 | 17 | const container = document.getElementById('sourceFilesContainer'); |
| 18 | + |
9 | 19 | const row = document.createElement('div'); |
10 | 20 | row.className = 'single-input-container'; |
| 21 | + |
11 | 22 | const input = document.createElement('input'); |
12 | | - input.type = 'text'; input.value = filePath; input.placeholder = 'Enter Source File'; |
13 | | - const rm = document.createElement('button'); |
14 | | - rm.className = 'remove-button'; rm.textContent = '✖'; |
15 | | - rm.addEventListener('click', () => row.remove()); |
16 | | - row.append(input, rm); |
| 23 | + input.type = 'text'; |
| 24 | + input.value = filePath; |
| 25 | + input.placeholder = 'Enter Source File'; |
| 26 | + |
| 27 | + const removeButton = document.createElement('button'); |
| 28 | + removeButton.className = 'remove-button'; |
| 29 | + removeButton.textContent = '✖'; |
| 30 | + removeButton.addEventListener('click', () => row.remove()); |
| 31 | + |
| 32 | + row.append(input, removeButton); |
17 | 33 | container.appendChild(row); |
18 | 34 | } |
19 | 35 |
|
| 36 | +/** |
| 37 | + * Adds a new compiler/testsuites row based on the selected project path. |
| 38 | + */ |
20 | 39 | function addCompilerRow() { |
21 | | - const ps = document.getElementById('projectPath'); |
22 | | - const info = projectMap.get(ps.value); |
23 | | - if (!info) return; |
| 40 | + const projectSelect = document.getElementById('projectPath'); |
| 41 | + const projectInfo = projectMap.get(projectSelect.value); |
| 42 | + |
| 43 | + if (!projectInfo) return; // No project info available |
| 44 | + |
24 | 45 | const row = document.createElement('div'); |
25 | 46 | row.className = 'double-input-container'; |
26 | | - const comp = document.createElement('select'); |
27 | | - comp.innerHTML = info.compilers.map(c => `<option>${c}</option>`).join(''); |
28 | | - const ts = document.createElement('select'); |
29 | | - ts.innerHTML = info.testsuites.map(t => `<option>${t}</option>`).join(''); |
30 | | - const rm = document.createElement('button'); |
31 | | - rm.className = 'remove-button'; rm.textContent = '✖'; |
32 | | - rm.addEventListener('click', () => row.remove()); |
33 | | - row.append(comp, ts, rm); |
| 47 | + |
| 48 | + const compilerSelect = document.createElement('select'); |
| 49 | + compilerSelect.innerHTML = projectInfo.compilers.map(c => `<option>${c}</option>`).join(''); |
| 50 | + |
| 51 | + const testsuiteSelect = document.createElement('select'); |
| 52 | + testsuiteSelect.innerHTML = projectInfo.testsuites.map(t => `<option>${t}</option>`).join(''); |
| 53 | + |
| 54 | + const removeButton = document.createElement('button'); |
| 55 | + removeButton.className = 'remove-button'; |
| 56 | + removeButton.textContent = '✖'; |
| 57 | + removeButton.addEventListener('click', () => row.remove()); |
| 58 | + |
| 59 | + row.append(compilerSelect, testsuiteSelect, removeButton); |
34 | 60 | document.getElementById('compilerContainer').appendChild(row); |
35 | 61 | } |
36 | 62 |
|
| 63 | +/** |
| 64 | + * Gathers form data and sends a 'submit' message to the extension. |
| 65 | + */ |
37 | 66 | function submitForm() { |
38 | 67 | const projectPath = document.getElementById('projectPath').value; |
| 68 | + |
| 69 | + // Collect all source file input values |
39 | 70 | const sourceFiles = Array.from( |
40 | 71 | document.querySelectorAll('#sourceFilesContainer input') |
41 | | - ).map(i => i.value); |
| 72 | + ).map(input => input.value); |
| 73 | + |
| 74 | + // Collect compiler/testsuites selections |
42 | 75 | const testsuiteArgs = Array.from( |
43 | 76 | document.querySelectorAll('#compilerContainer .double-input-container') |
44 | | - ).map(r => { |
45 | | - const [c, t] = r.querySelectorAll('select'); |
46 | | - return `${c.value}/${t.value}`; |
| 77 | + ).map(row => { |
| 78 | + const [compilerSelect, testsuiteSelect] = row.querySelectorAll('select'); |
| 79 | + return `${compilerSelect.value}/${testsuiteSelect.value}`; |
47 | 80 | }); |
| 81 | + |
| 82 | + // Validate form |
48 | 83 | if (!projectPath || testsuiteArgs.length === 0) { |
49 | | - vscode.postMessage({ command: 'error', message: 'Project Path and Testsuite are required.' }); |
| 84 | + vscode.postMessage({ |
| 85 | + command: 'error', |
| 86 | + message: 'Project Path and at least one Testsuite are required.' |
| 87 | + }); |
50 | 88 | return; |
51 | 89 | } |
52 | | - vscode.postMessage({ command: 'submit', projectPath, sourceFiles, testsuiteArgs }); |
| 90 | + |
| 91 | + // Submit collected data |
| 92 | + vscode.postMessage({ |
| 93 | + command: 'submit', |
| 94 | + projectPath, |
| 95 | + sourceFiles, |
| 96 | + testsuiteArgs |
| 97 | + }); |
53 | 98 | } |
54 | 99 |
|
| 100 | +/** |
| 101 | + * Cancels the operation and informs the extension. |
| 102 | + */ |
55 | 103 | function cancel() { |
56 | 104 | vscode.postMessage({ command: 'cancel' }); |
57 | 105 | } |
58 | 106 |
|
| 107 | +// --------------------------------------- |
| 108 | +// Initialization |
| 109 | +// --------------------------------------- |
| 110 | + |
| 111 | +/** |
| 112 | + * Initializes the form when the DOM content is fully loaded. |
| 113 | + */ |
59 | 114 | window.addEventListener('DOMContentLoaded', () => { |
60 | | - const ps = document.getElementById('projectPath'); |
| 115 | + const projectSelect = document.getElementById('projectPath'); |
| 116 | + |
| 117 | + // Populate the project path dropdown |
61 | 118 | for (const [key] of projectMap.entries()) { |
62 | | - const opt = document.createElement('option'); |
63 | | - opt.value = key; opt.text = key; ps.append(opt); |
| 119 | + const option = document.createElement('option'); |
| 120 | + option.value = key; |
| 121 | + option.text = key; |
| 122 | + projectSelect.append(option); |
64 | 123 | } |
65 | | - initialSourceFiles.forEach(f => addSourceRow(f)); |
66 | | - if (initialSourceFiles.length === 0) addSourceRow(); |
| 124 | + |
| 125 | + // Populate initial source files, or add an empty one if none exist |
| 126 | + initialSourceFiles.forEach(filePath => addSourceRow(filePath)); |
| 127 | + if (initialSourceFiles.length === 0) { |
| 128 | + addSourceRow(); |
| 129 | + } |
| 130 | + |
| 131 | + // Add an initial compiler/testsuites row |
67 | 132 | addCompilerRow(); |
68 | | - ps.addEventListener('change', () => { |
69 | | - document.querySelectorAll('.double-input-container').forEach(r => r.remove()); |
| 133 | + |
| 134 | + // Update compiler/testsuites when the project changes |
| 135 | + projectSelect.addEventListener('change', () => { |
| 136 | + document.querySelectorAll('.double-input-container').forEach(row => row.remove()); |
70 | 137 | addCompilerRow(); |
71 | 138 | }); |
| 139 | + |
| 140 | + // Hook up button event listeners |
72 | 141 | document.getElementById('btnAddSource').addEventListener('click', () => addSourceRow()); |
73 | 142 | document.getElementById('btnAddCompiler').addEventListener('click', addCompilerRow); |
74 | 143 | document.getElementById('btnSubmit').addEventListener('click', submitForm); |
|
0 commit comments