| 1 |
(function($) { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
$(function() { |
| 5 |
// Initialize modern animations and interactions |
| 6 |
initModernAnimations(); |
| 7 |
|
| 8 |
// Auto-save feedback for AI settings form specifically |
| 9 |
$('.king-addons-settings-form').on('submit', function(e) { |
| 10 |
// Ensure the form is not prevented from submitting |
| 11 |
showSaveAnimation(); |
| 12 |
|
| 13 |
// Explicitly allow form submission |
| 14 |
return true; |
| 15 |
}); |
| 16 |
|
| 17 |
// Input focus animations |
| 18 |
$('input, select, textarea').on('focus', function() { |
| 19 |
$(this).closest('tr').addClass('focused'); |
| 20 |
}).on('blur', function() { |
| 21 |
$(this).closest('tr').removeClass('focused'); |
| 22 |
}); |
| 23 |
|
| 24 |
// Checkbox animations |
| 25 |
$('input[type="checkbox"]').on('change', function() { |
| 26 |
if ($(this).is(':checked')) { |
| 27 |
$(this).addClass('checked-animation'); |
| 28 |
setTimeout(() => $(this).removeClass('checked-animation'), 300); |
| 29 |
} |
| 30 |
}); |
| 31 |
|
| 32 |
// Initialize existing functionality |
| 33 |
var $refreshButton = $('#king-addons-ai-refresh-models-button'); |
| 34 |
var $spinner = $('#king-addons-ai-refresh-models-spinner'); |
| 35 |
var $statusSpan = $('#king-addons-ai-refresh-models-status'); |
| 36 |
var $modelSelect = $('select[name="king_addons_ai_options[openai_model]"]'); |
| 37 |
|
| 38 |
$refreshButton.on('click', function() { |
| 39 |
if ($refreshButton.prop('disabled')) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
// Disable button and show spinner |
| 44 |
$refreshButton.prop('disabled', true); |
| 45 |
$spinner.css({ visibility: 'visible', display: 'inline-block' }).addClass('is-active'); |
| 46 |
$statusSpan.text(KingAddonsAiSettings.refreshing_text).css('color', ''); |
| 47 |
$modelSelect.prop('disabled', true); |
| 48 |
|
| 49 |
// AJAX request to refresh models |
| 50 |
$.ajax({ |
| 51 |
url: KingAddonsAiSettings.ajax_url, |
| 52 |
type: 'POST', |
| 53 |
data: { |
| 54 |
action: 'king_addons_ai_refresh_models', |
| 55 |
nonce: KingAddonsAiSettings.nonce |
| 56 |
}, |
| 57 |
dataType: 'json' |
| 58 |
}).done(function(response) { |
| 59 |
if (response.success && response.data.models) { |
| 60 |
var currentValue = $modelSelect.val(); |
| 61 |
$modelSelect.empty(); |
| 62 |
|
| 63 |
$.each(response.data.models, function(modelId, modelLabel) { |
| 64 |
var $option = $('<option></option>').val(modelId).text(modelLabel); |
| 65 |
if (modelId === currentValue) { |
| 66 |
$option.prop('selected', true); |
| 67 |
} |
| 68 |
$modelSelect.append($option); |
| 69 |
}); |
| 70 |
|
| 71 |
$statusSpan.text(KingAddonsAiSettings.refreshed_text).css('color', 'green'); |
| 72 |
setTimeout(function() { |
| 73 |
$statusSpan.text(''); |
| 74 |
}, 3000); |
| 75 |
} else { |
| 76 |
var message = response.data && response.data.message ? response.data.message : KingAddonsAiSettings.error_text; |
| 77 |
$statusSpan.text(message).css('color', 'red'); |
| 78 |
} |
| 79 |
}).fail(function(jqXHR) { |
| 80 |
var message = KingAddonsAiSettings.error_text; |
| 81 |
try { |
| 82 |
var errorResponse = JSON.parse(jqXHR.responseText); |
| 83 |
if (errorResponse.data && errorResponse.data.message) { |
| 84 |
message = errorResponse.data.message; |
| 85 |
} |
| 86 |
} catch (e) { |
| 87 |
// ignore JSON parse errors |
| 88 |
} |
| 89 |
$statusSpan.text(message).css('color', 'red'); |
| 90 |
}).always(function() { |
| 91 |
// Re-enable button and hide spinner |
| 92 |
$spinner.css({ visibility: 'hidden', display: 'none' }).removeClass('is-active'); |
| 93 |
$refreshButton.prop('disabled', false); |
| 94 |
$modelSelect.prop('disabled', false); |
| 95 |
}); |
| 96 |
}); |
| 97 |
|
| 98 |
// Modern animation functions |
| 99 |
function initModernAnimations() { |
| 100 |
// Stagger animation for form sections |
| 101 |
$('.king-addons-ai-settings h2').each(function(index) { |
| 102 |
$(this).css({ |
| 103 |
'animation-delay': (index * 0.1) + 's', |
| 104 |
'animation-fill-mode': 'forwards' |
| 105 |
}).addClass('slide-in-from-left'); |
| 106 |
}); |
| 107 |
|
| 108 |
// Stagger animation for form rows |
| 109 |
$('.king-addons-ai-settings .form-table tr').each(function(index) { |
| 110 |
$(this).css({ |
| 111 |
'animation-delay': (index * 0.05) + 's', |
| 112 |
'animation-fill-mode': 'forwards' |
| 113 |
}).addClass('fade-in-up'); |
| 114 |
}); |
| 115 |
|
| 116 |
// Header icon rotation on hover |
| 117 |
$('.king-addons-settings-header-icon').on('mouseenter', function() { |
| 118 |
$(this).css('transform', 'rotate(10deg) scale(1.05)'); |
| 119 |
}).on('mouseleave', function() { |
| 120 |
$(this).css('transform', 'rotate(0deg) scale(1)'); |
| 121 |
}); |
| 122 |
} |
| 123 |
|
| 124 |
function showSaveAnimation() { |
| 125 |
var $saveButton = $('.king-addons-save-button'); |
| 126 |
|
| 127 |
// Only show animation if button exists and is not already disabled |
| 128 |
if ($saveButton.length && !$saveButton.prop('disabled')) { |
| 129 |
// Show saving state immediately |
| 130 |
$saveButton.text('Saving...') |
| 131 |
.prop('disabled', true) |
| 132 |
.css({ |
| 133 |
'opacity': '0.8', |
| 134 |
'transform': 'scale(0.98)' |
| 135 |
}); |
| 136 |
} |
| 137 |
|
| 138 |
// No need for timeouts since page will reload on successful save |
| 139 |
} |
| 140 |
}); |
| 141 |
})(jQuery); |