const ConfigVLAN = function (currentDevice) { var modalId = 'VlanModal_' + currentDevice.data.id; var tableId = 'config_table_vlan_' + currentDevice.data.id; $('#' + modalId).remove(); var buttonHTML = document.getElementById('config_button_vlan_script').innerHTML; var modalHTML = document.getElementById('config_modal_vlan_script').innerHTML; var tableHTML = document.getElementById('config_table_vlan_script').innerHTML; modalHTML = modalHTML.replace('id="VlanModal"', 'id="' + modalId + '"'); tableHTML = tableHTML.replace('id="config_table_vlan"', 'id="' + tableId + '"'); var buttonElem = $(buttonHTML).appendTo('#config_switch_name'); buttonElem.attr('data-bs-target', '#' + modalId); var modalElem = $(modalHTML).appendTo('body'); var tableElem = $(tableHTML).appendTo('#' + modalId + ' .modal-body').hide(); $(document).ready(function () { $('[data-bs-toggle="tooltip"]').tooltip(); setupEventHandlers(currentDevice, modalId, tableId); }); }; function setupEventHandlers(currentDevice, modalId, tableId) { $('#' + modalId).find('#config_switch_vlan').off('click').on('click', function () { if ($(this).is(':checked')) { $('#' + tableId).show(); generateTableContent(currentDevice, '#' + tableId); } else { $('#' + tableId).hide(); } }); $('#' + modalId).find('#vlanConfigurationCancelIcon, #vlanConfigurationCancel').on('click', function () { restoreFormData(currentDevice, '#' + tableId); $('#' + modalId).modal('hide'); }); $('#' + modalId).find('#vlanConfigurationSubmit').on('click', function () { if ($('#' + modalId).find('#config_switch_vlan').is(':checked')) { saveCurrentFormData(currentDevice, '#' + tableId); // TODO explain why we need this fix // (for some reason, nodes aren't updating fast enough) var index = nodes.findIndex(function (n) { return n.data.id == currentDevice.data.id; }); nodes[index].interface = currentDevice.interface } else { resetInterfaceFields(currentDevice); } $('#' + modalId).modal('hide'); updateVlanButtonStyle(currentDevice); // Reset network state SetNetworkPlayerState(-1); DrawGraph(); PostNodesEdges(); }); $('#config_button_vlan').off('click').on('click', function () { if (areInterfaceFieldsFilled(currentDevice)) { $('#' + modalId).find('#config_switch_vlan').prop('checked', true); $('#' + tableId).show(); generateTableContent(currentDevice, '#' + tableId); } else { $('#' + modalId).find('#config_switch_vlan').prop('checked', false); $('#' + tableId).hide(); } $('#' + modalId).modal('show'); }); updateVlanButtonStyle(currentDevice); } function generateTableContent(currentDevice, tableSelector) { // Clearing previous lines in tbody $(tableSelector + ' tbody').empty(); var edgesMap = new Map(); for (var i = 0; i < edges.length; i++) { edgesMap.set(edges[i].data.id, edges[i]); } var nodesMap = new Map(); for (var i = 0; i < nodes.length; i++) { nodesMap.set(nodes[i].data.id, nodes[i].data.label); } for (var i = 0; i < currentDevice.interface.length; i++) { var interface = currentDevice.interface[i]; var connectedEdge = edgesMap.get(interface.connect); if (connectedEdge !== undefined) { var targetDeviceId = connectedEdge.data.target; // Checking whether the current device is the source or not if (connectedEdge.data.source === currentDevice.data.id) { targetDeviceId = connectedEdge.data.target; } else { targetDeviceId = connectedEdge.data.source; } var vlan = (interface.vlan !== null && interface.vlan !== undefined) ? interface.vlan : 1; var type_connection = (interface.type_connection !== null && interface.type_connection !== undefined) ? interface.type_connection : 0; var selectedAccess = type_connection === 0 ? 'selected' : ''; var selectedTrunk = type_connection === 1 ? 'selected' : ''; var row = '