Current File : /home/bqrcodec/contact.ancuong.com/wp-content/plugins/my-pro-id-generator//myproid_admin.js
jQuery(document).ready(function($) {
// --- Taxonomy and Term Selection ---
var taxonomySelect = $('#myproid_taxonomy');
var termSelect = $('#myproid_term');
var termLoader = $('#myproid_term_loader');
taxonomySelect.on('change', function() {
var selectedTaxonomy = $(this).val();
termSelect.prop('disabled', true).html('<option value="">' + MyProIdAjax.loadingText + '</option>'); // Disable and show loading
if (!selectedTaxonomy) {
termSelect.html('<option value="">-- Select Taxonomy First --</option>');
return;
}
termLoader.show(); // Show spinner/text
$.ajax({
url: MyProIdAjax.ajaxurl,
type: 'POST',
data: {
action: 'myproid_get_terms', // Matches PHP AJAX action hook
_ajax_nonce: MyProIdAjax.getTermsNonce, // Use the localized nonce
taxonomy: selectedTaxonomy
},
success: function(response) {
termSelect.prop('disabled', false).empty(); // Re-enable and clear
if (response.success && response.data.length > 0) {
termSelect.append('<option value="">-- Select Term --</option>');
$.each(response.data, function(index, term) {
termSelect.append($('<option>', {
value: term.id, // Use term ID as the value
text: term.text
}));
});
} else if (response.success) {
termSelect.append('<option value="">-- No terms found --</option>');
}
else {
termSelect.append('<option value="">-- Error loading terms --</option>');
console.error("Error fetching terms:", response.data || 'Unknown error');
}
},
error: function(jqXHR, textStatus, errorThrown) {
termSelect.prop('disabled', false).html('<option value="">-- Error loading terms --</option>');
console.error("AJAX Error:", textStatus, errorThrown);
},
complete: function() {
termLoader.hide(); // Hide spinner/text
}
});
});
// --- Meta Field Management (Add/Remove/Upload) ---
var metaWrapper = $('#myproid_meta_fields_wrapper');
// Add Meta Row
$('#myproid_add_meta').on('click', function() {
var newRow = metaWrapper.find('.myproid_meta_row:first').clone(); // Clone the first row as a template
newRow.find('input[type="text"]').val(''); // Clear input values in the new row
metaWrapper.append(newRow);
});
// Remove Meta Row (using event delegation for dynamically added rows)
metaWrapper.on('click', '.myproid_remove_meta', function() {
// Only remove if it's not the last row
if (metaWrapper.find('.myproid_meta_row').length > 1) {
$(this).closest('.myproid_meta_row').remove();
} else {
// Optionally clear the inputs if it's the last row instead of removing
$(this).closest('.myproid_meta_row').find('input[type="text"]').val('');
}
});
// Handle Media Upload Button Click (using event delegation)
var mediaUploader;
metaWrapper.on('click', '.myproid_upload_media', function(e) {
e.preventDefault();
var $button = $(this);
var $valueInput = $button.siblings('.myproid_meta_value'); // Target the input field next to the button
// If the uploader object has already been created, reopen the dialog
if (mediaUploader) {
mediaUploader.off('select'); // Detach previous select handler
} else {
// Extend the wp.media object
mediaUploader = wp.media({
title: MyProIdAjax.mediaUploadTitle || 'Choose Media', // Use localized title
button: {
text: MyProIdAjax.mediaUploadButton || 'Use this media' // Use localized button text
},
multiple: false // Allow only single file selection
});
}
// When a file is selected, grab the attachment ID and update the input field
mediaUploader.on('select', function() {
var attachment = mediaUploader.state().get('selection').first().toJSON();
$valueInput.val(attachment.id); // Set the input value to the attachment ID
// Optionally, display filename or thumbnail next to it (more complex)
});
// Open the uploader dialog
mediaUploader.open();
});
});