/** * Form Settings Panel JavaScript * * Handles the slide-out panel for form settings management. * * @package Forge12\DoubleOptIn * @since 4.1.0 */ (function($) { 'use strict'; var DoiFormSettings = { /** * Current form ID being edited. */ currentFormId: null, /** * Current form data. */ currentFormData: null, /** * Current integration type. */ currentIntegration: null, /** * Initialize the module. */ init: function() { this.bindEvents(); }, /** * Bind event handlers. */ bindEvents: function() { var self = this; // Tooltip positioning (fixed) $(document).on('mouseenter', '.doi-tooltip', function() { var $tip = $(this).find('.doi-tooltip-text'); var rect = this.getBoundingClientRect(); var tipW = 260; var left = rect.left + rect.width / 2 - tipW / 2; // Keep tooltip within viewport if (left < 8) left = 8; if (left + tipW > window.innerWidth - 8) left = window.innerWidth - tipW - 8; $tip.css({ left: left + 'px', bottom: (window.innerHeight - rect.top + 8) + 'px' }); }); // Toggle switch $(document).on('change', '.doi-toggle-input', function() { self.toggleForm($(this)); }); // Configure button $(document).on('click', '.doi-configure-btn', function() { var formId = $(this).data('form-id'); var formTitle = $(this).data('form-title'); var integration = $(this).data('integration') || ''; self.openPanel(formId, formTitle, integration); }); // Close panel $(document).on('click', '.doi-panel-close, .doi-panel-overlay, #doi-panel-cancel', function() { self.closePanel(); }); // Save settings $(document).on('click', '#doi-panel-save', function() { self.saveSettings(); }); // Escape key to close panel $(document).on('keydown', function(e) { if (e.key === 'Escape' && $('#doi-settings-panel').hasClass('open')) { self.closePanel(); } }); // Placeholder click to insert $(document).on('click', '.doi-placeholders code', function() { var placeholder = $(this).text(); var $body = $('#doi-body'); var cursorPos = $body[0].selectionStart; var text = $body.val(); $body.val(text.substring(0, cursorPos) + placeholder + text.substring(cursorPos)); $body.focus(); $body[0].selectionStart = $body[0].selectionEnd = cursorPos + placeholder.length; }); // Toggle between select and manual input for recipient $(document).on('click', '#doi-toggle-recipient-input', function() { var $select = $('#doi-recipient-select'); var $input = $('#doi-recipient'); if ($select.is(':visible')) { // Switch to manual input $input.val($select.val()).show(); $select.hide(); $(this).find('.dashicons').removeClass('dashicons-edit').addClass('dashicons-list-view'); } else { // Switch to select var currentVal = $input.val(); $select.show(); $input.hide(); // Try to select the current value if ($select.find('option[value="' + currentVal + '"]').length) { $select.val(currentVal); } $(this).find('.dashicons').removeClass('dashicons-list-view').addClass('dashicons-edit'); } }); // Sync select value to hidden input $(document).on('change', '#doi-recipient-select', function() { $('#doi-recipient').val($(this).val()); }); // Template selection change - toggle body editor vs custom template notice $(document).on('change', '#doi-template', function() { self.handleTemplateChange($(this).val()); }); // Unique Email: toggle options visibility $(document).on('change', '#doi-unique-email-enabled', function() { $('#doi-unique-email-options').toggle($(this).is(':checked')); }); // Unique Email: toggle redirect page field based on behavior $(document).on('change', '#doi-unique-email-behavior', function() { $('#doi-unique-email-redirect-field').toggle($(this).val() === 'redirect'); }); }, /** * Handle template selection change. * Shows/hides body editor based on whether a custom template is selected. * * @param {string} templateValue The selected template value. */ handleTemplateChange: function(templateValue) { var isCustomTemplate = templateValue && templateValue.indexOf('custom_') === 0; var $bodyEditor = $('#doi-body-editor'); var $customNotice = $('#doi-custom-template-notice'); var $editBtn = $('#doi-edit-template-btn'); if (isCustomTemplate) { // Custom template selected - hide body editor, show notice $bodyEditor.hide(); $customNotice.show(); // Update edit button link to include template ID var templateId = templateValue.replace('custom_', ''); var editorUrl = doiFormSettings.editorUrl + '&template_id=' + templateId; $editBtn.attr('href', editorUrl); } else { // Standard template - show body editor, hide notice $bodyEditor.show(); $customNotice.hide(); } }, /** * Toggle form enabled state. * * @param {jQuery} $toggle The toggle input element. */ toggleForm: function($toggle) { var self = this; var formId = $toggle.data('form-id'); var $label = $toggle.closest('.doi-toggle'); $label.addClass('loading'); $.ajax({ url: doiFormSettings.ajaxUrl, type: 'POST', data: { action: 'doi_toggle_form', nonce: doiFormSettings.nonce, form_id: formId }, success: function(response) { $label.removeClass('loading'); if (response.success) { self.showToast(response.data.message, 'success'); } else { // Revert toggle $toggle.prop('checked', !$toggle.prop('checked')); self.showToast(response.data.message || doiFormSettings.i18n.error, 'error'); } }, error: function(xhr) { $label.removeClass('loading'); $toggle.prop('checked', !$toggle.prop('checked')); self.showErrorFromResponse(xhr); } }); }, /** * Open the settings panel. * * @param {number} formId The form ID. * @param {string} formTitle The form title. * @param {string} integration The integration type (optional). */ openPanel: function(formId, formTitle, integration) { var self = this; var $panel = $('#doi-settings-panel'); this.currentFormId = formId; this.currentIntegration = integration || ''; // Update title $panel.find('.doi-panel-title').text( doiFormSettings.i18n.configure + ': ' + formTitle ); // Show panel $panel.addClass('open'); $('body').css('overflow', 'hidden'); // Show loading, hide form $panel.find('.doi-panel-loading').show(); $panel.find('.doi-settings-form').hide(); // Load settings $.ajax({ url: doiFormSettings.ajaxUrl, type: 'POST', data: { action: 'doi_get_form_settings', nonce: doiFormSettings.nonce, form_id: formId, integration: integration || '' }, success: function(response) { $panel.find('.doi-panel-loading').hide(); if (response.success) { self.currentFormData = response.data; self.populateForm(response.data); $panel.find('.doi-settings-form').show(); } else { self.showToast(response.data.message || doiFormSettings.i18n.error, 'error'); self.closePanel(); } }, error: function(xhr) { self.showErrorFromResponse(xhr); self.closePanel(); } }); }, /** * Close the settings panel. */ closePanel: function() { var $panel = $('#doi-settings-panel'); $panel.removeClass('open'); $('body').css('overflow', ''); this.currentFormId = null; this.currentFormData = null; this.currentIntegration = null; }, /** * Populate the form with data. * * @param {Object} data The form data. */ populateForm: function(data) { var settings = data.settings || {}; var fields = data.fields || {}; var templates = data.templates || {}; var categories = data.categories || {}; var pages = data.pages || {}; // Form ID $('#doi-form-id').val(data.id); // Handle Enable/Disable field based on integration type var isElementor = this.currentIntegration === 'elementor'; if (isElementor) { // For Elementor: show status badge instead of checkbox $('#doi-enable-field').hide(); $('#doi-status-field').show(); // Show appropriate badge based on enabled state if (settings.enabled) { $('#doi-status-active').show(); $('#doi-status-inactive').hide(); } else { $('#doi-status-active').hide(); $('#doi-status-inactive').show(); } } else { // For other integrations: show checkbox $('#doi-enable-field').show(); $('#doi-status-field').hide(); $('#doi-enabled').prop('checked', settings.enabled); } // Populate categories select var $category = $('#doi-category'); $category.empty(); $.each(categories, function(id, name) { $category.append($('