Spaces:
Running
Running
| // Global application state | |
| const AppState = { | |
| currentUser: null, | |
| posts: [], | |
| scheduledPosts: [], | |
| analytics: {}, | |
| settings: {} | |
| }; | |
| // Platform configurations | |
| const PLATFORMS = { | |
| telegram: { | |
| name: 'Telegram', | |
| icon: 'message-circle', | |
| color: '#0088cc', | |
| maxLength: 4096, | |
| supportsMedia: true, | |
| supportsLinks: true | |
| }, | |
| vk: { | |
| name: 'VK', | |
| icon: 'users', | |
| color: '#4a76a8', | |
| maxLength: 4096, | |
| supportsMedia: true, | |
| supportsLinks: true | |
| }, | |
| linkedin: { | |
| name: 'LinkedIn', | |
| icon: 'briefcase', | |
| color: '#0077b5', | |
| maxLength: 3000, | |
| supportsMedia: true, | |
| supportsLinks: true | |
| }, | |
| yandex_zen: { | |
| name: 'Яндекс.Дзен', | |
| icon: 'trending-up', | |
| color: '#ff0000', | |
| maxLength: 5000, | |
| supportsMedia: true, | |
| supportsLinks: true | |
| } | |
| }; | |
| // Initialize application | |
| document.addEventListener('DOMContentLoaded', function() { | |
| initializeApp(); | |
| setupEventListeners(); | |
| loadInitialData(); | |
| }); | |
| function initializeApp() { | |
| // Check for saved user preferences | |
| const savedTheme = localStorage.getItem('theme') || 'light'; | |
| setTheme(savedTheme); | |
| // Initialize platform connections | |
| initializePlatformConnections(); | |
| } | |
| function setupEventListeners() { | |
| // Theme toggle | |
| const themeToggle = document.getElementById('theme-toggle'); | |
| if (themeToggle) { | |
| themeToggle.addEventListener('click', toggleTheme); | |
| } | |
| // Platform selection toggles | |
| document.querySelectorAll('input[type="checkbox"][name="platform"]').forEach(checkbox => { | |
| checkbox.addEventListener('change', updatePlatformSelection); | |
| }); | |
| // Post character counters | |
| document.querySelectorAll('.post-content').forEach(textarea => { | |
| textarea.addEventListener('input', updateCharacterCount); | |
| }); | |
| } | |
| function loadInitialData() { | |
| // Load posts from localStorage or API | |
| const savedPosts = localStorage.getItem('userPosts'); | |
| if (savedPosts) { | |
| AppState.posts = JSON.parse(savedPosts); | |
| renderRecentPosts(); | |
| } | |
| // Load scheduled posts | |
| const savedScheduled = localStorage.getItem('scheduledPosts'); | |
| if (savedScheduled) { | |
| AppState.scheduledPosts = JSON.parse(savedScheduled); | |
| renderScheduledPosts(); | |
| } | |
| } | |
| // Theme management | |
| function setTheme(theme) { | |
| document.documentElement.setAttribute('data-theme', theme); | |
| localStorage.setItem('theme', theme); | |
| } | |
| function toggleTheme() { | |
| const currentTheme = document.documentElement.getAttribute('data-theme') || 'light'; | |
| const newTheme = currentTheme === 'light' ? 'dark' : 'light'; | |
| setTheme(newTheme); | |
| } | |
| // Platform management | |
| function initializePlatformConnections() { | |
| // Check which platforms are connected | |
| const connectedPlatforms = JSON.parse(localStorage.getItem('connectedPlatforms') || '{}'); | |
| Object.keys(PLATFORMS).forEach(platform => { | |
| const isConnected = connectedPlatforms[platform]; | |
| if (isConnected) { | |
| console.log(`${PLATFORMS[platform].name} подключен`); | |
| updatePlatformUI(platform, true); | |
| }); | |
| } | |
| function updatePlatformSelection(event) { | |
| const platform = event.target.value; | |
| const isSelected = event.target.checked; | |
| if (isSelected && !isPlatformConnected(platform)) { | |
| // Show connection modal | |
| showPlatformConnectionModal(platform); | |
| } | |
| } | |
| function isPlatformConnected(platform) { | |
| const connectedPlatforms = JSON.parse(localStorage.getItem('connectedPlatforms') || '{}'); | |
| return !!connectedPlatforms[platform]; | |
| } | |
| function showPlatformConnectionModal(platform) { | |
| // Implementation for platform connection modal | |
| const platformConfig = PLATFORMS[platform]; | |
| alert(`Для публикации в ${platformConfig.name} необходимо подключить аккаунт. Перейдите в настройки для подключения.`); | |
| } | |
| // Post management | |
| function createPost(postData) { | |
| const newPost = { | |
| id: generateId(), | |
| ...postData, | |
| createdAt: new Date().toISOString(), | |
| status: postData.scheduleFor ? 'scheduled' : 'draft' | |
| }; | |
| AppState.posts.unshift(newPost); | |
| savePostsToStorage(); | |
| if (postData.scheduleFor) { | |
| schedulePost(newPost); | |
| } | |
| return newPost; | |
| } | |
| function schedulePost(post) { | |
| AppState.scheduledPosts.push(post); | |
| saveScheduledPostsToStorage(); | |
| // Schedule the actual posting (simplified) | |
| const scheduleTime = new Date(post.scheduleFor).getTime(); | |
| const now = Date.now(); | |
| if (scheduleTime > now) { | |
| setTimeout(() => { | |
| publishPost(post); | |
| }, scheduleTime - now); | |
| } | |
| } | |
| function publishPost(post) { | |
| // Simulate publishing to selected platforms | |
| const selectedPlatforms = post.platforms || []; | |
| selectedPlatforms.forEach(platform => { | |
| console.log(`Публикация в ${PLATFORMS[platform].name}: ${post.content.substring(0, 50)}...`); | |
| // Update post status | |
| post.status = 'published'; | |
| post.publishedAt = new Date().toISOString(); | |
| // Move from scheduled to published | |
| const scheduledIndex = AppState.scheduledPosts.findIndex(p => p.id === post.id); | |
| if (scheduledIndex > -1) { | |
| AppState.scheduledPosts.splice(scheduledIndex, 1); | |
| saveScheduledPostsToStorage(); | |
| }); | |
| savePostsToStorage(); | |
| renderRecentPosts(); | |
| renderScheduledPosts(); | |
| } | |
| // Analytics functions | |
| function calculateEngagementRate(views, interactions) { | |
| return views > 0 ? (interactions / views * 100).toFixed(1) : 0; | |
| } | |
| function getPlatformPerformance(platform) { | |
| // Mock data - replace with actual API call | |
| return { | |
| reach: Math.floor(Math.random() * 10000), | |
| engagement: (Math.random() * 10).toFixed(1), | |
| growth: (Math.random() * 20 - 5).toFixed(1) | |
| }; | |
| } | |
| // Utility functions | |
| function generateId() { | |
| return Date.now().toString(36) + Math.random().toString(36).substr(2); | |
| } | |
| function formatDate(dateString) { | |
| const date = new Date(dateString); | |
| return date.toLocaleDateString('ru-RU', { | |
| day: 'numeric', | |
| month: 'short', | |
| hour: '2-digit', | |
| minute: '2-digit' | |
| }); | |
| } | |
| function savePostsToStorage() { | |
| localStorage.setItem('userPosts', JSON.stringify(AppState.posts)); | |
| } | |
| function saveScheduledPostsToStorage() { | |
| localStorage.setItem('scheduledPosts', JSON.stringify(AppState.scheduledPosts)); | |
| } | |
| // Render functions | |
| function renderRecentPosts() { | |
| const container = document.querySelector('.recent-posts-container'); | |
| if (!container) return; | |
| // Implementation for rendering recent posts | |
| } | |
| function renderScheduledPosts() { | |
| const container = document.querySelector('.scheduled-posts-container'); | |
| if (!container) return; | |
| // Implementation for rendering scheduled posts | |
| } | |
| function updateCharacterCount(event) { | |
| const textarea = event.target; | |
| const counter = textarea.nextElementSibling; | |
| if (counter && counter.classList.contains('char-counter')) { | |
| const count = textarea.value.length; | |
| const maxLength = textarea.getAttribute('maxlength') || 4096; | |
| counter.textContent = `${count}/${maxLength}`; | |
| if (count > maxLength * 0.9) { | |
| counter.classList.add('text-red-500'); | |
| } else { | |
| counter.classList.remove('text-red-500'); | |
| } | |
| } | |
| } | |
| // Export for use in other modules | |
| window.AppState = AppState; | |
| window.PLATFORMS = PLATFORMS; |