Files
roi-theme/admin/assets/js/theme-options.js
2025-11-13 21:51:06 -06:00

441 lines
15 KiB
JavaScript

/**
* Theme Options Admin JavaScript
*
* @package Apus_Theme
* @since 1.0.0
*/
(function($) {
'use strict';
var ApusThemeOptions = {
/**
* Initialize
*/
init: function() {
this.tabs();
this.imageUpload();
this.resetOptions();
this.exportOptions();
this.importOptions();
this.formValidation();
this.conditionalFields();
},
/**
* Tab Navigation
*/
tabs: function() {
// Tab click handler
$('.apus-tabs-nav a').on('click', function(e) {
e.preventDefault();
var tabId = $(this).attr('href');
// Update active states
$('.apus-tabs-nav li').removeClass('active');
$(this).parent().addClass('active');
// Show/hide tab content
$('.apus-tab-pane').removeClass('active');
$(tabId).addClass('active');
// Update URL hash without scrolling
if (history.pushState) {
history.pushState(null, null, tabId);
} else {
window.location.hash = tabId;
}
});
// Load tab from URL hash on page load
if (window.location.hash) {
var hash = window.location.hash;
if ($(hash).length) {
$('.apus-tabs-nav a[href="' + hash + '"]').trigger('click');
}
}
// Handle browser back/forward buttons
$(window).on('hashchange', function() {
if (window.location.hash) {
$('.apus-tabs-nav a[href="' + window.location.hash + '"]').trigger('click');
}
});
},
/**
* Image Upload
*/
imageUpload: function() {
var self = this;
var mediaUploader;
// Upload button click
$(document).on('click', '.apus-upload-image', function(e) {
e.preventDefault();
var button = $(this);
var container = button.closest('.apus-image-upload');
var preview = container.find('.apus-image-preview');
var input = container.find('.apus-image-id');
var removeBtn = container.find('.apus-remove-image');
// If the media uploader already exists, reopen it
if (mediaUploader) {
mediaUploader.open();
return;
}
// Create new media uploader
mediaUploader = wp.media({
title: apusAdminOptions.strings.selectImage,
button: {
text: apusAdminOptions.strings.useImage
},
multiple: false
});
// When an image is selected
mediaUploader.on('select', function() {
var attachment = mediaUploader.state().get('selection').first().toJSON();
// Set image ID
input.val(attachment.id);
// Show preview
var imgUrl = attachment.sizes && attachment.sizes.medium ?
attachment.sizes.medium.url : attachment.url;
preview.html('<img src="' + imgUrl + '" class="apus-preview-image" />');
// Show remove button
removeBtn.show();
});
// Open the uploader
mediaUploader.open();
});
// Remove button click
$(document).on('click', '.apus-remove-image', function(e) {
e.preventDefault();
var button = $(this);
var container = button.closest('.apus-image-upload');
var preview = container.find('.apus-image-preview');
var input = container.find('.apus-image-id');
// Clear values
input.val('');
preview.empty();
button.hide();
});
},
/**
* Reset Options
*/
resetOptions: function() {
$('#apus-reset-options').on('click', function(e) {
e.preventDefault();
if (!confirm(apusAdminOptions.strings.confirmReset)) {
return;
}
var button = $(this);
button.prop('disabled', true).addClass('updating-message');
$.ajax({
url: apusAdminOptions.ajaxUrl,
type: 'POST',
data: {
action: 'apus_reset_options',
nonce: apusAdminOptions.nonce
},
success: function(response) {
if (response.success) {
// Show success message
ApusThemeOptions.showNotice('success', response.data.message);
// Reload page after 1 second
setTimeout(function() {
window.location.reload();
}, 1000);
} else {
ApusThemeOptions.showNotice('error', response.data.message);
button.prop('disabled', false).removeClass('updating-message');
}
},
error: function() {
ApusThemeOptions.showNotice('error', apusAdminOptions.strings.error);
button.prop('disabled', false).removeClass('updating-message');
}
});
});
},
/**
* Export Options
*/
exportOptions: function() {
$('#apus-export-options').on('click', function(e) {
e.preventDefault();
var button = $(this);
button.prop('disabled', true).addClass('updating-message');
$.ajax({
url: apusAdminOptions.ajaxUrl,
type: 'POST',
data: {
action: 'apus_export_options',
nonce: apusAdminOptions.nonce
},
success: function(response) {
if (response.success) {
// Create download link
var blob = new Blob([response.data.data], { type: 'application/json' });
var url = window.URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = response.data.filename;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
ApusThemeOptions.showNotice('success', 'Options exported successfully!');
} else {
ApusThemeOptions.showNotice('error', response.data.message);
}
button.prop('disabled', false).removeClass('updating-message');
},
error: function() {
ApusThemeOptions.showNotice('error', apusAdminOptions.strings.error);
button.prop('disabled', false).removeClass('updating-message');
}
});
});
},
/**
* Import Options
*/
importOptions: function() {
var modal = $('#apus-import-modal');
var importData = $('#apus-import-data');
// Show modal
$('#apus-import-options').on('click', function(e) {
e.preventDefault();
modal.show();
});
// Close modal
$('.apus-modal-close, #apus-import-cancel').on('click', function() {
modal.hide();
importData.val('');
});
// Close modal on outside click
$(window).on('click', function(e) {
if ($(e.target).is(modal)) {
modal.hide();
importData.val('');
}
});
// Submit import
$('#apus-import-submit').on('click', function(e) {
e.preventDefault();
var data = importData.val().trim();
if (!data) {
alert('Please paste your import data.');
return;
}
var button = $(this);
button.prop('disabled', true).addClass('updating-message');
$.ajax({
url: apusAdminOptions.ajaxUrl,
type: 'POST',
data: {
action: 'apus_import_options',
nonce: apusAdminOptions.nonce,
import_data: data
},
success: function(response) {
if (response.success) {
ApusThemeOptions.showNotice('success', response.data.message);
modal.hide();
importData.val('');
// Reload page after 1 second
setTimeout(function() {
window.location.reload();
}, 1000);
} else {
ApusThemeOptions.showNotice('error', response.data.message);
button.prop('disabled', false).removeClass('updating-message');
}
},
error: function() {
ApusThemeOptions.showNotice('error', apusAdminOptions.strings.error);
button.prop('disabled', false).removeClass('updating-message');
}
});
});
},
/**
* Form Validation
*/
formValidation: function() {
$('.apus-options-form').on('submit', function(e) {
var valid = true;
var firstError = null;
// Validate required fields
$(this).find('[required]').each(function() {
if (!$(this).val()) {
valid = false;
$(this).addClass('error');
if (!firstError) {
firstError = $(this);
}
} else {
$(this).removeClass('error');
}
});
// Validate number fields
$(this).find('input[type="number"]').each(function() {
var val = $(this).val();
var min = $(this).attr('min');
var max = $(this).attr('max');
if (val && min && parseInt(val) < parseInt(min)) {
valid = false;
$(this).addClass('error');
if (!firstError) {
firstError = $(this);
}
}
if (val && max && parseInt(val) > parseInt(max)) {
valid = false;
$(this).addClass('error');
if (!firstError) {
firstError = $(this);
}
}
});
// Validate URL fields
$(this).find('input[type="url"]').each(function() {
var val = $(this).val();
if (val && !ApusThemeOptions.isValidUrl(val)) {
valid = false;
$(this).addClass('error');
if (!firstError) {
firstError = $(this);
}
}
});
if (!valid) {
e.preventDefault();
if (firstError) {
// Scroll to first error
$('html, body').animate({
scrollTop: firstError.offset().top - 100
}, 500);
firstError.focus();
}
ApusThemeOptions.showNotice('error', 'Please fix the errors in the form.');
return false;
}
// Add saving animation
$(this).find('.submit .button-primary').addClass('updating-message');
});
// Remove error class on input
$('.apus-options-form input, .apus-options-form select, .apus-options-form textarea').on('change input', function() {
$(this).removeClass('error');
});
},
/**
* Conditional Fields
*/
conditionalFields: function() {
// Enable/disable related posts options based on checkbox
$('#enable_related_posts').on('change', function() {
var checked = $(this).is(':checked');
var fields = $('#related_posts_count, #related_posts_taxonomy, #related_posts_title, #related_posts_columns');
fields.closest('tr').toggleClass('apus-field-dependency', !checked);
fields.prop('disabled', !checked);
}).trigger('change');
// Enable/disable breadcrumb separator based on breadcrumbs checkbox
$('#enable_breadcrumbs').on('change', function() {
var checked = $(this).is(':checked');
var field = $('#breadcrumb_separator');
field.closest('tr').toggleClass('apus-field-dependency', !checked);
field.prop('disabled', !checked);
}).trigger('change');
},
/**
* Show Notice
*/
showNotice: function(type, message) {
var notice = $('<div class="notice notice-' + type + ' is-dismissible"><p>' + message + '</p></div>');
$('.apus-theme-options h1').after(notice);
// Auto-dismiss after 5 seconds
setTimeout(function() {
notice.fadeOut(function() {
$(this).remove();
});
}, 5000);
// Scroll to top
$('html, body').animate({ scrollTop: 0 }, 300);
},
/**
* Validate URL
*/
isValidUrl: function(url) {
try {
new URL(url);
return true;
} catch (e) {
return false;
}
}
};
// Initialize on document ready
$(document).ready(function() {
ApusThemeOptions.init();
});
// Make it globally accessible
window.ApusThemeOptions = ApusThemeOptions;
})(jQuery);