From 3238f7133ac853adbe34cfdb3a99de1e7f0155ad Mon Sep 17 00:00:00 2001 From: Cosmo Date: Fri, 4 Oct 2024 18:46:23 -0400 Subject: [PATCH] Add dropdowns and submission with enter key --- index.html | 10 +++++----- script.js | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 03c36cc..b3ad52d 100644 --- a/index.html +++ b/index.html @@ -4,7 +4,7 @@ - Interactive Graph Visualizer + Polyculizer! @@ -13,13 +13,13 @@
- - + + - + - +
diff --git a/script.js b/script.js index c744e97..7299642 100644 --- a/script.js +++ b/script.js @@ -51,6 +51,7 @@ function addNode() { } nodes.add({ id: nodeName, label: nodeName }); document.getElementById('node-name').value = ''; + updateDropdowns(); saveGraph(); } @@ -68,8 +69,6 @@ function addEdge() { } const edgeId = createEdgeId(fromNode, toNode); edges.add({ id: edgeId, from: fromNode, to: toNode }); - document.getElementById('edge-from').value = ''; - document.getElementById('edge-to').value = ''; saveGraph(); } @@ -111,6 +110,7 @@ function renameNode() { document.getElementById('rename-old').value = ''; document.getElementById('rename-new').value = ''; + updateDropdowns(); saveGraph(); } @@ -132,6 +132,7 @@ function removeNode() { edges.remove(edgesToRemove.map(edge => edge.id)); nodes.remove(nodeName); document.getElementById('remove-node').value = ''; + updateDropdowns(); saveGraph(); } @@ -198,6 +199,8 @@ function initializeGraph() { } network = new vis.Network(container, data, options); network.on("afterDrawing", saveGraph); + updateDropdowns(); + setupEnterKeyListeners(); } // Add event listeners @@ -208,6 +211,34 @@ document.getElementById('remove-node-btn').addEventListener('click', removeNode) document.getElementById('save-graph').addEventListener('click', saveGraph); document.getElementById('load-graph').addEventListener('click', loadGraph); +// Setup Enter key listeners +setupEnterKeyListeners(); + if (typeof rison === 'undefined') { console.error('Rison library is not loaded. Make sure to include it in your HTML.'); } +function updateDropdowns() { + const nodeIds = nodes.getIds(); + const dropdowns = ['edge-from', 'edge-to', 'rename-old', 'remove-node']; + + dropdowns.forEach(id => { + const select = document.getElementById(id); + select.innerHTML = ''; + nodeIds.forEach(nodeId => { + const option = document.createElement('option'); + option.value = nodeId; + option.text = nodeId; + select.appendChild(option); + }); + }); +} +function handleEnterKey(event, actionFunction) { + if (event.key === 'Enter') { + actionFunction(); + } +} + +function setupEnterKeyListeners() { + document.getElementById('node-name').addEventListener('keyup', (event) => handleEnterKey(event, addNode)); + document.getElementById('rename-new').addEventListener('keyup', (event) => handleEnterKey(event, renameNode)); +}