File size: 6,385 Bytes
131bbbf | 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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | // SafeHost Pro - Global JavaScript
class SafeHostApp {
constructor() {
this.init();
}
init() {
console.log('SafeHost Pro initialized');
this.setupEventListeners();
this.checkAuthStatus();
this.loadUserPreferences();
}
setupEventListeners() {
// Mobile menu toggle
const mobileMenuBtn = document.getElementById('mobile-menu-btn');
const mobileMenu = document.getElementById('mobile-menu');
if (mobileMenuBtn && mobileMenu) {
mobileMenuBtn.addEventListener('click', () => {
mobileMenu.classList.toggle('hidden');
});
}
// Theme switcher
const themeToggle = document.getElementById('theme-toggle');
if (themeToggle) {
themeToggle.addEventListener('click', this.toggleTheme.bind(this));
}
// Smooth scrolling for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
}
checkAuthStatus() {
// Check if user is logged in (placeholder)
const token = localStorage.getItem('authToken');
if (token) {
this.updateUIForLoggedIn();
}
}
updateUIForLoggedIn() {
// Update navigation and UI for authenticated users
const authElements = document.querySelectorAll('[data-auth]');
authElements.forEach(el => {
const action = el.getAttribute('data-auth');
if (action === 'hide-if-authenticated') {
el.style.display = 'none';
} else if (action === 'show-if-authenticated') {
el.style.display = 'block';
}
});
}
toggleTheme() {
const html = document.documentElement;
const currentTheme = html.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
html.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
this.updateThemeIcon(newTheme);
}
updateThemeIcon(theme) {
const themeIcon = document.getElementById('theme-icon');
if (themeIcon) {
themeIcon.setAttribute('data-feather', theme === 'dark' ? 'sun' : 'moon');
feather.replace();
}
}
loadUserPreferences() {
// Load saved theme preference
const savedTheme = localStorage.getItem('theme') || 'light';
document.documentElement.setAttribute('data-theme', savedTheme);
this.updateThemeIcon(savedTheme);
// Load other user preferences
const preferences = JSON.parse(localStorage.getItem('userPreferences') || '{}');
this.applyPreferences(preferences);
}
applyPreferences(preferences) {
// Apply user preferences to UI
if (preferences.animationsDisabled) {
document.body.classList.add('no-animations');
}
}
// API helper methods
async apiCall(endpoint, options = {}) {
const baseURL = 'https://api.safehost.pro';
const token = localStorage.getItem('authToken');
const defaultOptions = {
headers: {
'Content-Type': 'application/json',
'Authorization': token ? `Bearer ${token}` : ''
}
};
try {
const response = await fetch(`${baseURL}${endpoint}`, {
...defaultOptions,
...options
});
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('API call failed:', error);
this.showNotification('Connection error. Please try again.', 'error');
throw error;
}
}
showNotification(message, type = 'info') {
// Create and show notification toast
const toast = document.createElement('div');
toast.className = `fixed top-4 right-4 px-6 py-3 rounded-lg shadow-lg text-white ${
type === 'error' ? 'bg-red-500' :
type === 'success' ? 'bg-green-500' : 'bg-blue-500'
} z-50`;
toast.textContent = message;
document.body.appendChild(toast);
setTimeout(() => {
toast.remove();
}, 3000);
}
// File upload helper
async uploadFile(file, onProgress = null) {
const formData = new FormData();
formData.append('file', file);
try {
const response = await fetch('/api/upload', {
method: 'POST',
body: formData,
onUploadProgress: onProgress
});
return await response.json();
} catch (error) {
console.error('Upload failed:', error);
throw error;
}
}
}
// Initialize app when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
window.safeHostApp = new SafeHostApp();
});
// Utility functions
const utils = {
formatBytes(bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i];
},
formatDate(date) {
return new Date(date).toLocaleDateString('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
});
},
debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
}; |