Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
191 lines (168 loc) · 6.3 KB
/
Copy pathscript.js
File metadata and controls
191 lines (168 loc) · 6.3 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Screen management
function showScreen(screenId, reviewContent = '') {
document.querySelectorAll('.screen').forEach(screen => {
screen.classList.remove('active');
});
document.getElementById(screenId).classList.add('active');
if (screenId === 'home-screen') {
updateRecentReviews();
} else if (screenId === 'report-screen') {
document.getElementById('report-content').innerHTML = marked.parse(reviewContent);
}
}
// File handling
let uploadedFiles = { submittal: null, spec: null };
document.addEventListener('DOMContentLoaded', function() {
initializeDragAndDrop();
initializeFileInputs();
});
function initializeDragAndDrop() {
const uploadAreas = document.querySelectorAll('.upload-area');
uploadAreas.forEach(area => {
area.addEventListener('dragover', handleDragOver);
area.addEventListener('drop', handleDrop);
area.addEventListener('dragenter', handleDragEnter);
area.addEventListener('dragleave', handleDragLeave);
});
}
function initializeFileInputs() {
const submittalInput = document.getElementById('submittal-file');
const specInput = document.getElementById('spec-file');
submittalInput.addEventListener('change', (e) => handleFileSelect(e, 'submittal'));
specInput.addEventListener('change', (e) => handleFileSelect(e, 'spec'));
}
function handleDragOver(e) {
e.preventDefault();
e.stopPropagation();
}
function handleDragEnter(e) {
e.preventDefault();
e.stopPropagation();
e.currentTarget.classList.add('dragover');
}
function handleDragLeave(e) {
e.preventDefault();
e.stopPropagation();
e.currentTarget.classList.remove('dragover');
}
function handleDrop(e) {
e.preventDefault();
e.stopPropagation();
const area = e.currentTarget;
area.classList.remove('dragover');
const files = e.dataTransfer.files;
if (files.length > 0) {
const file = files[0];
const fileType = area.id === 'submittal-upload' ? 'submittal' : 'spec';
processFile(file, fileType);
}
}
function handleFileSelect(e, fileType) {
const file = e.target.files[0];
if (file) {
processFile(file, fileType);
}
}
function processFile(file, fileType) {
const allowedTypes = ['application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'text/plain'];
if (!allowedTypes.includes(file.type)) {
alert('Please upload a PDF, DOC, DOCX, or TXT file.');
return;
}
const maxSize = 200 * 1024 * 1024; // 200MB
if (file.size > maxSize) {
alert('File size must be less than 200MB.');
return;
}
uploadedFiles[fileType] = file;
displayFileInfo(file, fileType);
updateGetReportButton();
}
function displayFileInfo(file, fileType) {
const fileInfo = document.getElementById(`${fileType}-file-info`);
const filename = fileInfo.querySelector('.filename');
filename.textContent = file.name;
fileInfo.style.display = 'flex';
}
function removeFile(fileType) {
uploadedFiles[fileType] = null;
const fileInfo = document.getElementById(`${fileType}-file-info`);
fileInfo.style.display = 'none';
const fileInput = document.getElementById(`${fileType}-file`);
fileInput.value = '';
updateGetReportButton();
}
function updateGetReportButton() {
const button = document.querySelector('.get-report-button');
const hasBothFiles = uploadedFiles.submittal && uploadedFiles.spec;
button.disabled = !hasBothFiles;
}
async function generateReport() {
if (!uploadedFiles.submittal || !uploadedFiles.spec) {
alert('Please upload both a submittal and a spec file.');
return;
}
const button = document.querySelector('.get-report-button');
const originalText = button.innerHTML;
button.innerHTML = 'Generating Report...';
button.disabled = true;
try {
const specText = await uploadedFiles.spec.text();
const formData = new FormData();
formData.append('spec_text', specText);
formData.append('submittal', uploadedFiles.submittal);
const response = await fetch('https://triplo-ebu1.onrender.com/generate_review', {
method: 'POST',
body: formData
});
if (!response.ok) {
throw new Error('Failed to generate review');
}
const data = await response.json();
saveToRecentReviews(data.review);
showScreen('report-screen', data.review);
} catch (error) {
alert('Error generating report: ' + error.message);
} finally {
button.innerHTML = originalText;
button.disabled = false;
uploadedFiles = { submittal: null, spec: null };
document.querySelectorAll('.file-info').forEach(info => info.style.display = 'none');
document.querySelectorAll('input[type="file"]').forEach(input => input.value = '');
updateGetReportButton();
}
}
function saveToRecentReviews(review) {
const reviews = JSON.parse(sessionStorage.getItem('recentReviews') || '[]');
const timestamp = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
const newReview = {
name: `Review ${reviews.length + 1}`,
timestamp: timestamp,
date: new Date().toISOString(),
content: review
};
reviews.unshift(newReview);
if (reviews.length > 10) reviews.pop();
sessionStorage.setItem('recentReviews', JSON.stringify(reviews));
}
function updateRecentReviews() {
const reviews = JSON.parse(sessionStorage.getItem('recentReviews') || '[]');
const reviewItems = document.querySelector('.review-items');
if (reviews.length === 0) {
reviewItems.innerHTML = '<p style="color: #999; text-align: center; padding: 20px;">No reviews yet</p>';
return;
}
reviewItems.innerHTML = reviews.map(review =>
`<button class="review-item" data-review="${encodeURIComponent(review.content)}">${review.name} ${review.timestamp}</button>`
).join('');
}
document.addEventListener('click', function(e) {
if (e.target.classList.contains('review-item')) {
const reviewContent = decodeURIComponent(e.target.dataset.review);
showScreen('report-screen', reviewContent);
}
});
document.addEventListener('DOMContentLoaded', function() {
showScreen('intro-screen');
updateRecentReviews();
});