diff options
Diffstat (limited to 'script.js')
| -rw-r--r-- | script.js | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/script.js b/script.js new file mode 100644 index 0000000..5bc60b0 --- /dev/null +++ b/script.js | |||
| @@ -0,0 +1,156 @@ | |||
| 1 | let generatedFiles = {}; | ||
| 2 | |||
| 3 | async function generateFiles() { | ||
| 4 | const configTextarea = document.getElementById('config'); | ||
| 5 | const generateBtn = document.getElementById('generateBtn'); | ||
| 6 | const loadingDiv = document.getElementById('loading'); | ||
| 7 | const errorDiv = document.getElementById('error'); | ||
| 8 | const resultsDiv = document.getElementById('results'); | ||
| 9 | |||
| 10 | const config = configTextarea.value.trim(); | ||
| 11 | |||
| 12 | if (!config) { | ||
| 13 | showError('Please enter a configuration before generating files.'); | ||
| 14 | return; | ||
| 15 | } | ||
| 16 | |||
| 17 | // Reset UI state | ||
| 18 | hideError(); | ||
| 19 | hideResults(); | ||
| 20 | showLoading(); | ||
| 21 | generateBtn.disabled = true; | ||
| 22 | |||
| 23 | try { | ||
| 24 | const response = await fetch('/', { | ||
| 25 | method: 'POST', | ||
| 26 | headers: { | ||
| 27 | 'Content-Type': 'application/json', | ||
| 28 | }, | ||
| 29 | body: JSON.stringify({ config: config }) | ||
| 30 | }); | ||
| 31 | |||
| 32 | const data = await response.json(); | ||
| 33 | |||
| 34 | if (!response.ok) { | ||
| 35 | throw new Error(data.error || 'Failed to generate files'); | ||
| 36 | } | ||
| 37 | |||
| 38 | // Store the generated files | ||
| 39 | generatedFiles = data; | ||
| 40 | |||
| 41 | // Display the results | ||
| 42 | displayResults(data); | ||
| 43 | |||
| 44 | } catch (error) { | ||
| 45 | console.error('Error generating files:', error); | ||
| 46 | showError('Error generating files: ' + error.message); | ||
| 47 | } finally { | ||
| 48 | hideLoading(); | ||
| 49 | generateBtn.disabled = false; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | function displayResults(files) { | ||
| 54 | const resultsDiv = document.getElementById('results'); | ||
| 55 | |||
| 56 | if (Object.keys(files).length === 0) { | ||
| 57 | showError('No files were generated.'); | ||
| 58 | return; | ||
| 59 | } | ||
| 60 | |||
| 61 | let html = '<h3>Generated Files</h3>'; | ||
| 62 | |||
| 63 | for (const [filename, content] of Object.entries(files)) { | ||
| 64 | html += ` | ||
| 65 | <div class="file-item"> | ||
| 66 | <h4>${filename}</h4> | ||
| 67 | <p>Size: ${content.length} characters</p> | ||
| 68 | <div class="file-buttons"> | ||
| 69 | <button onclick="toggleFileContent('${filename}')" id="view-${filename}"> | ||
| 70 | View Content | ||
| 71 | </button> | ||
| 72 | <button onclick="downloadFile('${filename}')" class="secondary"> | ||
| 73 | Download | ||
| 74 | </button> | ||
| 75 | </div> | ||
| 76 | <div id="content-${filename}" class="file-content">${escapeHtml(content)}</div> | ||
| 77 | </div> | ||
| 78 | `; | ||
| 79 | } | ||
| 80 | |||
| 81 | resultsDiv.innerHTML = html; | ||
| 82 | resultsDiv.style.display = 'block'; | ||
| 83 | } | ||
| 84 | |||
| 85 | function toggleFileContent(filename) { | ||
| 86 | const contentDiv = document.getElementById(`content-${filename}`); | ||
| 87 | const viewBtn = document.getElementById(`view-${filename}`); | ||
| 88 | |||
| 89 | if (contentDiv.style.display === 'none' || contentDiv.style.display === '') { | ||
| 90 | contentDiv.style.display = 'block'; | ||
| 91 | viewBtn.textContent = 'Hide Content'; | ||
| 92 | } else { | ||
| 93 | contentDiv.style.display = 'none'; | ||
| 94 | viewBtn.textContent = 'View Content'; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | function downloadFile(filename) { | ||
| 99 | if (!generatedFiles[filename]) { | ||
| 100 | showError('File not found: ' + filename); | ||
| 101 | return; | ||
| 102 | } | ||
| 103 | |||
| 104 | const content = generatedFiles[filename]; | ||
| 105 | const blob = new Blob([content], { type: 'text/csv' }); | ||
| 106 | const url = window.URL.createObjectURL(blob); | ||
| 107 | |||
| 108 | const a = document.createElement('a'); | ||
| 109 | a.href = url; | ||
| 110 | a.download = filename; | ||
| 111 | document.body.appendChild(a); | ||
| 112 | a.click(); | ||
| 113 | |||
| 114 | // Clean up | ||
| 115 | window.URL.revokeObjectURL(url); | ||
| 116 | document.body.removeChild(a); | ||
| 117 | } | ||
| 118 | |||
| 119 | function showError(message) { | ||
| 120 | const errorDiv = document.getElementById('error'); | ||
| 121 | errorDiv.textContent = message; | ||
| 122 | errorDiv.style.display = 'block'; | ||
| 123 | } | ||
| 124 | |||
| 125 | function hideError() { | ||
| 126 | const errorDiv = document.getElementById('error'); | ||
| 127 | errorDiv.style.display = 'none'; | ||
| 128 | } | ||
| 129 | |||
| 130 | function showLoading() { | ||
| 131 | const loadingDiv = document.getElementById('loading'); | ||
| 132 | loadingDiv.style.display = 'block'; | ||
| 133 | } | ||
| 134 | |||
| 135 | function hideLoading() { | ||
| 136 | const loadingDiv = document.getElementById('loading'); | ||
| 137 | loadingDiv.style.display = 'none'; | ||
| 138 | } | ||
| 139 | |||
| 140 | function hideResults() { | ||
| 141 | const resultsDiv = document.getElementById('results'); | ||
| 142 | resultsDiv.style.display = 'none'; | ||
| 143 | } | ||
| 144 | |||
| 145 | function escapeHtml(text) { | ||
| 146 | const div = document.createElement('div'); | ||
| 147 | div.textContent = text; | ||
| 148 | return div.innerHTML; | ||
| 149 | } | ||
| 150 | |||
| 151 | // Add Enter key support for the textarea (Ctrl+Enter to generate) | ||
| 152 | document.getElementById('config').addEventListener('keydown', function (event) { | ||
| 153 | if (event.ctrlKey && event.key === 'Enter') { | ||
| 154 | generateFiles(); | ||
| 155 | } | ||
| 156 | }); \ No newline at end of file | ||
