diff --git a/static/simplified_protocol_mapping.js b/static/simplified_protocol_mapping.js index a1ecc8e..c76acde 100644 --- a/static/simplified_protocol_mapping.js +++ b/static/simplified_protocol_mapping.js @@ -1,9 +1,21 @@ // Simplified Protocol Mapping Functions // Uses human-readable signal names and tags instead of complex IDs -let currentProtocolFilter = 'all'; -let editingSignalId = null; -let allTags = new Set(); +(function() { + 'use strict'; + + // Check if global variables already exist before declaring + if (typeof window.currentProtocolFilter === 'undefined') { + window.currentProtocolFilter = 'all'; + } + if (typeof window.editingSignalId === 'undefined') { + window.editingSignalId = null; + } + if (typeof window.allTags === 'undefined') { + window.allTags = new Set(); + } + + // Use window object variables directly to avoid redeclaration conflicts // Simplified Signal Management Functions async function loadAllSignals() { @@ -112,15 +124,38 @@ async function applyFilters() { // Modal Functions function showAddSignalModal() { - editingSignalId = null; - document.getElementById('modal-title').textContent = 'Add Protocol Signal'; - document.getElementById('signal-form').reset(); - document.getElementById('protocol-address-help').textContent = ''; - document.getElementById('signal-modal').style.display = 'block'; + console.log('showAddSignalModal called'); + window.editingSignalId = null; + + // Safely update modal elements if they exist + const modalTitle = document.getElementById('modal-title'); + if (modalTitle) { + modalTitle.textContent = 'Add Protocol Signal'; + } + + const signalForm = document.getElementById('signal-form'); + if (signalForm) { + signalForm.reset(); + } + + const protocolAddressHelp = document.getElementById('protocol-address-help'); + if (protocolAddressHelp) { + protocolAddressHelp.textContent = ''; + } + + const signalModal = document.getElementById('signal-modal'); + console.log('Modal element found:', signalModal); + if (signalModal) { + console.log('Setting modal display to block'); + signalModal.style.display = 'block'; + console.log('Modal display after setting:', signalModal.style.display); + } else { + console.error('signal-modal element not found!'); + } } function showEditSignalModal(signal) { - editingSignalId = signal.signal_id; + window.editingSignalId = signal.signal_id; document.getElementById('modal-title').textContent = 'Edit Protocol Signal'; // Populate form @@ -137,7 +172,7 @@ function showEditSignalModal(signal) { function closeSignalModal() { document.getElementById('signal-modal').style.display = 'none'; - editingSignalId = null; + window.editingSignalId = null; } function updateProtocolFields() { @@ -168,8 +203,8 @@ async function saveSignal(event) { try { let response; - if (editingSignalId) { - response = await fetch(`/api/v1/dashboard/protocol-signals/${editingSignalId}`, { + if (window.editingSignalId) { + response = await fetch(`/api/v1/dashboard/protocol-signals/${window.editingSignalId}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formData) @@ -185,7 +220,7 @@ async function saveSignal(event) { const data = await response.json(); if (data.success) { - showSimplifiedAlert(`Protocol signal ${editingSignalId ? 'updated' : 'created'} successfully!`, 'success'); + showSimplifiedAlert(`Protocol signal ${window.editingSignalId ? 'updated' : 'created'} successfully!`, 'success'); closeSignalModal(); loadAllSignals(); } else { @@ -266,21 +301,25 @@ function autoPopulateSignalForm(discoveryData) { if (isModalVisible) { clearInterval(waitForModal); + console.log('Modal is visible, populating fields...'); populateModalFields(discoveryData); } }, 50); - // Timeout after 2 seconds + // Timeout after 3 seconds (increased from 2) setTimeout(() => { clearInterval(waitForModal); const modal = document.getElementById('signal-modal'); if (modal && modal.style.display !== 'none') { + console.log('Modal opened within timeout, populating fields...'); populateModalFields(discoveryData); } else { console.error('Modal did not open within timeout period'); + console.log('Modal element:', modal); + console.log('Modal display style:', modal ? modal.style.display : 'no modal found'); showSimplifiedAlert('Could not open signal form. Please try opening it manually.', 'error'); } - }, 2000); + }, 3000); } function populateModalFields(discoveryData) { @@ -330,6 +369,13 @@ function populateModalFields(discoveryData) { // Utility Functions function showSimplifiedAlert(message, type = 'info') { const alertsDiv = document.getElementById('simplified-alerts'); + + // Only proceed if the alerts container exists + if (!alertsDiv) { + console.log(`Alert (${type}): ${message}`); + return; + } + const alertDiv = document.createElement('div'); alertDiv.className = `alert ${type === 'error' ? 'error' : 'success'}`; alertDiv.textContent = message; @@ -345,16 +391,20 @@ function showSimplifiedAlert(message, type = 'info') { }, 5000); } -// Initialize -document.addEventListener('DOMContentLoaded', function() { - const signalForm = document.getElementById('signal-form'); - if (signalForm) { - signalForm.addEventListener('submit', saveSignal); - } - - // Load initial data - loadAllSignals(); -}); + // Initialize + document.addEventListener('DOMContentLoaded', function() { + const signalForm = document.getElementById('signal-form'); + if (signalForm) { + signalForm.addEventListener('submit', saveSignal); + } + + // Load initial data + loadAllSignals(); + }); -// Expose functions to window for discovery integration -window.autoPopulateSignalForm = autoPopulateSignalForm; \ No newline at end of file + // Expose functions to window for discovery integration + window.autoPopulateSignalForm = autoPopulateSignalForm; + window.showAddSignalModal = showAddSignalModal; + window.applyFilters = applyFilters; + window.closeSignalModal = closeSignalModal; +})(); \ No newline at end of file