AlHaq_QA / static /script.js
a2zsolun's picture
Upload 34 files
922ec85 verified
Raw History Blame Contribute Delete
5.35 kB
// ============================================================
// Configuration
// ============================================================
const API_BASE_URL = 'http://localhost:8000';
const ASK_ENDPOINT = '/ask';
// ============================================================
// DOM Elements
// ============================================================
const questionInput = document.getElementById('questionInput');
const askBtn = document.getElementById('askBtn');
const btnText = document.getElementById('btnText');
const btnLoader = document.getElementById('btnLoader');
const resultsSection = document.getElementById('resultsSection');
const errorSection = document.getElementById('errorSection');
const answerText = document.getElementById('answerText');
const sourcesText = document.getElementById('sourcesText');
const languageDetected = document.getElementById('languageDetected');
const errorText = document.getElementById('errorText');
// ============================================================
// Event Listeners
// ============================================================
askBtn.addEventListener('click', askQuestion);
questionInput.addEventListener('keydown', (e) => {
if (e.ctrlKey && e.key === 'Enter') {
askQuestion();
}
});
// ============================================================
// Main Functions
// ============================================================
async function askQuestion() {
const question = questionInput.value.trim();
if (!question) {
showError('Please enter a question');
return;
}
// Hide previous results and errors
resultsSection.style.display = 'none';
errorSection.style.display = 'none';
// Show loading state
setLoading(true);
try {
// Make API request
const response = await fetch(API_BASE_URL + ASK_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
question: question
})
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || 'Failed to get answer');
}
const data = await response.json();
// Display results
displayResults(data);
} catch (error) {
console.error('Error:', error);
showError(`Error: ${error.message}`);
} finally {
setLoading(false);
}
}
function displayResults(data) {
// Set answer
answerText.innerHTML = formatText(data.answer);
// Set sources
sourcesText.innerHTML = formatText(data.sources);
// Set language
const langMap = {
'en': 'πŸ‡¬πŸ‡§ English',
'ar': 'πŸ‡ΈπŸ‡¦ Arabic',
'ur': 'πŸ‡΅πŸ‡° Urdu',
'fr': 'πŸ‡«πŸ‡· French',
'de': 'πŸ‡©πŸ‡ͺ German'
};
languageDetected.textContent = langMap[data.language] || data.language.toUpperCase();
// Show results section
resultsSection.style.display = 'block';
// Scroll to results
resultsSection.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
function showError(message) {
errorText.textContent = message;
errorSection.style.display = 'block';
errorSection.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
function setLoading(loading) {
askBtn.disabled = loading;
if (loading) {
btnText.style.display = 'none';
btnLoader.style.display = 'inline-block';
} else {
btnText.style.display = 'inline';
btnLoader.style.display = 'none';
}
}
function setQuestion(text) {
questionInput.value = text;
questionInput.focus();
}
function formatText(text) {
if (!text) return '<p>No content available</p>';
// Escape HTML
text = text
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
// Convert newlines to line breaks
text = text.replace(/\n/g, '<br>');
// Convert links
text = text.replace(
/\b(https?:\/\/[^\s]+)/g,
'<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>'
);
// Convert bold markers
text = text.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
text = text.replace(/__(.*?)__/g, '<strong>$1</strong>');
// Convert italic markers
text = text.replace(/\*(.*?)\*/g, '<em>$1</em>');
text = text.replace(/_(.*?)_/g, '<em>$1</em>');
return `<p>${text}</p>`;
}
// ============================================================
// Initialization
// ============================================================
console.log('Islamic QA - Static Frontend Loaded');
console.log(`API URL: ${API_BASE_URL}`);
// Check API connection on load
window.addEventListener('load', async () => {
try {
const response = await fetch(API_BASE_URL + '/health');
if (response.ok) {
console.log('βœ“ API connection successful');
}
} catch (error) {
console.warn('⚠ API connection failed:', error.message);
console.warn('Make sure to run: python main.py');
}
});