{{-- Sections Manager Logic for LandingSectionsEditor --}} // Cache de templates de formularios no cliente para evitar requisicoes AJAX redundantes const formTemplatesCache = {}; /** * Gerenciamento de overlay CUSTOM — sem Bootstrap Modal, sem backdrop do Bootstrap. * Usa contador para suportar futuros sub-modais (gallery picker inline). */ var lseOverlayCount = 0; /** * Exibe o overlay + painel do modal de secao. */ const showSectionModal = () => { lseOverlayCount++; document.getElementById('lse-overlay-' + name).classList.add('show'); document.getElementById('section-modal-' + name).classList.add('show'); document.body.classList.add('modal-open'); }; /** * Esconde o painel do modal e, se nenhum outro modal estiver aberto, o overlay. */ const hideSectionModal = () => { lseOverlayCount = Math.max(0, lseOverlayCount - 1); document.getElementById('section-modal-' + name).classList.remove('show'); if (lseOverlayCount === 0) { document.getElementById('lse-overlay-' + name).classList.remove('show'); document.body.classList.remove('modal-open'); document.body.style.removeProperty('padding-right'); document.body.style.removeProperty('overflow'); } }; /** * Registra os ouvintes de eventos do DOM especificos */ const registerEvents = () => { // Configuracao global AJAX para requisicoes desse editor $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': csrfToken } }); // Atualizar preview visual de icone ao alterar o select $(document).on('change', '.icon-selector', function() { var iconClass = $(this).val(); $(this).closest('.input-group').find('.input-group-text i').attr('class', iconClass); }); // Adicionar Secao (Clique no dropdown) $('.btn-add-section-' + name).on('click', function(e) { e.preventDefault(); currentType = $(this).data('type'); currentSectionId = ''; loadForm(currentType, ''); }); // Editar Secao $(document).on('click', '.lse-section-item-' + name + ' .btn-edit-section', function() { const $item = $(this).closest('.lse-section-item-' + name); currentType = $item.data('type'); currentSectionId = $item.data('id'); // Busca a secao no array local const section = sections.find(s => s.id === currentSectionId); const parameters = section ? section.parameters : {}; loadForm(currentType, currentSectionId, parameters); }); // Mover Secao para Cima $(document).on('click', '.lse-section-item-' + name + ' .btn-move-up-section', function(e) { e.stopPropagation(); const $item = $(this).closest('.lse-section-item-' + name); const id = $item.data('id'); const idx = sections.findIndex(s => s.id === id); if (idx > 0) { [sections[idx - 1], sections[idx]] = [sections[idx], sections[idx - 1]]; // Recalcula ordens sections.forEach((s, i) => { s.order = i + 1; }); persistState(); } }); // Mover Secao para Baixo $(document).on('click', '.lse-section-item-' + name + ' .btn-move-down-section', function(e) { e.stopPropagation(); const $item = $(this).closest('.lse-section-item-' + name); const id = $item.data('id'); const idx = sections.findIndex(s => s.id === id); if (idx !== -1 && idx < sections.length - 1) { [sections[idx], sections[idx + 1]] = [sections[idx + 1], sections[idx]]; // Recalcula ordens sections.forEach((s, i) => { s.order = i + 1; }); persistState(); } }); // Remover Secao $(document).on('click', '.lse-section-item-' + name + ' .btn-remove-section', function() { const $item = $(this).closest('.lse-section-item-' + name); const sectionId = $item.data('id'); removeSection(sectionId); }); // Salvar Secao no Modal $('#btn-modal-save-' + name).on('click', () => { saveSection(); }); // Fechar modal (botoes Fechar / X / overlay) $('#btn-modal-close-' + name + ', #btn-modal-close2-' + name).on('click', function() { hideSectionModal(); }); $('#lse-overlay-' + name).on('click', function() { hideSectionModal(); }); // --- Gerenciamento de Anexos / Imagens --- // Abrir o seletor de imagens global $(document).on('click', '.lse-container-{{ $name }} .btn-select-image, #section-modal-' + name + ' .btn-select-image', function() { activeImageInputId = $(this).data('input-id'); window.dispatchEvent(new CustomEvent('lotep:open-gallery-picker-lse_global_media_picker', { detail: { callback: (id, url) => { selectImage(id, url); } } })); }); // Limpar imagem selecionada $(document).on('click', '.lse-container-{{ $name }} .btn-clear-image, #section-modal-' + name + ' .btn-clear-image', function() { const inputId = $(this).data('input-id'); $(`#${inputId}`).val(''); $(`#${inputId}-preview-text`).val('Nenhuma imagem selecionada'); $(`#${inputId}-thumbnail`).html(''); $(this).prop('disabled', true); }); }; /** * Inicializa o reordenamento */ const initSortable = () => { // Desativado em prol do sistema de setinhas (Up/Down) fixa nos blocos }; /** * Recalcula o array de secoes baseado na ordem do DOM */ const updateOrderFromDOM = () => { // Mantido como fallback se necessario }; /** * Carrega o formulario de parametros via AJAX com suporte a Cache no Cliente */ const loadForm = (type, sectionId, parameters = {}) => { const $modalBody = $('#modal-body-content-' + name); const $modalDialog = $('#section-modal-' + name + ' .modal-dialog'); $modalBody.html('
Carregando formulario...
Nenhuma secao adicionada.
'); return; } let html = ''; sections.forEach((section, index) => { const data = sectionTypesData[section.type] || { icon: 'bi bi-box', title: section.type }; const isFirst = (index === 0); const isLast = (index === sections.length - 1); html += `