amine_dubs
commited on
Commit
·
91dbc5b
1
Parent(s):
841e389
- static/script.js +38 -38
static/script.js
CHANGED
@@ -2,35 +2,36 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
2 |
// Log when the page loads to confirm JavaScript is working
|
3 |
console.log("Translation app initialized");
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
const errorMessageDiv = document.getElementById('error-message');
|
14 |
-
const docLoadingIndicator = document.getElementById('doc-loading');
|
15 |
-
const debugInfoDiv = document.getElementById('debug-info');
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
// Create text loading indicator if it doesn't exist
|
24 |
-
let textLoadingDiv =
|
25 |
-
if (!textLoadingDiv) {
|
26 |
console.log("Creating missing text loading indicator");
|
27 |
textLoadingDiv = document.createElement('div');
|
28 |
textLoadingDiv.id = 'text-loading';
|
29 |
textLoadingDiv.className = 'loading-spinner';
|
30 |
textLoadingDiv.textContent = 'Translating...';
|
31 |
-
|
32 |
-
textForm.appendChild(textLoadingDiv);
|
33 |
-
}
|
34 |
}
|
35 |
|
36 |
// Helper function to display debug info
|
@@ -123,25 +124,24 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
123 |
// Show debug immediately to confirm the handler is working
|
124 |
showDebug('Translation submission triggered');
|
125 |
|
126 |
-
// Use correct field IDs matching the HTML
|
127 |
-
const textInput =
|
128 |
-
const sourceLangSelect =
|
129 |
-
const targetLangSelect =
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
if (!targetLangSelect) {
|
140 |
-
displayError('Target language select not found!');
|
141 |
return;
|
142 |
}
|
143 |
|
144 |
-
|
|
|
145 |
const sourceLang = sourceLangSelect.value;
|
146 |
const targetLang = targetLangSelect.value;
|
147 |
|
@@ -240,10 +240,10 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
240 |
clearFeedback();
|
241 |
|
242 |
const formData = new FormData(docForm);
|
243 |
-
const fileInput =
|
244 |
const button = docForm.querySelector('button');
|
245 |
|
246 |
-
if (!fileInput.files || fileInput.files.length === 0) {
|
247 |
displayError('Please select a document to upload.');
|
248 |
return;
|
249 |
}
|
|
|
2 |
// Log when the page loads to confirm JavaScript is working
|
3 |
console.log("Translation app initialized");
|
4 |
|
5 |
+
// Safely get DOM elements with error handling
|
6 |
+
function safeGetElement(id) {
|
7 |
+
const element = document.getElementById(id);
|
8 |
+
if (!element) {
|
9 |
+
console.error(`Element not found: #${id}`);
|
10 |
+
}
|
11 |
+
return element;
|
12 |
+
}
|
|
|
|
|
|
|
13 |
|
14 |
+
const textForm = safeGetElement('text-translation-form');
|
15 |
+
const docForm = safeGetElement('doc-translation-form');
|
16 |
+
const textResultBox = safeGetElement('text-result');
|
17 |
+
const textOutput = safeGetElement('text-output');
|
18 |
+
const docResultBox = safeGetElement('doc-result');
|
19 |
+
const docOutput = safeGetElement('doc-output');
|
20 |
+
const docFilename = safeGetElement('doc-filename');
|
21 |
+
const docSourceLang = safeGetElement('doc-source-lang');
|
22 |
+
const errorMessageDiv = safeGetElement('error-message');
|
23 |
+
const docLoadingIndicator = safeGetElement('doc-loading');
|
24 |
+
const debugInfoDiv = safeGetElement('debug-info');
|
25 |
|
26 |
// Create text loading indicator if it doesn't exist
|
27 |
+
let textLoadingDiv = safeGetElement('text-loading');
|
28 |
+
if (!textLoadingDiv && textForm) {
|
29 |
console.log("Creating missing text loading indicator");
|
30 |
textLoadingDiv = document.createElement('div');
|
31 |
textLoadingDiv.id = 'text-loading';
|
32 |
textLoadingDiv.className = 'loading-spinner';
|
33 |
textLoadingDiv.textContent = 'Translating...';
|
34 |
+
textForm.appendChild(textLoadingDiv);
|
|
|
|
|
35 |
}
|
36 |
|
37 |
// Helper function to display debug info
|
|
|
124 |
// Show debug immediately to confirm the handler is working
|
125 |
showDebug('Translation submission triggered');
|
126 |
|
127 |
+
// Use correct field IDs matching the HTML and check if they exist
|
128 |
+
const textInput = safeGetElement('text-input');
|
129 |
+
const sourceLangSelect = safeGetElement('source-lang-text');
|
130 |
+
const targetLangSelect = safeGetElement('target-lang-text');
|
131 |
|
132 |
+
// Check all required elements exist before proceeding
|
133 |
+
if (!textInput || !sourceLangSelect || !targetLangSelect) {
|
134 |
+
displayError('One or more required form elements are missing. Check your HTML structure.');
|
135 |
+
console.error('Missing elements:',
|
136 |
+
!textInput ? 'text-input' : '',
|
137 |
+
!sourceLangSelect ? 'source-lang-text' : '',
|
138 |
+
!targetLangSelect ? 'target-lang-text' : ''
|
139 |
+
);
|
|
|
|
|
140 |
return;
|
141 |
}
|
142 |
|
143 |
+
// Safely extract values
|
144 |
+
const sourceText = textInput.value ? textInput.value.trim() : '';
|
145 |
const sourceLang = sourceLangSelect.value;
|
146 |
const targetLang = targetLangSelect.value;
|
147 |
|
|
|
240 |
clearFeedback();
|
241 |
|
242 |
const formData = new FormData(docForm);
|
243 |
+
const fileInput = safeGetElement('doc-input');
|
244 |
const button = docForm.querySelector('button');
|
245 |
|
246 |
+
if (!fileInput || !fileInput.files || fileInput.files.length === 0) {
|
247 |
displayError('Please select a document to upload.');
|
248 |
return;
|
249 |
}
|