surveysparrow / script.js
Research3's picture
continue
cc50aa5 verified
Raw History Blame Contribute Delete
2.47 kB
// 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');
}