157 lines
6.3 KiB
JavaScript
157 lines
6.3 KiB
JavaScript
/**
|
|
* Scripts del panel de administración - Bloques en Transición
|
|
*/
|
|
|
|
(function($) {
|
|
'use strict';
|
|
|
|
$(document).ready(function() {
|
|
// Confirmación antes de cargar datos
|
|
$('.bloques-seeder form').on('submit', function(e) {
|
|
if (!confirm('¿Estás seguro de que quieres cargar los datos iniciales?')) {
|
|
e.preventDefault();
|
|
}
|
|
});
|
|
|
|
// Copiar shortcode al portapapeles
|
|
$('.bloques-dashboard-shortcodes code').on('click', function() {
|
|
const text = $(this).text();
|
|
navigator.clipboard.writeText(text).then(function() {
|
|
// Feedback visual
|
|
const $code = $(this);
|
|
const originalBg = $code.css('background');
|
|
$code.css('background', '#d4edda');
|
|
setTimeout(function() {
|
|
$code.css('background', originalBg);
|
|
}, 500);
|
|
}.bind(this));
|
|
});
|
|
|
|
// Añadir cursor pointer a los shortcodes
|
|
$('.bloques-dashboard-shortcodes code').css('cursor', 'pointer').attr('title', 'Clic para copiar');
|
|
|
|
// Mostrar/ocultar metaboxes de Iniciativa y Línea de Trabajo en el editor de entradas
|
|
if (typeof bloquesAdminData !== 'undefined' && bloquesAdminData.isPostEditor) {
|
|
initConditionalTaxonomyBoxes();
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Mostrar metaboxes de Iniciativa y Línea de Trabajo solo cuando
|
|
* la entrada tiene marcada la categoría "Bloques en Transición".
|
|
* Compatible con el editor clásico y Gutenberg.
|
|
*/
|
|
function initConditionalTaxonomyBoxes() {
|
|
const catIds = bloquesAdminData.bloquesCatIds || [];
|
|
|
|
if (!catIds.length) {
|
|
return;
|
|
}
|
|
|
|
// ── Editor clásico (metaboxes) ──
|
|
const $iniciativaBox = $('#iniciativadiv, #taxonomy-iniciativa').closest('.postbox');
|
|
const $lineaBox = $('#linea_trabajodiv, #taxonomy-linea_trabajo').closest('.postbox');
|
|
|
|
if ($iniciativaBox.length || $lineaBox.length) {
|
|
// Comprobar estado inicial y escuchar cambios en checkboxes de categorías
|
|
function checkClassicCategories() {
|
|
let isBloquesChecked = false;
|
|
|
|
catIds.forEach(function(catId) {
|
|
if ($('#in-category-' + catId).is(':checked')) {
|
|
isBloquesChecked = true;
|
|
}
|
|
});
|
|
|
|
if (isBloquesChecked) {
|
|
$iniciativaBox.slideDown(200);
|
|
$lineaBox.slideDown(200);
|
|
} else {
|
|
$iniciativaBox.slideUp(200);
|
|
$lineaBox.slideUp(200);
|
|
}
|
|
}
|
|
|
|
// Estado inicial (sin animación)
|
|
(function() {
|
|
let isBloquesChecked = false;
|
|
catIds.forEach(function(catId) {
|
|
if ($('#in-category-' + catId).is(':checked')) {
|
|
isBloquesChecked = true;
|
|
}
|
|
});
|
|
if (isBloquesChecked) {
|
|
$iniciativaBox.show();
|
|
$lineaBox.show();
|
|
} else {
|
|
$iniciativaBox.hide();
|
|
$lineaBox.hide();
|
|
}
|
|
})();
|
|
|
|
// Escuchar cambios en categorías
|
|
$('#categorychecklist').on('change', 'input[type="checkbox"]', checkClassicCategories);
|
|
}
|
|
|
|
// ── Gutenberg (editor de bloques) ──
|
|
if (typeof wp !== 'undefined' && wp.data && wp.data.select && wp.data.subscribe) {
|
|
let previousCategories = [];
|
|
|
|
// Selectores de los paneles de taxonomías en el sidebar de Gutenberg
|
|
function toggleGutenbergPanels(show) {
|
|
// Los metaboxes de taxonomías custom en Gutenberg tienen paneles con
|
|
// data-taxonomy o clases como .editor-post-taxonomies__hierarchical-terms-list
|
|
// Usamos un enfoque basado en MutationObserver + selectores
|
|
const selectors = [
|
|
'.editor-post-taxonomies__hierarchical-terms-list[data-taxonomy="iniciativa"]',
|
|
'.editor-post-taxonomies__hierarchical-terms-list[data-taxonomy="linea_trabajo"]',
|
|
// Paneles en el sidebar del editor
|
|
'[class*="iniciativa"]',
|
|
'[class*="linea_trabajo"]',
|
|
];
|
|
|
|
// Buscar los paneles padre (.components-panel__body) que contienen estas taxonomías
|
|
const panels = document.querySelectorAll('.components-panel__body');
|
|
panels.forEach(function(panel) {
|
|
const heading = panel.querySelector('.components-panel__body-title button');
|
|
if (heading) {
|
|
const text = heading.textContent.toLowerCase();
|
|
if (text.includes('iniciativa') || text.includes('línea') || text.includes('linea')) {
|
|
panel.style.display = show ? '' : 'none';
|
|
}
|
|
}
|
|
});
|
|
|
|
// También ocultar los metaboxes clásicos que pueda insertar WP en Gutenberg
|
|
['iniciativadiv', 'linea_trabajodiv'].forEach(function(id) {
|
|
const el = document.getElementById(id);
|
|
if (el) {
|
|
el.style.display = show ? '' : 'none';
|
|
}
|
|
});
|
|
}
|
|
|
|
wp.data.subscribe(function() {
|
|
const editor = wp.data.select('core/editor');
|
|
if (!editor) return;
|
|
|
|
const currentCategories = editor.getEditedPostAttribute('categories') || [];
|
|
|
|
// Solo actuar si las categorías cambiaron
|
|
if (JSON.stringify(currentCategories) === JSON.stringify(previousCategories)) {
|
|
return;
|
|
}
|
|
previousCategories = currentCategories.slice();
|
|
|
|
// Comprobar si alguna de las categorías de Bloques está seleccionada
|
|
const isBloquesChecked = catIds.some(function(catId) {
|
|
return currentCategories.includes(catId);
|
|
});
|
|
|
|
toggleGutenbergPanels(isBloquesChecked);
|
|
});
|
|
}
|
|
}
|
|
|
|
})(jQuery);
|