"Spidergram" should be a class with all static functions
Assistant
Here's an implementation of the Spidergram class with static methods connect, connectToId, and removeById. The class manages nodes in a spidergram diagram, creating and manipulating HTML elements to represent the nodes and their connections.
html
<!DOCTYPE html>
<html>
<head>
<style>
/* Basic styling for the spidergram nodes */
#spidergram-root {
position: relative;
margin: 20px;
}
.spidergram-node {
position: relative;
display: inline-block;
padding: 10px 15px;
margin: 10px;
background-color: #e3f2fd;
border: 1px solid #90caf9;
border-radius: 5px;
cursor: pointer;
text-align: center;
}
.spidergram-node::after {
content: '';
position: absolute;
top: -20px;
left: 50%;
width: 2px;
height: 20px;
background-color: #90caf9;
transform: translateX(-50%);
}
.spidergram-node.root::after {
display: none;
}
.spidergram-children {
display: flex;
justify-content: center;
flex-wrap: wrap;
}
</style>
</head>
<body>
<script>
class Spidergram {
static nodes = {}; // Stores node information by id
static rootElement = null;
/**
* Initializes the root element if it doesn't exist.
*/
static initialize() {
if (!Spidergram.rootElement) {
Spidergram.rootElement = document.createElement('div');
Spidergram.rootElement.id = 'spidergram-root';
document.body.appendChild(Spidergram.rootElement);
}
}
/**
* Connects an array of nodes as root nodes.
* @param {Array} nodesArray - Array of node objects with `text` and `id`.
*/
static connect(nodesArray) {
Spidergram.initialize();
nodesArray.forEach(node => {
Spidergram.addNode(null, node);
});
}
/**
* Connects an array of nodes to a parent node by id.
* @param {string} parentId - The id of the parent node.
* @param {Array} nodesArray - Array of node objects with `text` and `id`.
*/
static connectToId(parentId, nodesArray) {
nodesArray.forEach(node => {
Spidergram.addNode(parentId, node);
});
}
/**
* Removes a node and its children by id.
* @param {string} nodeId - The id of the node to remove.
*/
static removeById(nodeId) {
let nodeInfo = Spidergram.nodes[nodeId];
if (nodeInfo) {
// Remove the element from the DOM
nodeInfo.element.remove();
// Recursively remove child nodes
if (nodeInfo.children) {
nodeInfo.children.forEach(childId => {
Spidergram.removeById(childId);
});
}
// Remove from the nodes map
delete Spidergram.nodes[nodeId];
} else {
console.warn(`Node with id '${nodeId}' does not exist.`);
}
}
/**
* Adds a node to the spidergram.
* @param {string|null} parentId - The id of the parent node, or null for root nodes.
* @param {Object} nodeData - The node data object with `text` and `id`.
*/
static addNode(parentId, nodeData) {
// Create the node element
let nodeElement = document.createElement('div');
nodeElement.classList.add('spidergram-node');
nodeElement.id = nodeData.id;
nodeElement.innerText = nodeData.text;
// Store the node information
Spidergram.nodes[nodeData.id] = {
id: nodeData.id,
element: nodeElement,
parentId: parentId,
children: [],
};
if (parentId) {
// Append to parent node
let parentNodeInfo = Spidergram.nodes[parentId];
if (parentNodeInfo) {
// Add to parent's children list
parentNodeInfo.children.push(nodeData.id);
// Create or get the container for child nodes
let childrenContainer = parentNodeInfo.element.querySelector('.spidergram-children');
if (!childrenContainer) {
childrenContainer = document.createElement('div');
childrenContainer.classList.add('spidergram-children');
parentNodeInfo.element.appendChild(childrenContainer);
}
childrenContainer.appendChild(nodeElement);
} else {
console.error(`Parent node with id '${parentId}' doesn't exist.`);
}
} else {
// This is a root node
nodeElement.classList.add('root');
Spidergram.rootElement.appendChild(nodeElement);
}
}
}
// Example usage:
Spidergram.connect([
{
text: 'Root Node',
id: 'root1'
},
{
text: 'Another Root',
id: 'root2'
},
]);
Spidergram.connectToId('root1', [
{
text: 'Child 1',
id: 'child1'
},
{
text: 'Child 2',
id: 'child2'
},
]);
Spidergram.connectToId('child1', [
{
text: 'Grandchild 1',
id: 'grandchild1'
},
]);
// To remove a node:
// Spidergram.removeById('child2');
</script>
</body>
</html>
Explanation:
Spidergram Class:
The Spidergram class uses static methods and properties to manage spidergram nodes.
nodes: A static object that maps node ids to their information (element, parent, children).
rootElement: The root container for the spidergram diagram.
initialize():
Ensures that the rootElement is created and appended to the document body.
connect(nodesArray):
Takes an array of node objects and adds them as root nodes to the spidergram.
connectToId(parentId, nodesArray):
Adds nodes as children to the specified parent node.
Updates both the DOM and the internal nodes structure.
removeById(nodeId):
Recursively removes a node and all its descendant nodes.
Cleans up both the DOM and the internal nodes structure.
addNode(parentId, nodeData):
Creates a node element and adds it to the DOM.
Updates the internal nodes structure with parent-child relationships.
Handles both root nodes and child nodes.
Styling:
Basic CSS styles are provided to visually represent the nodes and their hierarchy.
Nodes are displayed inline with some padding, margin, and background color.
Child nodes are wrapped in a .spidergram-children container to allow for flexible layouts.
Usage:
To create root nodes, use the connect method with an array of node objects.
To add child nodes to a specific node, use connectToId with the parent id and an array of node objects.
To remove a node (and its descendants), use removeById with the node's id.
Note:
This implementation provides a basic structure for a spidergram. For a more sophisticated layout (e.g., radial or network diagrams), additional JavaScript and CSS would be required.
The example usage demonstrates how to create nodes and build the hierarchy.
The Spidergram class is designed to be extended or modified to suit specific needs, such as event handling or dynamic styling.