/** * HVAC Profile Sharing JavaScript * * @package HVAC_Community_Events * @version 1.0.0 */ (function($) { 'use strict'; /** * Profile Sharing Handler */ var ProfileSharing = { /** * Initialize the profile sharing system */ init: function() { this.bindEvents(); }, /** * Bind event handlers */ bindEvents: function() { // Share Profile button click $(document).on('click', '.hvac-share-profile-btn', this.openShareModal); // Modal close events $(document).on('click', '.hvac-modal-close', this.closeShareModal); $(document).on('click', '.hvac-share-modal', function(e) { if (e.target === this) { ProfileSharing.closeShareModal(); } }); // Copy URL button $(document).on('click', '.hvac-copy-url-btn', this.copyShareUrl); // Escape key to close modal $(document).on('keydown', function(e) { if (e.keyCode === 27 && $('.hvac-share-modal:visible').length) { ProfileSharing.closeShareModal(); } }); }, /** * Open the share profile modal */ openShareModal: function(e) { e.preventDefault(); var $button = $(this); var profileId = $button.data('profile-id'); if (!profileId) { console.error('No profile ID found'); return; } // Show the modal var $modal = $('#hvac-share-profile-modal'); $modal.fadeIn(300); // Prevent body scroll $('body').addClass('modal-open').css('overflow', 'hidden'); // Load the share data ProfileSharing.loadShareData(profileId); }, /** * Close the share profile modal */ closeShareModal: function(e) { if (e) { e.preventDefault(); } var $modal = $('#hvac-share-profile-modal'); $modal.fadeOut(300); // Restore body scroll $('body').removeClass('modal-open').css('overflow', ''); // Reset modal content setTimeout(function() { ProfileSharing.resetModal(); }, 300); }, /** * Load share data via AJAX */ loadShareData: function(profileId) { var $urlInput = $('#hvac-share-url'); var $cardContainer = $('#hvac-share-card-container'); // Reset loading states $urlInput.val('Loading...'); $cardContainer.removeClass('loaded').html( '
' ); // Make AJAX request $.ajax({ url: hvac_sharing.ajax_url, type: 'POST', data: { action: 'hvac_get_profile_share_data', profile_id: profileId, nonce: hvac_sharing.nonce }, success: function(response) { if (response.success && response.data) { ProfileSharing.populateShareData(response.data); } else { ProfileSharing.showError(response.data ? response.data.message : 'Unknown error occurred'); } }, error: function(xhr, status, error) { console.error('AJAX Error:', error); ProfileSharing.showError('Network error occurred. Please try again.'); } }); }, /** * Populate the modal with share data */ populateShareData: function(data) { var $urlInput = $('#hvac-share-url'); var $cardContainer = $('#hvac-share-card-container'); // Set the share URL $urlInput.val(data.share_url || ''); // Create the profile card HTML var cardHtml = ProfileSharing.createProfileCardHtml(data); // Update the card container $cardContainer.addClass('loaded').html(cardHtml); }, /** * Create profile card HTML */ createProfileCardHtml: function(data) { var avatarHtml = ''; if (data.profile_image) { avatarHtml = '