From 2fe1714e170231a1a014b8fff0c2117a7e41cc36 Mon Sep 17 00:00:00 2001 From: Caesar Kabalan Date: Sun, 13 Oct 2024 14:54:59 -0700 Subject: [PATCH] Allow changes to subnet input to adjust existing layout --- dist/js/main.js | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/dist/js/main.js b/dist/js/main.js index 28ae0bf..9c1e20c 100644 --- a/dist/js/main.js +++ b/dist/js/main.js @@ -150,14 +150,47 @@ function reset() { let rootCidr = rootNetwork + '/' + $('#netsize').val() if (cidrInput !== rootCidr) { show_warning_modal('
Your network input is not on a network boundary for this network size. It has been automatically changed:
' + $('#network').val() + ' -> ' + rootNetwork + '
') + $('#network').val(rootNetwork) + cidrInput = $('#network').val() + '/' + $('#netsize').val() + } + if (Object.keys(subnetMap).length > 0) { + // This page already has data imported, so lets see if we can just change the range + if (isMatchingSize(Object.keys(subnetMap)[0], cidrInput)) { + subnetMap = changeBaseNetwork(cidrInput) + } else { + // This is a page with existing data of a different subnet size, so make it blank + // Could be an opportunity here to do the following: + // - Prompt the user to confirm they want to clear the existing data + // - Resize the existing data anyway by making the existing network a subnetwork of their new input (if it + // is a larger network), or by just trimming the network to the new size (if it is a smaller network), + // or even resizing all of the containing networks by change in size of the base network. For example a + // base network going from /16 -> /18 would be all containing networks would be resized smaller (/+2), + // or bigger (/-2) if going from /18 -> /16. + subnetMap = {} + subnetMap[rootCidr] = {} + } + } else { + // This is a fresh page load with no existing data + subnetMap[rootCidr] = {} } - $('#network').val(rootNetwork) - subnetMap = {} - subnetMap[rootCidr] = {} maxNetSize = parseInt($('#netsize').val()) renderTable(operatingMode); } +function changeBaseNetwork(newBaseNetwork) { + // Minifiy it, to make all the keys in the subnetMap relative to their original base network + // Then expand it, but with the new CIDR as the base network, effectively converting from old to new. + let miniSubnetMap = {} + minifySubnetMap(miniSubnetMap, subnetMap, Object.keys(subnetMap)[0]) + let newSubnetMap = {} + expandSubnetMap(newSubnetMap, miniSubnetMap, newBaseNetwork) + return newSubnetMap +} + +function isMatchingSize(subnet1, subnet2) { + return subnet1.split('/')[1] === subnet2.split('/')[1]; +} + $('#calcbody').on('click', 'td.split,td.join', function(event) { // HTML DOM Data elements! Yay! See the `data-*` attributes of the HTML tags mutate_subnet_map(this.dataset.mutateVerb, this.dataset.subnet, '')