Spaces:
Running
Running
File size: 2,471 Bytes
cc50aa5 | 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 | // Shared functions across the application
// Initialize tooltips
function initTooltips() {
const tooltips = document.querySelectorAll('.tooltip');
tooltips.forEach(tooltip => {
tooltip.addEventListener('mouseenter', function() {
const tooltipText = this.querySelector('.tooltiptext');
tooltipText.style.visibility = 'visible';
tooltipText.style.opacity = '1';
});
tooltip.addEventListener('mouseleave', function() {
const tooltipText = this.querySelector('.tooltiptext');
tooltipText.style.visibility = 'hidden';
tooltipText.style.opacity = '0';
});
});
}
// Form validation helper
function validateForm(formId) {
const form = document.getElementById(formId);
if (!form) return false;
const inputs = form.querySelectorAll('input[required], select[required], textarea[required]');
let isValid = true;
inputs.forEach(input => {
if (!input.value.trim()) {
input.classList.add('border-red-500');
isValid = false;
} else {
input.classList.remove('border-red-500');
}
});
return isValid;
}
// Initialize the app when DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
// Initialize Feather Icons
if (typeof feather !== 'undefined') {
feather.replace();
}
// Initialize tooltips
initTooltips();
// Any other initialization code
console.log('SurveySparrow application initialized');
});
// API helper functions
async function fetchData(endpoint, method = 'GET', data = null) {
const options = {
method,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${localStorage.getItem('authToken')}`
}
};
if (data) {
options.body = JSON.stringify(data);
}
try {
const response = await fetch(`/api/${endpoint}`, options);
if (!response.ok) throw new Error('Network response was not ok');
return await response.json();
} catch (error) {
console.error('API call failed:', error);
throw error;
}
}
// Local storage helper
function setAuthToken(token) {
localStorage.setItem('authToken', token);
}
function getAuthToken() {
return localStorage.getItem('authToken');
}
function clearAuth() {
localStorage.removeItem('authToken');
} |