/** * HVAC Announcements Admin JavaScript * * @package HVAC_Community_Events */ jQuery(document).ready(function($) { 'use strict'; // State variables let currentPage = 1; let totalPages = 1; let currentStatus = 'any'; let searchTerm = ''; let editorInstance = null; // Initialize init(); /** * Initialize the announcements interface */ function init() { loadAnnouncements(); loadCategories(); initializeEventHandlers(); initializeEditor(); } /** * Initialize TinyMCE editor */ function initializeEditor() { if (typeof wp !== 'undefined' && wp.editor) { // Initialize WordPress editor wp.editor.initialize('announcement-content', { tinymce: { wpautop: true, plugins: 'lists link image media paste', toolbar1: 'formatselect | bold italic | alignleft aligncenter alignright | bullist numlist | link unlink | wp_adv', toolbar2: 'strikethrough hr forecolor pastetext removeformat charmap outdent indent undo redo wp_help', height: 300 }, quicktags: true, mediaButtons: true }); } } /** * Initialize event handlers */ function initializeEventHandlers() { // Add announcement button $('#add-announcement-btn').on('click', function() { openModal(); }); // Modal close buttons $('.modal-close, .modal-cancel').on('click', function() { closeModal(); }); // Form submission $('#announcement-form').on('submit', function(e) { e.preventDefault(); saveAnnouncement(); }); // Status filter $('#status-filter').on('change', function() { currentStatus = $(this).val(); currentPage = 1; loadAnnouncements(); }); // Search $('#search-btn').on('click', function() { searchTerm = $('#announcement-search').val(); currentPage = 1; loadAnnouncements(); }); $('#announcement-search').on('keypress', function(e) { if (e.which === 13) { searchTerm = $(this).val(); currentPage = 1; loadAnnouncements(); } }); // Pagination $('#prev-page').on('click', function() { if (currentPage > 1) { currentPage--; loadAnnouncements(); } }); $('#next-page').on('click', function() { if (currentPage < totalPages) { currentPage++; loadAnnouncements(); } }); // Edit/Delete actions (delegated) $(document).on('click', '.edit-announcement', function() { const id = $(this).data('id'); editAnnouncement(id); }); $(document).on('click', '.delete-announcement', function() { const id = $(this).data('id'); if (confirm(hvac_announcements.strings.confirm_delete)) { deleteAnnouncement(id); } }); // Featured image selection $('#select-featured-image').on('click', function(e) { e.preventDefault(); selectFeaturedImage(); }); $('#remove-featured-image').on('click', function(e) { e.preventDefault(); removeFeaturedImage(); }); } /** * Load announcements via AJAX */ function loadAnnouncements() { const data = { action: 'hvac_get_announcements', nonce: hvac_announcements.nonce, page: currentPage, per_page: 20, status: currentStatus, search: searchTerm }; $.post(hvac_announcements.ajax_url, data, function(response) { if (response.success) { displayAnnouncements(response.data.announcements); updatePagination(response.data.current_page, response.data.pages); } else { showError(response.data || hvac_announcements.strings.error_loading); } }); } /** * Display announcements in table */ function displayAnnouncements(announcements) { const tbody = $('#announcements-list'); tbody.empty(); if (announcements.length === 0) { tbody.append('No announcements found'); return; } announcements.forEach(function(announcement) { const row = $(''); // Title row.append('' + escapeHtml(announcement.title) + ''); // Status const statusClass = 'status-' + announcement.status; row.append('' + announcement.status + ''); // Categories const categories = announcement.categories.join(', ') || '-'; row.append('' + escapeHtml(categories) + ''); // Author row.append('' + escapeHtml(announcement.author) + ''); // Date row.append('' + announcement.date + ''); // Actions let actions = ''; if (announcement.can_edit) { actions += ' '; } if (announcement.can_delete) { actions += ''; } actions += ''; row.append(actions); tbody.append(row); }); } /** * Update pagination controls */ function updatePagination(current, total) { currentPage = current; totalPages = total; $('#current-page').text(current); $('#total-pages').text(total); $('#prev-page').prop('disabled', current <= 1); $('#next-page').prop('disabled', current >= total); } /** * Load categories for the form */ function loadCategories() { $.post(hvac_announcements.ajax_url, { action: 'hvac_get_announcement_categories', nonce: hvac_announcements.nonce }, function(response) { if (response.success) { displayCategories(response.data); } }); } /** * Display categories as checkboxes */ function displayCategories(categories) { const container = $('#categories-container'); container.empty(); if (categories.length === 0) { container.append('

No categories available

'); return; } categories.forEach(function(category) { const checkbox = $('