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